1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___RANGES_IOTA_VIEW_H
11#define _LIBCPP___RANGES_IOTA_VIEW_H
12
13#include <__assert>
14#include <__compare/three_way_comparable.h>
15#include <__concepts/arithmetic.h>
16#include <__concepts/constructible.h>
17#include <__concepts/convertible_to.h>
18#include <__concepts/copyable.h>
19#include <__concepts/equality_comparable.h>
20#include <__concepts/invocable.h>
21#include <__concepts/same_as.h>
22#include <__concepts/semiregular.h>
23#include <__concepts/totally_ordered.h>
24#include <__config>
25#include <__iterator/concepts.h>
26#include <__iterator/incrementable_traits.h>
27#include <__iterator/iterator_traits.h>
28#include <__iterator/unreachable_sentinel.h>
29#include <__ranges/enable_borrowed_range.h>
30#include <__ranges/movable_box.h>
31#include <__ranges/view_interface.h>
32#include <__type_traits/conditional.h>
33#include <__type_traits/decay.h>
34#include <__type_traits/is_nothrow_constructible.h>
35#include <__type_traits/make_unsigned.h>
36#include <__type_traits/type_identity.h>
37#include <__utility/forward.h>
38#include <__utility/move.h>
39
40#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
41# pragma GCC system_header
42#endif
43
44_LIBCPP_PUSH_MACROS
45#include <__undef_macros>
46
47_LIBCPP_BEGIN_NAMESPACE_STD
48
49#if _LIBCPP_STD_VER >= 20
50
51namespace ranges {
52template <class _Int>
53struct __get_wider_signed {
54 consteval static auto __call() {
55 if constexpr (sizeof(_Int) < sizeof(short))
56 return type_identity<short>{};
57 else if constexpr (sizeof(_Int) < sizeof(int))
58 return type_identity<int>{};
59 else if constexpr (sizeof(_Int) < sizeof(long))
60 return type_identity<long>{};
61 else if constexpr (sizeof(_Int) < sizeof(long long))
62 return type_identity<long long>{};
63# if _LIBCPP_HAS_INT128
64 else if constexpr (sizeof(_Int) <= sizeof(__int128))
65 return type_identity<__int128>{};
66# else
67 else if constexpr (sizeof(_Int) <= sizeof(long long))
68 return type_identity<long long>{};
69# endif
70 else
71 static_assert(false, "Found integer-like type that is bigger than the largest integer like type.");
72 }
73
74 using type = typename decltype(__call())::type;
75};
76
77template <class _Start>
78using _IotaDiffT _LIBCPP_NODEBUG =
79 typename _If< (!integral<_Start> || sizeof(iter_difference_t<_Start>) > sizeof(_Start)),
80 type_identity<iter_difference_t<_Start>>,
81 __get_wider_signed<_Start> >::type;
82
83template <class _Iter>
84concept __decrementable = incrementable<_Iter> && requires(_Iter __i) {
85 { --__i } -> same_as<_Iter&>;
86 { __i-- } -> same_as<_Iter>;
87};
88
89template <class _Iter>
90concept __advanceable =
91 __decrementable<_Iter> && totally_ordered<_Iter> &&
92 requires(_Iter __i, const _Iter __j, const _IotaDiffT<_Iter> __n) {
93 { __i += __n } -> same_as<_Iter&>;
94 { __i -= __n } -> same_as<_Iter&>;
95 _Iter(__j + __n);
96 _Iter(__n + __j);
97 _Iter(__j - __n);
98 { __j - __j } -> convertible_to<_IotaDiffT<_Iter>>;
99 };
100
101template <class>
102struct __iota_iterator_category {};
103
104template <incrementable _Tp>
105struct __iota_iterator_category<_Tp> {
106 using iterator_category = input_iterator_tag;
107};
108
109template <weakly_incrementable _Start, semiregular _BoundSentinel = unreachable_sentinel_t>
110 requires __weakly_equality_comparable_with<_Start, _BoundSentinel> && copyable<_Start>
111class iota_view : public view_interface<iota_view<_Start, _BoundSentinel>> {
112 struct __iterator : public __iota_iterator_category<_Start> {
113 friend class iota_view;
114
115 using iterator_concept =
116 _If<__advanceable<_Start>,
117 random_access_iterator_tag,
118 _If<__decrementable<_Start>,
119 bidirectional_iterator_tag,
120 _If<incrementable<_Start>,
121 forward_iterator_tag,
122 /*Else*/ input_iterator_tag>>>;
123
124 using value_type = _Start;
125 using difference_type = _IotaDiffT<_Start>;
126
127 _Start __value_ = _Start();
128
129 _LIBCPP_HIDE_FROM_ABI __iterator()
130 requires default_initializable<_Start>
131 = default;
132
133 _LIBCPP_HIDE_FROM_ABI constexpr explicit __iterator(_Start __value) : __value_(std::move(__value)) {}
134
135 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Start operator*() const
136 noexcept(is_nothrow_copy_constructible_v<_Start>) {
137 return __value_;
138 }
139
140 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
141 ++__value_;
142 return *this;
143 }
144
145 _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++*this; }
146
147 _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int)
148 requires incrementable<_Start>
149 {
150 auto __tmp = *this;
151 ++*this;
152 return __tmp;
153 }
154
155 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--()
156 requires __decrementable<_Start>
157 {
158 --__value_;
159 return *this;
160 }
161
162 _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int)
163 requires __decrementable<_Start>
164 {
165 auto __tmp = *this;
166 --*this;
167 return __tmp;
168 }
169
170 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n)
171 requires __advanceable<_Start>
172 {
173 if constexpr (__integer_like<_Start> && !__signed_integer_like<_Start>) {
174 if (__n >= difference_type(0)) {
175 __value_ += static_cast<_Start>(__n);
176 } else {
177 __value_ -= static_cast<_Start>(-__n);
178 }
179 } else {
180 __value_ += __n;
181 }
182 return *this;
183 }
184
185 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n)
186 requires __advanceable<_Start>
187 {
188 if constexpr (__integer_like<_Start> && !__signed_integer_like<_Start>) {
189 if (__n >= difference_type(0)) {
190 __value_ -= static_cast<_Start>(__n);
191 } else {
192 __value_ += static_cast<_Start>(-__n);
193 }
194 } else {
195 __value_ -= __n;
196 }
197 return *this;
198 }
199
200 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Start operator[](difference_type __n) const
201 requires __advanceable<_Start>
202 {
203 return _Start(__value_ + __n);
204 }
205
206 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
207 requires equality_comparable<_Start>
208 {
209 return __x.__value_ == __y.__value_;
210 }
211
212 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
213 requires totally_ordered<_Start>
214 {
215 return __x.__value_ < __y.__value_;
216 }
217
218 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
219 requires totally_ordered<_Start>
220 {
221 return __y < __x;
222 }
223
224 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
225 requires totally_ordered<_Start>
226 {
227 return !(__y < __x);
228 }
229
230 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y)
231 requires totally_ordered<_Start>
232 {
233 return !(__x < __y);
234 }
235
236 _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
237 requires totally_ordered<_Start> && three_way_comparable<_Start>
238 {
239 return __x.__value_ <=> __y.__value_;
240 }
241
242 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(__iterator __i, difference_type __n)
243 requires __advanceable<_Start>
244 {
245 __i += __n;
246 return __i;
247 }
248
249 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, __iterator __i)
250 requires __advanceable<_Start>
251 {
252 return __i + __n;
253 }
254
255 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(__iterator __i, difference_type __n)
256 requires __advanceable<_Start>
257 {
258 __i -= __n;
259 return __i;
260 }
261
262 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type
263 operator-(const __iterator& __x, const __iterator& __y)
264 requires __advanceable<_Start>
265 {
266 if constexpr (__integer_like<_Start>) {
267 if constexpr (__signed_integer_like<_Start>) {
268 return difference_type(difference_type(__x.__value_) - difference_type(__y.__value_));
269 }
270 if (__y.__value_ > __x.__value_) {
271 return difference_type(-difference_type(__y.__value_ - __x.__value_));
272 }
273 return difference_type(__x.__value_ - __y.__value_);
274 }
275 return __x.__value_ - __y.__value_;
276 }
277 };
278
279 struct __sentinel {
280 friend class iota_view;
281
282 private:
283 _BoundSentinel __bound_sentinel_ = _BoundSentinel();
284
285 public:
286 _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
287 _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(_BoundSentinel __bound_sentinel)
288 : __bound_sentinel_(std::move(__bound_sentinel)) {}
289
290 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __sentinel& __y) {
291 return __x.__value_ == __y.__bound_sentinel_;
292 }
293
294 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr iter_difference_t<_Start>
295 operator-(const __iterator& __x, const __sentinel& __y)
296 requires sized_sentinel_for<_BoundSentinel, _Start>
297 {
298 return __x.__value_ - __y.__bound_sentinel_;
299 }
300
301 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr iter_difference_t<_Start>
302 operator-(const __sentinel& __x, const __iterator& __y)
303 requires sized_sentinel_for<_BoundSentinel, _Start>
304 {
305 return -(__y - __x);
306 }
307 };
308
309 _Start __value_ = _Start();
310 _BoundSentinel __bound_sentinel_ = _BoundSentinel();
311
312public:
313 _LIBCPP_HIDE_FROM_ABI iota_view()
314 requires default_initializable<_Start>
315 = default;
316
317 _LIBCPP_HIDE_FROM_ABI constexpr explicit iota_view(_Start __value) : __value_(std::move(__value)) {}
318
319 _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23
320 iota_view(type_identity_t<_Start> __value, type_identity_t<_BoundSentinel> __bound_sentinel)
321 : __value_(std::move(__value)), __bound_sentinel_(std::move(__bound_sentinel)) {
322 // Validate the precondition if possible.
323 if constexpr (totally_ordered_with<_Start, _BoundSentinel>) {
324 _LIBCPP_ASSERT_VALID_INPUT_RANGE(
325 bool(__value_ <= __bound_sentinel_), "iota_view: bound must be reachable from value");
326 }
327 }
328
329 _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(__iterator __first, __iterator __last)
330 requires same_as<_Start, _BoundSentinel>
331 : iota_view(std::move(__first.__value_), std::move(__last.__value_)) {}
332
333 _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(__iterator __first, _BoundSentinel __last)
334 requires same_as<_BoundSentinel, unreachable_sentinel_t>
335 : iota_view(std::move(__first.__value_), std::move(__last)) {}
336
337 _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(__iterator __first, __sentinel __last)
338 requires(!same_as<_Start, _BoundSentinel> && !same_as<_BoundSentinel, unreachable_sentinel_t>)
339 : iota_view(std::move(__first.__value_), std::move(__last.__bound_sentinel_)) {}
340
341 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() const { return __iterator{__value_}; }
342
343 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const {
344 if constexpr (same_as<_BoundSentinel, unreachable_sentinel_t>)
345 return unreachable_sentinel;
346 else
347 return __sentinel{__bound_sentinel_};
348 }
349
350 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator end() const
351 requires same_as<_Start, _BoundSentinel>
352 {
353 return __iterator{__bound_sentinel_};
354 }
355
356 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const { return __value_ == __bound_sentinel_; }
357
358 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
359 requires(same_as<_Start, _BoundSentinel> && __advanceable<_Start>) ||
360 (integral<_Start> && integral<_BoundSentinel>) || sized_sentinel_for<_BoundSentinel, _Start>
361 {
362 if constexpr (__integer_like<_Start> && __integer_like<_BoundSentinel>) {
363 return (__value_ < 0)
364 ? ((__bound_sentinel_ < 0)
365 ? std::__to_unsigned_like(-__value_) - std::__to_unsigned_like(-__bound_sentinel_)
366 : std::__to_unsigned_like(__bound_sentinel_) + std::__to_unsigned_like(-__value_))
367 : std::__to_unsigned_like(__bound_sentinel_) - std::__to_unsigned_like(__value_);
368 } else {
369 return std::__to_unsigned_like(__bound_sentinel_ - __value_);
370 }
371 }
372};
373
374template <class _Start, class _BoundSentinel>
375 requires(!__integer_like<_Start> || !__integer_like<_BoundSentinel> ||
376 (__signed_integer_like<_Start> == __signed_integer_like<_BoundSentinel>))
377iota_view(_Start, _BoundSentinel) -> iota_view<_Start, _BoundSentinel>;
378
379template <class _Start, class _BoundSentinel>
380inline constexpr bool enable_borrowed_range<iota_view<_Start, _BoundSentinel>> = true;
381
382namespace views {
383namespace __iota {
384struct __fn {
385 template <class _Start>
386 requires(requires(_Start __s) { ranges::iota_view<decay_t<_Start>>(std::forward<_Start>(__s)); })
387 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Start&& __start) const
388 noexcept(noexcept(ranges::iota_view<decay_t<_Start>>(std::forward<_Start>(__start)))) {
389 return ranges::iota_view<decay_t<_Start>>(std::forward<_Start>(__start));
390 }
391
392 template <class _Start, class _BoundSentinel>
393 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto
394 operator()(_Start&& __start, _BoundSentinel&& __bound_sentinel) const noexcept(
395 noexcept(ranges::iota_view(std::forward<_Start>(__start), std::forward<_BoundSentinel>(__bound_sentinel))))
396 -> decltype(ranges::iota_view(std::forward<_Start>(__start), std::forward<_BoundSentinel>(__bound_sentinel))) {
397 return ranges::iota_view(std::forward<_Start>(__start), std::forward<_BoundSentinel>(__bound_sentinel));
398 }
399};
400} // namespace __iota
401
402inline namespace __cpo {
403inline constexpr auto iota = __iota::__fn{};
404} // namespace __cpo
405
406# if _LIBCPP_STD_VER >= 26
407
408inline constexpr auto indices = [] [[nodiscard]] (__integer_like auto __size) static {
409 return ranges::views::iota(decltype(__size){}, __size);
410};
411
412# endif
413
414} // namespace views
415} // namespace ranges
416
417#endif // _LIBCPP_STD_VER >= 20
418
419_LIBCPP_END_NAMESPACE_STD
420
421_LIBCPP_POP_MACROS
422
423#endif // _LIBCPP___RANGES_IOTA_VIEW_H
424