aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/is_in.h
blob: 83f8f5645e21bc618cd858b853519fbd1947e347 (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
#pragma once
 
#include "typetraits.h"

#include <algorithm> 
#include <initializer_list>

template <class I, class T>
static inline bool IsIn(I f, I l, const T& v); 

template <class C, class T>
static inline bool IsIn(const C& c, const T& e); 

namespace NIsInHelper {
    Y_HAS_MEMBER(find, FindMethod);
    Y_HAS_SUBTYPE(const_iterator, ConstIterator);
    Y_HAS_SUBTYPE(key_type, KeyType);

    template <class T> 
    using TIsAssocCont = TConjunction<THasFindMethod<T>, THasConstIterator<T>, THasKeyType<T>>;

    template <class C, class T, bool isAssoc> 
    struct TIsInTraits { 
        static bool IsIn(const C& c, const T& e) { 
            using std::begin;
            using std::end;
            return ::IsIn(begin(c), end(c), e);
        } 
    }; 

    template <class C, class T> 
    struct TIsInTraits<C, T, true> { 
        static bool IsIn(const C& c, const T& e) { 
            return c.find(e) != c.end(); 
        } 
    }; 
} 

template <class I, class T>
static inline bool IsIn(I f, I l, const T& v) { 
    return std::find(f, l, v) != l; 
}

template <class C, class T>
static inline bool IsIn(const C& c, const T& e) { 
    using namespace NIsInHelper;
    return TIsInTraits<C, T, TIsAssocCont<C>::value>::IsIn(c, e);
}

template <class T, class U>
static inline bool IsIn(std::initializer_list<T> l, const U& e) {
    return ::IsIn(l.begin(), l.end(), e); 
}