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 | #pragma once
#include "internal/dev_env.hpp"
#include "internal/ranges.hpp"
#include "internal/type_traits.hpp"
#include "utility/functional.hpp"
#include <ranges><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <concepts><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <tuple><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iterator><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
namespace uni {
template<std::ranges::input_range... Views> requires(std::ranges::view<Views> && ...) && (sizeof...(Views) > 0)
struct zip_view : std::ranges::view_interface<zip_view<Views...>> {
private:
std::tuple<Views...> _views;
public:
template<bool> struct iterator;
template<bool> struct sentinel;
zip_view() = default;
constexpr explicit zip_view(Views... views) noexcept(NO_EXCEPT) : _views(std::move(views)...) {}
constexpr auto begin() noexcept(NO_EXCEPT) requires(!(internal::simple_view<Views> && ...))
{
return iterator<false>(tuple_transform(std::ranges::begin, this->_views));
}
constexpr auto begin() const noexcept(NO_EXCEPT) requires(std::ranges::range<const Views> && ...)
{
return iterator<true>(tuple_transform(std::ranges::begin, this->_views));
}
constexpr auto end() noexcept(NO_EXCEPT) requires(!(internal::simple_view<Views> && ...))
{
if constexpr(!internal::zip_is_common<Views...>)
return sentinel<false>(tuple_transform(std::ranges::end, this->_views));
else if constexpr((std::ranges::random_access_range<Views> && ...))
return begin() + std::iter_difference_t<iterator<false>>(this->size());
else
return iterator<false>(tuple_transform(std::ranges::end, this->_views));
}
constexpr auto end() const noexcept(NO_EXCEPT) requires(std::ranges::range<const Views> && ...)
{
if constexpr(!internal::zip_is_common<const Views...>)
return sentinel<true>(tuple_transform(std::ranges::end, this->_views));
else if constexpr((std::ranges::random_access_range<const Views> && ...))
return this->begin() + std::iter_difference_t<iterator<true>>(this->size());
else
return iterator<true>(tuple_transform(std::ranges::end, _views));
}
constexpr auto size() noexcept(NO_EXCEPT) requires(std::ranges::sized_range<Views> && ...)
{
return std::apply(
[](auto... sizes)
{
using size_type = std::make_unsigned_t<std::common_type_t<decltype(sizes)...>>;
return uni::min(size_type(sizes)...);
},
tuple_transform(std::ranges::size, _views)
);
}
constexpr auto size() const noexcept(NO_EXCEPT) requires(std::ranges::sized_range<const Views> && ...)
{
return std::apply(
[](auto... sizes)
{
using size_type = std::make_unsigned_t<std::common_type_t<decltype(sizes)...>>;
return uni::min(size_type(sizes)...);
},
tuple_transform(std::ranges::size, _views)
);
}
};
template<class... Ranges> zip_view(Ranges &&...) -> zip_view<std::views::all_t<Ranges>...>;
namespace internal {
template<class iterator>
constexpr const typename iterator::iterator_collection& get_current(const iterator& itr) noexcept(NO_EXCEPT);
} // namespace internal
template<std::ranges::input_range... Views> requires(std::ranges::view<Views> && ...) && (sizeof...(Views) > 0)
template<bool Const>
struct zip_view<Views...>::iterator
: internal::zip_view_iterator_category<Const, Views...> {
using iterator_collection = internal::tuple_or_pair_t<
std::ranges::iterator_t<internal::maybe_const_t<Const, Views>>...
>;
private:
friend struct zip_view;
template<bool> friend struct zip_view::sentinel;
iterator_collection _current;
constexpr explicit iterator(decltype(_current) current) : _current(std::move(current)) {}
// template<std::copy_constructible F, std::ranges::input_range... Vs>
// requires
// (std::ranges::view<Vs> && ...) && (sizeof...(Vs) > 0) && std::is_object_v<F> &&
// std::regular_invocable<F&, std::ranges::range_reference_t<Vs>...> &&
// internal::can_reference<std::invoke_result_t<F&, std::ranges::range_reference_t<Vs>...>>
// friend struct zip_transform_view;
public:
using iterator_concept = internal::most_primitive_iterator_concept<Const, Views...>;
using value_type = internal::tuple_or_pair_t<std::ranges::range_value_t<internal::maybe_const_t<Const, Views>>...>;
using difference_type = std::common_type_t<std::ranges::range_difference_t<internal::maybe_const_t<Const, Views>>...>;
iterator() = default;
constexpr iterator(iterator<!Const> itr) noexcept(NO_EXCEPT)
requires Const &&
(
std::convertible_to<
std::ranges::iterator_t<Views>,
std::ranges::iterator_t<internal::maybe_const_t<Const, Views>>
> && ...
)
: _current(std::move(itr._current))
{}
constexpr auto operator*() const noexcept(NO_EXCEPT) {
const auto f = [](auto &itr) -> decltype(auto) { return *itr; };<--- Parameter 'itr' can be declared as reference to const
return tuple_transform(f, this->_current);
}
constexpr iterator& operator++() noexcept(NO_EXCEPT) {
tuple_for_each([](auto &itr) { ++itr; }, this->_current);
return *this;
}
constexpr void operator++(int) noexcept(NO_EXCEPT) { ++*this; }
constexpr iterator operator++(int) noexcept(NO_EXCEPT)
requires internal::all_forward<Const, Views...>
{
const auto res = *this; ++*this; return res;
}
constexpr iterator& operator--() noexcept(NO_EXCEPT)
requires internal::all_bidirectional<Const, Views...>
{
tuple_for_each([](auto &itr) { --itr; }, this->_current);
return *this;
}
constexpr iterator operator--(int) noexcept(NO_EXCEPT)
requires internal::all_bidirectional<Const, Views...>
{
const auto res = *this; --*this; return res;
}
constexpr iterator& operator+=(const difference_type diff) noexcept(NO_EXCEPT)
requires internal::all_random_access<Const, Views...>
{
const auto f = [&]<class Itr>(Itr& itr) constexpr noexcept(NO_EXCEPT) {
itr += std::iter_difference_t<Itr>(diff);
};
tuple_for_each(f, this->_current);
return *this;
}
constexpr iterator& operator-=(const difference_type diff) noexcept(NO_EXCEPT)
requires internal::all_random_access<Const, Views...>
{
const auto f = [&]<class Itr>(Itr& itr) constexpr noexcept(NO_EXCEPT) {
itr -= std::iter_difference_t<Itr>(diff);
};
tuple_for_each(f, this->_current);
return *this;
}
constexpr auto operator[](const difference_type diff) const noexcept(NO_EXCEPT)
requires internal::all_random_access<Const, Views...>
{
const auto f = [&]<class Itr>(Itr& itr) constexpr noexcept(NO_EXCEPT) -> decltype(auto) {
return itr[std::iter_difference_t<Itr>(diff)];
};
return tuple_transform(f, _current);
}
friend constexpr bool operator==(const iterator& lhs, const iterator& rhs) noexcept(NO_EXCEPT)
requires (
std::equality_comparable<
std::ranges::iterator_t<internal::maybe_const_t<Const, Views>>
> && ...
)
{
if constexpr(internal::all_bidirectional<Const, Views...>)
return lhs._current == rhs._current;
else
return [&]<std::size_t... Is>(std::index_sequence<Is...>) constexpr noexcept(NO_EXCEPT) {
return (
(std::get<Is>(lhs._current) == std::get<Is>(rhs._current)) || ...
);
}(std::make_index_sequence<sizeof...(Views)>{});
}
friend constexpr auto operator<=>(const iterator& lhs, const iterator& rhs) noexcept(NO_EXCEPT)
requires internal::all_random_access<Const, Views...>
{
return lhs._current <=> rhs._current;
}
friend constexpr iterator operator+(const iterator& itr, const difference_type diff) noexcept(NO_EXCEPT)
requires internal::all_random_access<Const, Views...>
{
auto res = itr; res += diff; return res;
}
friend constexpr iterator operator+(const difference_type diff, const iterator& itr) noexcept(NO_EXCEPT)
requires internal::all_random_access<Const, Views...>
{
auto res = itr; res += diff; return res;
}
friend constexpr iterator operator-(const iterator& itr, const difference_type diff) noexcept(NO_EXCEPT)
requires internal::all_random_access<Const, Views...>
{
auto res = itr; res -= diff; return res;
}
friend constexpr difference_type operator-(const iterator& lhs, const iterator& rhs) noexcept(NO_EXCEPT)
requires (
std::sized_sentinel_for<
std::ranges::iterator_t<internal::maybe_const_t<Const, Views>>,
std::ranges::iterator_t<internal::maybe_const_t<Const, Views>>
> && ...
)
{
return [&]<std::size_t... Is>(std::index_sequence<Is...>) constexpr noexcept(NO_EXCEPT) {
return std::ranges::min(
{
difference_type(std::get<Is>(lhs._current) - std::get<Is>(rhs._current)) ...
},
std::ranges::less{},
[](const difference_type diff) constexpr noexcept(NO_EXCEPT) {
return to_unsigned(diff < 0 ? -diff : diff);
}
);
}(std::make_index_sequence<sizeof...(Views)>{});
}
friend constexpr auto iter_move(const iterator& itr) noexcept(NO_EXCEPT) {
return tuple_transform(std::ranges::iter_move, itr._current);
}
friend constexpr void iter_swap(const iterator& lhs, const iterator& res) noexcept(NO_EXCEPT)
requires (
std::indirectly_swappable<
std::ranges::iterator_t<internal::maybe_const_t<Const, Views>>
> && ...
)
{
[&]<std::size_t... Is>(std::index_sequence<Is...>) constexpr noexcept(NO_EXCEPT) {
(
std::ranges::iter_swap(std::get<Is>(lhs._current), std::get<Is>(res._current)), ...
);
}(std::make_index_sequence<sizeof...(Views)>{});
}
template<class Itr> friend constexpr const typename Itr::iterator_collection& internal::get_current(const Itr&) noexcept(NO_EXCEPT);
};
template<class iterator>
constexpr const typename iterator::iterator_collection& internal::get_current(const iterator& itr) noexcept(NO_EXCEPT) { return itr._current; };
template<std::ranges::input_range... Views> requires(std::ranges::view<Views> && ...) && (sizeof...(Views) > 0)
template<bool Const>
struct zip_view<Views...>::sentinel {
friend struct zip_view;
template<bool> friend struct zip_view::iterator;
using sentinel_collection = internal::tuple_or_pair_t<
std::ranges::sentinel_t<internal::maybe_const_t<Const, Views>>...
>;
sentinel_collection _end;
constexpr explicit sentinel(sentinel_collection end) noexcept(NO_EXCEPT) : _end(end) {}
public:
sentinel() = default;
constexpr sentinel(sentinel<!Const> itr) noexcept(NO_EXCEPT)
requires Const &&
(
std::convertible_to<
std::ranges::sentinel_t<Views>,
std::ranges::sentinel_t<internal::maybe_const_t<Const, Views>>
> && ...
)
: _end(std::move(itr._end))
{}
template<bool Const_>
requires (
std::sentinel_for<
std::ranges::sentinel_t<internal::maybe_const_t<Const, Views>>,
std::ranges::iterator_t<internal::maybe_const_t<Const_, Views>>
> && ...
)
friend constexpr bool operator==(const iterator<Const_>& lhs, const sentinel& rhs) noexcept(NO_EXCEPT)
{
return [&]<std::size_t... Is>(std::index_sequence<Is...>) constexpr noexcept(NO_EXCEPT) {
return (
(std::get<Is>(internal::get_current(lhs)) == std::get<Is>(rhs._end)) || ...);
}(std::make_index_sequence<sizeof...(Views)>{});
}
template<bool Const_>
requires (
std::sized_sentinel_for<
std::ranges::sentinel_t<internal::maybe_const_t<Const, Views>>,
std::ranges::iterator_t<internal::maybe_const_t<Const_, Views>>
> && ...
)
friend constexpr auto operator-(const iterator<Const_>& lhs, const sentinel& rhs) noexcept(NO_EXCEPT)
{
using return_type = std::common_type_t<std::ranges::range_difference_t<internal::maybe_const_t<Const_, Views>>...>;
return [&]<std::size_t... Is>(std::index_sequence<Is...>) constexpr noexcept(NO_EXCEPT) {
return std::ranges::min(
{ return_type(std::get<Is>(internal::get_current(lhs)) - std::get<Is>(rhs._end))... },
std::ranges::less{},
[](const return_type diff) {
return to_unsigned(diff < 0 ? -diff : diff);
}
);
}(std::make_index_sequence<sizeof...(Views)>{});
}
template<bool Const_>
requires (
std::sized_sentinel_for<
std::ranges::sentinel_t<internal::maybe_const_t<Const, Views>>,
std::ranges::iterator_t<internal::maybe_const_t<Const_, Views>>
> && ...
)
friend constexpr auto operator-(const sentinel &lhs, const iterator<Const_>& rhs) noexcept(NO_EXCEPT)
{
return -(rhs - lhs);
}
};
namespace views {
namespace internal {
template<class... Ts>
concept can_zip_view = requires { zip_view<std::views::all_t<Ts>...>(std::declval<Ts>()...); };
} // namespace internal
struct Zip {
template<class... Ts> requires(sizeof...(Ts) == 0 || internal::can_zip_view<Ts...>)
constexpr auto operator() [[nodiscard]] (Ts&&... vs) const {
if constexpr(sizeof...(Ts) == 0) return std::views::empty<std::tuple<>>;
else return zip_view<std::views::all_t<Ts>...>(std::forward<Ts>(vs)...);
}
};
inline constexpr Zip zip;
} // namespace views
} // namespace uni.
namespace std::ranges {
template<class... Views>
inline constexpr bool enable_borrowed_range<uni::zip_view<Views...>> = (enable_borrowed_range<Views> && ...);
}
|