-
Notifications
You must be signed in to change notification settings - Fork 2
/
enumerate_view.hpp
341 lines (293 loc) · 9.92 KB
/
enumerate_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
// 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::enumerate.
*/
// File might be included from multiple libraries.
#ifndef SEQAN_STD_ENUMERATE_VIEW
#define SEQAN_STD_ENUMERATE_VIEW
#include <cassert>
#include <ranges>
#ifdef __cpp_lib_ranges_enumerate
namespace seqan::stl::views
{
using std::ranges::views::enumerate;
} // namespace seqan::stl::views
#else
# include "all_view.hpp"
# include "concepts.hpp"
# include "detail/adaptor_from_functor.hpp"
# include "detail/compiler_definitions.hpp"
# include "detail/exposition_only.hpp"
namespace seqan::stl::ranges
{
template <std::ranges::view V>
requires seqan::stl::detail::range_with_movable_references<V>
class enumerate_view : public std::ranges::view_interface<enumerate_view<V>>
{
// clang-format off
SEQAN_STD_NESTED_VISIBILITY
// clang-format on
V base_ = V();
template <bool Const>
class iterator;
template <bool Const>
class sentinel;
public:
constexpr enumerate_view()
requires std::default_initializable<V>
= default;
constexpr explicit enumerate_view(V base) : base_{std::move(base)}
{}
constexpr auto begin()
requires (!seqan::stl::detail::simple_view<V>)
{
return iterator<false>{std::ranges::begin(base_), 0};
}
constexpr auto begin() const
requires seqan::stl::detail::range_with_movable_references<V const>
{
return iterator<true>{std::ranges::begin(base_), 0};
}
constexpr auto end()
requires (!seqan::stl::detail::simple_view<V>)
{
if constexpr (std::ranges::forward_range<V> && std::ranges::common_range<V> && std::ranges::sized_range<V>)
return iterator<false>{std::ranges::end(base_), std::ranges::distance(base_)};
else
return sentinel<false>{std::ranges::end(base_)};
}
constexpr auto end() const
requires seqan::stl::detail::range_with_movable_references<V const>
{
if constexpr (std::ranges::forward_range<V const> && std::ranges::common_range<V const>
&& std::ranges::sized_range<V const>)
return iterator<true>{std::ranges::end(base_), std::ranges::distance(base_)};
else
return sentinel<true>{std::ranges::end(base_)};
}
constexpr auto size()
requires std::ranges::sized_range<V>
{
return std::ranges::size(base_);
}
constexpr auto size() const
requires std::ranges::sized_range<V const>
{
return std::ranges::size(base_);
}
constexpr V base() const &
requires std::copy_constructible<V>
{
return base_;
}
constexpr V base() &&
{
return std::move(base_);
}
};
template <class R>
enumerate_view(R &&) -> enumerate_view<seqan::stl::views::all_t<R>>;
template <std::ranges::view V>
requires seqan::stl::detail::range_with_movable_references<V>
template <bool Const>
class enumerate_view<V>::iterator
{
friend enumerate_view;
using Base = seqan::stl::detail::maybe_const<Const, V>;
public:
using iterator_category = std::input_iterator_tag;
using iterator_concept = std::conditional_t<
std::ranges::random_access_range<Base>,
std::random_access_iterator_tag,
std::conditional_t<
std::ranges::bidirectional_range<Base>,
std::bidirectional_iterator_tag,
std::conditional_t<std::ranges::forward_range<Base>, std::forward_iterator_tag, std::input_iterator_tag>>>;
using difference_type = std::ranges::range_difference_t<Base>;
using value_type = std::tuple<std::ranges::range_difference_t<Base>, std::ranges::range_value_t<Base>>;
private:
using reference_type = std::tuple<std::ranges::range_difference_t<Base>, std::ranges::range_reference_t<Base>>;
std::ranges::iterator_t<Base> current_ = std::ranges::iterator_t<Base>{};
difference_type pos_ = 0;
constexpr explicit iterator(std::ranges::iterator_t<Base> current, difference_type pos) :
current_{std::move(current)},
pos_{pos}
{}
public:
iterator()
requires std::default_initializable<std::ranges::iterator_t<Base>>
= default;
constexpr iterator(iterator<!Const> i)
requires Const && std::convertible_to<std::ranges::iterator_t<V>, std::ranges::iterator_t<Base>>
: current_{std::move(i.current_)}, pos_{i.pos_}
{}
constexpr std::ranges::iterator_t<Base> const & base() const & noexcept
{
return current_;
}
constexpr std::ranges::iterator_t<Base> base() &&
{
return std::move(current_);
}
constexpr difference_type index() const noexcept
{
return pos_;
}
constexpr auto operator*() const
{
return reference_type{pos_, *current_};
}
constexpr iterator & operator++()
{
++current_;
++pos_;
return *this;
}
constexpr void operator++(int)
{
++*this;
}
constexpr iterator operator++(int)
requires std::ranges::forward_range<Base>
{
auto temp = *this;
++*this;
return temp;
}
constexpr iterator & operator--()
requires std::ranges::bidirectional_range<Base>
{
--current_;
--pos_;
return *this;
}
constexpr iterator operator--(int)
requires std::ranges::bidirectional_range<Base>
{
auto temp = *this;
--*this;
return temp;
}
constexpr iterator & operator+=(difference_type n)
requires std::ranges::random_access_range<Base>
{
current_ += n;
pos_ += n;
return *this;
}
constexpr iterator & operator-=(difference_type n)
requires std::ranges::random_access_range<Base>
{
current_ -= n;
pos_ -= n;
return *this;
}
constexpr auto operator[](difference_type x) const
requires std::ranges::random_access_range<Base>
{
return reference_type{pos_ + x, current_[x]};
}
friend constexpr bool operator==(iterator const & x, iterator const & y) noexcept
{
return x.pos_ == y.pos_;
}
# ifdef __cpp_lib_three_way_comparison
friend constexpr std::strong_ordering operator<=>(iterator const & x, iterator const & y) noexcept
{
return x.pos_ <=> y.pos_;
}
# endif
friend constexpr iterator operator+(iterator const & x, difference_type y)
requires std::ranges::random_access_range<Base>
{
auto temp = x;
temp += y;
return temp;
}
friend constexpr iterator operator+(difference_type x, iterator const & y)
requires std::ranges::random_access_range<Base>
{
return y + x;
}
friend constexpr iterator operator-(iterator const & x, difference_type y)
requires std::ranges::random_access_range<Base>
{
auto temp = x;
temp -= y;
return temp;
}
friend constexpr difference_type operator-(iterator const & x, iterator const & y) noexcept
{
return x.pos_ - y.pos_;
}
friend constexpr auto
iter_move(iterator const & i) noexcept(noexcept(std::ranges::iter_move(i.current_))
&& std::is_nothrow_move_constructible_v<std::ranges::range_value_t<Base>>)
{
return std::tuple<difference_type, std::ranges::range_rvalue_reference_t<Base>>{
i.pos_,
std::ranges::iter_move(i.current_)};
}
};
template <std::ranges::view V>
requires seqan::stl::detail::range_with_movable_references<V>
template <bool Const>
class enumerate_view<V>::sentinel
{
friend enumerate_view;
using Base = seqan::stl::detail::maybe_const<Const, V>;
std::ranges::sentinel_t<Base> end_ = std::ranges::sentinel_t<Base>{};
constexpr explicit sentinel(std::ranges::sentinel_t<Base> end) : end_{std::move(end)}
{}
public:
sentinel() = default;
constexpr sentinel(sentinel<!Const> other)
requires Const && std::convertible_to<std::ranges::sentinel_t<V>, std::ranges::sentinel_t<Base>>
: end_{std::move(other.end_)}
{}
constexpr std::ranges::sentinel_t<Base> base() const
{
return end_;
}
template <bool OtherConst>
requires std::sentinel_for<std::ranges::sentinel_t<Base>,
std::ranges::iterator_t<seqan::stl::detail::maybe_const<OtherConst, V>>>
friend constexpr bool operator==(iterator<OtherConst> const & x, sentinel const & y) noexcept
{
return x.current_ == y.end_;
}
template <bool OtherConst>
requires std::sentinel_for<std::ranges::sentinel_t<Base>,
std::ranges::iterator_t<seqan::stl::detail::maybe_const<OtherConst, V>>>
friend constexpr std::ranges::range_difference_t<seqan::stl::detail::maybe_const<OtherConst, V>>
operator-(iterator<OtherConst> const & x, sentinel const & y)
{
return x.current_ - y.end_;
}
template <bool OtherConst>
requires std::sized_sentinel_for<std::ranges::sentinel_t<Base>,
std::ranges::iterator_t<seqan::stl::detail::maybe_const<OtherConst, V>>>
friend constexpr std::ranges::range_difference_t<seqan::stl::detail::maybe_const<OtherConst, V>>
operator-(sentinel const & x, iterator<OtherConst> const & y)
{
return x.end_ - y.current_;
}
};
struct enumerate_fn
{
template <seqan::stl::ranges::viewable_range Range>
constexpr auto operator()(Range && range) const
{
return enumerate_view{std::forward<Range>(range)};
}
};
} // namespace seqan::stl::ranges
namespace seqan::stl::views
{
inline constexpr auto enumerate = seqan::stl::ranges::enumerate_fn{};
} // namespace seqan::stl::views
#endif // ifdef __cpp_lib_ranges_enumerate
#endif // SEQAN_STD_ENUMERATE_VIEW