|
| 1 | +module; |
| 2 | + |
| 3 | +#include <concepts> |
| 4 | +#include <limits> |
| 5 | +#include <type_traits> |
| 6 | + |
| 7 | +export module mcpplibs.primitives.algorithms.limits; |
| 8 | + |
| 9 | +import mcpplibs.primitives.underlying; |
| 10 | + |
| 11 | +namespace mcpplibs::primitives::algorithms::details { |
| 12 | + |
| 13 | +template <typename T> |
| 14 | +concept has_numeric_limits = |
| 15 | + std::numeric_limits<std::remove_cv_t<T>>::is_specialized; |
| 16 | + |
| 17 | +template <typename T> |
| 18 | +consteval auto category_from_type() -> underlying::category { |
| 19 | + using value_type = std::remove_cv_t<T>; |
| 20 | + if constexpr (std_bool<value_type>) { |
| 21 | + return underlying::category::boolean; |
| 22 | + } else if constexpr (std_char<value_type>) { |
| 23 | + return underlying::category::character; |
| 24 | + } else if constexpr (std::numeric_limits<value_type>::is_integer) { |
| 25 | + return underlying::category::integer; |
| 26 | + } else { |
| 27 | + return underlying::category::floating; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +} // namespace mcpplibs::primitives::algorithms::details |
| 32 | + |
| 33 | +export namespace mcpplibs::primitives::algorithms { |
| 34 | + |
| 35 | +template <typename T> struct limits { |
| 36 | + using value_type = std::remove_cv_t<T>; |
| 37 | + using rep_type = value_type; |
| 38 | + |
| 39 | + static constexpr bool enabled = false; |
| 40 | + static constexpr bool is_specialized = false; |
| 41 | + static constexpr bool is_bounded = false; |
| 42 | + static constexpr bool is_exact = false; |
| 43 | + static constexpr bool is_signed = false; |
| 44 | + static constexpr bool is_integer = false; |
| 45 | + static constexpr bool is_iec559 = false; |
| 46 | + static constexpr bool has_infinity = false; |
| 47 | + static constexpr bool has_quiet_nan = false; |
| 48 | + static constexpr int digits = 0; |
| 49 | + static constexpr int digits10 = 0; |
| 50 | + static constexpr int radix = 0; |
| 51 | + static constexpr auto kind = static_cast<underlying::category>(-1); |
| 52 | + |
| 53 | + static constexpr auto min() noexcept -> value_type { return {}; } |
| 54 | + static constexpr auto lowest() noexcept -> value_type { return {}; } |
| 55 | + static constexpr auto max() noexcept -> value_type { return {}; } |
| 56 | + static constexpr auto epsilon() noexcept -> value_type { return {}; } |
| 57 | + static constexpr auto infinity() noexcept -> value_type { return {}; } |
| 58 | + static constexpr auto quiet_nan() noexcept -> value_type { return {}; } |
| 59 | +}; |
| 60 | + |
| 61 | +template <typename T> |
| 62 | + requires details::has_numeric_limits<T> |
| 63 | +struct limits<T> { |
| 64 | + using value_type = std::remove_cv_t<T>; |
| 65 | + using rep_type = value_type; |
| 66 | + |
| 67 | + static constexpr bool enabled = true; |
| 68 | + static constexpr bool is_specialized = true; |
| 69 | + static constexpr bool is_bounded = std::numeric_limits<value_type>::is_bounded; |
| 70 | + static constexpr bool is_exact = std::numeric_limits<value_type>::is_exact; |
| 71 | + static constexpr bool is_signed = std::numeric_limits<value_type>::is_signed; |
| 72 | + static constexpr bool is_integer = std::numeric_limits<value_type>::is_integer; |
| 73 | + static constexpr bool is_iec559 = std::numeric_limits<value_type>::is_iec559; |
| 74 | + static constexpr bool has_infinity = |
| 75 | + std::numeric_limits<value_type>::has_infinity; |
| 76 | + static constexpr bool has_quiet_nan = |
| 77 | + std::numeric_limits<value_type>::has_quiet_NaN; |
| 78 | + static constexpr int digits = std::numeric_limits<value_type>::digits; |
| 79 | + static constexpr int digits10 = std::numeric_limits<value_type>::digits10; |
| 80 | + static constexpr int radix = std::numeric_limits<value_type>::radix; |
| 81 | + static constexpr auto kind = details::category_from_type<value_type>(); |
| 82 | + |
| 83 | + static constexpr auto min() noexcept -> value_type { |
| 84 | + return std::numeric_limits<value_type>::min(); |
| 85 | + } |
| 86 | + |
| 87 | + static constexpr auto lowest() noexcept -> value_type { |
| 88 | + return std::numeric_limits<value_type>::lowest(); |
| 89 | + } |
| 90 | + |
| 91 | + static constexpr auto max() noexcept -> value_type { |
| 92 | + return std::numeric_limits<value_type>::max(); |
| 93 | + } |
| 94 | + |
| 95 | + static constexpr auto epsilon() noexcept -> value_type { |
| 96 | + return std::numeric_limits<value_type>::epsilon(); |
| 97 | + } |
| 98 | + |
| 99 | + static constexpr auto infinity() noexcept -> value_type { |
| 100 | + return std::numeric_limits<value_type>::infinity(); |
| 101 | + } |
| 102 | + |
| 103 | + static constexpr auto quiet_nan() noexcept -> value_type { |
| 104 | + return std::numeric_limits<value_type>::quiet_NaN(); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +template <underlying_type T> |
| 109 | + requires (!details::has_numeric_limits<T> && |
| 110 | + !std::same_as<std::remove_cv_t<T>, |
| 111 | + typename underlying::traits<std::remove_cv_t<T>>::rep_type> && |
| 112 | + limits<typename underlying::traits<std::remove_cv_t<T>>::rep_type>::enabled) |
| 113 | +struct limits<T> { |
| 114 | + using value_type = std::remove_cv_t<T>; |
| 115 | + using rep_type = underlying::traits<value_type>::rep_type; |
| 116 | + using rep_limits = limits<rep_type>; |
| 117 | + |
| 118 | + static constexpr bool enabled = true; |
| 119 | + static constexpr bool is_specialized = rep_limits::is_specialized; |
| 120 | + static constexpr bool is_bounded = rep_limits::is_bounded; |
| 121 | + static constexpr bool is_exact = rep_limits::is_exact; |
| 122 | + static constexpr bool is_signed = rep_limits::is_signed; |
| 123 | + static constexpr bool is_integer = rep_limits::is_integer; |
| 124 | + static constexpr bool is_iec559 = rep_limits::is_iec559; |
| 125 | + static constexpr bool has_infinity = rep_limits::has_infinity; |
| 126 | + static constexpr bool has_quiet_nan = rep_limits::has_quiet_nan; |
| 127 | + static constexpr int digits = rep_limits::digits; |
| 128 | + static constexpr int digits10 = rep_limits::digits10; |
| 129 | + static constexpr int radix = rep_limits::radix; |
| 130 | + static constexpr auto kind = underlying::traits<value_type>::kind; |
| 131 | + |
| 132 | + static constexpr auto min() noexcept -> value_type { |
| 133 | + return underlying::traits<value_type>::from_rep(rep_limits::min()); |
| 134 | + } |
| 135 | + |
| 136 | + static constexpr auto lowest() noexcept -> value_type { |
| 137 | + return underlying::traits<value_type>::from_rep(rep_limits::lowest()); |
| 138 | + } |
| 139 | + |
| 140 | + static constexpr auto max() noexcept -> value_type { |
| 141 | + return underlying::traits<value_type>::from_rep(rep_limits::max()); |
| 142 | + } |
| 143 | + |
| 144 | + static constexpr auto epsilon() noexcept -> value_type { |
| 145 | + return underlying::traits<value_type>::from_rep(rep_limits::epsilon()); |
| 146 | + } |
| 147 | + |
| 148 | + static constexpr auto infinity() noexcept -> value_type { |
| 149 | + return underlying::traits<value_type>::from_rep(rep_limits::infinity()); |
| 150 | + } |
| 151 | + |
| 152 | + static constexpr auto quiet_nan() noexcept -> value_type { |
| 153 | + return underlying::traits<value_type>::from_rep(rep_limits::quiet_nan()); |
| 154 | + } |
| 155 | +}; |
| 156 | + |
| 157 | +template <typename T> |
| 158 | +concept limited_type = limits<std::remove_cvref_t<T>>::enabled; |
| 159 | + |
| 160 | +template <typename T> |
| 161 | +using limit_value_t = limits<std::remove_cvref_t<T>>::value_type; |
| 162 | + |
| 163 | +template <limited_type T> |
| 164 | +constexpr auto min_value() noexcept( |
| 165 | + noexcept(limits<std::remove_cvref_t<T>>::min())) -> limit_value_t<T> { |
| 166 | + return limits<std::remove_cvref_t<T>>::min(); |
| 167 | +} |
| 168 | + |
| 169 | +template <limited_type T> |
| 170 | +constexpr auto lowest_value() noexcept( |
| 171 | + noexcept(limits<std::remove_cvref_t<T>>::lowest())) -> limit_value_t<T> { |
| 172 | + return limits<std::remove_cvref_t<T>>::lowest(); |
| 173 | +} |
| 174 | + |
| 175 | +template <limited_type T> |
| 176 | +constexpr auto max_value() noexcept( |
| 177 | + noexcept(limits<std::remove_cvref_t<T>>::max())) -> limit_value_t<T> { |
| 178 | + return limits<std::remove_cvref_t<T>>::max(); |
| 179 | +} |
| 180 | + |
| 181 | +template <limited_type T> |
| 182 | +constexpr auto epsilon_value() noexcept( |
| 183 | + noexcept(limits<std::remove_cvref_t<T>>::epsilon())) -> limit_value_t<T> { |
| 184 | + return limits<std::remove_cvref_t<T>>::epsilon(); |
| 185 | +} |
| 186 | + |
| 187 | +template <limited_type T> |
| 188 | +constexpr auto infinity_value() noexcept( |
| 189 | + noexcept(limits<std::remove_cvref_t<T>>::infinity())) -> limit_value_t<T> { |
| 190 | + return limits<std::remove_cvref_t<T>>::infinity(); |
| 191 | +} |
| 192 | + |
| 193 | +template <limited_type T> |
| 194 | +constexpr auto quiet_nan_value() noexcept( |
| 195 | + noexcept(limits<std::remove_cvref_t<T>>::quiet_nan())) -> limit_value_t<T> { |
| 196 | + return limits<std::remove_cvref_t<T>>::quiet_nan(); |
| 197 | +} |
| 198 | + |
| 199 | +} // namespace mcpplibs::primitives::algorithms |
0 commit comments