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 | #pragma once
#include <concepts><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ranges><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "internal/type_traits.hpp"
#include "internal/ranges.hpp"
namespace uni {
namespace internal {
template<class Range>
concept range_with_movable_reference =
std::ranges::input_range<Range> && std::move_constructible<std::ranges::range_reference_t<Range>> &&
std::move_constructible<std::ranges::range_rvalue_reference_t<Range>>;
} // namespace internal
template<std::ranges::view View>
requires internal::range_with_movable_reference<View>
struct enumerate_view : public std::ranges::view_interface<enumerate_view<View>> {
private:
View _base = View();
template<bool Const> class iterator;
template<bool Const> class sentinel;
public:
enumerate_view()
requires std::default_initializable<View>
= default;
constexpr explicit enumerate_view(View base) : _base(std::move(base)) {}
constexpr auto begin()
requires(!internal::simple_view<View>)
{
return iterator<false>(std::ranges::begin(this->_base), 0);
}
constexpr auto begin() const
requires internal::range_with_movable_reference<const View>
{
return iterator<true>(std::ranges::begin(this->_base), 0);
}
constexpr auto end()
requires(!internal::simple_view<View>)
{
if constexpr(std::ranges::common_range<View> && std::ranges::sized_range<View>) {
return iterator<false>(std::ranges::end(_base), std::ranges::distance(this->_base));
}
else {
return sentinel<false>(std::ranges::end(_base));
}
}
constexpr auto end() const
requires internal::range_with_movable_reference<const View>
{
if constexpr(std::ranges::common_range<const View> && std::ranges::sized_range<const View>) {
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<View>
{
return std::ranges::size(_base);
}
constexpr auto size() const
requires std::ranges::sized_range<const View>
{
return std::ranges::size(_base);
}
constexpr View base() const &
requires std::copy_constructible<View>
{
return _base;
}
constexpr View base() && { return std::move(_base); }
};
template<class Range>
enumerate_view(Range&& ) -> enumerate_view<std::views::all_t<Range>>;
template<std::ranges::view View>
requires internal::range_with_movable_reference<View>
template<bool Const>
class enumerate_view<View>::iterator {
using Base = internal::maybe_const_t<Const, View>;
friend enumerate_view;
public:
using iterator_category = std::iterator_traits<std::ranges::iterator_t<Base>>::iterator_category;
using iterator_concept = internal::most_primitive_iterator_concept<Const, View>;
using difference_type = std::ranges::range_difference_t<Base>;
using value_type = std::tuple<difference_type, std::ranges::range_value_t<Base>>;
private:
using rangeeference_type = std::tuple<difference_type, 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, const 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> itr)
requires Const && std::convertible_to<std::ranges::iterator_t<View>, std::ranges::iterator_t<Base>>
: _current(std::move(itr._current)), _pos(itr._pos)
{}
constexpr const std::ranges::iterator_t<Base>& base() const & noexcept {
return this->_current;
}
constexpr std::ranges::iterator_t<Base> base() && { return std::move(this->_current); }
constexpr difference_type index() const noexcept { return this->_pos; }
constexpr auto operator*() const {
return rangeeference_type(this->_pos, *this->_current);
}
constexpr iterator& operator++() {
++this->_current;
++this->_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>
{
--this->_current;
--this->_pos;
return *this;
}
constexpr iterator operator--(int)
requires std::ranges::bidirectional_range<Base>
{
auto temp = *this;
--*this;
return temp;
}
constexpr iterator& operator+=(const difference_type diff)
requires std::ranges::random_access_range<Base>
{
this->_current += diff;
this->_pos += diff;
return *this;
}
constexpr iterator& operator-=(const difference_type diff)
requires std::ranges::random_access_range<Base>
{
this->_current -= diff;
this->_pos -= diff;
return *this;
}
constexpr auto operator[](const difference_type diff) const
requires std::ranges::random_access_range<Base>
{
return rangeeference_type(this->_pos + diff, this->_current[diff]);
}
friend constexpr bool operator==(const iterator& lhs, const iterator& rhs) noexcept {
return lhs._pos == rhs._pos;
}
friend constexpr std::strong_ordering
operator<=>(const iterator& lhs, const iterator& rhs) noexcept {
return lhs._pos <=> rhs._pos;
}
friend constexpr iterator operator+(iterator lhs, const difference_type rhs)
requires std::ranges::random_access_range<Base>
{
return (lhs += rhs);
}
friend constexpr iterator operator+(const difference_type lhs, const iterator& rhs)
requires std::ranges::random_access_range<Base>
{
return rhs += lhs;
}
friend constexpr iterator operator-(iterator& lhs, const difference_type rhs)
requires std::ranges::random_access_range<Base>
{
return lhs -= rhs;
}
friend constexpr difference_type operator-(const iterator& lhs, const iterator& rhs) {
return lhs._pos - rhs._pos;
}
friend constexpr auto iter_move(const iterator& itr)
noexcept(
noexcept (std::ranges::iter_move(itr._current)) &&
std::is_nothrow_move_constructible_v<std::ranges::range_rvalue_reference_t<Base>>
)
{
return std::tuple<difference_type, std::ranges::range_rvalue_reference_t<Base>>(
itr._pos, std::ranges::iter_move(itr._current)
);
}
};
template<std::ranges::view View>
requires internal::range_with_movable_reference<View>
template<bool Const>
class enumerate_view<View>::sentinel {
using Base = internal::maybe_const_t<Const, View>;
std::ranges::sentinel_t<Base> _end = std::ranges::sentinel_t<Base>();
constexpr explicit sentinel(std::ranges::sentinel_t<Base> end) : _end(std::move(end)) {}
friend enumerate_view;
public:
sentinel() = default;
constexpr sentinel(sentinel<!Const> other)
requires Const && std::convertible_to<std::ranges::sentinel_t<View>, std::ranges::sentinel_t<Base>>
: _end(std::move(other._end)) {}
constexpr std::ranges::sentinel_t<Base> base() const { return this->_end; }
template<bool Const_>
requires
std::sentinel_for<
std::ranges::sentinel_t<Base>, std::ranges::iterator_t<internal::maybe_const_t<Const_, View>>
>
friend constexpr bool operator==(const iterator<Const_>& lhs, const sentinel& rhs) {
return lhs._current == rhs._end;
}
template<bool Const_>
requires
std::sized_sentinel_for<
std::ranges::sentinel_t<Base>, std::ranges::iterator_t<internal::maybe_const_t<Const_, View>>
>
friend constexpr std::ranges::range_difference_t<internal::maybe_const_t<Const_, View>>
operator-(const iterator<Const_>& lhs, const sentinel& rhs) {
return lhs._current - rhs._end;
}
template<bool Const_>
requires std::sized_sentinel_for<
std::ranges::sentinel_t<Base>, std::ranges::iterator_t<internal::maybe_const_t<Const_, View>>>
friend constexpr std::ranges::range_difference_t<internal::maybe_const_t<Const_, View>>
operator-(const sentinel& lhs, const iterator<Const_>& rhs) {
return lhs._end - rhs._current;
}
};
namespace views {
namespace internal {
template<class T>
concept can_enumerate_view =
requires { enumerate_view<std::views::all_t<T>>(std::declval<T>()); };
} // namespace internal
struct Enumerate : adaptor::range_adaptor_closure<Enumerate> {
template<std::ranges::viewable_range Range>
requires internal::can_enumerate_view<Range>
constexpr auto operator() [[nodiscard]] (Range&& range) const {
return enumerate_view<std::views::all_t<Range>>(std::forward<Range>(range));
}
};
inline constexpr Enumerate enumerate;
} // namespace views
} // namespace uni
namespace std::ranges {
template<class T>
inline constexpr bool enable_borrowed_range<uni::enumerate_view<T>> = enable_borrowed_range<T>;
} // namespace std
|