aboutsummaryrefslogtreecommitdiff
path: root/Source/Utils/Algorithms/IsIn.H
blob: c9d2f477ef88d9d273f552bbfc50195f829a6c58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Copyright 2022 Andrew Myers, Luca Fedeli, Maxence Thevenet
 * Revathi Jambunathan
 *
 * This file is part of WarpX.
 *
 * License: BSD-3-Clause-LBNL
 */

#ifndef WARPX_UTILS_ALGORITHMS_ISIN_H_
#define WARPX_UTILS_ALGORITHMS_ISIN_H_

#include <algorithm>
#include <vector>

namespace utils::algorithms
{
    /** \brief Returns true if an item of type TE is in a vector
     * of TV objects (provided that TE can be converted into TV), false otherwise
     *
     * @tparam TV the typename of the vector elements
     * @tparam TE the typename of the item
     *
     * @param vect a vector of TV objects
     * @param elem an object of type TE
     *
     * @return true if elem is in vect, false otherwise
     */
    template <typename TV, typename TE,
        class = typename std::enable_if<std::is_convertible<TE,TV>::value>::type>
    bool is_in(const std::vector<TV>& vect,
               const TE& elem)
    {
        return (std::find(vect.begin(), vect.end(), elem) != vect.end());
    }


    /** \brief Returns true if any of the items of a vector<TE> is contained
     * in another vector<TV> (provided that TE can be converted into TV)
     *
     * @tparam TV the typename of the first vector elements
     * @tparam TV the typename of the second vector elements
     *
     * @param vect a vector of TV objects
     * @param elems a vector of TE objects
     *
     * @return true if any element of elems is in vect, false otherwise
     */
    template <typename TV, typename TE,
        class = typename std::enable_if<std::is_convertible<TE,TV>::value>::type>
    bool any_of_is_in(const std::vector<TV>& vect,
               const std::vector<TE>& elems)
    {
        return std::any_of(elems.begin(), elems.end(),
            [&](const auto elem){return is_in(vect, elem);});
    }
}

#endif //WARPX_UTILS_ALGORITHMS_ISIN_H_