-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinteger.cpp
More file actions
executable file
·359 lines (276 loc) · 12.6 KB
/
Copy pathinteger.cpp
File metadata and controls
executable file
·359 lines (276 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright David Stone 2018.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module;
#include <bounded/assert.hpp>
#include <bounded/conditional.hpp>
export module bounded.integer;
import bounded.bounded_by_range;
import bounded.bounded_integer;
import bounded.builtin_integer;
import bounded.builtin_min_max_value;
import bounded.comparison;
import bounded.concepts;
import bounded.homogeneous_equals;
import bounded.integral;
import bounded.is_bounded_integer;
import bounded.isomorphic_to_integral;
import bounded.normalize;
import bounded.safe_compare;
import bounded.safe_extreme;
import bounded.unchecked;
import bounded.underlying_type_t;
import numeric_traits;
import std_module;
namespace bounded {
export template<auto minimum, auto maximum>
struct integer;
} // namespace bounded
template<auto minimum, auto maximum>
constexpr auto numeric_traits::max_value<bounded::integer<minimum, maximum>> = bounded::integer<maximum, maximum>();
template<auto minimum, auto maximum>
constexpr auto numeric_traits::min_value<bounded::integer<minimum, maximum>> = bounded::integer<minimum, minimum>();
namespace bounded {
template<auto minimum, auto maximum>
constexpr auto is_bounded_integer<integer<minimum, maximum>> = true;
template<auto minimum, auto maximum>
constexpr auto builtin_max_value<integer<minimum, maximum>> = maximum;
template<auto minimum, auto maximum>
constexpr auto builtin_min_value<integer<minimum, maximum>> = minimum;
} // namespace bounded
// I do not have to specialize the single-argument version, as it just returns
// the type passed in, which will always work.
template<bounded::bounded_integer LHS, bounded::bounded_integer RHS> requires std::same_as<LHS, std::decay_t<LHS>> and std::same_as<RHS, std::decay_t<RHS>>
struct std::common_type<LHS, RHS> {
using type = bounded::integer<
bounded::normalize<bounded::safe_min(
bounded::builtin_min_value<LHS>,
bounded::builtin_min_value<RHS>
)>,
bounded::normalize<bounded::safe_max(
bounded::builtin_max_value<LHS>,
bounded::builtin_max_value<RHS>
)>
>;
};
namespace bounded {
namespace detail {
export template<integral T, integral Minimum, integral Maximum>
constexpr auto assume_in_range_impl(T const value, Minimum, Maximum) {
return integer<
normalize<safe_max(builtin_min_value<Minimum>, builtin_min_value<T>)>,
normalize<safe_min(builtin_max_value<Maximum>, builtin_max_value<T>)>
>(value, unchecked);
}
} // namespace detail
export constexpr auto assume_in_range(integral auto const value, integral auto const minimum, integral auto const maximum) {
if (value < minimum or maximum < value) {
std::unreachable();
}
return ::bounded::detail::assume_in_range_impl(value, minimum, maximum);
}
export template<integral Target>
constexpr auto assume_in_range(integral auto const value) {
constexpr auto minimum = numeric_traits::min_value<Target>;
constexpr auto maximum = numeric_traits::max_value<Target>;
if (integer(value) < integer(minimum) or integer(maximum) < integer(value)) {
std::unreachable();
}
if constexpr (bounded_integer<Target>) {
return ::bounded::detail::assume_in_range_impl(value, minimum, maximum);
} else {
return static_cast<Target>(value);
}
}
export template<auto value>
using constant_t = integer<value, value>;
export template<auto value>
constexpr auto constant = constant_t<normalize<value>>();
// TODO: Report clang bug requiring this specialization
template<auto value_>
struct integer<value_, value_> {
static_assert(std::same_as<decltype(value_), std::remove_const_t<decltype(normalize<value_>)>>);
using underlying_type = underlying_type_t<value_, value_>;
static constexpr auto value() -> underlying_type {
return static_cast<underlying_type>(value_);
}
integer() = default;
constexpr integer(integer const &) = default;
constexpr integer(integer &&) = default;
constexpr integer(overlapping_integer<value_, value_> auto, unchecked_t) {
}
template<bounded_by_range<value_, value_> T>
constexpr explicit(std::is_enum_v<T>) integer(T) {
}
constexpr auto operator=(integer const & other) & -> integer & = default;
constexpr auto operator=(integer && other) & -> integer & = default;
constexpr auto operator=(overlapping_integer<value_, value_> auto const other) & -> integer & {
if (other != constant<value_>) {
std::unreachable();
}
return *this;
}
// Do not verify that the value is in range because the user has requested a
// conversion out of the safety of bounded::integer. It is subject to all
// the standard rules of conversion from one integer type to another.
template<typename T> requires (builtin_integer<T> or std::is_enum_v<T> or std::floating_point<T>)
constexpr explicit operator T() const {
return static_cast<T>(value_);
}
friend constexpr auto operator<=>(integer, integer) = default;
};
template<auto minimum, auto maximum>
struct integer {
static_assert(std::same_as<decltype(minimum), std::remove_const_t<decltype(normalize<minimum>)>>);
static_assert(std::same_as<decltype(maximum), std::remove_const_t<decltype(normalize<maximum>)>>);
static_assert(safe_compare(minimum, maximum) <= 0, "Maximum cannot be less than minimum");
using underlying_type = underlying_type_t<minimum, maximum>;
constexpr auto value() const -> underlying_type {
[[assume(minimum <= m_value and m_value <= maximum)]];
return m_value;
}
integer() = default;
constexpr integer(integer const &) = default;
constexpr integer(integer &&) = default;
constexpr integer(overlapping_integer<minimum, maximum> auto const other, unchecked_t):
m_value(underlying_type(other))
{
}
template<bounded_by_range<minimum, maximum> T>
constexpr explicit(std::same_as<T, bool> or std::is_enum_v<T>) integer(T const other):
m_value(underlying_type(other))
{
}
constexpr auto operator=(integer const & other) & -> integer & = default;
constexpr auto operator=(integer && other) & -> integer & = default;
constexpr auto operator=(overlapping_integer<minimum, maximum> auto const other) & -> integer & {
return *this = integer(::bounded::assume_in_range(other, constant<minimum>, constant<maximum>));
}
// Do not verify that the value is in range because the user has requested a
// conversion out of the safety of bounded::integer. It is subject to all
// the standard rules of conversion from one integer type to another.
template<typename T> requires (builtin_integer<T> or std::is_enum_v<T> or std::floating_point<T>)
constexpr explicit operator T() const {
return static_cast<T>(value());
}
friend constexpr auto operator<=>(integer, integer) = default;
public:
// Do not access this directly. This must be public to be usable as a
// non-type template parameter in C++20.
[[no_unique_address]] underlying_type m_value;
};
template<typename T>
constexpr auto deduced_min = builtin_min_value<T>;
template<typename T>
constexpr auto deduced_max = builtin_max_value<T>;
template<typename T> requires(!numeric_traits::has_min_value<T>)
constexpr auto deduced_min<T> = builtin_min_value<std::underlying_type_t<T>>;
template<typename T> requires(!numeric_traits::has_max_value<T>)
constexpr auto deduced_max<T> = builtin_max_value<std::underlying_type_t<T>>;
template<typename T>
integer(T value) -> integer<deduced_min<T>, deduced_max<T>>;
} // namespace bounded
static_assert(bounded::bounded_integer<bounded::integer<0, 0>>);
static_assert(bounded::bounded_integer<bounded::integer<0, 1>>);
static_assert(bounded::integral<bounded::integer<0, 0>>);
static_assert(bounded::integral<bounded::integer<0, 1>>);
static_assert(bounded::isomorphic_to_integral<bounded::integer<0, 0>>);
static_assert(bounded::isomorphic_to_integral<bounded::integer<0, 1>>);
static_assert(bounded::assume_in_range(bounded::constant<5>, bounded::constant<0>, bounded::constant<10>) == bounded::constant<5>);
// This should not compile
// constexpr auto value2 = bounded::assume_in_range(15, bounded::constant<0>, bounded::constant<10>);
static_assert(bounded::assume_in_range(2, 1, 3) == bounded::integer(2));
static_assert(homogeneous_equals(bounded::normalize<bounded::constant<0>>, 0));
using type1 = bounded::integer<1, 5>;
using type2 = bounded::integer<3, 10>;
static_assert(std::same_as<std::common_type_t<type1, type2>, bounded::integer<1, 10>>);
using type3 = bounded::integer<-5, -5>;
static_assert(std::same_as<std::common_type_t<type1, type2, type3>, bounded::integer<-5, 10>>);
using type4 = bounded::integer<0, 0>;
static_assert(std::same_as<std::common_type_t<type1, type2, type3, type4>, bounded::integer<-5, 10>>);
static_assert(std::is_empty_v<bounded::constant_t<0>>);
static_assert(std::is_empty_v<bounded::constant_t<1>>);
static_assert(std::is_empty_v<bounded::constant_t<-1>>);
static_assert(std::is_empty_v<bounded::constant_t<bounded::builtin_min_value<numeric_traits::max_signed_t>>>);
static_assert(std::is_empty_v<bounded::constant_t<bounded::builtin_max_value<numeric_traits::max_signed_t>>>);
static_assert(std::is_empty_v<bounded::constant_t<bounded::builtin_max_value<numeric_traits::max_unsigned_t>>>);
static_assert(!bounded::convertible_to<bool, bounded::integer<0, 1>>);
static_assert(bounded::constructible_from<bounded::integer<0, 1>, bool>);
static_assert(!bounded::constructible_from<bounded::integer<0, 0>, bool>);
namespace check_constructibility {
constexpr auto min = numeric_traits::min_value<int>;
constexpr auto max = numeric_traits::max_value<int>;
using type = bounded::integer<min, max>;
static_assert(
bounded::overlapping_integer<type, min, max>,
"Bounds of type do not overlap its own range."
);
static_assert(
bounded::convertible_to<int, type>,
"Cannot convert integer type to bounded::integer with same range."
);
static_assert(
bounded::constructible_from<type, type, bounded::unchecked_t>,
"Cannot construct a type from itself with unchecked_t."
);
static_assert(
!bounded::convertible_to<bool, type>,
"Should not be able to convert a bool to a bounded::integer."
);
static_assert(
bounded::constructible_from<type, bool>,
"Should be able to construct a bounded::integer from a bool."
);
}
static_assert(homogeneous_equals(
BOUNDED_CONDITIONAL(true, bounded::constant<7>, bounded::constant<9>),
bounded::integer<7, 9>(bounded::constant<7>)
));
constexpr auto check_assignment() {
bounded::integer<0, 10> x(bounded::constant<5>);
static_assert(!std::is_assignable_v<decltype((x)), bounded::constant_t<11>>);
x = bounded::integer<10, 11>(bounded::constant<10>);
BOUNDED_ASSERT(x == bounded::constant<10>);
return true;
}
static_assert(check_assignment());
enum class bounded_enum{};
template<std::same_as<bounded_enum> T>
constexpr auto numeric_traits::min_value<T> = 0;
template<std::same_as<bounded_enum> T>
constexpr auto numeric_traits::max_value<T> = 0;
enum unscoped_enum : int {};
static_assert(!bounded::constructible_from<bounded::integer<0, 10>, unscoped_enum>);
static_assert(!bounded::convertible_to<unscoped_enum, bounded::integer<0, 10>>);
enum class scoped_enum {};
static_assert(!bounded::constructible_from<bounded::integer<0, 10>, scoped_enum>);
static_assert(!bounded::convertible_to<scoped_enum, bounded::integer<0, 10>>);
static_assert(bounded::constructible_from<bounded::integer<0, 10>, bounded_enum>);
static_assert(!bounded::convertible_to<bounded_enum, bounded::integer<0, 10>>);
static_assert(bounded::integer(bounded_enum{}) == bounded::constant<0>);
static_assert(bounded::constant<bounded_enum{}> == bounded::constant<0>);
enum class bounded_integer_enum{};
template<std::same_as<bounded_integer_enum> T>
constexpr auto numeric_traits::min_value<T> = bounded::constant<0>;
template<std::same_as<bounded_integer_enum> T>
constexpr auto numeric_traits::max_value<T> = bounded::constant<0>;
static_assert(!bounded::convertible_to<bounded_integer_enum, bounded::integer<0, 0>>);
static_assert(bounded::integer(bounded_integer_enum{}) == bounded::constant<0>);
static_assert(bounded::constant<bounded_integer_enum{}> == bounded::constant<0>);
enum class enum_bounded_enum{};
template<std::same_as<enum_bounded_enum> T>
constexpr auto numeric_traits::min_value<T> = enum_bounded_enum();
template<std::same_as<enum_bounded_enum> T>
constexpr auto numeric_traits::max_value<T> = enum_bounded_enum();
static_assert(!bounded::convertible_to<enum_bounded_enum, bounded::integer<0, 0>>);
static_assert(bounded::integer(enum_bounded_enum{}) == bounded::constant<0>);
static_assert(bounded::constant<enum_bounded_enum{}> == bounded::constant<0>);
auto check_compile_time_comparison_of_runtime_constant() -> void {
auto one = bounded::constant<1>;
auto two = bounded::constant<2>;
static_assert(one == one);
static_assert(one <=> one == 0);
static_assert(one != two);
static_assert(one <=> two < 0);
}