-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartesian_product_view.h
394 lines (317 loc) · 11.7 KB
/
cartesian_product_view.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#ifndef CARTESIAN_PRODUCT_VIEW_H
#define CARTESIAN_PRODUCT_VIEW_H
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <ranges>
#include <tuple>
#include <type_traits>
#include <utility>
template <bool deep_const, typename... Views>
class cartesian_product_view final : public std::ranges::view_base {
static_assert((... && (std::ranges::forward_range<Views> && std::ranges::view<Views>)));
template <typename T>
struct arrow_proxy {
T t;
T operator*() && {
return t;
}
T* operator->() {
return &t;
}
};
template <bool propagate_const, std::size_t index, std::size_t size, typename ViewsTuple>
class cartesian_iterator : protected cartesian_iterator<propagate_const, index + 1, size, ViewsTuple> {
using base = cartesian_iterator<propagate_const, index + 1, size, ViewsTuple>;
using aux = cartesian_iterator<propagate_const, size, size, ViewsTuple>;
public:
using aux::iterator_category;
using aux::iterator_concept;
using aux::difference_type;
using aux::value_type;
using aux::reference;
using aux::pointer;
cartesian_iterator() = default;
cartesian_iterator(bool const begin_end, ViewsTuple* views)
: base{ begin_end, views }
, it{
!begin_end
? std::ranges::begin(std::get<index>(*views))
: [&] {
auto& views = std::get<index>(*aux::views);
if constexpr (std::ranges::common_range<std::tuple_element_t<index, ViewsTuple>>) {
return std::ranges::end(views);
} else {
return std::ranges::next(std::ranges::begin(views), std::ranges::end(views));
}
}()
}
{}
cartesian_iterator& operator++() {
assert(!empty() && "Attempting to increment empty cartesian_product_view<> iterator!");
assert(it != std::ranges::end(std::get<index>(*aux::views)) && "Attempting to increment end cartesian_product_view<> iterator!");
static_cast<void>(next());
return *this;
}
cartesian_iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
typename aux::reference operator*() const {
assert(!empty() && "Attempting to dereference empty cartesian_product_view<> iterator!");
assert(it != std::ranges::end(std::get<index>(*aux::views)) && "Attempting to dereference end cartesian_product_view<> iterator!");
return read();
}
typename aux::pointer operator->() const {
return { **this };
}
bool operator==(cartesian_iterator const& other) const {
#ifndef NDEBUG
return base::operator==(other) && it == other.it;
#else
return it == other.it && base::operator==(other);
#endif
}
cartesian_iterator& operator--() requires (... && std::ranges::bidirectional_range<Views>) {
assert(!empty() && "Attempting to decrement empty cartesian_product_view<> iterator!");
if (it == std::ranges::end(std::get<index>(*aux::views))) {
reset_rbegin();
} else {
auto const underflow = prev();
assert(!underflow && "Attempting to decrement begin cartesian_product_view<> iterator!");
}
return *this;
}
cartesian_iterator operator--(int) requires (... && std::ranges::bidirectional_range<Views>) {
auto ret = *this;
--*this;
return ret;
}
protected:
bool next() {
if (base::next()){
if (++it != std::ranges::end(std::get<index>(*aux::views))) {
base::reset_begin();
} else {
return true;
}
}
return false;
}
bool prev() requires (... && std::ranges::bidirectional_range<Views>) {
if (base::prev()) {
if (it != std::ranges::begin(std::get<index>(*aux::views))) {
--it;
base::reset_rbegin();
} else {
return true;
}
}
return false;
}
auto read() const {
return std::tuple_cat(std::tuple<std::tuple_element_t<index, typename aux::reference>>{ *it }, base::read());
}
void reset_begin() {
base::reset_begin();
it = std::ranges::begin(std::get<index>(*aux::views));
}
void reset_rbegin() requires (... && std::ranges::bidirectional_range<Views>) {
base::reset_rbegin();
it =
[&] {
auto& views = std::get<index>(*aux::views);
if constexpr (std::ranges::common_range<std::tuple_element_t<index, ViewsTuple>>) {
return std::ranges::prev(std::ranges::end(views));
} else {
return std::ranges::prev(std::ranges::next(std::ranges::begin(views), std::ranges::end(views)));
}
}()
;
}
#ifndef NDEBUG
bool empty() const {
return std::ranges::empty(std::get<index>(*aux::views)) || base::empty();
}
#endif
private:
std::ranges::iterator_t<std::tuple_element_t<index, ViewsTuple>> it;
};
template <bool propagate_const, std::size_t size, typename ViewsTuple>
class cartesian_iterator<propagate_const, size, size, ViewsTuple> {
class iterator_traits {
template <std::ranges::range Range>
class element {
using reference = std::ranges::range_reference_t<Range>;
public:
using type =
std::conditional_t<
propagate_const && std::is_reference_v<reference>,
std::conditional_t<
std::is_lvalue_reference_v<reference>,
std::remove_cvref_t<reference> const&,
std::remove_cvref_t<reference> const&&
>,
reference
>
;
};
template <std::ranges::range Range>
using element_t = typename element<Range>::type;
template <typename View>
using maybe_const = std::conditional_t<std::is_const_v<ViewsTuple>, View const, View>;
public:
using iterator_category = std::input_iterator_tag;
using iterator_concept =
std::conditional_t<
(... && std::ranges::bidirectional_range<maybe_const<Views>>),
std::bidirectional_iterator_tag,
std::forward_iterator_tag
>
;
using difference_type =
std::common_type_t<
std::intmax_t,
std::ranges::range_difference_t<maybe_const<Views>>...
>
;
using value_type = std::tuple<std::ranges::range_value_t<maybe_const<Views>>...>;
using reference = std::tuple<element_t<maybe_const<Views>>...>;
using pointer = arrow_proxy<reference>;
};
public:
using iterator_category = typename iterator_traits::iterator_category;
using iterator_concept = typename iterator_traits::iterator_concept;
using difference_type = typename iterator_traits::difference_type;
using value_type = typename iterator_traits::value_type;
using reference = typename iterator_traits::reference;
using pointer = typename iterator_traits::pointer;
cartesian_iterator() = default;
cartesian_iterator(bool, ViewsTuple* views)
: views{ views }
{}
cartesian_iterator& operator++() {
assert(!"Attempting to increment empty cartesian_product_view<> iterator!");
return *this;
}
cartesian_iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
auto operator*() const {
assert(!"Attempting to dereference empty cartesian_product_view<> iterator!");
return std::tuple{};
}
arrow_proxy<std::tuple<>> operator->() const {
return { **this };
}
bool operator==(cartesian_iterator const& other) const {
assert(views == other.views && "Attempting to compare incompatible cartesian_product_view<> iterators!");
return true;
}
cartesian_iterator& operator--() {
assert(!"Attempting to decrement empty cartesian_product_view<> iterator!");
return *this;
}
cartesian_iterator operator--(int) {
auto ret = *this;
--*this;
return ret;
}
protected:
bool prev() const {
return true;
}
bool next() const {
return true;
}
auto read() const {
return std::tuple{};
}
void reset_begin() const {}
void reset_rbegin() const {}
#ifndef NDEBUG
bool empty() const {
return false; // despite seeming to be controversial
}
#endif
ViewsTuple* views;
};
public:
cartesian_product_view() = default;
cartesian_product_view(Views... views) requires (sizeof...(Views) != 0)
: views{ std::move(views)... }
{}
auto begin() {
return iterator(false, views);
}
auto end() {
return iterator(true, views);
}
auto begin() const {
return iterator(false, views);
}
auto end() const {
return iterator(true, views);
}
auto cbegin() const {
return begin();
}
auto cend() const {
return end();
}
auto size() requires (... && std::ranges::sized_range<Views>) {
return size(views);
}
auto size() const requires (... && std::ranges::sized_range<Views const>) {
return size(views);
}
private:
template <typename T>
static auto iterator(bool const begin_end, T& views) {
return
cartesian_iterator<
deep_const && std::is_const_v<T>,
0,
sizeof...(Views),
std::remove_reference_t<decltype(views)>
> {
begin_end
|| std::apply(
[](auto&... views) {
return (... || std::ranges::empty(views));
},
views
),
&views
}
;
}
template <typename T>
static auto size(T& views) {
return
std::apply(
[](auto&... views) {
return (std::size_t{ sizeof...(views) != 0 } * ... * std::ranges::size(views));
},
views
)
;
}
std::tuple<Views...> views;
};
template <typename... Ranges>
cartesian_product_view(Ranges&&...) -> cartesian_product_view<true, std::views::all_t<Ranges>...>;
template <bool deep_const>
struct cartesian_product_fn {
template <typename... Ranges>
requires (... && (std::ranges::forward_range<Ranges> && std::ranges::viewable_range<Ranges>))
constexpr auto operator()(Ranges&&... ranges) const {
return cartesian_product_view<deep_const, std::views::all_t<Ranges>...>{ std::views::all(std::forward<Ranges>(ranges))... };
}
};
inline constexpr cartesian_product_fn<true> cartesian_product{};
#endif