aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/skip_list/compare.h
blob: ac98b3e1cedf21caf299f34aa7ae5875b7b9b587 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once

#include <util/generic/typetraits.h>
#include <util/str_stl.h>

namespace NThreading {
    namespace NImpl {
        Y_HAS_MEMBER(compare);
        Y_HAS_MEMBER(Compare);

        template <typename T>
        inline int CompareImpl(const T& l, const T& r) {
            if (l < r) {
                return -1;
            } else if (r < l) {
                return +1;
            } else {
                return 0;
            }
        }

        template <bool val>
        struct TSmallCompareSelector {
            template <typename T>
            static inline int Compare(const T& l, const T& r) {
                return CompareImpl(l, r);
            }
        };

        template <>
        struct TSmallCompareSelector<true> {
            template <typename T>
            static inline int Compare(const T& l, const T& r) {
                return l.compare(r);
            }
        };

        template <bool val>
        struct TBigCompareSelector {
            template <typename T>
            static inline int Compare(const T& l, const T& r) {
                return TSmallCompareSelector<THascompare<T>::value>::Compare(l, r);
            }
        };

        template <>
        struct TBigCompareSelector<true> {
            template <typename T>
            static inline int Compare(const T& l, const T& r) {
                return l.Compare(r);
            }
        };

        template <typename T>
        struct TCompareSelector: public TBigCompareSelector<THasCompare<T>::value> {
        };
    }

    ////////////////////////////////////////////////////////////////////////////////
    // Generic compare function

    template <typename T>
    inline int Compare(const T& l, const T& r) {
        return NImpl::TCompareSelector<T>::Compare(l, r);
    }

    ////////////////////////////////////////////////////////////////////////////////
    // Generic compare functor

    template <typename T>
    struct TCompare {
        inline int operator()(const T& l, const T& r) const {
            return Compare(l, r);
        }
    };

}