/Users/deen/code/yugabyte-db/src/yb/util/std_util.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_UTIL_STD_UTIL_H |
15 | | #define YB_UTIL_STD_UTIL_H |
16 | | |
17 | | // Implementation of std functions we want to use, but cannot until we switch to newer C++. |
18 | | |
19 | | namespace yb { |
20 | | |
21 | | namespace std_util { |
22 | | |
23 | | // cmp_* code is based on examples from https://en.cppreference.com/w/cpp/utility/intcmp |
24 | | // TODO: remove once we switch to C++20: |
25 | | |
26 | | template <class T, class U> |
27 | | constexpr std::enable_if_t<std::is_signed<T>::value == std::is_signed<U>::value, bool> cmp_equal( |
28 | | T t, U u) noexcept { |
29 | | return t == u; |
30 | | } |
31 | | |
32 | | template <class T, class U> |
33 | | constexpr std::enable_if_t<std::is_signed<T>::value && !std::is_signed<U>::value, bool> cmp_equal( |
34 | | T t, U u) noexcept { |
35 | | using UT = std::make_unsigned_t<T>; |
36 | | return t < 0 ? false : UT(t) == u; |
37 | | } |
38 | | |
39 | | template <class T, class U> |
40 | | constexpr std::enable_if_t<!std::is_signed<T>::value && std::is_signed<U>::value, bool> cmp_equal( |
41 | | T t, U u) noexcept { |
42 | | using UU = std::make_unsigned_t<U>; |
43 | | return u < 0 ? false : t == UU(u); |
44 | | } |
45 | | |
46 | | template <class T, class U> |
47 | | constexpr std::enable_if_t<std::is_signed<T>::value == std::is_signed<U>::value, bool> cmp_less( |
48 | | T t, U u) noexcept { |
49 | | return t < u; |
50 | | } |
51 | | |
52 | | template <class T, class U> |
53 | | constexpr std::enable_if_t<std::is_signed<T>::value && !std::is_signed<U>::value, bool> cmp_less( |
54 | 23.9M | T t, U u) noexcept { |
55 | 23.9M | using UT = std::make_unsigned_t<T>; |
56 | 23.9M | return t < 0 ? true0 : UT(t) < u; |
57 | 23.9M | } std::__1::enable_if<(std::is_signed<long>::value) && (!(std::is_signed<unsigned int>::value)), bool>::type yb::std_util::cmp_less<long, unsigned int>(long, unsigned int) Line | Count | Source | 54 | 23.9M | T t, U u) noexcept { | 55 | 23.9M | using UT = std::make_unsigned_t<T>; | 56 | 23.9M | return t < 0 ? true0 : UT(t) < u; | 57 | 23.9M | } |
std::__1::enable_if<(std::is_signed<int>::value) && (!(std::is_signed<unsigned long>::value)), bool>::type yb::std_util::cmp_less<int, unsigned long>(int, unsigned long) Line | Count | Source | 54 | 7.57k | T t, U u) noexcept { | 55 | 7.57k | using UT = std::make_unsigned_t<T>; | 56 | 7.57k | return t < 0 ? true0 : UT(t) < u; | 57 | 7.57k | } |
|
58 | | |
59 | | template <class T, class U> |
60 | | constexpr std::enable_if_t<!std::is_signed<T>::value && std::is_signed<U>::value, bool> cmp_less( |
61 | 857k | T t, U u) noexcept { |
62 | 857k | using UU = std::make_unsigned_t<U>; |
63 | 857k | return u < 0 ? false0 : t < UU(u); |
64 | 857k | } |
65 | | |
66 | | template <class T, class U> |
67 | | constexpr bool cmp_not_equal(T t, U u) noexcept { |
68 | | return !cmp_equal(t, u); |
69 | | } |
70 | | |
71 | | template <class T, class U> |
72 | 7.57k | constexpr bool cmp_greater(T t, U u) noexcept { |
73 | 7.57k | return cmp_less(u, t); |
74 | 7.57k | } |
75 | | |
76 | | template <class T, class U> |
77 | | constexpr bool cmp_less_equal(T t, U u) noexcept { |
78 | | return !cmp_greater(t, u); |
79 | | } |
80 | | |
81 | | template <class T, class U> |
82 | 857k | constexpr bool cmp_greater_equal(T t, U u) noexcept { |
83 | 857k | return !cmp_less(t, u); |
84 | 857k | } |
85 | | |
86 | | } // namespace std_util |
87 | | |
88 | | } // namespace yb |
89 | | |
90 | | #endif // YB_UTIL_STD_UTIL_H |