-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathswitch_ranges.h
executable file
·378 lines (307 loc) · 7.27 KB
/
switch_ranges.h
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// See also http://en.cppreference.com/w/cpp/algorithm/iota
#pragma once
#include <utility>
// range is (start():inclusive ... stop():exclusive)
// i.e [left, right)
template<typename VT = uint32_t, typename LT = uint32_t>
struct range_base final
{
struct iterator
{
VT i;
constexpr iterator(const VT index)
: i{index}
{
}
constexpr VT inline operator*() const
{
return i;
}
constexpr void operator++() noexcept
{
++i;
}
constexpr bool operator!=(const iterator &o) const noexcept
{
return i != o.i;
}
};
VT offset;
LT len;
constexpr iterator begin() const noexcept
{
return iterator(offset);
}
constexpr iterator end() const noexcept
{
return iterator(stop());
}
constexpr range_base()
: offset{0}, len{0}
{
}
constexpr range_base(const range_base &o)
: offset(o.offset), len(o.len)
{
}
constexpr range_base(const VT _o, const LT _l)
: offset(_o), len(_l)
{
}
//e.g range32_t{10,15}
constexpr range_base(const std::pair<VT, VT> p)
: offset{p.first}, len{p.second - p.first}
{
}
constexpr range_base(const LT l)
: offset{0}, len{l}
{
}
constexpr auto size() const noexcept
{
return len;
}
constexpr auto empty() const noexcept
{
return 0 == len;
}
constexpr operator bool() const noexcept
{
return len;
}
constexpr bool SpansAll() const noexcept
{
return std::numeric_limits<VT>::min() == offset && std::numeric_limits<VT>::max() == stop();
}
void SetSpansAll() noexcept
{
offset = std::numeric_limits<VT>::min();
len = std::numeric_limits<VT>::max() - offset;
}
constexpr void Set(const VT _o, const LT _l) noexcept
{
offset = _o;
len = _l;
}
constexpr void setStartEnd(const VT lo, const VT hi) noexcept
{
offset = lo;
len = hi - lo;
}
constexpr auto &operator=(const range_base &o) noexcept
{
offset = o.offset;
len = o.len;
return *this;
}
constexpr void SetEnd(const VT e) noexcept
{
len = e - offset;
}
// Matching SetEnd(); adjusts offset of a valid range
constexpr void reset_offset(const VT start) noexcept
{
len = stop() - start;
offset = start;
}
constexpr VT mid() const noexcept // (left + right) / 2
{
return offset + (len >> 1);
}
constexpr VT stop() const noexcept
{
return offset + len;
}
constexpr VT start() const noexcept
{
return offset;
}
// TODO: optimize
// Very handy for iterating a subset e.g
// for (auto i : range32(offset, perPage).ClippedTo(total) { .. }
constexpr range_base<VT, LT> ClippedTo(const VT lim) const
{
range_base<VT, LT> res;
res.offset = Min(offset, lim);
res.len = Min(stop(), lim) - res.offset;
return res;
}
constexpr bool Contains(const VT o) const noexcept
{
// https://twitter.com/EricLengyel/status/546120250450653184
// Single comparison impl. Works fine except shouldn't work for 64bit scalars
return sizeof(VT) == 8
? o >= offset && o < stop()
: uint32_t(o - offset) < len; // o in [offset, offset+len)
}
constexpr bool operator<(const range_base &o) const noexcept
{
return offset < o.offset || (offset == o.offset && len < o.len);
}
constexpr bool operator<=(const range_base &o) const noexcept
{
return offset < o.offset || (offset == o.offset && len <= o.len);
}
constexpr bool operator>(const range_base &o) const noexcept
{
return offset > o.offset || (offset == o.offset && len > o.len);
}
constexpr bool operator>=(const range_base &o) const noexcept
{
return offset > o.offset || (offset == o.offset && len >= o.len);
}
template<typename T>
constexpr bool operator==(const T &o) const noexcept
{
return offset == o.offset && len == o.len;
}
template<typename T>
constexpr bool operator!=(const T &o) const noexcept
{
return offset != o.offset || len != o.len;
}
range_base Intersection(const range_base &o) const noexcept
{
// A range containing the indices that exist in both ranges
if (stop() <= o.offset || o.stop() <= offset)
return range_base(0, 0);
else
{
const auto _o = Max(offset, o.offset);
return range_base(_o, Min(stop(), o.stop()) - _o);
}
}
void ClipOffsetTo(const VT o) noexcept
{
if (offset < o)
{
if (o >= stop())
{
offset = o;
len = 0;
}
else
{
const auto d = o - offset;
offset = o;
len -= d;
}
}
}
void ClipEndTo(const VT e) noexcept
{
const auto end = stop();
if (e < end)
{
if (e < offset)
len = 0;
else
len -= end - e;
}
}
constexpr bool Overlaps(const range_base &o) const noexcept
{
// range is (start() inclusive, stop() non inclusive)
// e.g [start, end)
//
// alternative formula: a0 <= b1 && b0 <= a1
// https://fgiesen.wordpress.com/2011/10/16/checking-for-interval-overlap/
return !(stop() <= o.offset || o.stop() <= offset);
}
constexpr bool Contains(const range_base &o) const noexcept
{
return offset <= o.offset && stop() >= o.stop();
}
constexpr auto Union(const range_base &o) const noexcept
{
const auto _o = Min(offset, o.offset);
return range_base(_o, Max(stop(), o.stop()) - _o);
}
// http://en.wikipedia.org/wiki/Disjoint_union
// Make sure they overlap
uint8_t DisjointUnion(const range_base &o, range_base *out) const noexcept
{
const range_base *const b = out;
if (offset < o.offset)
{
out->offset = offset;
out->len = o.offset - offset;
++out;
}
else if (o.offset < offset)
{
out->offset = o.offset;
out->len = offset - o.offset;
++out;
}
const auto thisEnd = stop(), thatEnd = o.stop();
if (thisEnd < thatEnd)
{
out->offset = thisEnd;
out->len = thatEnd - thisEnd;
++out;
}
else if (thatEnd < thisEnd)
{
out->offset = thatEnd;
out->len = thisEnd - thatEnd;
++out;
}
return out - b;
}
void TrimLeft(const LT span) noexcept
{
offset+=span;
len-=span;
}
[[deprecated("use reset() please")]] void Unset()
{
offset = 0;
len = 0;
}
constexpr void reset() noexcept
{
offset = 0;
len = 0;
}
};
// e.g InBetweenRange(tm.tm_hour, 1, 5)
template<typename VT>
static constexpr bool IsBetweenRange(const VT v, const VT s, const VT e) noexcept
{
return sizeof(VT) == 8
? v >= s && v < e
: uint32_t(v - s) < (e - s); // o in [offset, offset+len)
};
template<typename VT>
static constexpr bool IsBetweenRangeInclusive(const VT v, const VT s, const VT e) noexcept
{
return sizeof(VT) == 8
? v >= s && v <= e
: uint32_t(v - s) <= (e - s); // o in [offset, offset+len]
};
using range8_t = range_base<uint8_t, uint8_t>;
using range16_t = range_base<uint16_t, uint16_t>;
using range32_t = range_base<uint32_t, uint32_t>;
using range64_t = range_base<uint64_t, uint64_t>;
using rangestr_t = range_base<const char *, uint32_t>; // Please ust strwlen instead
// great for iteration e.g
// {
// struct foo values[128];
// uint8_t cnt=5;
//
// for (const auto v : Switch::make_range(values, cnt)) { .. }
// }
template<typename VT, typename LT>
static constexpr auto MakeRange(const VT s, const LT l) noexcept -> range_base<VT, LT>
{
return {s, l};
}
namespace Switch
{
template<typename VT, typename LT>
static constexpr auto make_range(const VT s, const LT l) noexcept
{
return range_base<VT, LT>(s, l);
}
}