Sen API
Sen Libraries
Loading...
Searching...
No Matches
integer_compare.h
Go to the documentation of this file.
1// === integer_compare.h ===============================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_BASE_INTEGER_COMPARE_H
9#define SEN_CORE_BASE_INTEGER_COMPARE_H
10
11#if defined(__cpp_lib_integer_comparison_functions)
12# include <utility>
13#endif
14
15// std
16#include <type_traits>
17
18#if defined(__cpp_lib_integer_comparison_functions)
19namespace sen::std_util
20{
21using std::cmp_equal;
22using std::cmp_greater;
23using std::cmp_greater_equal;
24using std::cmp_less;
25using std::cmp_less_equal;
26using std::cmp_not_equal;
27} // namespace sen::std_util
28#else
29namespace sen::std_util
30{
31
32// Forward implementation of std::cmp_* from C++20.
33// Impl provided by: https://en.cppreference.com/w/cpp/utility/intcmp.html
34template <class T, class U>
35// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
36constexpr bool cmp_equal(T t, U u) noexcept
37{
38 if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
39 {
40 return t == u;
41 }
42 else if constexpr (std::is_signed_v<T>)
43 {
44 // NOLINTNEXTLINE(google-readability-casting): not a c-style cast
45 return t >= 0 && std::make_unsigned_t<T>(t) == u;
46 }
47 else
48 {
49 // NOLINTNEXTLINE(google-readability-casting): not a c-style cast
50 return u >= 0 && std::make_unsigned_t<U>(u) == t;
51 }
52}
53
54template <class T, class U>
55// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
56constexpr bool cmp_not_equal(T t, U u) noexcept
57{
58 return !cmp_equal(t, u);
59}
60
61template <class T, class U>
62// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
63constexpr bool cmp_less(T t, U u) noexcept
64{
65 if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
66 {
67 return t < u;
68 }
69 else if constexpr (std::is_signed_v<T>)
70 {
71 // NOLINTNEXTLINE(google-readability-casting): not a c-style cast
72 return t < 0 || std::make_unsigned_t<T>(t) < u;
73 }
74 else
75 {
76 // NOLINTNEXTLINE(google-readability-casting): not a c-style cast
77 return u >= 0 && t < std::make_unsigned_t<U>(u);
78 }
79}
80
81template <class T, class U>
82// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
83constexpr bool cmp_greater(T t, U u) noexcept
84{
85 return cmp_less(u, t);
86}
87
88template <class T, class U>
89// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
90constexpr bool cmp_less_equal(T t, U u) noexcept
91{
92 return !cmp_less(u, t);
93}
94
95template <class T, class U>
96// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
97constexpr bool cmp_greater_equal(T t, U u) noexcept
98{
99 return !cmp_less(t, u);
100}
101
102} // namespace sen::std_util
103#endif
104
105#endif // SEN_CORE_BASE_INTEGER_COMPARE_H
Definition bits.h:26
constexpr bool cmp_greater(T t, U u) noexcept
Definition integer_compare.h:83
constexpr bool cmp_equal(T t, U u) noexcept
Definition integer_compare.h:36
constexpr bool cmp_not_equal(T t, U u) noexcept
Definition integer_compare.h:56
constexpr bool cmp_less_equal(T t, U u) noexcept
Definition integer_compare.h:90
constexpr bool cmp_less(T t, U u) noexcept
Definition integer_compare.h:63
constexpr bool cmp_greater_equal(T t, U u) noexcept
Definition integer_compare.h:97