1 | /* |
2 | * Copyright 2018-present Facebook, Inc. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #pragma once |
18 | |
19 | namespace folly { |
20 | |
21 | enum class ordering : int { lt = -1, eq = 0, gt = 1 }; |
22 | |
23 | template <typename T> |
24 | constexpr ordering to_ordering(T c) { |
25 | return ordering(int(c < T(0)) * -1 + int(c > T(0))); |
26 | } |
27 | |
28 | namespace detail { |
29 | |
30 | template <typename C, ordering o, bool ne> |
31 | struct cmp_pred : private C { |
32 | using C::C; |
33 | |
34 | template <typename A, typename B> |
35 | constexpr bool operator()(A&& a, B&& b) const { |
36 | return ne ^ (C::operator()(static_cast<A&&>(a), static_cast<B&&>(b)) == o); |
37 | } |
38 | }; |
39 | |
40 | } // namespace detail |
41 | |
42 | template <typename C> |
43 | struct compare_equal_to : detail::cmp_pred<C, ordering::eq, 0> { |
44 | using detail::cmp_pred<C, ordering::eq, 0>::cmp_pred; |
45 | }; |
46 | |
47 | template <typename C> |
48 | struct compare_not_equal_to : detail::cmp_pred<C, ordering::eq, 1> { |
49 | using detail::cmp_pred<C, ordering::eq, 1>::cmp_pred; |
50 | }; |
51 | |
52 | template <typename C> |
53 | struct compare_less : detail::cmp_pred<C, ordering::lt, 0> { |
54 | using detail::cmp_pred<C, ordering::lt, 0>::cmp_pred; |
55 | }; |
56 | |
57 | template <typename C> |
58 | struct compare_less_equal : detail::cmp_pred<C, ordering::gt, 1> { |
59 | using detail::cmp_pred<C, ordering::gt, 1>::cmp_pred; |
60 | }; |
61 | |
62 | template <typename C> |
63 | struct compare_greater : detail::cmp_pred<C, ordering::gt, 0> { |
64 | using detail::cmp_pred<C, ordering::gt, 0>::cmp_pred; |
65 | }; |
66 | |
67 | template <typename C> |
68 | struct compare_greater_equal : detail::cmp_pred<C, ordering::lt, 1> { |
69 | using detail::cmp_pred<C, ordering::lt, 1>::cmp_pred; |
70 | }; |
71 | |
72 | } // namespace folly |
73 | |