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
19namespace folly {
20
21enum class ordering : int { lt = -1, eq = 0, gt = 1 };
22
23template <typename T>
24constexpr ordering to_ordering(T c) {
25 return ordering(int(c < T(0)) * -1 + int(c > T(0)));
26}
27
28namespace detail {
29
30template <typename C, ordering o, bool ne>
31struct 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
42template <typename C>
43struct compare_equal_to : detail::cmp_pred<C, ordering::eq, 0> {
44 using detail::cmp_pred<C, ordering::eq, 0>::cmp_pred;
45};
46
47template <typename C>
48struct compare_not_equal_to : detail::cmp_pred<C, ordering::eq, 1> {
49 using detail::cmp_pred<C, ordering::eq, 1>::cmp_pred;
50};
51
52template <typename C>
53struct compare_less : detail::cmp_pred<C, ordering::lt, 0> {
54 using detail::cmp_pred<C, ordering::lt, 0>::cmp_pred;
55};
56
57template <typename C>
58struct compare_less_equal : detail::cmp_pred<C, ordering::gt, 1> {
59 using detail::cmp_pred<C, ordering::gt, 1>::cmp_pred;
60};
61
62template <typename C>
63struct compare_greater : detail::cmp_pred<C, ordering::gt, 0> {
64 using detail::cmp_pred<C, ordering::gt, 0>::cmp_pred;
65};
66
67template <typename C>
68struct 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