-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselector.hpp
612 lines (495 loc) · 18 KB
/
selector.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#pragma once
#include <array>
#include <numeric>
#include <functional>
#include "shape.hpp"
// ============================================================================
namespace nd // ND_API_START
{
template<int Rank, int Axis> struct selector;
/**
* Creates a selector without count (memory extent) information, e.g:
*
* auto sel = make_selector(_0|100|2, _|50|100);
*
* This selector can return size, skips, and shape information, but thinks
* it has a count of 1 on each axis.
*/
template<typename... Index>
static inline auto make_selector(Index... index)
{
return selector<sizeof...(Index), 0>().select(index...);
}
/**
* Returns a selector with a different count, by reading from the given
* iterator range. The iterator difference must match the selector rank,
* but no checking is performed that the existing selection is within the
* new count.
*/
template<typename Selector, typename First, typename Second>
static inline auto with_count(Selector sel, First begin, Second end)
{
auto it = begin;
auto n = 0;
if (end - begin != sel.rank)
{
throw std::invalid_argument("with_count got wrong number of axes");
}
while (it != end)
{
sel.count[n++] = int(*it++);
}
return sel;
}
} // ND_API_END
// ============================================================================
template<int Rank, int Axis = 0> // ND_IMPL_START
struct nd::selector
{
enum { rank = Rank, axis = Axis };
// ========================================================================
selector()
{
for (int n = 0; n < rank; ++n)
{
count[n] = 1;
start[n] = 0;
final[n] = 1;
skips[n] = 1;
}
}
template<typename... Dims>
selector(Dims... dims) : selector(std::array<int, rank>{dims...})
{
static_assert(sizeof...(Dims) == rank,
"selector: number of count arguments must match rank");
}
selector(std::array<int, rank> count) : count(count)
{
for (int n = 0; n < rank; ++n)
{
start[n] = 0;
final[n] = count[n];
skips[n] = 1;
}
}
selector(
std::array<int, rank> count,
std::array<int, rank> start,
std::array<int, rank> final,
std::array<int, rank> skips)
: count(count)
, start(start)
, final(final)
, skips(skips)
{
}
// ========================================================================
template <int R = rank, int A = axis, typename std::enable_if<A == rank - 1>::type* = nullptr>
selector<rank - 1, axis - 1> collapse() const
{
static_assert(rank > 0, "selector: cannot collapse zero-rank selector");
auto res = selector<rank - 1, axis - 1>();
for (int n = 0; n < rank - 2; ++n)
{
res.count[n] = count[n];
res.start[n] = start[n];
res.final[n] = final[n];
res.skips[n] = skips[n];
}
res.count[axis - 1] = count[axis] * count[axis - 1];
res.start[axis - 1] = count[axis] * start[axis - 1] + start[axis];
res.final[axis - 1] = count[axis] * final[axis - 1] + start[axis];
res.skips[axis - 1] = count[axis];
return res;
}
template <int R = rank, int A = axis, typename std::enable_if<A < rank - 1>::type* = nullptr>
selector<rank - 1, axis> collapse() const
{
static_assert(rank > 0, "selector: cannot collapse zero-rank selector");
auto res = selector<rank - 1, axis>();
for (int n = 0; n < axis; ++n)
{
res.count[n] = count[n];
res.start[n] = start[n];
res.final[n] = final[n];
res.skips[n] = skips[n];
}
for (int n = axis + 1; n < rank - 1; ++n)
{
res.count[n] = count[n + 1];
res.start[n] = start[n + 1];
res.final[n] = final[n + 1];
res.skips[n] = skips[n + 1];
}
res.count[axis] = count[axis + 1] * count[axis];
res.start[axis] = count[axis + 1] * start[axis] + start[axis + 1];
res.final[axis] = count[axis + 1] * final[axis] + start[axis + 1];
res.skips[axis] = 1;
return res;
}
selector<rank, axis + 1> skip(int skips_index) const
{
return select(std::make_tuple(start[axis], final[axis], skips_index));
}
selector<rank, axis + 1> slice(int lower_index, int upper_index, int skips_index) const
{
static_assert(axis < rank, "selector: cannot select on axis greater than or equal to rank");
auto res = selector<rank, axis + 1> { count, start, final, skips };
res.start[axis] = start[axis] + lower_index;
res.final[axis] = start[axis] + upper_index;
res.skips[axis] = skips[axis] * skips_index;
return res;
}
selector<rank, axis + 1> select(axis::selection selection) const
{
return slice(selection.lower, selection.upper, selection.skips);
}
selector<rank, axis + 1> select(axis::range range) const
{
return slice(range.lower, range.upper, 1);
}
auto select(axis::index index) const
{
return slice(index.lower);
}
selector<rank, axis + 1> select(axis::all) const
{
return {count, start, final, skips};
}
selector<rank, axis + 1> select(std::tuple<int, int, int> selection) const
{
return slice(
std::get<0>(selection),
std::get<1>(selection),
std::get<2>(selection));
}
selector<rank, axis + 1> select(std::tuple<int, int> range) const
{
return slice(std::get<0>(range), std::get<1>(range), 1);
}
auto select(int index) const
{
return slice(index, index + 1, 1).drop().collapse();
}
template<typename First, typename... Rest>
auto select(First first, Rest... rest) const
{
return select(first).select(rest...);
}
template<int new_axis>
selector<rank, new_axis> on() const
{
static_assert(new_axis >= 0 && new_axis < rank, "invalid selector axis");
return {count, start, final, skips};
}
selector<rank> reset() const
{
return {count, start, final, skips};
}
selector<rank, axis - 1> drop() const
{
static_assert(axis > 0, "invalid selector axis");
return {count, start, final, skips};
}
std::array<int, rank> strides() const
{
std::array<int, rank> s;
s[rank - 1] = 1;
for (int n = rank - 2; n >= 0; --n)
{
s[n] = s[n + 1] * count[n + 1];
}
return s;
}
std::array<int, rank> shape() const
{
std::array<int, rank> s;
for (int n = 0; n < rank; ++n)
{
s[n] = shape(n);
}
return s;
}
int shape(int axis) const
{
return final[axis] / skips[axis] - start[axis] / skips[axis];
}
bool empty() const
{
for (int n = 0; n < rank; ++n)
{
if (count[n] == 0)
{
return true;
}
}
return false;
}
bool contiguous() const
{
for (int n = 0; n < rank; ++n)
{
if (start[n] != 0 || final[n] != count[n] || skips[n] != 1)
{
return false;
}
}
return true;
}
std::size_t size() const
{
auto s = shape();
return std::accumulate(s.begin(), s.end(), 1, std::multiplies<int>());
}
bool operator==(const selector<rank, axis>& other) const
{
return count == other.count &&
start == other.start &&
final == other.final &&
skips == other.skips;
}
bool operator!=(const selector<rank, axis>& other) const
{
return count != other.count ||
start != other.start ||
final != other.final ||
skips != other.skips;
}
bool next(std::array<int, rank>& index) const
{
int n = rank - 1;
index[n] += skips[n];
while (index[n] >= final[n])
{
if (n == 0)
{
index = final;
return false;
}
index[n] = start[n];
--n;
index[n] += skips[n];
}
return true;
}
template<typename... Index>
bool contains(Index... index) const
{
static_assert(sizeof...(Index) == rank, "selector: index size must match rank");
auto S = shape::make_shape(index...);
for (int n = 0; n < rank; ++n)
{
auto start_index = std::get<0>(S[n]);
auto final_index = std::get<1>(S[n]);
if (start_index < 0 || final_index > final[n] / skips[n] - start[n] / skips[n])
{
return false;
}
}
return true;
}
selector<rank, axis> shift(int dist) const
{
auto sel = *this;
sel.start[axis] = std::max(sel.start[axis] + dist * skips[axis], 0);
sel.final[axis] = std::min(sel.final[axis] + dist * skips[axis], sel.count[axis]);
return sel;
}
// ========================================================================
class iterator
{
public:
iterator() {}
iterator(selector<rank> sel, std::array<int, rank> ind) : sel(sel), ind(ind) {}
iterator& operator++() { sel.next(ind); return *this; }
iterator operator++(int) { auto ret = *this; this->operator++(); return ret; }
bool operator==(iterator other) const { return ind == other.ind; }
bool operator!=(iterator other) const { return ind != other.ind; }
const std::array<int, rank>& operator*() const { return ind; }
private:
selector<rank> sel;
std::array<int, rank> ind;
};
iterator begin() const { return {reset(), start}; }
iterator end() const { return {reset(), final}; }
// ========================================================================
std::array<int, rank> count;
std::array<int, rank> start;
std::array<int, rank> final;
std::array<int, rank> skips;
// ========================================================================
template<int other_rank, int other_axis>
friend struct selector;
}; // ND_IMPL_END
// ============================================================================
#ifdef TEST_SELECTOR
#include "catch.hpp"
using namespace nd;
TEST_CASE("selector<3> does construct and compare correctly", "[selector]")
{
auto S = selector<3>(10, 12, 14);
CHECK(S.strides() == std::array<int, 3>{168, 14, 1});
CHECK(S.shape() == std::array<int, 3>{10, 12, 14});
CHECK(S == S.select(std::make_tuple(0, 10, 1)).on<0>());
CHECK(S != S.select(std::make_tuple(0, 10, 2)).on<0>());
}
TEST_CASE("make_selector works correctly", "[make_selector]")
{
auto _ = axis::all();
CHECK(make_selector(_|0|2).count[0] == 1);
CHECK(make_selector(_|0|2).start[0] == 0);
CHECK(make_selector(_|0|2).final[0] == 2);
CHECK(make_selector(_|0|2).skips[0] == 1);
CHECK(make_selector(_|0|5) == make_selector(_|0|5|1));
}
TEST_CASE("selector<2> does select-collapse operations correctly", "[selector::select]")
{
auto S = selector<2>(3, 4);
SECTION("Selections collapsing axis 0 @ i = 0 have the correct count, stride, and shape")
{
CHECK(S.select(0, std::make_tuple(0, 4)).count == std::array<int, 1>{12});
CHECK(S.select(0, std::make_tuple(0, 4)).start == std::array<int, 1>{0});
CHECK(S.select(0, std::make_tuple(0, 4)).final == std::array<int, 1>{4});
CHECK(S.select(0, std::make_tuple(0, 4)).strides() == std::array<int, 1>{1});
CHECK(S.select(0, std::make_tuple(0, 4)).shape() == std::array<int, 1>{4});
}
SECTION("Selections collapsing axis 0 @ i = 1 have the correct count, stride, and shape")
{
CHECK(S.select(1, std::make_tuple(0, 4)).count == std::array<int, 1>{12});
CHECK(S.select(1, std::make_tuple(0, 4)).start == std::array<int, 1>{4});
CHECK(S.select(1, std::make_tuple(0, 4)).final == std::array<int, 1>{8});
CHECK(S.select(1, std::make_tuple(0, 4)).skips == std::array<int, 1>{1});
CHECK(S.select(1, std::make_tuple(0, 4)).shape() == std::array<int, 1>{4});
}
SECTION("Selections collapsing a subset of axis 0 @ i = 0 have the correct count, stride, and shape")
{
CHECK(S.select(0, std::make_tuple(0, 2)).count == std::array<int, 1>{12});
CHECK(S.select(0, std::make_tuple(0, 2)).start == std::array<int, 1>{0});
CHECK(S.select(0, std::make_tuple(0, 2)).final == std::array<int, 1>{2});
CHECK(S.select(0, std::make_tuple(0, 2)).skips == std::array<int, 1>{1});
CHECK(S.select(0, std::make_tuple(0, 2)).shape() == std::array<int, 1>{2});
}
SECTION("Selections collapsing axis 1 at j = 0 have the correct count, stride, and shape")
{
CHECK(S.select(std::make_tuple(0, 3), 0).count == std::array<int, 1>{12});
CHECK(S.select(std::make_tuple(0, 3), 0).start == std::array<int, 1>{0});
CHECK(S.select(std::make_tuple(0, 3), 0).final == std::array<int, 1>{12});
CHECK(S.select(std::make_tuple(0, 3), 0).skips == std::array<int, 1>{4});
CHECK(S.select(std::make_tuple(0, 3), 0).shape() == std::array<int, 1>{3});
}
SECTION("Selections collapsing axis 1 at j = 1 have the correct count, stride, and shape")
{
CHECK(S.select(std::make_tuple(0, 3), 1).count == std::array<int, 1>{12}); // [1, 5, 9, 13)
CHECK(S.select(std::make_tuple(0, 3), 1).start == std::array<int, 1>{1});
CHECK(S.select(std::make_tuple(0, 3), 1).final == std::array<int, 1>{13});
CHECK(S.select(std::make_tuple(0, 3), 1).skips == std::array<int, 1>{4});
CHECK(S.select(std::make_tuple(0, 3), 1).shape() == std::array<int, 1>{3});
}
SECTION("Selections collapsing a subset of axis 1 at j = 0 have the correct count, stride, and shape")
{
CHECK(S.select(std::make_tuple(0, 2), 0).count == std::array<int, 1>{12});
CHECK(S.select(std::make_tuple(0, 2), 0).start == std::array<int, 1>{0});
CHECK(S.select(std::make_tuple(0, 2), 0).final == std::array<int, 1>{8});
CHECK(S.select(std::make_tuple(0, 2), 0).skips == std::array<int, 1>{4});
CHECK(S.select(std::make_tuple(0, 2), 0).shape() == std::array<int, 1>{2});
}
SECTION("Selections collapsing only axis 0 have the correct count, stride, and shape")
{
CHECK(S.select(0).count == std::array<int, 1>{3 * 4});
CHECK(S.select(0).start == std::array<int, 1>{0});
CHECK(S.select(0).final == std::array<int, 1>{4});
CHECK(S.select(0).strides() == std::array<int, 1>{1});
CHECK(S.select(0).shape() == std::array<int, 1>{4});
}
}
TEST_CASE("selector<2> does select-collapse operations correctly (regression)", "[regression]")
{
SECTION("Selections on last element are correct")
{
auto _ = axis::all();
auto A = nd::selector<2>(5, 5);
CHECK(A.select(_, 1).size() == 5);
CHECK(A.select(1, _).size() == 5);
CHECK(A.select(_, 1).count[0] == 25);
CHECK(A.select(_, 1).start[0] == 1);
CHECK(A.select(_, 1).final[0] == 26);
CHECK(A.select(_, 1).skips[0] == 5);
CHECK(A.select(_, 1).shape(0) == 5);
CHECK(A.select(1, _).count[0] == 25);
CHECK(A.select(1, _).start[0] == 5);
CHECK(A.select(1, _).final[0] == 10);
CHECK(A.select(1, _).skips[0] == 1);
CHECK(A.select(1, _).shape(0) == 5);
CHECK(A.select(_, 4).count[0] == 25);
CHECK(A.select(_, 4).start[0] == 4);
CHECK(A.select(_, 4).final[0] == 29);
CHECK(A.select(_, 4).skips[0] == 5);
CHECK(A.select(_, 4).shape(0) == 5);
CHECK(A.select(4, _).count[0] == 25);
CHECK(A.select(4, _).start[0] == 20);
CHECK(A.select(4, _).final[0] == 25);
CHECK(A.select(4, _).skips[0] == 1);
CHECK(A.select(4, _).shape(0) == 5);
}
}
TEST_CASE("selector<1> selects correctly with skip applied multiple times", "[selector::skip]")
{
auto S = selector<1>(64);
CHECK(S.skip(2).shape()[0] == 32);
CHECK(S.skip(2).on<0>().skip(2).shape()[0] == 16);
CHECK(S.skip(2).on<0>().skip(2).on<0>().skip(2).shape()[0] == 8);
}
TEST_CASE("selector<4> skips on all dimensions correctly", "[selector::skip]")
{
auto S = selector<4>(2, 4, 6, 8);
CHECK(S.skip(2).skip(4).skip(6).skip(8).size() == 1);
}
TEST_CASE("selector<1> next advances properly", "[selector::next]")
{
auto S = selector<1>(10);
auto I = std::array<int, 1>{0};
auto i = 0;
do {
CHECK(i == I[0]);
++i;
} while (S.next(I));
}
TEST_CASE("selector<2> next advances properly", "[selector::next]")
{
auto S = selector<2>(10, 10);
auto I = std::array<int, 2>{0, 0};
auto i = 0;
auto j = 0;
do {
CHECK(i == I[0]);
CHECK(j == I[1]);
if (++j == 10)
{
j = 0;
++i;
}
} while (S.next(I));
}
TEST_CASE("selector<2> subset iterator passes sanity checks", "[selector::iterator]")
{
auto S = selector<2>(10, 10).slice(2, 8, 1).slice(4, 6, 1);
auto I = std::array<int, 2>{2, 4};
for (auto index : S)
{
CHECK(index == I);
S.next(I);
}
}
TEST_CASE("selector can be shifed", "[selector::shift]")
{
CHECK(selector<2>(10, 5).on<0>().shift(+2).shape()[0] == 8);
CHECK(selector<2>(10, 5).on<0>().shift(+2).shape()[1] == 5);
CHECK(selector<2>(10, 5).on<0>().shift(-1).shape()[0] == 9);
CHECK(selector<2>(10, 5).on<0>().shift(-1).shape()[1] == 5);
CHECK(selector<2>(10, 5).on<1>().shift(-2).shape()[0] == 10);
CHECK(selector<2>(10, 5).on<1>().shift(-2).shape()[1] == 3);
CHECK(selector<2>(10, 5).on<1>().shift(+1).shape()[0] == 10);
CHECK(selector<2>(10, 5).on<1>().shift(+1).shape()[1] == 4);
}
#endif // TEST_SELECTOR