-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobi.hpp
361 lines (310 loc) · 11 KB
/
cobi.hpp
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
360
361
/* Compile time bound integers. For documentation, see README.md.
* (C) Srdjan Veljkovic
* License: MIT (see LICENSE)
*/
#if !defined(INC_COBI)
#define INC_COBI
#include <type_traits>
#include <limits>
#include <algorithm>
template<class T, T D, T G = D> struct cobrng {
static_assert(std::is_integral<T>::value, "T must be an integer type");
static_assert(D <= G, "Upper bound must be at least as high as lower");
static_assert(std::numeric_limits<T>::max() > G, "Upper bound must be less than maximum value");
T i;
public:
constexpr cobrng() : i(D) {}
static constexpr cobrng begin() { return cobrng{}; }
static constexpr cobrng end() { cobrng x; x.i = G+1; return x; }
constexpr cobrng& operator++() { i = (i <= G) ? i+1 : i; return *this; }
constexpr cobrng& operator--() { i = (i > D) ? i-1 : i; return *this; }
constexpr const T operator*() const { return i; }
constexpr bool operator==(cobrng<T,D,G> x) { return i == x.i; }
constexpr bool operator!=(cobrng<T,D,G> x) { return i != x.i; }
};
template <class T, T D, T G = D> struct cobi {
static_assert(std::is_integral<T>::value, "T must be an integer type");
static_assert(D <= G, "Upper bound must be at least as high as lower");
static constexpr T DD = D;
static constexpr T GG = G;
constexpr cobi()
: i(D)
{
}
static constexpr cobrng<T,D,G> range() {
return cobrng<T,D,G>{};
}
template <T PD, T PG>
constexpr cobi(cobi<T, PD, PG> const& a)
: i(a.get())
{
static_assert(D <= PD, "Out of lower bounds");
static_assert(G >= PG, "Out of upper bounds");
}
template <T PD, T PG>
constexpr cobi(cobi<T, PD, PG>&& a)
: i(a.get())
{
static_assert(D <= PD, "Out of lower bounds");
static_assert(G >= PG, "Out of upper bounds");
}
template <T PD, T PG> void operator=(cobi<T, PD, PG> a)
{
static_assert(D <= PD, "Out of lower bounds");
static_assert(G >= PG, "Out of upper bounds");
i = a.get();
}
constexpr bool be(T x)
{
if ((x >= D) && (x <= G)) {
i = x;
return true;
}
return false;
}
static constexpr bool could(T x) { return ((x >= D) && (x <= G)); }
template <template <class> class V>
static constexpr V<cobi<T, D, G>> be(T x)
{
cobi<T, D, G> rzlt;
if (rzlt.be(x)) {
return rzlt;
}
else {
return {};
}
}
constexpr bool advance()
{
if (i < G) {
++i;
return true;
}
return false;
}
constexpr bool ebb()
{
if (i > D) {
--i;
return true;
}
return false;
}
constexpr static cobi<T, D, G> greatest()
{
cobi<T, D, G> rslt;
rslt.be(G);
return rslt;
}
constexpr static cobi<T, D, G> smallest()
{
cobi<T, D, G> rslt;
rslt.be(D);
return rslt;
}
constexpr bool operator==(T x) const { return i == x; }
constexpr bool operator!=(T x) const { return !(i == x); }
constexpr bool operator<(T x) const { return i < x; }
constexpr bool operator<=(T x) const { return i <= x; }
constexpr bool operator>(T x) const { return !(i <= x); }
constexpr bool operator>=(T x) const { return !(i < x); }
template <class U, U D1, U G1, U D2, U G2>
constexpr friend bool operator==(cobi<U, D1, G1> x, cobi<U, D2, G2> y);
template <class U, U D1, U G1>
constexpr bool operator!=(cobi<U, D1, G1> x) const
{
return !(*this == x);
}
template <class U, U D1, U G1, U D2, U G2>
constexpr friend bool operator<(cobi<U, D1, G1> x, cobi<U, D2, G2> y);
template <class U, U D1, U G1>
constexpr bool operator<=(cobi<U, D1, G1> x) const
{
return (*this < x) || (*this == x);
}
template <class U, U D1, U G1>
constexpr bool operator>(cobi<U, D1, G1> x) const
{
return !(*this <= x);
}
template <class U, U D1, U G1>
constexpr bool operator>=(cobi<U, D1, G1> x) const
{
return !(*this < x);
}
template <class U, U D1, U G1>
friend constexpr cobi<U, -G1, -D1> operator-(cobi<U, D1, G1> x);
template <class U, U D1, U G1, U D2, U G2>
friend constexpr cobi<U, D1 + D2, G1 + G2> operator+(cobi<U, D1, G1> x, cobi<U, D2, G2> y);
template <class U, U D1, U G1, U D2, U G2>
friend constexpr cobi<U, D1 - G2, G1 - D2> operator-(cobi<U, D1, G1> x, cobi<U, D2, G2> y);
template <class U, U D1, U G1, U D2, U G2>
friend constexpr cobi<U,
std::min(std::min(D1* D2, G1* G2), std::min(D1* G2, G1* D2)),
std::max(std::max(D1* D2, G1* G2), std::max(D1* G2, G1* D2))>
operator*(cobi<U, D1, G1> x, cobi<U, D2, G2> y);
template <class U, U D1, U G1, U D2, U G2>
friend constexpr cobi<U,
std::min(std::min(D1 / D2, G1 / G2), std::min(D1 / G2, G1 / D2)),
std::max(std::max(D1 / D2, G1 / G2), std::max(D1 / G2, G1 / D2))>
operator/(cobi<U, D1, G1> x, cobi<U, D2, G2> y);
template <class U, U D1, U G1, U D2, U G2>
friend constexpr cobi<U,
std::min(std::min(D1 % D2, G1 % G2), std::min(D1 % G2, G1 % D2)),
std::max(std::max(D1 % D2, G1 % G2), std::max(D1 % G2, G1 % D2))>
operator%(cobi<U, D1, G1> x, cobi<U, D2, G2> y);
constexpr T get() const { return i; }
private:
T i;
};
template <int D, int G = D> using cobint = cobi<int, D, G>;
template <int D, int G = D> constexpr auto cobic = cobi<int, D, G>{};
template <class T, T D1, T G1, T D2, T G2>
constexpr bool operator==(cobi<T, D1, G1> x, cobi<T, D2, G2> y)
{
if constexpr ((G1 < D2) || (D1 > G2)) {
return false;
}
return x.i == y.i;
}
template <class T, T D, T G> constexpr bool operator==(T x, cobi<T, D, G> y)
{
return y == x;
}
template <class T, T D, T G> constexpr bool operator!=(T x, cobi<T, D, G> y)
{
return y != x;
}
template <class T, T D, T G> constexpr bool operator<(T x, cobi<T, D, G> y)
{
return y < x;
}
template <class T, T D, T G> constexpr bool operator<=(T x, cobi<T, D, G> y)
{
return y <= x;
}
template <class T, T D, T G> constexpr bool operator>(T x, cobi<T, D, G> y)
{
return y > x;
}
template <class T, T D, T G> constexpr bool operator>=(T x, cobi<T, D, G> y)
{
return y >= x;
}
template <class T, T D1, T G1, T D2, T G2>
inline constexpr bool operator<(cobi<T, D1, G1> x, cobi<T, D2, G2> y)
{
if constexpr (G1 < D2) {
return true;
}
else if constexpr (D1 > G2) {
return false;
}
return x.i < y.i;
}
template <class T, T D, T G> constexpr cobi<T, -G, -D> operator-(cobi<T, D,G> x) {
cobi<T, -G, -D> r;
r.i = -x.i;
return r;
}
template <class T, T D1, T G1, T D2, T G2>
constexpr cobi<T, D1+D2, G1+G2> operator+(cobi<T, D1,G1> x, cobi<T, D2,G2> y) {
#if defined(_MSC_VER)
constexpr auto upperG = std::max(G1, G2);
constexpr auto lowerG = std::min(G1, G2);
if constexpr (upperG >= 0) {
static_assert(std::numeric_limits<T>::max() - upperG >= lowerG,
"Addition Overflow");
}
constexpr auto upperD = std::max(D1, D2);
constexpr auto lowerD = std::min(D1, D2);
if constexpr (lowerD < 0) {
static_assert(std::numeric_limits<T>::lowest() - lowerD <= upperD,
"Addition Underflow");
}
#endif // defined(_MSC_VER)
cobi<T, D1 + D2, G1 + G2> r;
r.i = x.i + y.i;
return r;
}
template <class T, T D1, T G1, T D2, T G2>
constexpr cobi<T, D1-G2, G1-D2> operator-(cobi<T, D1,G1> x, cobi<T, D2,G2> y) {
return x + (-y);
}
template <class T, T D1, T G1, T D2, T G2>
constexpr cobi<T, std::min(std::min(D1*D2, G1*G2), std::min(D1*G2, G1*D2)),
std::max(std::max(D1*D2, G1*G2), std::max(D1*G2, G1*D2))>
operator*(cobi<T, D1,G1> x, cobi<T, D2,G2> y) {
#if defined(_MSC_VER)
if constexpr ((G1 > 0) && (G2 > 0)) {
static_assert(std::numeric_limits<T>::max() / G1 >= G2,
"Multiplication Overflow");
}
else if constexpr ((G1 > 0) && (D2 < 0)) {
static_assert(std::numeric_limits<T>::lowest() / D2 >= G1,
"Multiplication Underflow");
}
else if constexpr ((G1 < 0) && (G2 > 0)) {
static_assert(std::numeric_limits<T>::lowest() / G1 >= G2,
"Multiplication Underflow");
}
if constexpr ((D1 < 0) && (D2 < 0)) {
static_assert(std::numeric_limits<T>::max() / D1 <= D2,
"Multiplication Overflow");
}
#endif // defined(_MSC_VER)
cobi<T,
std::min(std::min(D1 * D2, G1 * G2), std::min(D1 * G2, G1 * D2)),
std::max(std::max(D1 * D2, G1 * G2), std::max(D1 * G2, G1 * D2))>
r;
r.i = x.i * y.i;
return r;
}
template <class T, T D1, T G1, T D2, T G2>
constexpr cobi<T, std::min(std::min(D1/D2, G1/G2), std::min(D1/G2, G1/D2)),
std::max(std::max(D1/D2, G1/G2), std::max(D1/G2, G1/D2))>
operator/(cobi<T, D1,G1> x, cobi<T, D2,G2> y)
{
static_assert((D1 > std::numeric_limits<T>::lowest()) || (G2 < -1) || (D2 > -1),
"Division Overflow");
static_assert((G2 < 0) || (D2 > 0), "Possible division by zero");
cobi<T,
std::min(std::min(D1 / D2, G1 / G2), std::min(D1 / G2, G1 / D2)),
std::max(std::max(D1 / D2, G1 / G2), std::max(D1 / G2, G1 / D2))>
r;
r.i = x.i / y.i;
return r;
}
template <class T, T D1, T G1, T D2, T G2>
constexpr cobi<T, std::min(std::min(D1%D2, G1%G2), std::min(D1%G2, G1%D2)),
std::max(std::max(D1%D2, G1%G2), std::max(D1%G2, G1%D2))>
operator%(cobi<T, D1,G1> x, cobi<T, D2,G2> y)
{
static_assert((D1 > std::numeric_limits<T>::lowest()) || (G2 < -1) || (D2 > -1),
"Modulo/Division Overflow");
static_assert((G2 < 0) || (D2 > 0), "Possible modulo/division by zero");
cobi<T,
std::min(std::min(D1 % D2, G1 % G2), std::min(D1 % G2, G1 % D2)),
std::max(std::max(D1 % D2, G1 % G2), std::max(D1 % G2, G1 % D2))>
r;
r.i = x.i / y.i;
return r;
}
template<class T, T D, T G = D> struct cobirange {
static_assert(std::is_integral<T>::value, "T must be an integer type");
static_assert(D <= G, "Upper bound must be at least as high as lower");
static_assert(std::numeric_limits<T>::max() > G, "Upper bound must be less than maximum value");
cobi<T,D,G+1> i;
public:
using value_type = cobi<T,D,G+1>;
constexpr cobirange() {}
constexpr cobirange(value_type x) { i = x; }
static constexpr cobirange begin() { return value_type::smallest(); }
static constexpr cobirange end() { return value_type::greatest(); }
constexpr cobirange& operator++() { i.advance(); return *this; }
constexpr cobirange& operator--() { i.ebb(); return *this; }
constexpr const cobi<T,D,G> operator*() const { cobi<T,D,G> r; r.be(i.get()); return r; }
constexpr bool operator==(cobirange x) { return i == x.i; }
constexpr bool operator!=(cobirange x) { return i != x.i; }
};
#endif // !defined(INC_COBI)