-
Notifications
You must be signed in to change notification settings - Fork 2
/
zip_view.hpp
561 lines (483 loc) · 19 KB
/
zip_view.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
// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
/*!\file
* \author Enrico Seiler <enrico.seiler AT fu-berlin.de>
* \brief Provides seqan::stl::views::zip.
*/
// File might be included from multiple libraries.
#ifndef SEQAN_STD_ZIP_VIEW
#define SEQAN_STD_ZIP_VIEW
#include <ranges>
// https://godbolt.org/z/YW7e785sd
#if defined __cpp_lib_ranges_zip && !defined(__clang__)
namespace seqan::stl::views
{
using std::ranges::views::zip;
} // namespace seqan::stl::views
#else
# include <algorithm>
# include <functional>
# include "all_view.hpp"
# include "concepts.hpp"
# include "detail/compiler_definitions.hpp"
# include "detail/exposition_only.hpp"
# include "pair.hpp"
# include "tuple.hpp"
namespace seqan::stl::detail::zip
{
template <bool is_const, typename... range_ts>
concept all_random_access = (std::ranges::random_access_range<maybe_const<is_const, range_ts>> && ...);
template <bool is_const, typename... range_ts>
concept all_bidirectional = (std::ranges::bidirectional_range<maybe_const<is_const, range_ts>> && ...);
template <bool is_const, typename... range_ts>
concept all_forward = (std::ranges::forward_range<maybe_const<is_const, range_ts>> && ...);
template <bool>
struct iterator_category_t;
template <>
struct iterator_category_t<true>
{
using iterator_category = std::input_iterator_tag;
};
template <>
struct iterator_category_t<false>
{};
template <typename... ts>
struct tuple_or_pair_impl;
template <typename... ts>
requires (sizeof...(ts) != 2)
struct tuple_or_pair_impl<ts...>
{
using type = seqan::stl::tuple<ts...>;
};
template <typename... ts>
requires (sizeof...(ts) == 2)
struct tuple_or_pair_impl<ts...>
{
using type = seqan::stl::pair<ts...>;
};
// https://eel.is/c++draft/range.zip#view-1
template <typename... ts>
using tuple_or_pair = tuple_or_pair_impl<ts...>::type;
// Returns a new tuple containing the result of applying a function to each tuple element.
// https://eel.is/c++draft/range.zip.view
template <typename fun_t, typename tuple_t>
constexpr auto tuple_transform(fun_t && f, tuple_t && tuple)
{
return std::apply(
[&]<typename... ts>(ts &&... elements)
{
return tuple_or_pair<std::invoke_result_t<fun_t &, ts>...>{std::invoke(f, std::forward<ts>(elements))...};
},
std::forward<tuple_t>(tuple));
}
// Applies a function to each tuple element.
// https://eel.is/c++draft/range.zip.view
template <typename fun_t, typename tuple_t>
constexpr void tuple_for_each(fun_t && f, tuple_t && tuple)
{
std::apply(
[&]<typename... ts>(ts &&... elements)
{
(std::invoke(f, std::forward<ts>(elements)), ...);
},
std::forward<tuple_t>(tuple));
}
// std::abs has problems with ambiguous overloads
template <typename t>
constexpr t abs(t && v) noexcept
{
if constexpr (std::is_signed_v<t>)
return v < 0 ? -v : v;
else
return v;
}
// https://eel.is/c++draft/range.zip#concept:zip-is-common
template <typename... range_ts>
concept zip_is_common =
(sizeof...(range_ts) == 1 && (std::ranges::common_range<range_ts> && ...))
|| (!(std::ranges::bidirectional_range<range_ts> && ...) && (std::ranges::common_range<range_ts> && ...))
|| ((std::ranges::random_access_range<range_ts> && ...) && (std::ranges::sized_range<range_ts> && ...));
} // namespace seqan::stl::detail::zip
namespace seqan::stl::ranges
{
template <std::ranges::input_range... Views>
requires (std::ranges::view<Views> && ...) && (sizeof...(Views) > 0)
class zip_view : public std::ranges::view_interface<zip_view<Views...>>
{
private:
seqan::stl::tuple<Views...> views_;
template <bool>
class iterator;
template <bool>
class sentinel;
public:
zip_view()
requires (std::is_default_constructible_v<Views> && ...)
= default;
constexpr explicit zip_view(Views... views) : views_(std::move(views)...)
{}
constexpr auto begin()
requires (!(seqan::stl::detail::simple_view<Views> && ...))
{
return iterator<false>(seqan::stl::detail::zip::tuple_transform(std::ranges::begin, views_));
}
constexpr auto begin() const
requires (std::ranges::range<Views const> && ...)
{
return iterator<true>(seqan::stl::detail::zip::tuple_transform(std::ranges::begin, views_));
}
constexpr auto end()
requires (!(seqan::stl::detail::simple_view<Views> && ...))
{
if constexpr (!seqan::stl::detail::zip::zip_is_common<Views...>)
return sentinel<false>(seqan::stl::detail::zip::tuple_transform(std::ranges::end, views_));
else if constexpr ((std::ranges::random_access_range<Views> && ...))
return begin() + std::iter_difference_t<iterator<false>>(size());
else
return iterator<false>(seqan::stl::detail::zip::tuple_transform(std::ranges::end, views_));
}
constexpr auto end() const
requires (std::ranges::range<Views const> && ...)
{
if constexpr (!seqan::stl::detail::zip::zip_is_common<Views const...>)
return sentinel<true>(seqan::stl::detail::zip::tuple_transform(std::ranges::end, views_));
else if constexpr ((std::ranges::random_access_range<Views const> && ...))
return begin() + std::iter_difference_t<iterator<true>>(size());
else
return iterator<true>(seqan::stl::detail::zip::tuple_transform(std::ranges::end, views_));
}
constexpr auto size()
requires (std::ranges::sized_range<Views> && ...)
{
return std::apply(
[](auto... sizes)
{
using common_size_t = std::make_unsigned_t<std::common_type_t<decltype(sizes)...>>;
return std::ranges::min({static_cast<common_size_t>(sizes)...});
},
seqan::stl::detail::zip::tuple_transform(std::ranges::size, views_));
}
constexpr auto size() const
requires (std::ranges::sized_range<Views const> && ...)
{
return std::apply(
[](auto... sizes)
{
using common_size_t = std::make_unsigned_t<std::common_type_t<decltype(sizes)...>>;
return std::ranges::min({static_cast<common_size_t>(sizes)...});
},
seqan::stl::detail::zip::tuple_transform(std::ranges::size, views_));
}
};
template <typename... range_ts>
zip_view(range_ts &&...) -> zip_view<seqan::stl::views::all_t<range_ts>...>;
template <std::ranges::input_range... Views>
requires (std::ranges::view<Views> && ...) && (sizeof...(Views) > 0)
template <bool Const>
class zip_view<Views...>::iterator :
public seqan::stl::detail::zip::iterator_category_t<seqan::stl::detail::zip::all_forward<Const, Views...>>
{
private:
constexpr explicit iterator(seqan::stl::detail::zip::tuple_or_pair<
std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>>...> current) :
current_(std::move(current))
{}
friend class zip_view<Views...>;
// clang-format off
SEQAN_STD_NESTED_VISIBILITY
// clang-format on
seqan::stl::detail::zip::tuple_or_pair<std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>>...>
current_;
public:
using iterator_concept =
std::conditional_t<seqan::stl::detail::zip::all_random_access<Const, Views...>,
std::random_access_iterator_tag,
std::conditional_t<seqan::stl::detail::zip::all_bidirectional<Const, Views...>,
std::bidirectional_iterator_tag,
std::conditional_t<seqan::stl::detail::zip::all_forward<Const, Views...>,
std::forward_iterator_tag,
std::input_iterator_tag>>>;
using value_type = seqan::stl::detail::zip::tuple_or_pair<
std::ranges::range_value_t<seqan::stl::detail::maybe_const<Const, Views>>...>;
using difference_type =
std::common_type_t<std::ranges::range_difference_t<seqan::stl::detail::maybe_const<Const, Views>>...>;
iterator() = default;
constexpr iterator(iterator<!Const> i)
requires Const
&& (std::convertible_to<std::ranges::iterator_t<Views>,
std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>>>
&& ...)
: current_(std::move(i.current))
{}
constexpr auto operator*() const
{
return seqan::stl::detail::zip::tuple_transform(
[](auto & i) -> decltype(auto)
{
return *i;
},
current_);
}
constexpr iterator & operator++()
{
seqan::stl::detail::zip::tuple_for_each(
[](auto & i)
{
++i;
},
current_);
return *this;
}
constexpr void operator++(int)
{
++*this;
}
constexpr iterator operator++(int)
requires seqan::stl::detail::zip::all_forward<Const, Views...>
{
auto tmp = *this;
++*this;
return tmp;
}
constexpr iterator & operator--()
requires seqan::stl::detail::zip::all_bidirectional<Const, Views...>
{
seqan::stl::detail::zip::tuple_for_each(
[](auto & i)
{
--i;
},
current_);
return *this;
}
constexpr iterator operator--(int)
requires seqan::stl::detail::zip::all_bidirectional<Const, Views...>
{
auto tmp = *this;
--*this;
return tmp;
}
constexpr iterator & operator+=(difference_type x)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
seqan::stl::detail::zip::tuple_for_each(
[&]<typename I>(I & i)
{
i += std::iter_difference_t<I>(x);
},
current_);
return *this;
}
constexpr iterator & operator-=(difference_type x)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
seqan::stl::detail::zip::tuple_for_each(
[&]<typename I>(I & i)
{
i -= std::iter_difference_t<I>(x);
},
current_);
return *this;
}
constexpr auto operator[](difference_type n) const
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
return seqan::stl::detail::zip::tuple_transform(
[&]<typename I>(I & i) -> decltype(auto)
{
return i[std::iter_difference_t<I>(n)];
},
current_);
}
friend constexpr bool operator==(iterator const & x, iterator const & y)
requires (std::equality_comparable<std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>>>
&& ...)
{
if constexpr (seqan::stl::detail::zip::all_bidirectional<Const, Views...>)
{
return x.current_ == y.current_;
}
else
{
return [&]<size_t... N>(std::integer_sequence<size_t, N...>)
{
return ((std::get<N>(x.current_) == std::get<N>(y.current_)) || ...);
}(std::index_sequence_for<Views...>{});
}
}
friend constexpr bool operator<(iterator const & x, iterator const & y)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
return x.current_ < y.current_;
}
friend constexpr bool operator>(iterator const & x, iterator const & y)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
return y < x;
}
friend constexpr bool operator<=(iterator const & x, iterator const & y)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
return !(y < x);
}
friend constexpr bool operator>=(iterator const & x, iterator const & y)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
return !(x < y);
}
# ifdef __cpp_lib_three_way_comparison
friend constexpr auto operator<=>(iterator const & x, iterator const & y)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
&& (std::three_way_comparable<std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>>>
&& ...)
{
return x.current_ <=> y.current_;
}
# endif
friend constexpr iterator operator+(iterator const & i, difference_type n)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
auto r = i;
r += n;
return r;
}
friend constexpr iterator operator+(difference_type n, iterator const & i)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
return i + n;
}
friend constexpr iterator operator-(iterator const & i, difference_type n)
requires seqan::stl::detail::zip::all_random_access<Const, Views...>
{
auto r = i;
r -= n;
return r;
}
friend constexpr difference_type operator-(iterator const & x, iterator const & y)
requires (std::sized_sentinel_for<std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>>,
std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>>>
&& ...)
{
return [&]<size_t... N>(std::integer_sequence<size_t, N...>)
{
return std::ranges::min(
{static_cast<difference_type>(std::get<N>(x.current_) - std::get<N>(y.current_))...},
[](difference_type a, difference_type b)
{
return seqan::stl::detail::zip::abs(b) < seqan::stl::detail::zip::abs(a);
});
}(std::index_sequence_for<Views...>{});
}
friend constexpr auto iter_move(iterator const & i) noexcept(
(noexcept(std::ranges::iter_move(
std::declval<std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>> const &>()))
&& ...)
&& (std::is_nothrow_move_constructible_v<
std::ranges::range_rvalue_reference_t<seqan::stl::detail::maybe_const<Const, Views>>>
&& ...))
{
return seqan::stl::detail::zip::tuple_transform(std::ranges::iter_move, i.current_);
}
friend constexpr void iter_swap(iterator const & l, iterator const & r) noexcept(
(noexcept(std::ranges::iter_swap(
std::declval<std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>> const &>(),
std::declval<std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>> const &>()))
&& ...))
requires (std::indirectly_swappable<std::ranges::iterator_t<seqan::stl::detail::maybe_const<Const, Views>>>
&& ...)
{
[&]<size_t... N>(std::integer_sequence<size_t, N...>)
{
(std::ranges::iter_swap(std::get<N>(l.current_), std::get<N>(r.current)), ...);
}(std::index_sequence_for<Views...>{});
}
};
template <std::ranges::input_range... Views>
requires (std::ranges::view<Views> && ...) && (sizeof...(Views) > 0)
template <bool Const>
class zip_view<Views...>::sentinel
{
private:
constexpr explicit sentinel(seqan::stl::detail::zip::tuple_or_pair<
std::ranges::sentinel_t<seqan::stl::detail::maybe_const<Const, Views>>...> end) :
end_(std::move(end))
{}
friend class zip_view<Views...>;
// clang-format off
SEQAN_STD_NESTED_VISIBILITY
// clang-format on
seqan::stl::detail::zip::tuple_or_pair<std::ranges::sentinel_t<seqan::stl::detail::maybe_const<Const, Views>>...>
end_;
public:
sentinel() = default;
constexpr sentinel(sentinel<!Const> i)
requires Const
&& (std::convertible_to<std::ranges::sentinel_t<Views>,
std::ranges::sentinel_t<seqan::stl::detail::maybe_const<Const, Views>>>
&& ...)
: end_(std::move(i.end_))
{}
template <bool OtherConst>
requires (std::sentinel_for<std::ranges::sentinel_t<seqan::stl::detail::maybe_const<Const, Views>>,
std::ranges::iterator_t<seqan::stl::detail::maybe_const<OtherConst, Views>>>
&& ...)
friend constexpr bool operator==(iterator<OtherConst> const & x, sentinel const & y)
{
return [&]<size_t... N>(std::integer_sequence<size_t, N...>)
{
return ((std::get<N>(x.current_) == std::get<N>(y.end_)) || ...);
}(std::index_sequence_for<Views...>{});
}
template <bool OtherConst>
requires (std::sized_sentinel_for<std::ranges::sentinel_t<seqan::stl::detail::maybe_const<Const, Views>>,
std::ranges::iterator_t<seqan::stl::detail::maybe_const<OtherConst, Views>>>
&& ...)
friend constexpr std::common_type_t<
std::ranges::range_difference_t<seqan::stl::detail::maybe_const<OtherConst, Views>>...>
operator-(iterator<OtherConst> const & x, sentinel const & y)
{
using return_t =
std::common_type_t<std::ranges::range_difference_t<seqan::stl::detail::maybe_const<OtherConst, Views>>...>;
return [&]<size_t... N>(std::integer_sequence<size_t, N...>)
{
return std::ranges::min({static_cast<return_t>(std::get<N>(x.current_) - std::get<N>(y.end_))...},
[](return_t a, return_t b)
{
return seqan::stl::detail::zip::abs(b) < seqan::stl::detail::zip::abs(a);
});
}(std::index_sequence_for<Views...>{});
}
template <bool OtherConst>
requires (std::sized_sentinel_for<std::ranges::sentinel_t<seqan::stl::detail::maybe_const<Const, Views>>,
std::ranges::iterator_t<seqan::stl::detail::maybe_const<OtherConst, Views>>>
&& ...)
friend constexpr std::common_type_t<
std::ranges::range_difference_t<seqan::stl::detail::maybe_const<OtherConst, Views>>...>
operator-(sentinel const & y, iterator<OtherConst> const & x)
{
return -(x - y);
}
};
struct zip_fn
{
template <seqan::stl::ranges::viewable_range... urng_ts>
requires (sizeof...(urng_ts) == 0)
constexpr auto operator()(urng_ts &&...) const
{
return std::views::empty<seqan::stl::tuple<>>;
}
template <seqan::stl::ranges::viewable_range... urng_ts>
requires (sizeof...(urng_ts) > 1)
constexpr auto operator()(urng_ts &&... ranges) const
{
return zip_view{std::forward<urng_ts>(ranges)...};
}
};
} // namespace seqan::stl::ranges
namespace seqan::stl::views
{
inline constexpr auto zip = seqan::stl::ranges::zip_fn{};
} // namespace seqan::stl::views
#endif // ifdef __cpp_lib_ranges_zip
#endif // SEQAN_STD_ZIP_VIEW