VCCC  2024.05
VisualCamp Common C++ library
cmp.hpp
Go to the documentation of this file.
1 //
2 // Created by YongGyu Lee on 5/8/24.
3 //
4 
5 #ifndef VCCC_UTILITY_CMP_HPP_
6 #define VCCC_UTILITY_CMP_HPP_
7 
8 #include <type_traits>
9 
14 
15 namespace vccc {
16 namespace detail {
17 
18 template<typename T, typename U, typename... SameSignity>
19 constexpr bool cmp_equal_impl(T t, U u, SameSignity...) noexcept {
20  return t == u;
21 }
22 
23 template<typename T, typename U>
24 constexpr bool cmp_equal_impl(T t, U u, std::true_type, std::false_type) noexcept {
25  return t >= 0 && std::make_unsigned_t<T>(t) == u;
26 }
27 
28 template<typename T, typename U>
29 constexpr bool cmp_equal_impl(T t, U u, std::false_type, std::true_type) noexcept {
30  return u >= 0 && std::make_unsigned_t<U>(u) == t;
31 }
32 
33 template<typename T, typename U, typename... SameSignity>
34 constexpr bool cmp_less_impl(T t, U u, SameSignity...) noexcept {
35  return t < u;
36 }
37 
38 template<typename T, typename U>
39 constexpr bool cmp_less_impl(T t, U u, std::true_type, std::false_type) noexcept {
40  return t < 0 || std::make_unsigned_t<T>(t) < u;
41 }
42 
43 template<typename T, typename U>
44 constexpr bool cmp_less_impl(T t, U u, std::false_type, std::true_type) noexcept {
45  return u >= 0 && t < std::make_unsigned_t<U>(u);
46 }
47 
48 template<typename T>
49 using cmp_requires_one =
50  conjunction<
51  std::is_integral<T>,
52  negation<is_character<T>>,
53  negation<std::is_same<T, bool>>
54  >;
55 template<typename T, typename U>
56 using cmp_requires = conjunction<cmp_requires_one<remove_cvref_t<T>>, cmp_requires_one<remove_cvref_t<U>>>;
57 
58 } // namespace detail
59 
62 
63 template<typename T, typename U>
64 constexpr bool cmp_equal(T t, U u) noexcept {
65  static_assert(detail::cmp_requires<T, U>::value, "Both values must be integral, not a character type and not bool");
66  return vccc::detail::cmp_equal_impl(t, u, std::is_signed<T>{}, std::is_signed<U>{});
67 }
68 
69 template<typename T, typename U>
70 constexpr bool cmp_not_equal(T t, U u) noexcept {
71  return !vccc::cmp_equal(t, u);
72 }
73 
74 template<typename T, typename U>
75 constexpr bool cmp_less(T t, U u) noexcept {
76  static_assert(detail::cmp_requires<T, U>::value, "Both values must be integral, not a character type and not bool");
77  return vccc::detail::cmp_less_impl(t, u, std::is_signed<T>{}, std::is_signed<U>{});
78 }
79 
80 template<typename T, typename U>
81 constexpr bool cmp_greater(T t, U u) noexcept {
82  return vccc::cmp_less(u, t);
83 }
84 
85 template<class T, class U>
86 constexpr bool cmp_less_equal(T t, U u) noexcept {
87  return !vccc::cmp_less(u, t);
88 }
89 
90 template<class T, class U>
91 constexpr bool cmp_greater_equal(T t, U u) noexcept {
92  return !vccc::cmp_less(t, u);
93 }
94 
96 
97 } // namespace vccc
98 
99 #endif // VCCC_UTILITY_CMP_HPP_
constexpr bool cmp_greater(T t, U u) noexcept
Definition: cmp.hpp:81
constexpr bool cmp_greater_equal(T t, U u) noexcept
Definition: cmp.hpp:91
constexpr bool cmp_not_equal(T t, U u) noexcept
Definition: cmp.hpp:70
constexpr bool cmp_less(T t, U u) noexcept
Definition: cmp.hpp:75
constexpr bool cmp_less_equal(T t, U u) noexcept
Definition: cmp.hpp:86
constexpr bool cmp_equal(T t, U u) noexcept
Definition: cmp.hpp:64
Definition: directory.h:12
Definition: conjunction.hpp:22