libstdc++: ranges Source File

libstdc++

ranges
Go to the documentation of this file.
1// <ranges> -*- C++ -*-
2
3// Copyright (C) 2019-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/ranges
26 * This is a Standard C++ Library header.
27 * @ingroup concepts
28 */
29
30#ifndef _GLIBCXX_RANGES
31#define _GLIBCXX_RANGES 1
32
33#if __cplusplus > 201703L
34
35#ifdef _GLIBCXX_SYSHDR
36#pragma GCC system_header
37#endif
38
39#include <concepts>
40
41#if __cpp_lib_concepts
42
43#include <compare>
44#include <initializer_list>
45#include <iterator>
46#include <optional>
47#include <span>
48#include <string_view>
49#include <tuple>
50#if __cplusplus > 202002L
51#include <utility>
52#include <variant>
53#endif
54#include <bits/binders.h>
55#include <bits/ranges_util.h>
56#include <bits/refwrap.h>
57
58#define __glibcxx_want_algorithm_default_value_type
59#define __glibcxx_want_hardened_view_interface
60#define __glibcxx_want_ranges
61#define __glibcxx_want_ranges_as_const
62#define __glibcxx_want_ranges_as_rvalue
63#define __glibcxx_want_ranges_cache_latest
64#define __glibcxx_want_ranges_cartesian_product
65#define __glibcxx_want_ranges_concat
66#define __glibcxx_want_ranges_chunk
67#define __glibcxx_want_ranges_chunk_by
68#define __glibcxx_want_ranges_enumerate
69#define __glibcxx_want_ranges_filter
70#define __glibcxx_want_ranges_indices
71#define __glibcxx_want_ranges_join_with
72#define __glibcxx_want_ranges_repeat
73#define __glibcxx_want_ranges_slide
74#define __glibcxx_want_ranges_stride
75#define __glibcxx_want_ranges_to_container
76#define __glibcxx_want_ranges_as_input
77#define __glibcxx_want_ranges_zip
78#include <bits/version.h>
79
80#ifdef __glibcxx_generator // C++ >= 23 && __glibcxx_coroutine
81# include <bits/elements_of.h>
82#endif
83
84/**
85 * @defgroup ranges Ranges
86 *
87 * Components for dealing with ranges of elements.
88 */
89
90namespace std _GLIBCXX_VISIBILITY(default)
91{
92_GLIBCXX_BEGIN_NAMESPACE_VERSION
93namespace ranges
94{
95 // [range.access] customization point objects
96 // [range.req] range and view concepts
97 // [range.dangling] dangling iterator handling
98 // Defined in <bits/ranges_base.h>
99
100 // [view.interface] View interface
101 // [range.subrange] Sub-ranges
102 // Defined in <bits/ranges_util.h>
103
104 // C++20 24.6 [range.factories] Range factories
105
106 /// A view that contains no elements.
107 template<typename _Tp> requires is_object_v<_Tp>
108 class empty_view
109 : public view_interface<empty_view<_Tp>>
110 {
111 public:
112 static constexpr _Tp* begin() noexcept { return nullptr; }
113 static constexpr _Tp* end() noexcept { return nullptr; }
114 static constexpr _Tp* data() noexcept { return nullptr; }
115 static constexpr size_t size() noexcept { return 0; }
116 static constexpr bool empty() noexcept { return true; }
117 };
118
119 template<typename _Tp>
120 inline constexpr bool enable_borrowed_range<empty_view<_Tp>> = true;
121
122 namespace __detail
123 {
124#if __cpp_lib_ranges >= 202207L // C++ >= 23
125 // P2494R2 Relaxing range adaptors to allow for move only types
126 template<typename _Tp>
127 concept __boxable = move_constructible<_Tp> && is_object_v<_Tp>;
128#else
129 template<typename _Tp>
130 concept __boxable = copy_constructible<_Tp> && is_object_v<_Tp>;
131#endif
132
133 template<__boxable _Tp>
134 struct __box : std::optional<_Tp>
135 {
136 using std::optional<_Tp>::optional;
137
138 constexpr
139 __box()
140 noexcept(is_nothrow_default_constructible_v<_Tp>)
141 requires default_initializable<_Tp>
142 : std::optional<_Tp>{std::in_place}
143 { }
144
145 __box(const __box&) = default;
146 __box(__box&&) = default;
147
148 using std::optional<_Tp>::operator=;
149
150 // _GLIBCXX_RESOLVE_LIB_DEFECTS
151 // 3477. Simplify constraints for semiregular-box
152 // 3572. copyable-box should be fully constexpr
153 constexpr __box&
154 operator=(const __box& __that)
155 noexcept(is_nothrow_copy_constructible_v<_Tp>)
156 requires (!copyable<_Tp>) && copy_constructible<_Tp>
157 {
158 if (this != std::__addressof(__that))
159 {
160 if ((bool)__that)
161 this->emplace(*__that);
162 else
163 this->reset();
164 }
165 return *this;
166 }
167
168 constexpr __box&
169 operator=(__box&& __that)
170 noexcept(is_nothrow_move_constructible_v<_Tp>)
171 requires (!movable<_Tp>)
172 {
173 if (this != std::__addressof(__that))
174 {
175 if ((bool)__that)
176 this->emplace(std::move(*__that));
177 else
178 this->reset();
179 }
180 return *this;
181 }
182 };
183
184 template<typename _Tp>
185 concept __boxable_copyable
186 = copy_constructible<_Tp>
187 && (copyable<_Tp> || (is_nothrow_move_constructible_v<_Tp>
188 && is_nothrow_copy_constructible_v<_Tp>));
189 template<typename _Tp>
190 concept __boxable_movable
191 = (!copy_constructible<_Tp>)
192 && (movable<_Tp> || is_nothrow_move_constructible_v<_Tp>);
193
194 // For types which are already copyable (or since C++23, movable)
195 // this specialization of the box wrapper stores the object directly
196 // without going through std::optional. It provides just the subset of
197 // the primary template's API that we currently use.
198 template<__boxable _Tp>
199 requires __boxable_copyable<_Tp> || __boxable_movable<_Tp>
200 struct __box<_Tp>
201 {
202 private:
203 [[no_unique_address]] _Tp _M_value = _Tp();
204
205 public:
206 __box() requires default_initializable<_Tp> = default;
207
208 constexpr explicit
209 __box(const _Tp& __t)
210 noexcept(is_nothrow_copy_constructible_v<_Tp>)
211 requires copy_constructible<_Tp>
212 : _M_value(__t)
213 { }
214
215 constexpr explicit
216 __box(_Tp&& __t)
217 noexcept(is_nothrow_move_constructible_v<_Tp>)
218 : _M_value(std::move(__t))
219 { }
220
221 template<typename... _Args>
222 requires constructible_from<_Tp, _Args...>
223 constexpr explicit
224 __box(in_place_t, _Args&&... __args)
225 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
226 : _M_value(std::forward<_Args>(__args)...)
227 { }
228
229 __box(const __box&) = default;
230 __box(__box&&) = default;
231 __box& operator=(const __box&) requires copyable<_Tp> = default;
232 __box& operator=(__box&&) requires movable<_Tp> = default;
233
234 // When _Tp is nothrow_copy_constructible but not copy_assignable,
235 // copy assignment is implemented via destroy-then-copy-construct.
236 constexpr __box&
237 operator=(const __box& __that) noexcept
238 requires (!copyable<_Tp>) && copy_constructible<_Tp>
239 {
240 static_assert(is_nothrow_copy_constructible_v<_Tp>);
241 if (this != std::__addressof(__that))
242 {
243 _M_value.~_Tp();
244 std::construct_at(std::__addressof(_M_value), *__that);
245 }
246 return *this;
247 }
248
249 // Likewise for move assignment.
250 constexpr __box&
251 operator=(__box&& __that) noexcept
252 requires (!movable<_Tp>)
253 {
254 static_assert(is_nothrow_move_constructible_v<_Tp>);
255 if (this != std::__addressof(__that))
256 {
257 _M_value.~_Tp();
258 std::construct_at(std::__addressof(_M_value), std::move(*__that));
259 }
260 return *this;
261 }
262
263 constexpr bool
264 has_value() const noexcept
265 { return true; };
266
267 constexpr _Tp&
268 operator*() & noexcept
269 { return _M_value; }
270
271 constexpr const _Tp&
272 operator*() const & noexcept
273 { return _M_value; }
274
275 constexpr _Tp&&
276 operator*() && noexcept
277 { return std::move(_M_value); }
278
279 constexpr const _Tp&&
280 operator*() const && noexcept
281 { return std::move(_M_value); }
282
283 constexpr _Tp*
284 operator->() noexcept
285 { return std::__addressof(_M_value); }
286
287 constexpr const _Tp*
288 operator->() const noexcept
289 { return std::__addressof(_M_value); }
290 };
291
292 namespace __func_handle
293 {
294 template<typename _Fn>
295 struct _Inplace
296 {
297 _Inplace() = default;
298
299 constexpr explicit
300 _Inplace(_Fn __func) noexcept
301 : _M_fn(__func)
302 { }
303
304 template<typename... _Iters>
305 constexpr decltype(auto)
306 _M_call_deref(const _Iters&... __iters) const
307 noexcept(noexcept(_M_fn(*__iters...)))
308 { return _M_fn(*__iters...); }
309
310 template<typename _DistType, typename... _Iters>
311 constexpr decltype(auto)
312 _M_call_subscript(const _DistType __n, const _Iters&... __iters) const
313 noexcept(noexcept(_M_fn(__iters[iter_difference_t<_Iters>(__n)]...)))
314 { return _M_fn(__iters[iter_difference_t<_Iters>(__n)]...); }
315
316 private:
317 [[no_unique_address]] _Fn _M_fn = _Fn();
318 };
319
320 template<typename _Fn>
321 struct _InplaceMemPtr
322 {
323 _InplaceMemPtr() = default;
324
325 constexpr explicit
326 _InplaceMemPtr(_Fn __func) noexcept
327 : _M_ptr(__func)
328 {}
329
330 template<typename... _Iters>
331 constexpr decltype(auto)
332 _M_call_deref(const _Iters&... __iters) const
333 noexcept(noexcept(std::__invoke(_M_ptr, *__iters...)))
334 { return std::__invoke(_M_ptr, *__iters...); }
335
336 template<typename _DistType, typename... _Iters>
337 constexpr decltype(auto)
338 _M_call_subscript(const _DistType __n, const _Iters&... __iters) const
339 noexcept(noexcept(std::__invoke(_M_ptr, __iters[iter_difference_t<_Iters>(__n)]...)))
340 { return std::__invoke(_M_ptr, __iters[iter_difference_t<_Iters>(__n)]...); }
341
342 private:
343 _Fn _M_ptr = nullptr;
344 };
345
346 template<typename _Fn>
347 struct _ViaPointer
348 {
349 _ViaPointer() = default;
350
351 constexpr explicit
352 _ViaPointer(_Fn& __func) noexcept
353 : _M_ptr(std::addressof(__func))
354 { }
355
356 template<typename _Un>
357 requires (!is_const_v<_Un>) && is_same_v<const _Un, _Fn>
358 constexpr
359 _ViaPointer(_ViaPointer<_Un> __other) noexcept
360 : _M_ptr(__other._M_ptr)
361 { }
362
363 template<typename... _Iters>
364 constexpr decltype(auto)
365 _M_call_deref(const _Iters&... __iters) const
366 noexcept(noexcept((*_M_ptr)(*__iters...)))
367 { return (*_M_ptr)(*__iters...); }
368
369 template<typename _DistType, typename... _Iters>
370 constexpr decltype(auto)
371 _M_call_subscript(const _DistType __n, const _Iters&... __iters) const
372 noexcept(noexcept((*_M_ptr)(__iters[iter_difference_t<_Iters>(__n)]...)))
373 { return (*_M_ptr)(__iters[iter_difference_t<_Iters>(__n)]...); }
374
375 private:
376 _Fn* _M_ptr = nullptr;
377
378 template<typename>
379 friend struct _ViaPointer;
380 };
381
382 template<typename _Fn>
383 struct _StaticCall
384 {
385 _StaticCall() = default;
386
387 constexpr explicit
388 _StaticCall(const _Fn&) noexcept
389 {}
390
391 template<typename... _Iters>
392 static constexpr decltype(auto)
393 _M_call_deref(const _Iters&... __iters)
394 noexcept(noexcept(_Fn::operator()(*__iters...)))
395 { return _Fn::operator()(*__iters...); }
396
397 template<typename _DistType, typename... _Iters>
398 static constexpr decltype(auto)
399 _M_call_subscript(_DistType __n, const _Iters&... __iters)
400 noexcept(noexcept(_Fn::operator()(__iters[iter_difference_t<_Iters>(__n)]...)))
401 { return _Fn::operator()(__iters[iter_difference_t<_Iters>(__n)]...); }
402 };
403
404 template<typename _Fn, typename... _Iters>
405 consteval auto
406 __select()
407 {
408 using _Fd = remove_cv_t<_Fn>;
409 if constexpr (is_member_pointer_v<_Fd>)
410 return __func_handle::_InplaceMemPtr<_Fd>();
411 else if constexpr (is_function_v<remove_pointer_t<_Fd>>)
412 return __func_handle::_Inplace<_Fd>();
413 else if constexpr (__is_std_op_wrapper<_Fd>)
414 return __func_handle::_Inplace<_Fd>();
415 else if constexpr (requires (const _Iters&... __iters)
416 { _Fd::operator()(*__iters...); })
417 return __func_handle::_StaticCall<_Fd>();
418 else
419 return __func_handle::_ViaPointer<_Fn>();
420 };
421 } // __func_handle
422
423 template<typename _Fn, typename... _Iters>
424 using __func_handle_t = decltype(__func_handle::__select<_Fn, _Iters...>());
425 } // namespace __detail
426
427 /// A view that contains exactly one element.
428#if __cpp_lib_ranges >= 202207L // C++ >= 23
429 template<move_constructible _Tp>
430#else
431 template<copy_constructible _Tp>
432#endif
433 requires is_object_v<_Tp>
434 class single_view : public view_interface<single_view<_Tp>>
435 {
436 public:
437 single_view() requires default_initializable<_Tp> = default;
438
439 constexpr explicit
440 single_view(const _Tp& __t)
441 noexcept(is_nothrow_copy_constructible_v<_Tp>)
442 requires copy_constructible<_Tp>
443 : _M_value(__t)
444 { }
445
446 constexpr explicit
447 single_view(_Tp&& __t)
448 noexcept(is_nothrow_move_constructible_v<_Tp>)
449 : _M_value(std::move(__t))
450 { }
451
452 // _GLIBCXX_RESOLVE_LIB_DEFECTS
453 // 3428. single_view's in place constructor should be explicit
454 template<typename... _Args>
455 requires constructible_from<_Tp, _Args...>
456 constexpr explicit
457 single_view(in_place_t, _Args&&... __args)
458 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
459 : _M_value{in_place, std::forward<_Args>(__args)...}
460 { }
461
462 constexpr _Tp*
463 begin() noexcept
464 { return data(); }
465
466 constexpr const _Tp*
467 begin() const noexcept
468 { return data(); }
469
470 constexpr _Tp*
471 end() noexcept
472 { return data() + 1; }
473
474 constexpr const _Tp*
475 end() const noexcept
476 { return data() + 1; }
477
478 // _GLIBCXX_RESOLVE_LIB_DEFECTS
479 // 4035. single_view should provide empty
480 static constexpr bool
481 empty() noexcept
482 { return false; }
483
484 static constexpr size_t
485 size() noexcept
486 { return 1; }
487
488 constexpr _Tp*
489 data() noexcept
490 { return _M_value.operator->(); }
491
492 constexpr const _Tp*
493 data() const noexcept
494 { return _M_value.operator->(); }
495
496 private:
497 [[no_unique_address]] __detail::__box<_Tp> _M_value;
498 };
499
500 template<typename _Tp>
501 single_view(_Tp) -> single_view<_Tp>;
502
503 namespace __detail
504 {
505 template<typename _Wp>
506 constexpr auto __to_signed_like(_Wp __w) noexcept
507 {
508 if constexpr (!integral<_Wp>)
509 return iter_difference_t<_Wp>();
510 else if constexpr (sizeof(iter_difference_t<_Wp>) > sizeof(_Wp))
511 return iter_difference_t<_Wp>(__w);
512 else if constexpr (sizeof(ptrdiff_t) > sizeof(_Wp))
513 return ptrdiff_t(__w);
514 else if constexpr (sizeof(long long) > sizeof(_Wp))
515 return (long long)(__w);
516#ifdef __SIZEOF_INT128__
517 else if constexpr (__SIZEOF_INT128__ > sizeof(_Wp))
518 return __int128(__w);
519#endif
520 else
521 return __max_diff_type(__w);
522 }
523
524 template<typename _Wp>
525 using __iota_diff_t = decltype(__to_signed_like(std::declval<_Wp>()));
526
527 template<typename _It>
528 concept __decrementable = incrementable<_It>
529 && requires(_It __i)
530 {
531 { --__i } -> same_as<_It&>;
532 { __i-- } -> same_as<_It>;
533 };
534
535 template<typename _It>
536 concept __advanceable = __decrementable<_It> && totally_ordered<_It>
537 && requires( _It __i, const _It __j, const __iota_diff_t<_It> __n)
538 {
539 { __i += __n } -> same_as<_It&>;
540 { __i -= __n } -> same_as<_It&>;
541 _It(__j + __n);
542 _It(__n + __j);
543 _It(__j - __n);
544 { __j - __j } -> convertible_to<__iota_diff_t<_It>>;
545 };
546
547 template<typename _Winc>
548 struct __iota_view_iter_cat
549 { };
550
551 template<incrementable _Winc>
552 struct __iota_view_iter_cat<_Winc>
553 { using iterator_category = input_iterator_tag; };
554 } // namespace __detail
555
556 template<weakly_incrementable _Winc,
557 semiregular _Bound = unreachable_sentinel_t>
558 requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound>
559 && copyable<_Winc>
560 class iota_view : public view_interface<iota_view<_Winc, _Bound>>
561 {
562 private:
563 struct _Sentinel;
564
565 struct _Iterator : __detail::__iota_view_iter_cat<_Winc>
566 {
567 private:
568 static auto
569 _S_iter_concept()
570 {
571 using namespace __detail;
572 if constexpr (__advanceable<_Winc>)
573 return random_access_iterator_tag{};
574 else if constexpr (__decrementable<_Winc>)
575 return bidirectional_iterator_tag{};
576 else if constexpr (incrementable<_Winc>)
577 return forward_iterator_tag{};
578 else
579 return input_iterator_tag{};
580 }
581
582 public:
583 using iterator_concept = decltype(_S_iter_concept());
584 // iterator_category defined in __iota_view_iter_cat
585 using value_type = _Winc;
586 using difference_type = __detail::__iota_diff_t<_Winc>;
587
588 _Iterator() requires default_initializable<_Winc> = default;
589
590 constexpr explicit
591 _Iterator(_Winc __value)
592 : _M_value(__value) { }
593
594 constexpr _Winc
595 operator*() const noexcept(is_nothrow_copy_constructible_v<_Winc>)
596 { return _M_value; }
597
598 constexpr _Iterator&
599 operator++()
600 {
601 ++_M_value;
602 return *this;
603 }
604
605 constexpr void
606 operator++(int)
607 { ++*this; }
608
609 constexpr _Iterator
610 operator++(int) requires incrementable<_Winc>
611 {
612 auto __tmp = *this;
613 ++*this;
614 return __tmp;
615 }
616
617 constexpr _Iterator&
618 operator--() requires __detail::__decrementable<_Winc>
619 {
620 --_M_value;
621 return *this;
622 }
623
624 constexpr _Iterator
625 operator--(int) requires __detail::__decrementable<_Winc>
626 {
627 auto __tmp = *this;
628 --*this;
629 return __tmp;
630 }
631
632 constexpr _Iterator&
633 operator+=(difference_type __n) requires __detail::__advanceable<_Winc>
634 {
635 using __detail::__is_integer_like;
636 using __detail::__is_signed_integer_like;
637 if constexpr (__is_integer_like<_Winc>
638 && !__is_signed_integer_like<_Winc>)
639 {
640 if (__n >= difference_type(0))
641 _M_value += static_cast<_Winc>(__n);
642 else
643 _M_value -= static_cast<_Winc>(-__n);
644 }
645 else
646 _M_value += __n;
647 return *this;
648 }
649
650 constexpr _Iterator&
651 operator-=(difference_type __n) requires __detail::__advanceable<_Winc>
652 {
653 using __detail::__is_integer_like;
654 using __detail::__is_signed_integer_like;
655 if constexpr (__is_integer_like<_Winc>
656 && !__is_signed_integer_like<_Winc>)
657 {
658 if (__n >= difference_type(0))
659 _M_value -= static_cast<_Winc>(__n);
660 else
661 _M_value += static_cast<_Winc>(-__n);
662 }
663 else
664 _M_value -= __n;
665 return *this;
666 }
667
668 constexpr _Winc
669 operator[](difference_type __n) const
670 requires __detail::__advanceable<_Winc>
671 { return _Winc(_M_value + __n); }
672
673 friend constexpr bool
674 operator==(const _Iterator& __x, const _Iterator& __y)
675 requires equality_comparable<_Winc>
676 { return __x._M_value == __y._M_value; }
677
678 friend constexpr bool
679 operator<(const _Iterator& __x, const _Iterator& __y)
680 requires totally_ordered<_Winc>
681 { return __x._M_value < __y._M_value; }
682
683 friend constexpr bool
684 operator>(const _Iterator& __x, const _Iterator& __y)
685 requires totally_ordered<_Winc>
686 { return __y < __x; }
687
688 friend constexpr bool
689 operator<=(const _Iterator& __x, const _Iterator& __y)
690 requires totally_ordered<_Winc>
691 { return !(__y < __x); }
692
693 friend constexpr bool
694 operator>=(const _Iterator& __x, const _Iterator& __y)
695 requires totally_ordered<_Winc>
696 { return !(__x < __y); }
697
698#ifdef __cpp_lib_three_way_comparison
699 friend constexpr auto
700 operator<=>(const _Iterator& __x, const _Iterator& __y)
701 requires totally_ordered<_Winc> && three_way_comparable<_Winc>
702 { return __x._M_value <=> __y._M_value; }
703#endif
704
705 friend constexpr _Iterator
706 operator+(_Iterator __i, difference_type __n)
707 requires __detail::__advanceable<_Winc>
708 {
709 __i += __n;
710 return __i;
711 }
712
713 friend constexpr _Iterator
714 operator+(difference_type __n, _Iterator __i)
715 requires __detail::__advanceable<_Winc>
716 { return __i += __n; }
717
718 friend constexpr _Iterator
719 operator-(_Iterator __i, difference_type __n)
720 requires __detail::__advanceable<_Winc>
721 {
722 __i -= __n;
723 return __i;
724 }
725
726 friend constexpr difference_type
727 operator-(const _Iterator& __x, const _Iterator& __y)
728 requires __detail::__advanceable<_Winc>
729 {
730 using __detail::__is_integer_like;
731 using __detail::__is_signed_integer_like;
732 using _Dt = difference_type;
733 if constexpr (__is_integer_like<_Winc>)
734 {
735 if constexpr (__is_signed_integer_like<_Winc>)
736 return _Dt(_Dt(__x._M_value) - _Dt(__y._M_value));
737 else
738 return (__y._M_value > __x._M_value)
739 ? _Dt(-_Dt(__y._M_value - __x._M_value))
740 : _Dt(__x._M_value - __y._M_value);
741 }
742 else
743 return __x._M_value - __y._M_value;
744 }
745
746 private:
747 _Winc _M_value = _Winc();
748
749 friend iota_view;
750 friend _Sentinel;
751 };
752
753 struct _Sentinel
754 {
755 private:
756 _Bound _M_bound = _Bound();
757
758 public:
759 _Sentinel() = default;
760
761 constexpr explicit
762 _Sentinel(_Bound __bound)
763 : _M_bound(__bound) { }
764
765 friend constexpr bool
766 operator==(const _Iterator& __x, const _Sentinel& __y)
767 { return __x._M_value == __y._M_bound; }
768
769 friend constexpr iter_difference_t<_Winc>
770 operator-(const _Iterator& __x, const _Sentinel& __y)
771 requires sized_sentinel_for<_Bound, _Winc>
772 { return -(__y._M_bound - __x._M_value); }
773
774 friend constexpr iter_difference_t<_Winc>
775 operator-(const _Sentinel& __x, const _Iterator& __y)
776 requires sized_sentinel_for<_Bound, _Winc>
777 { return __x._M_bound - __y._M_value; }
778
779 friend iota_view;
780 };
781
782 _Winc _M_value = _Winc();
783 [[no_unique_address]] _Bound _M_bound = _Bound();
784
785 public:
786 iota_view() requires default_initializable<_Winc> = default;
787
788 constexpr explicit
789 iota_view(_Winc __value)
790 : _M_value(__value)
791 { }
792
793 constexpr explicit
794 iota_view(type_identity_t<_Winc> __value,
795 type_identity_t<_Bound> __bound)
796 : _M_value(__value), _M_bound(__bound)
797 {
798 if constexpr (totally_ordered_with<_Winc, _Bound>)
799 __glibcxx_assert( bool(__value <= __bound) );
800 }
801
802 constexpr explicit
803 iota_view(_Iterator __first, _Iterator __last)
804 requires same_as<_Winc, _Bound>
805 : iota_view(__first._M_value, __last._M_value)
806 { }
807
808 constexpr explicit
809 iota_view(_Iterator __first, unreachable_sentinel_t __last)
810 requires same_as<_Bound, unreachable_sentinel_t>
811 : iota_view(__first._M_value, __last)
812 { }
813
814 constexpr explicit
815 iota_view(_Iterator __first, _Sentinel __last)
816 requires (!same_as<_Winc, _Bound>) && (!same_as<_Bound, unreachable_sentinel_t>)
817 : iota_view(__first._M_value, __last._M_bound)
818 { }
819
820 constexpr _Iterator
821 begin() const { return _Iterator{_M_value}; }
822
823 constexpr auto
824 end() const
825 {
826 if constexpr (same_as<_Bound, unreachable_sentinel_t>)
827 return unreachable_sentinel;
828 else
829 return _Sentinel{_M_bound};
830 }
831
832 constexpr _Iterator
833 end() const requires same_as<_Winc, _Bound>
834 { return _Iterator{_M_bound}; }
835
836 // _GLIBCXX_RESOLVE_LIB_DEFECTS
837 // 4001. iota_view should provide empty
838 constexpr bool
839 empty() const
840 { return _M_value == _M_bound; }
841
842 constexpr auto
843 size() const
844 requires (same_as<_Winc, _Bound> && __detail::__advanceable<_Winc>)
845 || (integral<_Winc> && integral<_Bound>)
846 || sized_sentinel_for<_Bound, _Winc>
847 {
848 using __detail::__is_integer_like;
849 using __detail::__to_unsigned_like;
850 if constexpr (integral<_Winc> && integral<_Bound>)
851 {
852 using _Up = make_unsigned_t<decltype(_M_bound - _M_value)>;
853 return _Up(_M_bound) - _Up(_M_value);
854 }
855 else if constexpr (__is_integer_like<_Winc>)
856 return __to_unsigned_like(_M_bound) - __to_unsigned_like(_M_value);
857 else
858 return __to_unsigned_like(_M_bound - _M_value);
859 }
860 };
861
862 template<typename _Winc, typename _Bound>
863 requires (!__detail::__is_integer_like<_Winc>
864 || !__detail::__is_integer_like<_Bound>
865 || (__detail::__is_signed_integer_like<_Winc>
866 == __detail::__is_signed_integer_like<_Bound>))
867 iota_view(_Winc, _Bound) -> iota_view<_Winc, _Bound>;
868
869 template<typename _Winc, typename _Bound>
870 inline constexpr bool
871 enable_borrowed_range<iota_view<_Winc, _Bound>> = true;
872
873namespace views
874{
875 template<typename _Tp>
876 inline constexpr empty_view<_Tp> empty{};
877
878 namespace __detail
879 {
880 template<typename _Tp>
881 concept __can_single_view
882 = requires { single_view<decay_t<_Tp>>(std::declval<_Tp>()); };
883 } // namespace __detail
884
885 struct _Single
886 {
887 template<__detail::__can_single_view _Tp>
888 constexpr auto
889 operator() [[nodiscard]] (_Tp&& __e) const
890 noexcept(noexcept(single_view<decay_t<_Tp>>(std::forward<_Tp>(__e))))
891 { return single_view<decay_t<_Tp>>(std::forward<_Tp>(__e)); }
892 };
893
894 inline constexpr _Single single{};
895
896 namespace __detail
897 {
898 template<typename... _Args>
899 concept __can_iota_view = requires { iota_view(std::declval<_Args>()...); };
900 } // namespace __detail
901
902 struct _Iota
903 {
904 // _GLIBCXX_RESOLVE_LIB_DEFECTS
905 // 4096. views::iota(views::iota(0)) should be rejected
906 template<__detail::__can_iota_view _Tp>
907 constexpr iota_view<decay_t<_Tp>>
908 operator() [[nodiscard]] (_Tp&& __e) const
909 { return iota_view<decay_t<_Tp>>(std::forward<_Tp>(__e)); }
910
911 template<typename _Tp, typename _Up>
912 requires __detail::__can_iota_view<_Tp, _Up>
913 constexpr auto
914 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
915 { return iota_view(std::forward<_Tp>(__e), std::forward<_Up>(__f)); }
916 };
917
918 inline constexpr _Iota iota{};
919
920#ifdef __cpp_lib_ranges_indices // C++ >= 26
921 struct _Indices
922 {
923 template<ranges::__detail::__is_integer_like _Tp>
924 requires __detail::__can_iota_view<_Tp>
925 [[nodiscard]] constexpr auto
926 operator() (_Tp __e) const noexcept
927 { return iota(_Tp{}, __e); }
928 };
929
930 inline constexpr _Indices indices{};
931#endif // __cpp_lib_ranges_indices
932} // namespace views
933
934#if _GLIBCXX_HOSTED
935 namespace __detail
936 {
937 template<typename _Val, typename _CharT, typename _Traits>
938 concept __stream_extractable
939 = requires(basic_istream<_CharT, _Traits>& is, _Val& t) { is >> t; };
940 } // namespace __detail
941
942 template<movable _Val, typename _CharT,
943 typename _Traits = char_traits<_CharT>>
944 requires default_initializable<_Val>
945 && __detail::__stream_extractable<_Val, _CharT, _Traits>
946 class basic_istream_view
947 : public view_interface<basic_istream_view<_Val, _CharT, _Traits>>
948 {
949 public:
950 constexpr explicit
951 basic_istream_view(basic_istream<_CharT, _Traits>& __stream)
952 : _M_stream(std::__addressof(__stream))
953 { }
954
955 constexpr auto
956 begin()
957 {
958 *_M_stream >> _M_object;
959 return _Iterator{this};
960 }
961
962 constexpr default_sentinel_t
963 end() const noexcept
964 { return default_sentinel; }
965
966 private:
967 basic_istream<_CharT, _Traits>* _M_stream;
968 _Val _M_object = _Val();
969
970 struct _Iterator
971 {
972 public:
973 using iterator_concept = input_iterator_tag;
974 using difference_type = ptrdiff_t;
975 using value_type = _Val;
976
977 constexpr explicit
978 _Iterator(basic_istream_view* __parent) noexcept
979 : _M_parent(__parent)
980 { }
981
982 _Iterator(const _Iterator&) = delete;
983 _Iterator(_Iterator&&) = default;
984 _Iterator& operator=(const _Iterator&) = delete;
985 _Iterator& operator=(_Iterator&&) = default;
986
987 _Iterator&
988 operator++()
989 {
990 *_M_parent->_M_stream >> _M_parent->_M_object;
991 return *this;
992 }
993
994 void
995 operator++(int)
996 { ++*this; }
997
998 _Val&
999 operator*() const
1000 { return _M_parent->_M_object; }
1001
1002 friend bool
1003 operator==(const _Iterator& __x, default_sentinel_t)
1004 { return !*__x._M_parent->_M_stream; }
1005
1006 private:
1007 basic_istream_view* _M_parent;
1008 };
1009
1010 friend _Iterator;
1011 };
1012
1013 template<typename _Val>
1014 using istream_view = basic_istream_view<_Val, char>;
1015
1016 template<typename _Val>
1017 using wistream_view = basic_istream_view<_Val, wchar_t>;
1018
1019namespace views
1020{
1021 namespace __detail
1022 {
1023 template<typename _Tp, typename _Up>
1024 concept __can_istream_view = requires (_Up __e) {
1025 basic_istream_view<_Tp, typename _Up::char_type, typename _Up::traits_type>(__e);
1026 };
1027 } // namespace __detail
1028
1029 template<typename _Tp>
1030 struct _Istream
1031 {
1032 template<typename _CharT, typename _Traits>
1033 constexpr auto
1034 operator() [[nodiscard]] (basic_istream<_CharT, _Traits>& __e) const
1035 requires __detail::__can_istream_view<_Tp, remove_reference_t<decltype(__e)>>
1036 { return basic_istream_view<_Tp, _CharT, _Traits>(__e); }
1037 };
1038
1039 template<typename _Tp>
1040 inline constexpr _Istream<_Tp> istream;
1041}
1042#endif // HOSTED
1043
1044 // C++20 24.7 [range.adaptors] Range adaptors
1045
1046namespace __detail
1047{
1048 template<typename _Tp, int _Disc>
1049 struct _Absent { };
1050
1051 // Alias for a type that is conditionally present
1052 // (and is an empty type otherwise).
1053 // Data members using this alias should use [[no_unique_address]] so that
1054 // they take no space when not needed.
1055 // The optional template parameter _Disc is for discriminating two otherwise
1056 // equivalent absent types so that even they can overlap.
1057 template<bool _Present, typename _Tp, int _Disc = 0>
1058 using __maybe_present_t = __conditional_t<_Present, _Tp, _Absent<_Tp, _Disc>>;
1059
1060 // Alias for a type that is conditionally const.
1061 template<bool _Const, typename _Tp>
1062 using __maybe_const_t = __conditional_t<_Const, const _Tp, _Tp>;
1063
1064} // namespace __detail
1065
1066// Shorthand for __detail::__maybe_const_t.
1067using __detail::__maybe_const_t;
1068
1069namespace views::__adaptor
1070{
1071 // True if the range adaptor _Adaptor can be applied with _Args.
1072 template<typename _Adaptor, typename... _Args>
1073 concept __adaptor_invocable
1074 = requires { std::declval<_Adaptor>()(declval<_Args>()...); };
1075
1076 // True if the range adaptor non-closure _Adaptor can be partially applied
1077 // with _Args.
1078 template<typename _Adaptor, typename... _Args>
1079 concept __adaptor_partial_app_viable = (_Adaptor::_S_arity > 1)
1080 && (sizeof...(_Args) == _Adaptor::_S_arity - 1)
1081 && (constructible_from<decay_t<_Args>, _Args> && ...);
1082
1083 template<typename _Adaptor, typename... _Args>
1084 struct _Partial;
1085
1086 template<typename _Lhs, typename _Rhs>
1087 struct _Pipe;
1088
1089 // The base class of every range adaptor closure.
1090 //
1091 // The derived class should define the optional static data member
1092 // _S_has_simple_call_op to true if the behavior of this adaptor is
1093 // independent of the constness/value category of the adaptor object.
1094 template<typename _Derived>
1095 struct _RangeAdaptorClosure;
1096
1097 template<typename _Tp, typename _Up>
1098 requires (!same_as<_Tp, _RangeAdaptorClosure<_Up>>)
1099 void __is_range_adaptor_closure_fn
1100 (const _Tp&, const _RangeAdaptorClosure<_Up>&); // not defined
1101
1102 template<typename _Tp>
1103 concept __is_range_adaptor_closure
1104 = requires (_Tp __t) { __adaptor::__is_range_adaptor_closure_fn(__t, __t); };
1105
1106#pragma GCC diagnostic push
1107#pragma GCC diagnostic ignored "-Wdangling-reference"
1108 // range | adaptor is equivalent to adaptor(range).
1109 template<typename _Self, typename _Range>
1110 requires __is_range_adaptor_closure<_Self>
1111 && __adaptor_invocable<_Self, _Range>
1112 constexpr auto
1113 operator|(_Range&& __r, _Self&& __self)
1114 { return std::forward<_Self>(__self)(std::forward<_Range>(__r)); }
1115
1116 // Compose the adaptors __lhs and __rhs into a pipeline, returning
1117 // another range adaptor closure object.
1118 template<typename _Lhs, typename _Rhs>
1119 requires __is_range_adaptor_closure<_Lhs>
1120 && __is_range_adaptor_closure<_Rhs>
1121 constexpr auto
1122 operator|(_Lhs&& __lhs, _Rhs&& __rhs)
1123 {
1124 return _Pipe<decay_t<_Lhs>, decay_t<_Rhs>>{std::forward<_Lhs>(__lhs),
1125 std::forward<_Rhs>(__rhs)};
1126 }
1127#pragma GCC diagnostic pop
1128
1129 template<typename _Derived>
1130 struct _RangeAdaptorClosure
1131 {
1132 // In non-modules compilation ADL finds these operators either way and
1133 // the friend declarations are redundant. But with the std module these
1134 // friend declarations enable ADL to find these operators without having
1135 // to export them.
1136 template<typename _Self, typename _Range>
1137 requires __is_range_adaptor_closure<_Self>
1138 && __adaptor_invocable<_Self, _Range>
1139 friend constexpr auto
1140 operator|(_Range&& __r, _Self&& __self);
1141
1142 template<typename _Lhs, typename _Rhs>
1143 requires __is_range_adaptor_closure<_Lhs>
1144 && __is_range_adaptor_closure<_Rhs>
1145 friend constexpr auto
1146 operator|(_Lhs&& __lhs, _Rhs&& __rhs);
1147 };
1148
1149 // The base class of every range adaptor non-closure.
1150 //
1151 // The static data member _Derived::_S_arity must contain the total number of
1152 // arguments that the adaptor takes, and the class _Derived must introduce
1153 // _RangeAdaptor::operator() into the class scope via a using-declaration.
1154 //
1155 // The optional static data member _Derived::_S_has_simple_extra_args should
1156 // be defined to true if the behavior of this adaptor is independent of the
1157 // constness/value category of the extra arguments. This data member could
1158 // also be defined as a variable template parameterized by the types of the
1159 // extra arguments.
1160 template<typename _Derived>
1161 struct _RangeAdaptor
1162 {
1163 // Partially apply the arguments __args to the range adaptor _Derived,
1164 // returning a range adaptor closure object.
1165 template<typename... _Args>
1166 requires __adaptor_partial_app_viable<_Derived, _Args...>
1167 constexpr auto
1168 operator()(_Args&&... __args) const
1169 {
1170 return _Partial<_Derived, decay_t<_Args>...>{0, std::forward<_Args>(__args)...};
1171 }
1172 };
1173
1174 // True if the range adaptor closure _Adaptor has a simple operator(), i.e.
1175 // one that's not overloaded according to constness or value category of the
1176 // _Adaptor object.
1177 template<typename _Adaptor>
1178 concept __closure_has_simple_call_op = _Adaptor::_S_has_simple_call_op;
1179
1180 // True if the behavior of the range adaptor non-closure _Adaptor is
1181 // independent of the value category of its extra arguments _Args.
1182 template<typename _Adaptor, typename... _Args>
1183 concept __adaptor_has_simple_extra_args = _Adaptor::_S_has_simple_extra_args
1184 || _Adaptor::template _S_has_simple_extra_args<_Args...>;
1185
1186 // A range adaptor closure that represents partial application of
1187 // the range adaptor _Adaptor with arguments _Args.
1188 template<typename _Adaptor, typename... _Args>
1189 struct _Partial : _RangeAdaptorClosure<_Partial<_Adaptor, _Args...>>
1190 {
1191 using _Binder = _Bind_back_t<_Adaptor, _Args...>;
1192 [[no_unique_address]] _Binder _M_binder;
1193
1194 // First parameter is to ensure this constructor is never used
1195 // instead of the copy/move constructor.
1196 template<typename... _Ts>
1197 constexpr
1198 _Partial(int, _Ts&&... __args)
1199 : _M_binder(0, _Adaptor(), std::forward<_Ts>(__args)...)
1200 { }
1201
1202 // Invoke _Adaptor with arguments __r, _M_args... according to the
1203 // value category of this _Partial object.
1204#if _GLIBCXX_EXPLICIT_THIS_PARAMETER
1205# pragma GCC diagnostic push
1206# pragma GCC diagnostic ignored "-Wc++23-extensions" // deducing this
1207 template<typename _Self, typename _Range>
1208 requires __adaptor_invocable<_Adaptor, _Range, __like_t<_Self, _Args>...>
1209 constexpr auto
1210 operator()(this _Self&& __self, _Range&& __r)
1211 {
1212 return _Binder::_S_call(__like_t<_Self, _Partial>(__self)._M_binder,
1213 std::forward<_Range>(__r));
1214 }
1215# pragma GCC diagnostic pop
1216#else
1217 template<typename _Range>
1218 requires __adaptor_invocable<_Adaptor, _Range, const _Args&...>
1219 constexpr auto
1220 operator()(_Range&& __r) const &
1221 { return _Binder::_S_call(_M_binder, std::forward<_Range>(__r)); }
1222
1223 template<typename _Range>
1224 requires __adaptor_invocable<_Adaptor, _Range, _Args...>
1225 constexpr auto
1226 operator()(_Range&& __r) &&
1227 { return _Binder::_S_call(std::move(_M_binder), std::forward<_Range>(__r)); }
1228
1229 template<typename _Range>
1230 constexpr auto
1231 operator()(_Range&& __r) const && = delete;
1232#endif
1233 };
1234
1235 // Partial specialization of the primary template for the case where the extra
1236 // arguments of the adaptor can always be safely and efficiently forwarded by
1237 // const reference. This lets us get away with a single operator() overload,
1238 // which makes overload resolution failure diagnostics more concise.
1239 template<typename _Adaptor, typename... _Args>
1240 requires __adaptor_has_simple_extra_args<_Adaptor, _Args...>
1241 && (is_trivially_copy_constructible_v<_Args> && ...)
1242 struct _Partial<_Adaptor, _Args...> : _RangeAdaptorClosure<_Partial<_Adaptor, _Args...>>
1243 {
1244 using _Binder = _Bind_back_t<_Adaptor, _Args...>;
1245 [[no_unique_address]] _Binder _M_binder;
1246
1247 template<typename... _Ts>
1248 constexpr
1249 _Partial(int, _Ts&&... __args)
1250 : _M_binder(0, _Adaptor(), std::forward<_Ts>(__args)...)
1251 { }
1252
1253 // Invoke _Adaptor with arguments __r, const _M_args&... regardless
1254 // of the value category of this _Partial object.
1255 template<typename _Range>
1256 requires __adaptor_invocable<_Adaptor, _Range, const _Args&...>
1257 constexpr auto
1258 operator()(_Range&& __r) const
1259 { return _Binder::_S_call(_M_binder, std::forward<_Range>(__r)); }
1260
1261 static constexpr bool _S_has_simple_call_op = true;
1262 };
1263
1264 template<typename _Lhs, typename _Rhs, typename _Range>
1265 concept __pipe_invocable
1266 = requires { std::declval<_Rhs>()(std::declval<_Lhs>()(std::declval<_Range>())); };
1267
1268 // A range adaptor closure that represents composition of the range
1269 // adaptor closures _Lhs and _Rhs.
1270 template<typename _Lhs, typename _Rhs>
1271 struct _Pipe : _RangeAdaptorClosure<_Pipe<_Lhs, _Rhs>>
1272 {
1273 [[no_unique_address]] _Lhs _M_lhs;
1274 [[no_unique_address]] _Rhs _M_rhs;
1275
1276 template<typename _Tp, typename _Up>
1277 constexpr
1278 _Pipe(_Tp&& __lhs, _Up&& __rhs)
1279 : _M_lhs(std::forward<_Tp>(__lhs)), _M_rhs(std::forward<_Up>(__rhs))
1280 { }
1281
1282 // Invoke _M_rhs(_M_lhs(__r)) according to the value category of this
1283 // range adaptor closure object.
1284#if _GLIBCXX_EXPLICIT_THIS_PARAMETER
1285# pragma GCC diagnostic push
1286# pragma GCC diagnostic ignored "-Wc++23-extensions" // deducing this
1287 template<typename _Self, typename _Range>
1288 requires __pipe_invocable<__like_t<_Self, _Lhs>, __like_t<_Self, _Rhs>, _Range>
1289 constexpr auto
1290 operator()(this _Self&& __self, _Range&& __r)
1291 {
1292 return (__like_t<_Self, _Pipe>(__self)._M_rhs
1293 (__like_t<_Self, _Pipe>(__self)._M_lhs
1294 (std::forward<_Range>(__r))));
1295 }
1296# pragma GCC diagnostic pop
1297#else
1298 template<typename _Range>
1299 requires __pipe_invocable<const _Lhs&, const _Rhs&, _Range>
1300 constexpr auto
1301 operator()(_Range&& __r) const &
1302 { return _M_rhs(_M_lhs(std::forward<_Range>(__r))); }
1303
1304 template<typename _Range>
1305 requires __pipe_invocable<_Lhs, _Rhs, _Range>
1306 constexpr auto
1307 operator()(_Range&& __r) &&
1308 { return std::move(_M_rhs)(std::move(_M_lhs)(std::forward<_Range>(__r))); }
1309
1310 template<typename _Range>
1311 constexpr auto
1312 operator()(_Range&& __r) const && = delete;
1313#endif
1314 };
1315
1316 // A partial specialization of the above primary template for the case where
1317 // both adaptor operands have a simple operator(). This in turn lets us
1318 // implement composition using a single simple operator(), which makes
1319 // overload resolution failure diagnostics more concise.
1320 template<typename _Lhs, typename _Rhs>
1321 requires __closure_has_simple_call_op<_Lhs>
1322 && __closure_has_simple_call_op<_Rhs>
1323 struct _Pipe<_Lhs, _Rhs> : _RangeAdaptorClosure<_Pipe<_Lhs, _Rhs>>
1324 {
1325 [[no_unique_address]] _Lhs _M_lhs;
1326 [[no_unique_address]] _Rhs _M_rhs;
1327
1328 template<typename _Tp, typename _Up>
1329 constexpr
1330 _Pipe(_Tp&& __lhs, _Up&& __rhs)
1331 : _M_lhs(std::forward<_Tp>(__lhs)), _M_rhs(std::forward<_Up>(__rhs))
1332 { }
1333
1334 template<typename _Range>
1335 requires __pipe_invocable<const _Lhs&, const _Rhs&, _Range>
1336 constexpr auto
1337 operator()(_Range&& __r) const
1338 { return _M_rhs(_M_lhs(std::forward<_Range>(__r))); }
1339
1340 static constexpr bool _S_has_simple_call_op = true;
1341 };
1342} // namespace views::__adaptor
1343
1344#if __cpp_lib_ranges >= 202202L
1345 // P2387R3 Pipe support for user-defined range adaptors
1346 template<typename _Derived>
1347 requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
1348 class range_adaptor_closure
1349 : public views::__adaptor::_RangeAdaptorClosure<_Derived>
1350 { };
1351#endif
1352
1353 template<range _Range> requires is_object_v<_Range>
1354 class ref_view : public view_interface<ref_view<_Range>>
1355 {
1356 private:
1357 _Range* _M_r;
1358
1359 static void _S_fun(_Range&); // not defined
1360 static void _S_fun(_Range&&) = delete;
1361
1362 public:
1363 template<__detail::__different_from<ref_view> _Tp>
1364 requires convertible_to<_Tp, _Range&>
1365 && requires { _S_fun(declval<_Tp>()); }
1366 constexpr
1367 ref_view(_Tp&& __t)
1368 noexcept(noexcept(static_cast<_Range&>(std::declval<_Tp>())))
1369 : _M_r(std::__addressof(static_cast<_Range&>(std::forward<_Tp>(__t))))
1370 { }
1371
1372 constexpr _Range&
1373 base() const
1374 { return *_M_r; }
1375
1376 constexpr iterator_t<_Range>
1377 begin() const
1378 { return ranges::begin(*_M_r); }
1379
1380 constexpr sentinel_t<_Range>
1381 end() const
1382 { return ranges::end(*_M_r); }
1383
1384 constexpr bool
1385 empty() const requires requires { ranges::empty(*_M_r); }
1386 { return ranges::empty(*_M_r); }
1387
1388 constexpr auto
1389 size() const requires sized_range<_Range>
1390 { return ranges::size(*_M_r); }
1391
1392 constexpr auto
1393 data() const requires contiguous_range<_Range>
1394 { return ranges::data(*_M_r); }
1395 };
1396
1397 template<typename _Range>
1398 ref_view(_Range&) -> ref_view<_Range>;
1399
1400 template<typename _Tp>
1401 inline constexpr bool enable_borrowed_range<ref_view<_Tp>> = true;
1402
1403 template<range _Range>
1404 requires movable<_Range>
1405 && (!__detail::__is_initializer_list<remove_cv_t<_Range>>)
1406 class owning_view : public view_interface<owning_view<_Range>>
1407 {
1408 private:
1409 _Range _M_r = _Range();
1410
1411 public:
1412 owning_view() requires default_initializable<_Range> = default;
1413
1414 constexpr
1415 owning_view(_Range&& __t)
1416 noexcept(is_nothrow_move_constructible_v<_Range>)
1417 : _M_r(std::move(__t))
1418 { }
1419
1420 owning_view(owning_view&&) = default;
1421 owning_view& operator=(owning_view&&) = default;
1422
1423 constexpr _Range&
1424 base() & noexcept
1425 { return _M_r; }
1426
1427 constexpr const _Range&
1428 base() const& noexcept
1429 { return _M_r; }
1430
1431 constexpr _Range&&
1432 base() && noexcept
1433 { return std::move(_M_r); }
1434
1435 constexpr const _Range&&
1436 base() const&& noexcept
1437 { return std::move(_M_r); }
1438
1439 constexpr iterator_t<_Range>
1440 begin()
1441 { return ranges::begin(_M_r); }
1442
1443 constexpr sentinel_t<_Range>
1444 end()
1445 { return ranges::end(_M_r); }
1446
1447 constexpr auto
1448 begin() const requires range<const _Range>
1449 { return ranges::begin(_M_r); }
1450
1451 constexpr auto
1452 end() const requires range<const _Range>
1453 { return ranges::end(_M_r); }
1454
1455 constexpr bool
1456 empty() requires requires { ranges::empty(_M_r); }
1457 { return ranges::empty(_M_r); }
1458
1459 constexpr bool
1460 empty() const requires requires { ranges::empty(_M_r); }
1461 { return ranges::empty(_M_r); }
1462
1463 constexpr auto
1464 size() requires sized_range<_Range>
1465 { return ranges::size(_M_r); }
1466
1467 constexpr auto
1468 size() const requires sized_range<const _Range>
1469 { return ranges::size(_M_r); }
1470
1471 constexpr auto
1472 data() requires contiguous_range<_Range>
1473 { return ranges::data(_M_r); }
1474
1475 constexpr auto
1476 data() const requires contiguous_range<const _Range>
1477 { return ranges::data(_M_r); }
1478 };
1479
1480 template<typename _Tp>
1481 inline constexpr bool enable_borrowed_range<owning_view<_Tp>>
1482 = enable_borrowed_range<_Tp>;
1483
1484 namespace views
1485 {
1486 namespace __detail
1487 {
1488 template<typename _Range>
1489 concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
1490
1491 template<typename _Range>
1492 concept __can_owning_view = requires { owning_view{std::declval<_Range>()}; };
1493 } // namespace __detail
1494
1495 struct _All : __adaptor::_RangeAdaptorClosure<_All>
1496 {
1497 template<typename _Range>
1498 static constexpr bool
1499 _S_noexcept()
1500 {
1501 if constexpr (view<decay_t<_Range>>)
1502 return is_nothrow_constructible_v<decay_t<_Range>, _Range>;
1503 else if constexpr (__detail::__can_ref_view<_Range>)
1504 return true;
1505 else
1506 return noexcept(owning_view{std::declval<_Range>()});
1507 }
1508
1509 template<viewable_range _Range>
1510 requires view<decay_t<_Range>>
1511 || __detail::__can_ref_view<_Range>
1512 || __detail::__can_owning_view<_Range>
1513 constexpr auto
1514 operator() [[nodiscard]] (_Range&& __r) const
1515 noexcept(_S_noexcept<_Range>())
1516 {
1517 if constexpr (view<decay_t<_Range>>)
1518 return std::forward<_Range>(__r);
1519 else if constexpr (__detail::__can_ref_view<_Range>)
1520 return ref_view{std::forward<_Range>(__r)};
1521 else
1522 return owning_view{std::forward<_Range>(__r)};
1523 }
1524
1525 static constexpr bool _S_has_simple_call_op = true;
1526 };
1527
1528 inline constexpr _All all;
1529
1530 template<viewable_range _Range>
1531 using all_t = decltype(all(std::declval<_Range>()));
1532 } // namespace views
1533
1534 namespace __detail
1535 {
1536 template<typename _Tp>
1537 struct __non_propagating_cache
1538 {
1539 // When _Tp is not an object type (e.g. is a reference type), we make
1540 // __non_propagating_cache<_Tp> empty rather than ill-formed so that
1541 // users can easily conditionally declare data members with this type
1542 // (such as join_view::_M_inner).
1543 };
1544
1545 template<typename _Tp>
1546 requires is_object_v<_Tp>
1547 struct __non_propagating_cache<_Tp>
1548 : protected _Optional_base<_Tp>
1549 {
1550 __non_propagating_cache() = default;
1551
1552 constexpr
1553 __non_propagating_cache(const __non_propagating_cache&) noexcept
1554 { }
1555
1556 constexpr
1557 __non_propagating_cache(__non_propagating_cache&& __other) noexcept
1558 { __other._M_reset(); }
1559
1560 constexpr __non_propagating_cache&
1561 operator=(const __non_propagating_cache& __other) noexcept
1562 {
1563 if (std::__addressof(__other) != this)
1564 this->_M_reset();
1565 return *this;
1566 }
1567
1568 constexpr __non_propagating_cache&
1569 operator=(__non_propagating_cache&& __other) noexcept
1570 {
1571 this->_M_reset();
1572 __other._M_reset();
1573 return *this;
1574 }
1575
1576 constexpr __non_propagating_cache&
1577 operator=(_Tp __val)
1578 {
1579 this->_M_reset();
1580 this->_M_payload._M_construct(std::move(__val));
1581 return *this;
1582 }
1583
1584 constexpr explicit
1585 operator bool() const noexcept
1586 { return this->_M_is_engaged(); }
1587
1588 constexpr _Tp&
1589 operator*() noexcept
1590 { return this->_M_get(); }
1591
1592 constexpr const _Tp&
1593 operator*() const noexcept
1594 { return this->_M_get(); }
1595
1596 template<typename _Iter>
1597 constexpr _Tp&
1598 _M_emplace_deref(const _Iter& __i)
1599 {
1600 this->_M_reset();
1601 auto __f = [] (auto& __x) { return *__x; };
1602 this->_M_payload._M_apply(_Optional_func{__f}, __i);
1603 return this->_M_get();
1604 }
1605
1606 using _Optional_base<_Tp>::_M_reset;
1607 };
1608
1609 template<range _Range>
1610 struct _CachedPosition
1611 {
1612 constexpr bool
1613 _M_has_value() const
1614 { return false; }
1615
1616 constexpr iterator_t<_Range>
1617 _M_get(const _Range&) const
1618 {
1619 __glibcxx_assert(false);
1620 __builtin_unreachable();
1621 }
1622
1623 constexpr void
1624 _M_set(const _Range&, const iterator_t<_Range>&) const
1625 { }
1626 };
1627
1628 template<forward_range _Range>
1629 struct _CachedPosition<_Range>
1630 : protected __non_propagating_cache<iterator_t<_Range>>
1631 {
1632 constexpr bool
1633 _M_has_value() const
1634 { return this->_M_is_engaged(); }
1635
1636 constexpr iterator_t<_Range>
1637 _M_get(const _Range&) const
1638 {
1639 __glibcxx_assert(_M_has_value());
1640 return **this;
1641 }
1642
1643 constexpr void
1644 _M_set(const _Range&, const iterator_t<_Range>& __it)
1645 {
1646 __glibcxx_assert(!_M_has_value());
1647 std::construct_at(std::__addressof(this->_M_payload._M_payload),
1648 in_place, __it);
1649 this->_M_payload._M_engaged = true;
1650 }
1651 };
1652
1653 template<random_access_range _Range>
1654 struct _CachedPosition<_Range>
1655 {
1656 private:
1657 range_difference_t<_Range> _M_offset = -1;
1658
1659 public:
1660 _CachedPosition() = default;
1661
1662 constexpr
1663 _CachedPosition(const _CachedPosition&) = default;
1664
1665 constexpr
1666 _CachedPosition(_CachedPosition&& __other) noexcept
1667 { *this = std::move(__other); }
1668
1669 constexpr _CachedPosition&
1670 operator=(const _CachedPosition&) = default;
1671
1672 constexpr _CachedPosition&
1673 operator=(_CachedPosition&& __other) noexcept
1674 {
1675 // Propagate the cached offset, but invalidate the source.
1676 _M_offset = __other._M_offset;
1677 __other._M_offset = -1;
1678 return *this;
1679 }
1680
1681 constexpr bool
1682 _M_has_value() const
1683 { return _M_offset >= 0; }
1684
1685 constexpr iterator_t<_Range>
1686 _M_get(_Range& __r) const
1687 {
1688 __glibcxx_assert(_M_has_value());
1689 return ranges::begin(__r) + _M_offset;
1690 }
1691
1692 constexpr void
1693 _M_set(_Range& __r, const iterator_t<_Range>& __it)
1694 {
1695 __glibcxx_assert(!_M_has_value());
1696 _M_offset = __it - ranges::begin(__r);
1697 }
1698 };
1699 } // namespace __detail
1700
1701 namespace __detail
1702 {
1703 template<typename _Base>
1704 struct __filter_view_iter_cat
1705 { };
1706
1707 template<forward_range _Base>
1708 struct __filter_view_iter_cat<_Base>
1709 {
1710 private:
1711 static auto
1712 _S_iter_cat()
1713 {
1714 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
1715 if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
1716 return bidirectional_iterator_tag{};
1717 else if constexpr (derived_from<_Cat, forward_iterator_tag>)
1718 return forward_iterator_tag{};
1719 else
1720 return _Cat{};
1721 }
1722 public:
1723 using iterator_category = decltype(_S_iter_cat());
1724 };
1725 } // namespace __detail
1726
1727 template<input_range _Vp,
1728 indirect_unary_predicate<iterator_t<_Vp>> _Pred>
1729 requires view<_Vp> && is_object_v<_Pred>
1730 class filter_view : public view_interface<filter_view<_Vp, _Pred>>
1731 {
1732 private:
1733 template<bool _Const>
1734 struct _Sentinel;
1735
1736 template<bool _Const>
1737 struct _Iterator : __detail::__filter_view_iter_cat<_Vp>
1738 {
1739 private:
1740 static constexpr auto
1741 _S_iter_concept()
1742 {
1743 if constexpr (_Const)
1744 return input_iterator_tag{};
1745 else if constexpr (bidirectional_range<_Vp>)
1746 return bidirectional_iterator_tag{};
1747 else if constexpr (forward_range<_Vp>)
1748 return forward_iterator_tag{};
1749 else
1750 return input_iterator_tag{};
1751 }
1752
1753 friend filter_view;
1754 friend _Iterator<!_Const>;
1755
1756 using _Parent = __maybe_const_t<_Const, filter_view>;
1757 using _Base = __maybe_const_t<_Const, _Vp>;
1758 using _Base_iter = iterator_t<_Base>;
1759
1760 _Base_iter _M_current = _Base_iter();
1761 _Parent* _M_parent = nullptr;
1762
1763 public:
1764 using iterator_concept = decltype(_S_iter_concept());
1765 // iterator_category defined in __filter_view_iter_cat
1766 using value_type = range_value_t<_Base>;
1767 using difference_type = range_difference_t<_Base>;
1768
1769 _Iterator() requires default_initializable<_Base_iter> = default;
1770
1771 constexpr
1772 _Iterator(_Parent* __parent, _Base_iter __current)
1773 : _M_current(std::move(__current)),
1774 _M_parent(__parent)
1775 { }
1776
1777 constexpr
1778 _Iterator(_Iterator<!_Const> __i)
1779 requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
1780 : _M_current(std::move(__i._M_current)),
1781 _M_parent(std::move(__i._M_parent))
1782 { }
1783
1784 constexpr const _Base_iter&
1785 base() const & noexcept
1786 { return _M_current; }
1787
1788 constexpr _Base_iter
1789 base() &&
1790 { return std::move(_M_current); }
1791
1792 constexpr range_reference_t<_Base>
1793 operator*() const
1794 { return *_M_current; }
1795
1796 constexpr _Base_iter
1797 operator->() const
1798 requires __detail::__has_arrow<_Base_iter>
1799 && copyable<_Base_iter>
1800 { return _M_current; }
1801
1802 constexpr _Iterator&
1803 operator++()
1804 {
1805 _M_current = ranges::find_if(std::move(++_M_current),
1806 ranges::end(_M_parent->_M_base),
1807 std::ref(*_M_parent->_M_pred));
1808 return *this;
1809 }
1810
1811 constexpr void
1812 operator++(int)
1813 { ++*this; }
1814
1815 constexpr _Iterator
1816 operator++(int) requires forward_range<_Base>
1817 {
1818 auto __tmp = *this;
1819 ++*this;
1820 return __tmp;
1821 }
1822
1823 constexpr _Iterator&
1824 operator--() requires bidirectional_range<_Base>
1825 {
1826 do
1827 --_M_current;
1828 while (!std::__invoke(*_M_parent->_M_pred, *_M_current));
1829 return *this;
1830 }
1831
1832 constexpr _Iterator
1833 operator--(int) requires bidirectional_range<_Base>
1834 {
1835 auto __tmp = *this;
1836 --*this;
1837 return __tmp;
1838 }
1839
1840 friend constexpr bool
1841 operator==(const _Iterator& __x, const _Iterator& __y)
1842 requires equality_comparable<_Base_iter>
1843 { return __x._M_current == __y._M_current; }
1844
1845 friend constexpr range_rvalue_reference_t<_Base>
1846 iter_move(const _Iterator& __i)
1847 noexcept(noexcept(ranges::iter_move(__i._M_current)))
1848 { return ranges::iter_move(__i._M_current); }
1849
1850 friend constexpr void
1851 iter_swap(const _Iterator& __x, const _Iterator& __y)
1852 noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
1853 requires indirectly_swappable<_Base_iter>
1854 { ranges::iter_swap(__x._M_current, __y._M_current); }
1855 };
1856
1857 template<bool _Const>
1858 struct _Sentinel
1859 {
1860 private:
1861 using _Parent = __maybe_const_t<_Const, filter_view>;
1862 using _Base = __maybe_const_t<_Const, _Vp>;
1863 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
1864
1865 friend _Sentinel<!_Const>;
1866
1867 public:
1868 _Sentinel() = default;
1869
1870 constexpr explicit
1871 _Sentinel(_Parent* __parent)
1872 : _M_end(ranges::end(__parent->_M_base))
1873 { }
1874
1875 constexpr
1876 _Sentinel(_Sentinel<!_Const> __i)
1877 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
1878 : _M_end(std::move(__i._M_end))
1879 { }
1880
1881 constexpr sentinel_t<_Base>
1882 base() const
1883 { return _M_end; }
1884
1885 template<bool _Const2>
1886 requires sentinel_for<sentinel_t<_Base>,
1887 iterator_t<__maybe_const_t<_Const2, _Vp>>>
1888 friend constexpr bool
1889 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
1890 { return __x._M_current == __y._M_end; }
1891 };
1892
1893 _Vp _M_base = _Vp();
1894 [[no_unique_address]] __detail::__box<_Pred> _M_pred;
1895 [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
1896
1897 public:
1898 filter_view() requires (default_initializable<_Vp>
1899 && default_initializable<_Pred>)
1900 = default;
1901
1902 constexpr explicit
1903 filter_view(_Vp __base, _Pred __pred)
1904 : _M_base(std::move(__base)), _M_pred(std::move(__pred))
1905 { }
1906
1907 constexpr _Vp
1908 base() const& requires copy_constructible<_Vp>
1909 { return _M_base; }
1910
1911 constexpr _Vp
1912 base() &&
1913 { return std::move(_M_base); }
1914
1915 constexpr const _Pred&
1916 pred() const
1917 { return *_M_pred; }
1918
1919 constexpr _Iterator<false>
1920 begin()
1921 {
1922 if (_M_cached_begin._M_has_value())
1923 return {this, _M_cached_begin._M_get(_M_base)};
1924
1925 __glibcxx_assert(_M_pred.has_value());
1926 auto __it = ranges::find_if(ranges::begin(_M_base),
1927 ranges::end(_M_base),
1928 std::ref(*_M_pred));
1929 _M_cached_begin._M_set(_M_base, __it);
1930 return {this, std::move(__it)};
1931 }
1932
1933 constexpr _Iterator<true>
1934 begin() const
1935 requires (input_range<const _Vp> && !forward_range<const _Vp>
1936 && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>)
1937 {
1938 __glibcxx_assert(_M_pred.has_value());
1939 auto __it = ranges::find_if(ranges::begin(_M_base),
1940 ranges::end(_M_base),
1941 std::ref(*_M_pred));
1942 return {this, std::move(__it)};
1943 }
1944
1945 constexpr auto
1946 end()
1947 {
1948 if constexpr (common_range<_Vp>)
1949 return _Iterator<false>{this, ranges::end(_M_base)};
1950 else
1951 return _Sentinel<false>{this};
1952 }
1953
1954 constexpr _Sentinel<true>
1955 end() const
1956 requires (input_range<const _Vp> && !forward_range<const _Vp>
1957 && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>)
1958 { return _Sentinel<true>{this}; }
1959 };
1960
1961 template<typename _Range, typename _Pred>
1962 filter_view(_Range&&, _Pred) -> filter_view<views::all_t<_Range>, _Pred>;
1963
1964 namespace views
1965 {
1966 namespace __detail
1967 {
1968 template<typename _Range, typename _Pred>
1969 concept __can_filter_view
1970 = requires { filter_view(std::declval<_Range>(), std::declval<_Pred>()); };
1971 } // namespace __detail
1972
1973 struct _Filter : __adaptor::_RangeAdaptor<_Filter>
1974 {
1975 template<viewable_range _Range, typename _Pred>
1976 requires __detail::__can_filter_view<_Range, _Pred>
1977 constexpr auto
1978 operator() [[nodiscard]] (_Range&& __r, _Pred&& __p) const
1979 {
1980 return filter_view(std::forward<_Range>(__r), std::forward<_Pred>(__p));
1981 }
1982
1983 using _RangeAdaptor<_Filter>::operator();
1984 static constexpr int _S_arity = 2;
1985 static constexpr bool _S_has_simple_extra_args = true;
1986 };
1987
1988 inline constexpr _Filter filter;
1989 } // namespace views
1990
1991#if __cpp_lib_ranges >= 202207L // C++ >= 23
1992 template<input_range _Vp, move_constructible _Fp>
1993#else
1994 template<input_range _Vp, copy_constructible _Fp>
1995#endif
1996 requires view<_Vp> && is_object_v<_Fp>
1997 && regular_invocable<_Fp&, range_reference_t<_Vp>>
1998 && std::__detail::__can_reference<invoke_result_t<_Fp&,
1999 range_reference_t<_Vp>>>
2000 class transform_view : public view_interface<transform_view<_Vp, _Fp>>
2001 {
2002 private:
2003 template<bool _Const>
2004 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2005
2006 template<bool _Const>
2007 struct __iter_cat
2008 { };
2009
2010 template<bool _Const>
2011 requires forward_range<_Base<_Const>>
2012 struct __iter_cat<_Const>
2013 {
2014 private:
2015 static auto
2016 _S_iter_cat()
2017 {
2018 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2019 // 3564. transform_view::iterator<true>::value_type and
2020 // iterator_category should use const F&
2021 using _Base = transform_view::_Base<_Const>;
2022 using _Res = invoke_result_t<__maybe_const_t<_Const, _Fp>&,
2023 range_reference_t<_Base>>;
2024 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2025 // 3798. Rvalue reference and iterator_category
2026 if constexpr (is_reference_v<_Res>)
2027 {
2028 using _Cat
2029 = typename iterator_traits<iterator_t<_Base>>::iterator_category;
2030 if constexpr (derived_from<_Cat, contiguous_iterator_tag>)
2031 return random_access_iterator_tag{};
2032 else
2033 return _Cat{};
2034 }
2035 else
2036 return input_iterator_tag{};
2037 }
2038 public:
2039 using iterator_category = decltype(_S_iter_cat());
2040 };
2041
2042 template<bool _Const>
2043 struct _Sentinel;
2044
2045 template<bool _Const>
2046 struct _Iterator : __iter_cat<_Const>
2047 {
2048 private:
2049 using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
2050 using _Base = transform_view::_Base<_Const>;
2051 using _Base_iter = iterator_t<_Base>;
2052 using _Func_handle = __detail::__func_handle_t<
2053 __detail::__maybe_const_t<_Const, _Fp>,
2054 _Base_iter>;
2055
2056 static auto
2057 _S_iter_concept()
2058 {
2059 if constexpr (random_access_range<_Base>)
2060 return random_access_iterator_tag{};
2061 else if constexpr (bidirectional_range<_Base>)
2062 return bidirectional_iterator_tag{};
2063 else if constexpr (forward_range<_Base>)
2064 return forward_iterator_tag{};
2065 else
2066 return input_iterator_tag{};
2067 }
2068
2069 _Base_iter _M_current = _Base_iter();
2070 [[no_unique_address]] _Func_handle _M_fun;
2071
2072 public:
2073 using iterator_concept = decltype(_S_iter_concept());
2074 // iterator_category defined in __transform_view_iter_cat
2075 using value_type
2076 = remove_cvref_t<invoke_result_t<__maybe_const_t<_Const, _Fp>&,
2077 range_reference_t<_Base>>>;
2078 using difference_type = range_difference_t<_Base>;
2079
2080 _Iterator() requires default_initializable<_Base_iter> = default;
2081
2082 constexpr
2083 _Iterator(_Func_handle __fun, _Base_iter __current)
2084 : _M_current(std::move(__current)), _M_fun(__fun)
2085 { }
2086
2087 constexpr
2088 _Iterator(_Parent* __parent, _Base_iter __current)
2089 : _M_current(std::move(__current)), _M_fun(*__parent->_M_fun)
2090 {}
2091
2092 constexpr
2093 _Iterator(_Iterator<!_Const> __i)
2094 requires _Const
2095 && convertible_to<iterator_t<_Vp>, _Base_iter>
2096 : _M_current(std::move(__i._M_current)), _M_fun(__i._M_fun)
2097 { }
2098
2099 constexpr const _Base_iter&
2100 base() const & noexcept
2101 { return _M_current; }
2102
2103 constexpr _Base_iter
2104 base() &&
2105 { return std::move(_M_current); }
2106
2107 constexpr decltype(auto)
2108 operator*() const
2109 noexcept(noexcept(_M_fun._M_call_deref(_M_current)))
2110 { return _M_fun._M_call_deref(_M_current); }
2111
2112 constexpr _Iterator&
2113 operator++()
2114 {
2115 ++_M_current;
2116 return *this;
2117 }
2118
2119 constexpr void
2120 operator++(int)
2121 { ++_M_current; }
2122
2123 constexpr _Iterator
2124 operator++(int) requires forward_range<_Base>
2125 {
2126 auto __tmp = *this;
2127 ++*this;
2128 return __tmp;
2129 }
2130
2131 constexpr _Iterator&
2132 operator--() requires bidirectional_range<_Base>
2133 {
2134 --_M_current;
2135 return *this;
2136 }
2137
2138 constexpr _Iterator
2139 operator--(int) requires bidirectional_range<_Base>
2140 {
2141 auto __tmp = *this;
2142 --*this;
2143 return __tmp;
2144 }
2145
2146 constexpr _Iterator&
2147 operator+=(difference_type __n) requires random_access_range<_Base>
2148 {
2149 _M_current += __n;
2150 return *this;
2151 }
2152
2153 constexpr _Iterator&
2154 operator-=(difference_type __n) requires random_access_range<_Base>
2155 {
2156 _M_current -= __n;
2157 return *this;
2158 }
2159
2160 constexpr decltype(auto)
2161 operator[](difference_type __n) const
2162 requires random_access_range<_Base>
2163 { return _M_fun._M_call_subscript(__n, _M_current); }
2164
2165 friend constexpr bool
2166 operator==(const _Iterator& __x, const _Iterator& __y)
2167 requires equality_comparable<_Base_iter>
2168 { return __x._M_current == __y._M_current; }
2169
2170 friend constexpr bool
2171 operator<(const _Iterator& __x, const _Iterator& __y)
2172 requires random_access_range<_Base>
2173 { return __x._M_current < __y._M_current; }
2174
2175 friend constexpr bool
2176 operator>(const _Iterator& __x, const _Iterator& __y)
2177 requires random_access_range<_Base>
2178 { return __y < __x; }
2179
2180 friend constexpr bool
2181 operator<=(const _Iterator& __x, const _Iterator& __y)
2182 requires random_access_range<_Base>
2183 { return !(__y < __x); }
2184
2185 friend constexpr bool
2186 operator>=(const _Iterator& __x, const _Iterator& __y)
2187 requires random_access_range<_Base>
2188 { return !(__x < __y); }
2189
2190#ifdef __cpp_lib_three_way_comparison
2191 friend constexpr auto
2192 operator<=>(const _Iterator& __x, const _Iterator& __y)
2193 requires random_access_range<_Base>
2194 && three_way_comparable<_Base_iter>
2195 { return __x._M_current <=> __y._M_current; }
2196#endif
2197
2198 friend constexpr _Iterator
2199 operator+(_Iterator __i, difference_type __n)
2200 requires random_access_range<_Base>
2201 { return {__i._M_fun, __i._M_current + __n}; }
2202
2203 friend constexpr _Iterator
2204 operator+(difference_type __n, _Iterator __i)
2205 requires random_access_range<_Base>
2206 { return {__i._M_fun, __i._M_current + __n}; }
2207
2208 friend constexpr _Iterator
2209 operator-(_Iterator __i, difference_type __n)
2210 requires random_access_range<_Base>
2211 { return {__i._M_fun, __i._M_current - __n}; }
2212
2213 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2214 // 3483. transform_view::iterator's difference is overconstrained
2215 friend constexpr difference_type
2216 operator-(const _Iterator& __x, const _Iterator& __y)
2217 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
2218 { return __x._M_current - __y._M_current; }
2219
2220 friend constexpr decltype(auto)
2221 iter_move(const _Iterator& __i) noexcept(noexcept(*__i))
2222 {
2223 if constexpr (is_lvalue_reference_v<decltype(*__i)>)
2224 return std::move(*__i);
2225 else
2226 return *__i;
2227 }
2228
2229 friend _Iterator<!_Const>;
2230 template<bool> friend struct _Sentinel;
2231 };
2232
2233 template<bool _Const>
2234 struct _Sentinel
2235 {
2236 private:
2237 using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
2238 using _Base = transform_view::_Base<_Const>;
2239
2240 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
2241
2242 public:
2243 _Sentinel() = default;
2244
2245 constexpr explicit
2246 _Sentinel(sentinel_t<_Base> __end)
2247 : _M_end(__end)
2248 { }
2249
2250 constexpr
2251 _Sentinel(_Sentinel<!_Const> __i)
2252 requires _Const
2253 && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
2254 : _M_end(std::move(__i._M_end))
2255 { }
2256
2257 constexpr sentinel_t<_Base>
2258 base() const
2259 { return _M_end; }
2260
2261 template<bool _Const2>
2262 requires sentinel_for<sentinel_t<_Base>,
2263 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
2264 friend constexpr bool
2265 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
2266 { return __x._M_current == __y._M_end; }
2267
2268 template<bool _Const2,
2269 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
2270 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
2271 friend constexpr range_difference_t<_Base2>
2272 operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
2273 { return -(__y._M_end - __x._M_current); }
2274
2275 template<bool _Const2,
2276 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
2277 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
2278 friend constexpr range_difference_t<_Base2>
2279 operator-(const _Sentinel& __y, const _Iterator<_Const2>& __x)
2280 { return __y._M_end - __x._M_current; }
2281
2282 friend _Sentinel<!_Const>;
2283 };
2284
2285 _Vp _M_base = _Vp();
2286 [[no_unique_address]] __detail::__box<_Fp> _M_fun;
2287
2288 public:
2289 transform_view() requires (default_initializable<_Vp>
2290 && default_initializable<_Fp>)
2291 = default;
2292
2293 constexpr explicit
2294 transform_view(_Vp __base, _Fp __fun)
2295 : _M_base(std::move(__base)), _M_fun(std::move(__fun))
2296 { }
2297
2298 constexpr _Vp
2299 base() const& requires copy_constructible<_Vp>
2300 { return _M_base ; }
2301
2302 constexpr _Vp
2303 base() &&
2304 { return std::move(_M_base); }
2305
2306 constexpr _Iterator<false>
2307 begin()
2308 { return _Iterator<false>{this, ranges::begin(_M_base)}; }
2309
2310 constexpr _Iterator<true>
2311 begin() const
2312 requires range<const _Vp>
2313 && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
2314 { return _Iterator<true>{this, ranges::begin(_M_base)}; }
2315
2316 constexpr _Sentinel<false>
2317 end()
2318 { return _Sentinel<false>{ranges::end(_M_base)}; }
2319
2320 constexpr _Iterator<false>
2321 end() requires common_range<_Vp>
2322 { return _Iterator<false>{this, ranges::end(_M_base)}; }
2323
2324 constexpr _Sentinel<true>
2325 end() const
2326 requires range<const _Vp>
2327 && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
2328 { return _Sentinel<true>{ranges::end(_M_base)}; }
2329
2330 constexpr _Iterator<true>
2331 end() const
2332 requires common_range<const _Vp>
2333 && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
2334 { return _Iterator<true>{this, ranges::end(_M_base)}; }
2335
2336 constexpr auto
2337 size() requires sized_range<_Vp>
2338 { return ranges::size(_M_base); }
2339
2340 constexpr auto
2341 size() const requires sized_range<const _Vp>
2342 { return ranges::size(_M_base); }
2343 };
2344
2345 template<typename _Range, typename _Fp>
2346 transform_view(_Range&&, _Fp) -> transform_view<views::all_t<_Range>, _Fp>;
2347
2348 namespace views
2349 {
2350 namespace __detail
2351 {
2352 template<typename _Range, typename _Fp>
2353 concept __can_transform_view
2354 = requires { transform_view(std::declval<_Range>(), std::declval<_Fp>()); };
2355 } // namespace __detail
2356
2357 struct _Transform : __adaptor::_RangeAdaptor<_Transform>
2358 {
2359 template<viewable_range _Range, typename _Fp>
2360 requires __detail::__can_transform_view<_Range, _Fp>
2361 constexpr auto
2362 operator() [[nodiscard]] (_Range&& __r, _Fp&& __f) const
2363 {
2364 return transform_view(std::forward<_Range>(__r), std::forward<_Fp>(__f));
2365 }
2366
2367 using _RangeAdaptor<_Transform>::operator();
2368 static constexpr int _S_arity = 2;
2369 static constexpr bool _S_has_simple_extra_args = true;
2370 };
2371
2372 inline constexpr _Transform transform;
2373 } // namespace views
2374
2375 template<view _Vp>
2376 class take_view : public view_interface<take_view<_Vp>>
2377 {
2378 private:
2379 template<bool _Const>
2380 using _CI = counted_iterator<
2381 iterator_t<__detail::__maybe_const_t<_Const, _Vp>>>;
2382
2383 template<bool _Const>
2384 struct _Sentinel
2385 {
2386 private:
2387 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2388 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
2389
2390 public:
2391 _Sentinel() = default;
2392
2393 constexpr explicit
2394 _Sentinel(sentinel_t<_Base> __end)
2395 : _M_end(__end)
2396 { }
2397
2398 constexpr
2399 _Sentinel(_Sentinel<!_Const> __s)
2400 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
2401 : _M_end(std::move(__s._M_end))
2402 { }
2403
2404 constexpr sentinel_t<_Base>
2405 base() const
2406 { return _M_end; }
2407
2408 friend constexpr bool
2409 operator==(const _CI<_Const>& __y, const _Sentinel& __x)
2410 { return __y.count() == 0 || __y.base() == __x._M_end; }
2411
2412 template<bool _OtherConst = !_Const,
2413 typename _Base2 = __detail::__maybe_const_t<_OtherConst, _Vp>>
2414 requires sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
2415 friend constexpr bool
2416 operator==(const _CI<_OtherConst>& __y, const _Sentinel& __x)
2417 { return __y.count() == 0 || __y.base() == __x._M_end; }
2418
2419 friend _Sentinel<!_Const>;
2420 };
2421
2422 _Vp _M_base = _Vp();
2423 range_difference_t<_Vp> _M_count = 0;
2424
2425 public:
2426 take_view() requires default_initializable<_Vp> = default;
2427
2428 constexpr explicit
2429 take_view(_Vp __base, range_difference_t<_Vp> __count)
2430 : _M_base(std::move(__base)), _M_count(std::move(__count))
2431 { }
2432
2433 constexpr _Vp
2434 base() const& requires copy_constructible<_Vp>
2435 { return _M_base; }
2436
2437 constexpr _Vp
2438 base() &&
2439 { return std::move(_M_base); }
2440
2441 constexpr auto
2442 begin() requires (!__detail::__simple_view<_Vp>)
2443 {
2444 if constexpr (sized_range<_Vp>)
2445 {
2446 if constexpr (random_access_range<_Vp>)
2447 return ranges::begin(_M_base);
2448 else
2449 {
2450 auto __sz = size();
2451 return counted_iterator(ranges::begin(_M_base), __sz);
2452 }
2453 }
2454 else
2455 return counted_iterator(ranges::begin(_M_base), _M_count);
2456 }
2457
2458 constexpr auto
2459 begin() const requires range<const _Vp>
2460 {
2461 if constexpr (sized_range<const _Vp>)
2462 {
2463 if constexpr (random_access_range<const _Vp>)
2464 return ranges::begin(_M_base);
2465 else
2466 {
2467 auto __sz = size();
2468 return counted_iterator(ranges::begin(_M_base), __sz);
2469 }
2470 }
2471 else
2472 return counted_iterator(ranges::begin(_M_base), _M_count);
2473 }
2474
2475 constexpr auto
2476 end() requires (!__detail::__simple_view<_Vp>)
2477 {
2478 if constexpr (sized_range<_Vp>)
2479 {
2480 if constexpr (random_access_range<_Vp>)
2481 return ranges::begin(_M_base) + size();
2482 else
2483 return default_sentinel;
2484 }
2485 else
2486 return _Sentinel<false>{ranges::end(_M_base)};
2487 }
2488
2489 constexpr auto
2490 end() const requires range<const _Vp>
2491 {
2492 if constexpr (sized_range<const _Vp>)
2493 {
2494 if constexpr (random_access_range<const _Vp>)
2495 return ranges::begin(_M_base) + size();
2496 else
2497 return default_sentinel;
2498 }
2499 else
2500 return _Sentinel<true>{ranges::end(_M_base)};
2501 }
2502
2503 constexpr auto
2504 size() requires sized_range<_Vp>
2505 {
2506 auto __n = ranges::size(_M_base);
2507 return std::min(__n, static_cast<decltype(__n)>(_M_count));
2508 }
2509
2510 constexpr auto
2511 size() const requires sized_range<const _Vp>
2512 {
2513 auto __n = ranges::size(_M_base);
2514 return std::min(__n, static_cast<decltype(__n)>(_M_count));
2515 }
2516 };
2517
2518 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2519 // 3447. Deduction guides for take_view and drop_view have different
2520 // constraints
2521 template<typename _Range>
2522 take_view(_Range&&, range_difference_t<_Range>)
2523 -> take_view<views::all_t<_Range>>;
2524
2525 template<typename _Tp>
2526 inline constexpr bool enable_borrowed_range<take_view<_Tp>>
2527 = enable_borrowed_range<_Tp>;
2528
2529 namespace views
2530 {
2531 namespace __detail
2532 {
2533 template<typename _Range>
2534 inline constexpr bool __is_empty_view = false;
2535
2536 template<typename _Tp>
2537 inline constexpr bool __is_empty_view<empty_view<_Tp>> = true;
2538
2539 template<typename _Range>
2540 inline constexpr bool __is_basic_string_view = false;
2541
2542 template<typename _CharT, typename _Traits>
2543 inline constexpr bool __is_basic_string_view<basic_string_view<_CharT, _Traits>>
2544 = true;
2545
2546 using ranges::__detail::__is_subrange;
2547
2548 template<typename _Range>
2549 inline constexpr bool __is_iota_view = false;
2550
2551 template<typename _Winc, typename _Bound>
2552 inline constexpr bool __is_iota_view<iota_view<_Winc, _Bound>> = true;
2553
2554 template<typename _Range>
2555 inline constexpr bool __is_repeat_view = false;
2556
2557 template<typename _Range>
2558 constexpr auto
2559 __take_of_repeat_view(_Range&&, range_difference_t<_Range>); // defined later
2560
2561 template<typename _Range, typename _Dp>
2562 concept __can_take_view
2563 = requires { take_view(std::declval<_Range>(), std::declval<_Dp>()); };
2564 } // namespace __detail
2565
2566 struct _Take : __adaptor::_RangeAdaptor<_Take>
2567 {
2568 template<viewable_range _Range, typename _Dp = range_difference_t<_Range>>
2569 requires __detail::__can_take_view<_Range, _Dp>
2570 constexpr auto
2571 operator() [[nodiscard]] (_Range&& __r, type_identity_t<_Dp> __n) const
2572 {
2573 using _Tp = remove_cvref_t<_Range>;
2574 if constexpr (__detail::__is_empty_view<_Tp>)
2575 return _Tp();
2576#ifdef __cpp_lib_optional_range_support // >= C++26
2577 else if constexpr (__is_optional_v<_Tp> && view<_Tp>)
2578 return __n ? std::forward<_Range>(__r) : _Tp();
2579#endif
2580 else if constexpr (random_access_range<_Tp>
2581 && sized_range<_Tp>
2582 && (std::__detail::__is_span<_Tp>
2583 || __detail::__is_basic_string_view<_Tp>
2584 || __detail::__is_subrange<_Tp>
2585 || __detail::__is_iota_view<_Tp>))
2586 {
2587 __n = std::min<_Dp>(ranges::distance(__r), __n);
2588 auto __begin = ranges::begin(__r);
2589 auto __end = __begin + __n;
2590 if constexpr (std::__detail::__is_span<_Tp>)
2591 return span<typename _Tp::element_type>(__begin, __end);
2592 else if constexpr (__detail::__is_basic_string_view<_Tp>)
2593 return _Tp(__begin, __end);
2594 else if constexpr (__detail::__is_subrange<_Tp>)
2595 return subrange<iterator_t<_Tp>>(__begin, __end);
2596 else
2597 return iota_view(*__begin, *__end);
2598 }
2599 else if constexpr (__detail::__is_repeat_view<_Tp>)
2600 return __detail::__take_of_repeat_view(std::forward<_Range>(__r), __n);
2601 else
2602 return take_view(std::forward<_Range>(__r), __n);
2603 }
2604
2605 using _RangeAdaptor<_Take>::operator();
2606 static constexpr int _S_arity = 2;
2607 // The count argument of views::take is not always simple -- it can be
2608 // e.g. a move-only class that's implicitly convertible to the difference
2609 // type. But an integer-like count argument is surely simple.
2610 template<typename _Tp>
2611 static constexpr bool _S_has_simple_extra_args
2612 = ranges::__detail::__is_integer_like<_Tp>;
2613 };
2614
2615 inline constexpr _Take take;
2616 } // namespace views
2617
2618 template<view _Vp, typename _Pred>
2619 requires input_range<_Vp> && is_object_v<_Pred>
2620 && indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
2621 class take_while_view : public view_interface<take_while_view<_Vp, _Pred>>
2622 {
2623 template<bool _Const>
2624 struct _Sentinel
2625 {
2626 private:
2627 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2628
2629 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
2630 const _Pred* _M_pred = nullptr;
2631
2632 public:
2633 _Sentinel() = default;
2634
2635 constexpr explicit
2636 _Sentinel(sentinel_t<_Base> __end, const _Pred* __pred)
2637 : _M_end(__end), _M_pred(__pred)
2638 { }
2639
2640 constexpr
2641 _Sentinel(_Sentinel<!_Const> __s)
2642 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
2643 : _M_end(__s._M_end), _M_pred(__s._M_pred)
2644 { }
2645
2646 constexpr sentinel_t<_Base>
2647 base() const { return _M_end; }
2648
2649 friend constexpr bool
2650 operator==(const iterator_t<_Base>& __x, const _Sentinel& __y)
2651 { return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); }
2652
2653 template<bool _OtherConst = !_Const,
2654 typename _Base2 = __detail::__maybe_const_t<_OtherConst, _Vp>>
2655 requires sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
2656 friend constexpr bool
2657 operator==(const iterator_t<_Base2>& __x, const _Sentinel& __y)
2658 { return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); }
2659
2660 friend _Sentinel<!_Const>;
2661 };
2662
2663 _Vp _M_base = _Vp();
2664 [[no_unique_address]] __detail::__box<_Pred> _M_pred;
2665
2666 public:
2667 take_while_view() requires (default_initializable<_Vp>
2668 && default_initializable<_Pred>)
2669 = default;
2670
2671 constexpr explicit
2672 take_while_view(_Vp __base, _Pred __pred)
2673 : _M_base(std::move(__base)), _M_pred(std::move(__pred))
2674 { }
2675
2676 constexpr _Vp
2677 base() const& requires copy_constructible<_Vp>
2678 { return _M_base; }
2679
2680 constexpr _Vp
2681 base() &&
2682 { return std::move(_M_base); }
2683
2684 constexpr const _Pred&
2685 pred() const
2686 { return *_M_pred; }
2687
2688 constexpr auto
2689 begin() requires (!__detail::__simple_view<_Vp>)
2690 { return ranges::begin(_M_base); }
2691
2692 constexpr auto
2693 begin() const requires range<const _Vp>
2694 && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>
2695 { return ranges::begin(_M_base); }
2696
2697 constexpr auto
2698 end() requires (!__detail::__simple_view<_Vp>)
2699 { return _Sentinel<false>(ranges::end(_M_base),
2700 std::__addressof(*_M_pred)); }
2701
2702 constexpr auto
2703 end() const requires range<const _Vp>
2704 && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>
2705 { return _Sentinel<true>(ranges::end(_M_base),
2706 std::__addressof(*_M_pred)); }
2707 };
2708
2709 template<typename _Range, typename _Pred>
2710 take_while_view(_Range&&, _Pred)
2711 -> take_while_view<views::all_t<_Range>, _Pred>;
2712
2713 namespace views
2714 {
2715 namespace __detail
2716 {
2717 template<typename _Range, typename _Pred>
2718 concept __can_take_while_view
2719 = requires { take_while_view(std::declval<_Range>(), std::declval<_Pred>()); };
2720 } // namespace __detail
2721
2722 struct _TakeWhile : __adaptor::_RangeAdaptor<_TakeWhile>
2723 {
2724 template<viewable_range _Range, typename _Pred>
2725 requires __detail::__can_take_while_view<_Range, _Pred>
2726 constexpr auto
2727 operator() [[nodiscard]] (_Range&& __r, _Pred&& __p) const
2728 {
2729 return take_while_view(std::forward<_Range>(__r), std::forward<_Pred>(__p));
2730 }
2731
2732 using _RangeAdaptor<_TakeWhile>::operator();
2733 static constexpr int _S_arity = 2;
2734 static constexpr bool _S_has_simple_extra_args = true;
2735 };
2736
2737 inline constexpr _TakeWhile take_while;
2738 } // namespace views
2739
2740 template<view _Vp>
2741 class drop_view : public view_interface<drop_view<_Vp>>
2742 {
2743 private:
2744 _Vp _M_base = _Vp();
2745 range_difference_t<_Vp> _M_count = 0;
2746
2747 // ranges::next(begin(base), count, end(base)) is O(1) if _Vp satisfies
2748 // both random_access_range and sized_range. Otherwise, cache its result.
2749 static constexpr bool _S_needs_cached_begin
2750 = !(random_access_range<const _Vp> && sized_range<const _Vp>);
2751 [[no_unique_address]]
2752 __detail::__maybe_present_t<_S_needs_cached_begin,
2753 __detail::_CachedPosition<_Vp>>
2754 _M_cached_begin;
2755
2756 public:
2757 drop_view() requires default_initializable<_Vp> = default;
2758
2759 constexpr explicit
2760 drop_view(_Vp __base, range_difference_t<_Vp> __count)
2761 : _M_base(std::move(__base)), _M_count(__count)
2762 { __glibcxx_assert(__count >= 0); }
2763
2764 constexpr _Vp
2765 base() const& requires copy_constructible<_Vp>
2766 { return _M_base; }
2767
2768 constexpr _Vp
2769 base() &&
2770 { return std::move(_M_base); }
2771
2772 // This overload is disabled for simple views with constant-time begin().
2773 constexpr auto
2774 begin()
2775 requires (!(__detail::__simple_view<_Vp>
2776 && random_access_range<const _Vp>
2777 && sized_range<const _Vp>))
2778 {
2779 if constexpr (_S_needs_cached_begin)
2780 if (_M_cached_begin._M_has_value())
2781 return _M_cached_begin._M_get(_M_base);
2782
2783 auto __it = ranges::next(ranges::begin(_M_base),
2784 _M_count, ranges::end(_M_base));
2785 if constexpr (_S_needs_cached_begin)
2786 _M_cached_begin._M_set(_M_base, __it);
2787 return __it;
2788 }
2789
2790 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2791 // 3482. drop_view's const begin should additionally require sized_range
2792 constexpr auto
2793 begin() const
2794 requires random_access_range<const _Vp> && sized_range<const _Vp>
2795 {
2796 return ranges::begin(_M_base) + ranges::min(ranges::distance(_M_base),
2797 _M_count);
2798 }
2799
2800 constexpr auto
2801 end() requires (!__detail::__simple_view<_Vp>)
2802 { return ranges::end(_M_base); }
2803
2804 constexpr auto
2805 end() const requires range<const _Vp>
2806 { return ranges::end(_M_base); }
2807
2808 constexpr auto
2809 size() requires sized_range<_Vp>
2810 {
2811 const auto __s = ranges::size(_M_base);
2812 const auto __c = static_cast<decltype(__s)>(_M_count);
2813 return __s < __c ? 0 : __s - __c;
2814 }
2815
2816 constexpr auto
2817 size() const requires sized_range<const _Vp>
2818 {
2819 const auto __s = ranges::size(_M_base);
2820 const auto __c = static_cast<decltype(__s)>(_M_count);
2821 return __s < __c ? 0 : __s - __c;
2822 }
2823 };
2824
2825 template<typename _Range>
2826 drop_view(_Range&&, range_difference_t<_Range>)
2827 -> drop_view<views::all_t<_Range>>;
2828
2829 template<typename _Tp>
2830 inline constexpr bool enable_borrowed_range<drop_view<_Tp>>
2831 = enable_borrowed_range<_Tp>;
2832
2833 namespace views
2834 {
2835 namespace __detail
2836 {
2837 template<typename _Range>
2838 constexpr auto
2839 __drop_of_repeat_view(_Range&&, range_difference_t<_Range>); // defined later
2840
2841 template<typename _Range, typename _Dp>
2842 concept __can_drop_view
2843 = requires { drop_view(std::declval<_Range>(), std::declval<_Dp>()); };
2844 } // namespace __detail
2845
2846 struct _Drop : __adaptor::_RangeAdaptor<_Drop>
2847 {
2848 template<viewable_range _Range, typename _Dp = range_difference_t<_Range>>
2849 requires __detail::__can_drop_view<_Range, _Dp>
2850 constexpr auto
2851 operator() [[nodiscard]] (_Range&& __r, type_identity_t<_Dp> __n) const
2852 {
2853 using _Tp = remove_cvref_t<_Range>;
2854 if constexpr (__detail::__is_empty_view<_Tp>)
2855 return _Tp();
2856#ifdef __cpp_lib_optional_range_support // >= C++26
2857 else if constexpr (__is_optional_v<_Tp> && view<_Tp>)
2858 return __n ? _Tp() : std::forward<_Range>(__r);
2859#endif
2860 else if constexpr (random_access_range<_Tp>
2861 && sized_range<_Tp>
2862 && (std::__detail::__is_span<_Tp>
2863 || __detail::__is_basic_string_view<_Tp>
2864 || __detail::__is_iota_view<_Tp>
2865 || __detail::__is_subrange<_Tp>))
2866 {
2867 __n = std::min<_Dp>(ranges::distance(__r), __n);
2868 auto __begin = ranges::begin(__r) + __n;
2869 auto __end = ranges::end(__r);
2870 if constexpr (std::__detail::__is_span<_Tp>)
2871 return span<typename _Tp::element_type>(__begin, __end);
2872 else if constexpr (__detail::__is_subrange<_Tp>)
2873 {
2874 if constexpr (_Tp::_S_store_size)
2875 {
2876 using ranges::__detail::__to_unsigned_like;
2877 auto __m = ranges::distance(__r) - __n;
2878 return _Tp(__begin, __end, __to_unsigned_like(__m));
2879 }
2880 else
2881 return _Tp(__begin, __end);
2882 }
2883 else
2884 return _Tp(__begin, __end);
2885 }
2886 else if constexpr (__detail::__is_repeat_view<_Tp>)
2887 return __detail::__drop_of_repeat_view(std::forward<_Range>(__r), __n);
2888 else
2889 return drop_view(std::forward<_Range>(__r), __n);
2890 }
2891
2892 using _RangeAdaptor<_Drop>::operator();
2893 static constexpr int _S_arity = 2;
2894 template<typename _Tp>
2895 static constexpr bool _S_has_simple_extra_args
2896 = _Take::_S_has_simple_extra_args<_Tp>;
2897 };
2898
2899 inline constexpr _Drop drop;
2900 } // namespace views
2901
2902 template<view _Vp, typename _Pred>
2903 requires input_range<_Vp> && is_object_v<_Pred>
2904 && indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
2905 class drop_while_view : public view_interface<drop_while_view<_Vp, _Pred>>
2906 {
2907 private:
2908 _Vp _M_base = _Vp();
2909 [[no_unique_address]] __detail::__box<_Pred> _M_pred;
2910 [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
2911
2912 public:
2913 drop_while_view() requires (default_initializable<_Vp>
2914 && default_initializable<_Pred>)
2915 = default;
2916
2917 constexpr explicit
2918 drop_while_view(_Vp __base, _Pred __pred)
2919 : _M_base(std::move(__base)), _M_pred(std::move(__pred))
2920 { }
2921
2922 constexpr _Vp
2923 base() const& requires copy_constructible<_Vp>
2924 { return _M_base; }
2925
2926 constexpr _Vp
2927 base() &&
2928 { return std::move(_M_base); }
2929
2930 constexpr const _Pred&
2931 pred() const
2932 { return *_M_pred; }
2933
2934 constexpr auto
2935 begin()
2936 {
2937 if (_M_cached_begin._M_has_value())
2938 return _M_cached_begin._M_get(_M_base);
2939
2940 __glibcxx_assert(_M_pred.has_value());
2941 auto __it = ranges::find_if_not(ranges::begin(_M_base),
2942 ranges::end(_M_base),
2943 std::cref(*_M_pred));
2944 _M_cached_begin._M_set(_M_base, __it);
2945 return __it;
2946 }
2947
2948 constexpr auto
2949 end()
2950 { return ranges::end(_M_base); }
2951 };
2952
2953 template<typename _Range, typename _Pred>
2954 drop_while_view(_Range&&, _Pred)
2955 -> drop_while_view<views::all_t<_Range>, _Pred>;
2956
2957 template<typename _Tp, typename _Pred>
2958 inline constexpr bool enable_borrowed_range<drop_while_view<_Tp, _Pred>>
2959 = enable_borrowed_range<_Tp>;
2960
2961 namespace views
2962 {
2963 namespace __detail
2964 {
2965 template<typename _Range, typename _Pred>
2966 concept __can_drop_while_view
2967 = requires { drop_while_view(std::declval<_Range>(), std::declval<_Pred>()); };
2968 } // namespace __detail
2969
2970 struct _DropWhile : __adaptor::_RangeAdaptor<_DropWhile>
2971 {
2972 template<viewable_range _Range, typename _Pred>
2973 requires __detail::__can_drop_while_view<_Range, _Pred>
2974 constexpr auto
2975 operator() [[nodiscard]] (_Range&& __r, _Pred&& __p) const
2976 {
2977 return drop_while_view(std::forward<_Range>(__r),
2978 std::forward<_Pred>(__p));
2979 }
2980
2981 using _RangeAdaptor<_DropWhile>::operator();
2982 static constexpr int _S_arity = 2;
2983 static constexpr bool _S_has_simple_extra_args = true;
2984 };
2985
2986 inline constexpr _DropWhile drop_while;
2987 } // namespace views
2988
2989 namespace __detail
2990 {
2991 template<typename _Tp>
2992 constexpr _Tp&
2993 __as_lvalue(_Tp&& __t)
2994 { return static_cast<_Tp&>(__t); }
2995 } // namespace __detail
2996
2997 template<input_range _Vp>
2998 requires view<_Vp> && input_range<range_reference_t<_Vp>>
2999 class join_view : public view_interface<join_view<_Vp>>
3000 {
3001 private:
3002 using _InnerRange = range_reference_t<_Vp>;
3003
3004 template<bool _Const>
3005 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
3006
3007 template<bool _Const>
3008 using _Outer_iter = iterator_t<_Base<_Const>>;
3009
3010 template<bool _Const>
3011 using _Inner_iter = iterator_t<range_reference_t<_Base<_Const>>>;
3012
3013 template<bool _Const>
3014 static constexpr bool _S_ref_is_glvalue
3015 = is_reference_v<range_reference_t<_Base<_Const>>>;
3016
3017 template<bool _Const>
3018 struct __iter_cat
3019 { };
3020
3021 template<bool _Const>
3022 requires _S_ref_is_glvalue<_Const>
3023 && forward_range<_Base<_Const>>
3024 && forward_range<range_reference_t<_Base<_Const>>>
3025 struct __iter_cat<_Const>
3026 {
3027 private:
3028 static constexpr auto
3029 _S_iter_cat()
3030 {
3031 using _Outer_iter = join_view::_Outer_iter<_Const>;
3032 using _Inner_iter = join_view::_Inner_iter<_Const>;
3033 using _OuterCat = typename iterator_traits<_Outer_iter>::iterator_category;
3034 using _InnerCat = typename iterator_traits<_Inner_iter>::iterator_category;
3035 if constexpr (derived_from<_OuterCat, bidirectional_iterator_tag>
3036 && derived_from<_InnerCat, bidirectional_iterator_tag>
3037 && common_range<range_reference_t<_Base<_Const>>>)
3038 return bidirectional_iterator_tag{};
3039 else if constexpr (derived_from<_OuterCat, forward_iterator_tag>
3040 && derived_from<_InnerCat, forward_iterator_tag>)
3041 return forward_iterator_tag{};
3042 else
3043 return input_iterator_tag{};
3044 }
3045 public:
3046 using iterator_category = decltype(_S_iter_cat());
3047 };
3048
3049 template<bool _Const>
3050 struct _Sentinel;
3051
3052 template<bool _Const>
3053 struct _Iterator : __iter_cat<_Const>
3054 {
3055 private:
3056 using _Parent = __detail::__maybe_const_t<_Const, join_view>;
3057 using _Base = join_view::_Base<_Const>;
3058
3059 friend join_view;
3060
3061 static constexpr bool _S_ref_is_glvalue
3062 = join_view::_S_ref_is_glvalue<_Const>;
3063
3064 constexpr void
3065 _M_satisfy()
3066 {
3067 auto __update_inner = [this] (const iterator_t<_Base>& __x) -> auto&& {
3068 if constexpr (_S_ref_is_glvalue)
3069 return *__x;
3070 else
3071 return _M_parent->_M_inner._M_emplace_deref(__x);
3072 };
3073
3074 _Outer_iter& __outer = _M_get_outer();
3075 for (; __outer != ranges::end(_M_parent->_M_base); ++__outer)
3076 {
3077 auto&& __inner = __update_inner(__outer);
3078 _M_inner = ranges::begin(__inner);
3079 if (_M_inner != ranges::end(__inner))
3080 return;
3081 }
3082
3083 if constexpr (_S_ref_is_glvalue)
3084 {
3085 if constexpr (forward_iterator<_Inner_iter>)
3086 _M_inner = _Inner_iter();
3087 else
3088 _M_inner.reset();
3089 }
3090 }
3091
3092 static constexpr auto
3093 _S_iter_concept()
3094 {
3095 if constexpr (_S_ref_is_glvalue
3096 && bidirectional_range<_Base>
3097 && bidirectional_range<range_reference_t<_Base>>
3098 && common_range<range_reference_t<_Base>>)
3099 return bidirectional_iterator_tag{};
3100 else if constexpr (_S_ref_is_glvalue
3101 && forward_range<_Base>
3102 && forward_range<range_reference_t<_Base>>)
3103 return forward_iterator_tag{};
3104 else
3105 return input_iterator_tag{};
3106 }
3107
3108 using _Outer_iter = join_view::_Outer_iter<_Const>;
3109 using _Inner_iter = join_view::_Inner_iter<_Const>;
3110
3111 constexpr _Outer_iter&
3112 _M_get_outer()
3113 {
3114 if constexpr (forward_range<_Base>)
3115 return _M_outer;
3116 else
3117 return *_M_parent->_M_outer;
3118 }
3119
3120 constexpr const _Outer_iter&
3121 _M_get_outer() const
3122 {
3123 if constexpr (forward_range<_Base>)
3124 return _M_outer;
3125 else
3126 return *_M_parent->_M_outer;
3127 }
3128
3129 constexpr _Inner_iter&
3130 _M_get_inner() noexcept
3131 {
3132 if constexpr (forward_iterator<_Inner_iter>)
3133 return _M_inner;
3134 else
3135 return *_M_inner;
3136 }
3137
3138 constexpr const _Inner_iter&
3139 _M_get_inner() const noexcept
3140 {
3141 if constexpr (forward_iterator<_Inner_iter>)
3142 return _M_inner;
3143 else
3144 return *_M_inner;
3145 }
3146
3147 constexpr
3148 _Iterator(_Parent* __parent, _Outer_iter __outer) requires forward_range<_Base>
3149 : _M_outer(std::move(__outer)), _M_parent(__parent)
3150 { _M_satisfy(); }
3151
3152 constexpr explicit
3153 _Iterator(_Parent* __parent) requires (!forward_range<_Base>)
3154 : _M_parent(__parent)
3155 { _M_satisfy(); }
3156
3157 [[no_unique_address]]
3158 __detail::__maybe_present_t<forward_range<_Base>, _Outer_iter> _M_outer
3159 = decltype(_M_outer)();
3160 __conditional_t<forward_iterator<_Inner_iter>,
3161 _Inner_iter, optional<_Inner_iter>> _M_inner
3162 = decltype(_M_inner)();
3163 _Parent* _M_parent = nullptr;
3164
3165 public:
3166 using iterator_concept = decltype(_S_iter_concept());
3167 // iterator_category defined in __join_view_iter_cat
3168 using value_type = range_value_t<range_reference_t<_Base>>;
3169 using difference_type
3170 = common_type_t<range_difference_t<_Base>,
3171 range_difference_t<range_reference_t<_Base>>>;
3172
3173 _Iterator() = default;
3174
3175 constexpr
3176 _Iterator(_Iterator<!_Const> __i)
3177 requires _Const
3178 && convertible_to<iterator_t<_Vp>, _Outer_iter>
3179 && convertible_to<iterator_t<_InnerRange>, _Inner_iter>
3180 : _M_outer(std::move(__i._M_outer)), _M_inner(std::move(__i._M_inner)),
3181 _M_parent(__i._M_parent)
3182 { }
3183
3184 constexpr decltype(auto)
3185 operator*() const
3186 { return *_M_get_inner(); }
3187
3188 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3189 // 3500. join_view::iterator::operator->() is bogus
3190 constexpr _Inner_iter
3191 operator->() const
3192 requires __detail::__has_arrow<_Inner_iter>
3193 && copyable<_Inner_iter>
3194 { return _M_get_inner(); }
3195
3196 constexpr _Iterator&
3197 operator++()
3198 {
3199 auto&& __inner_range = [this] () -> auto&& {
3200 if constexpr (_S_ref_is_glvalue)
3201 return *_M_get_outer();
3202 else
3203 return *_M_parent->_M_inner;
3204 }();
3205 if (++_M_get_inner() == ranges::end(__inner_range))
3206 {
3207 ++_M_get_outer();
3208 _M_satisfy();
3209 }
3210 return *this;
3211 }
3212
3213 constexpr void
3214 operator++(int)
3215 { ++*this; }
3216
3217 constexpr _Iterator
3218 operator++(int)
3219 requires _S_ref_is_glvalue && forward_range<_Base>
3220 && forward_range<range_reference_t<_Base>>
3221 {
3222 auto __tmp = *this;
3223 ++*this;
3224 return __tmp;
3225 }
3226
3227 constexpr _Iterator&
3228 operator--()
3229 requires _S_ref_is_glvalue && bidirectional_range<_Base>
3230 && bidirectional_range<range_reference_t<_Base>>
3231 && common_range<range_reference_t<_Base>>
3232 {
3233 if (_M_outer == ranges::end(_M_parent->_M_base))
3234 _M_inner = ranges::end(__detail::__as_lvalue(*--_M_outer));
3235 while (_M_get_inner() == ranges::begin(__detail::__as_lvalue(*_M_outer)))
3236 _M_get_inner() = ranges::end(__detail::__as_lvalue(*--_M_outer));
3237 --_M_get_inner();
3238 return *this;
3239 }
3240
3241 constexpr _Iterator
3242 operator--(int)
3243 requires _S_ref_is_glvalue && bidirectional_range<_Base>
3244 && bidirectional_range<range_reference_t<_Base>>
3245 && common_range<range_reference_t<_Base>>
3246 {
3247 auto __tmp = *this;
3248 --*this;
3249 return __tmp;
3250 }
3251
3252 friend constexpr bool
3253 operator==(const _Iterator& __x, const _Iterator& __y)
3254 requires _S_ref_is_glvalue
3255 && forward_range<_Base>
3256 && equality_comparable<_Inner_iter>
3257 {
3258 return (__x._M_outer == __y._M_outer
3259 && __x._M_inner == __y._M_inner);
3260 }
3261
3262 friend constexpr decltype(auto)
3263 iter_move(const _Iterator& __i)
3264 noexcept(noexcept(ranges::iter_move(__i._M_get_inner())))
3265 { return ranges::iter_move(__i._M_get_inner()); }
3266
3267 friend constexpr void
3268 iter_swap(const _Iterator& __x, const _Iterator& __y)
3269 noexcept(noexcept(ranges::iter_swap(__x._M_get_inner(), __y._M_get_inner())))
3270 requires indirectly_swappable<_Inner_iter>
3271 { return ranges::iter_swap(__x._M_get_inner(), __y._M_get_inner()); }
3272
3273 friend _Iterator<!_Const>;
3274 template<bool> friend struct _Sentinel;
3275 };
3276
3277 template<bool _Const>
3278 struct _Sentinel
3279 {
3280 private:
3281 using _Parent = __detail::__maybe_const_t<_Const, join_view>;
3282 using _Base = join_view::_Base<_Const>;
3283
3284 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
3285
3286 public:
3287 _Sentinel() = default;
3288
3289 constexpr explicit
3290 _Sentinel(_Parent* __parent)
3291 : _M_end(ranges::end(__parent->_M_base))
3292 { }
3293
3294 constexpr
3295 _Sentinel(_Sentinel<!_Const> __s)
3296 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
3297 : _M_end(std::move(__s._M_end))
3298 { }
3299
3300 template<bool _Const2>
3301 requires sentinel_for<sentinel_t<_Base>,
3302 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
3303 friend constexpr bool
3304 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
3305 { return __x._M_get_outer() == __y._M_end; }
3306
3307 friend _Sentinel<!_Const>;
3308 };
3309
3310 _Vp _M_base = _Vp();
3311 [[no_unique_address]]
3312 __detail::__maybe_present_t<!forward_range<_Vp>,
3313 __detail::__non_propagating_cache<iterator_t<_Vp>>> _M_outer;
3314 [[no_unique_address]]
3315 __detail::__non_propagating_cache<remove_cv_t<_InnerRange>> _M_inner;
3316
3317 public:
3318 join_view() requires default_initializable<_Vp> = default;
3319
3320 constexpr explicit
3321 join_view(_Vp __base)
3322 : _M_base(std::move(__base))
3323 { }
3324
3325 constexpr _Vp
3326 base() const& requires copy_constructible<_Vp>
3327 { return _M_base; }
3328
3329 constexpr _Vp
3330 base() &&
3331 { return std::move(_M_base); }
3332
3333 constexpr auto
3334 begin()
3335 {
3336 if constexpr (forward_range<_Vp>)
3337 {
3338 constexpr bool __use_const
3339 = (__detail::__simple_view<_Vp>
3340 && is_reference_v<range_reference_t<_Vp>>);
3341 return _Iterator<__use_const>{this, ranges::begin(_M_base)};
3342 }
3343 else
3344 {
3345 _M_outer = ranges::begin(_M_base);
3346 return _Iterator<false>{this};
3347 }
3348 }
3349
3350 constexpr auto
3351 begin() const
3352 requires forward_range<const _Vp>
3353 && is_reference_v<range_reference_t<const _Vp>>
3354 && input_range<range_reference_t<const _Vp>>
3355 {
3356 return _Iterator<true>{this, ranges::begin(_M_base)};
3357 }
3358
3359 constexpr auto
3360 end()
3361 {
3362 if constexpr (forward_range<_Vp> && is_reference_v<_InnerRange>
3363 && forward_range<_InnerRange>
3364 && common_range<_Vp> && common_range<_InnerRange>)
3365 return _Iterator<__detail::__simple_view<_Vp>>{this,
3366 ranges::end(_M_base)};
3367 else
3368 return _Sentinel<__detail::__simple_view<_Vp>>{this};
3369 }
3370
3371 constexpr auto
3372 end() const
3373 requires forward_range<const _Vp>
3374 && is_reference_v<range_reference_t<const _Vp>>
3375 && input_range<range_reference_t<const _Vp>>
3376 {
3377 if constexpr (is_reference_v<range_reference_t<const _Vp>>
3378 && forward_range<range_reference_t<const _Vp>>
3379 && common_range<const _Vp>
3380 && common_range<range_reference_t<const _Vp>>)
3381 return _Iterator<true>{this, ranges::end(_M_base)};
3382 else
3383 return _Sentinel<true>{this};
3384 }
3385 };
3386
3387 template<typename _Range>
3388 explicit join_view(_Range&&) -> join_view<views::all_t<_Range>>;
3389
3390 namespace views
3391 {
3392 namespace __detail
3393 {
3394 template<typename _Range>
3395 concept __can_join_view
3396 = requires { join_view<all_t<_Range>>{std::declval<_Range>()}; };
3397 } // namespace __detail
3398
3399 struct _Join : __adaptor::_RangeAdaptorClosure<_Join>
3400 {
3401 template<viewable_range _Range>
3402 requires __detail::__can_join_view<_Range>
3403 constexpr auto
3404 operator() [[nodiscard]] (_Range&& __r) const
3405 {
3406 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3407 // 3474. Nesting join_views is broken because of CTAD
3408 return join_view<all_t<_Range>>{std::forward<_Range>(__r)};
3409 }
3410
3411 static constexpr bool _S_has_simple_call_op = true;
3412 };
3413
3414 inline constexpr _Join join;
3415 } // namespace views
3416
3417 namespace __detail
3418 {
3419 template<auto>
3420 struct __require_constant;
3421
3422 template<typename _Range>
3423 concept __tiny_range = sized_range<_Range>
3424 && requires
3425 { typename __require_constant<remove_reference_t<_Range>::size()>; }
3426 && (remove_reference_t<_Range>::size() <= 1);
3427
3428 template<typename _Base>
3429 struct __lazy_split_view_outer_iter_cat
3430 { };
3431
3432 template<forward_range _Base>
3433 struct __lazy_split_view_outer_iter_cat<_Base>
3434 { using iterator_category = input_iterator_tag; };
3435
3436 template<typename _Base>
3437 struct __lazy_split_view_inner_iter_cat
3438 { };
3439
3440 template<forward_range _Base>
3441 struct __lazy_split_view_inner_iter_cat<_Base>
3442 {
3443 private:
3444 static constexpr auto
3445 _S_iter_cat()
3446 {
3447 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
3448 if constexpr (derived_from<_Cat, forward_iterator_tag>)
3449 return forward_iterator_tag{};
3450 else
3451 return _Cat{};
3452 }
3453 public:
3454 using iterator_category = decltype(_S_iter_cat());
3455 };
3456 }
3457
3458 template<input_range _Vp, forward_range _Pattern>
3459 requires view<_Vp> && view<_Pattern>
3460 && indirectly_comparable<iterator_t<_Vp>, iterator_t<_Pattern>,
3461 ranges::equal_to>
3462 && (forward_range<_Vp> || __detail::__tiny_range<_Pattern>)
3463 class lazy_split_view : public view_interface<lazy_split_view<_Vp, _Pattern>>
3464 {
3465 private:
3466 template<bool _Const>
3467 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
3468
3469 template<bool _Const>
3470 struct _InnerIter;
3471
3472 template<bool _Const>
3473 struct _OuterIter
3474 : __detail::__lazy_split_view_outer_iter_cat<_Base<_Const>>
3475 {
3476 private:
3477 using _Parent = __detail::__maybe_const_t<_Const, lazy_split_view>;
3478 using _Base = lazy_split_view::_Base<_Const>;
3479
3480 // [range.lazy.split.outer] p1
3481 // Many of the following specifications refer to the notional member
3482 // current of outer-iterator. current is equivalent to current_ if
3483 // V models forward_range, and parent_->current_ otherwise.
3484 constexpr auto&
3485 __current() noexcept
3486 {
3487 if constexpr (forward_range<_Vp>)
3488 return _M_current;
3489 else
3490 return *_M_parent->_M_current;
3491 }
3492
3493 constexpr auto&
3494 __current() const noexcept
3495 {
3496 if constexpr (forward_range<_Vp>)
3497 return _M_current;
3498 else
3499 return *_M_parent->_M_current;
3500 }
3501
3502 _Parent* _M_parent = nullptr;
3503
3504 [[no_unique_address]]
3505 __detail::__maybe_present_t<forward_range<_Vp>,
3506 iterator_t<_Base>> _M_current
3507 = decltype(_M_current)();
3508 bool _M_trailing_empty = false;
3509
3510 public:
3511 using iterator_concept = __conditional_t<forward_range<_Base>,
3512 forward_iterator_tag,
3513 input_iterator_tag>;
3514 // iterator_category defined in __lazy_split_view_outer_iter_cat
3515 using difference_type = range_difference_t<_Base>;
3516
3517 struct value_type : view_interface<value_type>
3518 {
3519 private:
3520 _OuterIter _M_i = _OuterIter();
3521
3522 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3523 // 4013. lazy_split_view::outer-iterator::value_type should not
3524 // provide default constructor
3525 constexpr explicit
3526 value_type(_OuterIter __i)
3527 : _M_i(std::move(__i))
3528 { }
3529
3530 friend _OuterIter;
3531
3532 public:
3533 constexpr _InnerIter<_Const>
3534 begin() const
3535 { return _InnerIter<_Const>{_M_i}; }
3536
3537 constexpr default_sentinel_t
3538 end() const noexcept
3539 { return default_sentinel; }
3540 };
3541
3542 _OuterIter() = default;
3543
3544 constexpr explicit
3545 _OuterIter(_Parent* __parent) requires (!forward_range<_Base>)
3546 : _M_parent(__parent)
3547 { }
3548
3549 constexpr
3550 _OuterIter(_Parent* __parent, iterator_t<_Base> __current)
3551 requires forward_range<_Base>
3552 : _M_parent(__parent),
3553 _M_current(std::move(__current))
3554 { }
3555
3556 constexpr
3557 _OuterIter(_OuterIter<!_Const> __i)
3558 requires _Const
3559 && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
3560 : _M_parent(__i._M_parent), _M_current(std::move(__i._M_current)),
3561 _M_trailing_empty(__i._M_trailing_empty)
3562 { }
3563
3564 constexpr value_type
3565 operator*() const
3566 { return value_type{*this}; }
3567
3568 constexpr _OuterIter&
3569 operator++()
3570 {
3571 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3572 // 3505. lazy_split_view::outer-iterator::operator++ misspecified
3573 const auto __end = ranges::end(_M_parent->_M_base);
3574 if (__current() == __end)
3575 {
3576 _M_trailing_empty = false;
3577 return *this;
3578 }
3579 const auto [__pbegin, __pend] = subrange{_M_parent->_M_pattern};
3580 if (__pbegin == __pend)
3581 ++__current();
3582 else if constexpr (__detail::__tiny_range<_Pattern>)
3583 {
3584 __current() = ranges::find(std::move(__current()), __end,
3585 *__pbegin);
3586 if (__current() != __end)
3587 {
3588 ++__current();
3589 if (__current() == __end)
3590 _M_trailing_empty = true;
3591 }
3592 }
3593 else
3594 do
3595 {
3596 auto [__b, __p]
3597 = ranges::mismatch(__current(), __end, __pbegin, __pend);
3598 if (__p == __pend)
3599 {
3600 __current() = __b;
3601 if (__current() == __end)
3602 _M_trailing_empty = true;
3603 break;
3604 }
3605 } while (++__current() != __end);
3606 return *this;
3607 }
3608
3609 constexpr decltype(auto)
3610 operator++(int)
3611 {
3612 if constexpr (forward_range<_Base>)
3613 {
3614 auto __tmp = *this;
3615 ++*this;
3616 return __tmp;
3617 }
3618 else
3619 ++*this;
3620 }
3621
3622 friend constexpr bool
3623 operator==(const _OuterIter& __x, const _OuterIter& __y)
3624 requires forward_range<_Base>
3625 {
3626 return __x._M_current == __y._M_current
3627 && __x._M_trailing_empty == __y._M_trailing_empty;
3628 }
3629
3630 friend constexpr bool
3631 operator==(const _OuterIter& __x, default_sentinel_t)
3632 {
3633 return __x.__current() == ranges::end(__x._M_parent->_M_base)
3634 && !__x._M_trailing_empty;
3635 }
3636
3637 friend _OuterIter<!_Const>;
3638 friend _InnerIter<_Const>;
3639 };
3640
3641 template<bool _Const>
3642 struct _InnerIter
3643 : __detail::__lazy_split_view_inner_iter_cat<_Base<_Const>>
3644 {
3645 private:
3646 using _Base = lazy_split_view::_Base<_Const>;
3647
3648 constexpr bool
3649 __at_end() const
3650 {
3651 auto [__pcur, __pend] = subrange{_M_i._M_parent->_M_pattern};
3652 auto __end = ranges::end(_M_i._M_parent->_M_base);
3653 if constexpr (__detail::__tiny_range<_Pattern>)
3654 {
3655 const auto& __cur = _M_i_current();
3656 if (__cur == __end)
3657 return true;
3658 if (__pcur == __pend)
3659 return _M_incremented;
3660 return *__cur == *__pcur;
3661 }
3662 else
3663 {
3664 auto __cur = _M_i_current();
3665 if (__cur == __end)
3666 return true;
3667 if (__pcur == __pend)
3668 return _M_incremented;
3669 do
3670 {
3671 if (*__cur != *__pcur)
3672 return false;
3673 if (++__pcur == __pend)
3674 return true;
3675 } while (++__cur != __end);
3676 return false;
3677 }
3678 }
3679
3680 constexpr auto&
3681 _M_i_current() noexcept
3682 { return _M_i.__current(); }
3683
3684 constexpr auto&
3685 _M_i_current() const noexcept
3686 { return _M_i.__current(); }
3687
3688 _OuterIter<_Const> _M_i = _OuterIter<_Const>();
3689 bool _M_incremented = false;
3690
3691 public:
3692 using iterator_concept
3693 = typename _OuterIter<_Const>::iterator_concept;
3694 // iterator_category defined in __lazy_split_view_inner_iter_cat
3695 using value_type = range_value_t<_Base>;
3696 using difference_type = range_difference_t<_Base>;
3697
3698 _InnerIter() = default;
3699
3700 constexpr explicit
3701 _InnerIter(_OuterIter<_Const> __i)
3702 : _M_i(std::move(__i))
3703 { }
3704
3705 constexpr const iterator_t<_Base>&
3706 base() const& noexcept
3707 { return _M_i_current(); }
3708
3709 constexpr iterator_t<_Base>
3710 base() && requires forward_range<_Vp>
3711 { return std::move(_M_i_current()); }
3712
3713 constexpr decltype(auto)
3714 operator*() const
3715 { return *_M_i_current(); }
3716
3717 constexpr _InnerIter&
3718 operator++()
3719 {
3720 _M_incremented = true;
3721 if constexpr (!forward_range<_Base>)
3722 if constexpr (_Pattern::size() == 0)
3723 return *this;
3724 ++_M_i_current();
3725 return *this;
3726 }
3727
3728 constexpr decltype(auto)
3729 operator++(int)
3730 {
3731 if constexpr (forward_range<_Base>)
3732 {
3733 auto __tmp = *this;
3734 ++*this;
3735 return __tmp;
3736 }
3737 else
3738 ++*this;
3739 }
3740
3741 friend constexpr bool
3742 operator==(const _InnerIter& __x, const _InnerIter& __y)
3743 requires forward_range<_Base>
3744 { return __x._M_i == __y._M_i; }
3745
3746 friend constexpr bool
3747 operator==(const _InnerIter& __x, default_sentinel_t)
3748 { return __x.__at_end(); }
3749
3750 friend constexpr decltype(auto)
3751 iter_move(const _InnerIter& __i)
3752 noexcept(noexcept(ranges::iter_move(__i._M_i_current())))
3753 { return ranges::iter_move(__i._M_i_current()); }
3754
3755 friend constexpr void
3756 iter_swap(const _InnerIter& __x, const _InnerIter& __y)
3757 noexcept(noexcept(ranges::iter_swap(__x._M_i_current(),
3758 __y._M_i_current())))
3759 requires indirectly_swappable<iterator_t<_Base>>
3760 { ranges::iter_swap(__x._M_i_current(), __y._M_i_current()); }
3761 };
3762
3763 _Vp _M_base = _Vp();
3764 _Pattern _M_pattern = _Pattern();
3765 [[no_unique_address]]
3766 __detail::__maybe_present_t<!forward_range<_Vp>,
3767 __detail::__non_propagating_cache<iterator_t<_Vp>>> _M_current;
3768
3769
3770 public:
3771 lazy_split_view() requires (default_initializable<_Vp>
3772 && default_initializable<_Pattern>)
3773 = default;
3774
3775 constexpr explicit
3776 lazy_split_view(_Vp __base, _Pattern __pattern)
3777 : _M_base(std::move(__base)), _M_pattern(std::move(__pattern))
3778 { }
3779
3780 template<input_range _Range>
3781 requires constructible_from<_Vp, views::all_t<_Range>>
3782 && constructible_from<_Pattern, single_view<range_value_t<_Range>>>
3783 constexpr explicit
3784 lazy_split_view(_Range&& __r, range_value_t<_Range> __e)
3785 : _M_base(views::all(std::forward<_Range>(__r))),
3786 _M_pattern(views::single(std::move(__e)))
3787 { }
3788
3789 constexpr _Vp
3790 base() const& requires copy_constructible<_Vp>
3791 { return _M_base; }
3792
3793 constexpr _Vp
3794 base() &&
3795 { return std::move(_M_base); }
3796
3797 constexpr auto
3798 begin()
3799 {
3800 if constexpr (forward_range<_Vp>)
3801 {
3802 constexpr bool __simple
3803 = __detail::__simple_view<_Vp> && __detail::__simple_view<_Pattern>;
3804 return _OuterIter<__simple>{this, ranges::begin(_M_base)};
3805 }
3806 else
3807 {
3808 _M_current = ranges::begin(_M_base);
3809 return _OuterIter<false>{this};
3810 }
3811 }
3812
3813 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3814 // 3599. The const overload of lazy_split_view::begin should be
3815 // constrained by const Pattern
3816 constexpr auto
3817 begin() const requires forward_range<_Vp> && forward_range<const _Vp>
3818 && forward_range<const _Pattern>
3819 {
3820 return _OuterIter<true>{this, ranges::begin(_M_base)};
3821 }
3822
3823 constexpr auto
3824 end() requires forward_range<_Vp> && common_range<_Vp>
3825 {
3826 constexpr bool __simple
3827 = __detail::__simple_view<_Vp> && __detail::__simple_view<_Pattern>;
3828 return _OuterIter<__simple>{this, ranges::end(_M_base)};
3829 }
3830
3831 constexpr auto
3832 end() const
3833 {
3834 if constexpr (forward_range<_Vp>
3835 && forward_range<const _Vp>
3836 && common_range<const _Vp>
3837 && forward_range<const _Pattern>)
3838 return _OuterIter<true>{this, ranges::end(_M_base)};
3839 else
3840 return default_sentinel;
3841 }
3842 };
3843
3844 template<typename _Range, typename _Pattern>
3845 lazy_split_view(_Range&&, _Pattern&&)
3846 -> lazy_split_view<views::all_t<_Range>, views::all_t<_Pattern>>;
3847
3848 template<input_range _Range>
3849 lazy_split_view(_Range&&, range_value_t<_Range>)
3850 -> lazy_split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
3851
3852 namespace views
3853 {
3854 namespace __detail
3855 {
3856 template<typename _Range, typename _Pattern>
3857 concept __can_lazy_split_view
3858 = requires { lazy_split_view(std::declval<_Range>(), std::declval<_Pattern>()); };
3859 } // namespace __detail
3860
3861 struct _LazySplit : __adaptor::_RangeAdaptor<_LazySplit>
3862 {
3863 template<viewable_range _Range, typename _Pattern>
3864 requires __detail::__can_lazy_split_view<_Range, _Pattern>
3865 constexpr auto
3866 operator() [[nodiscard]] (_Range&& __r, _Pattern&& __f) const
3867 {
3868 return lazy_split_view(std::forward<_Range>(__r), std::forward<_Pattern>(__f));
3869 }
3870
3871 using _RangeAdaptor<_LazySplit>::operator();
3872 static constexpr int _S_arity = 2;
3873 // The pattern argument of views::lazy_split is not always simple -- it can be
3874 // a non-view range, the value category of which affects whether the call
3875 // is well-formed. But a scalar or a view pattern argument is surely
3876 // simple.
3877 template<typename _Pattern>
3878 static constexpr bool _S_has_simple_extra_args
3879 = is_scalar_v<_Pattern> || (view<_Pattern>
3880 && copy_constructible<_Pattern>);
3881 };
3882
3883 inline constexpr _LazySplit lazy_split;
3884 } // namespace views
3885
3886 template<forward_range _Vp, forward_range _Pattern>
3887 requires view<_Vp> && view<_Pattern>
3888 && indirectly_comparable<iterator_t<_Vp>, iterator_t<_Pattern>,
3889 ranges::equal_to>
3890 class split_view : public view_interface<split_view<_Vp, _Pattern>>
3891 {
3892 private:
3893 _Vp _M_base = _Vp();
3894 _Pattern _M_pattern = _Pattern();
3895 __detail::__non_propagating_cache<subrange<iterator_t<_Vp>>> _M_cached_begin;
3896
3897 struct _Iterator;
3898 struct _Sentinel;
3899
3900 public:
3901 split_view() requires (default_initializable<_Vp>
3902 && default_initializable<_Pattern>)
3903 = default;
3904
3905 constexpr explicit
3906 split_view(_Vp __base, _Pattern __pattern)
3907 : _M_base(std::move(__base)), _M_pattern(std::move(__pattern))
3908 { }
3909
3910 template<forward_range _Range>
3911 requires constructible_from<_Vp, views::all_t<_Range>>
3912 && constructible_from<_Pattern, single_view<range_value_t<_Range>>>
3913 constexpr explicit
3914 split_view(_Range&& __r, range_value_t<_Range> __e)
3915 : _M_base(views::all(std::forward<_Range>(__r))),
3916 _M_pattern(views::single(std::move(__e)))
3917 { }
3918
3919 constexpr _Vp
3920 base() const& requires copy_constructible<_Vp>
3921 { return _M_base; }
3922
3923 constexpr _Vp
3924 base() &&
3925 { return std::move(_M_base); }
3926
3927 constexpr _Iterator
3928 begin()
3929 {
3930 if (!_M_cached_begin)
3931 _M_cached_begin = _M_find_next(ranges::begin(_M_base));
3932 return {this, ranges::begin(_M_base), *_M_cached_begin};
3933 }
3934
3935 constexpr auto
3936 end()
3937 {
3938 if constexpr (common_range<_Vp>)
3939 return _Iterator{this, ranges::end(_M_base), {}};
3940 else
3941 return _Sentinel{this};
3942 }
3943
3944 constexpr subrange<iterator_t<_Vp>>
3945 _M_find_next(iterator_t<_Vp> __it)
3946 {
3947 auto [__b, __e] = ranges::search(subrange(__it, ranges::end(_M_base)), _M_pattern);
3948 if (__b != ranges::end(_M_base) && ranges::empty(_M_pattern))
3949 {
3950 ++__b;
3951 ++__e;
3952 }
3953 return {__b, __e};
3954 }
3955
3956 private:
3957 struct _Iterator
3958 {
3959 private:
3960 split_view* _M_parent = nullptr;
3961 iterator_t<_Vp> _M_cur = iterator_t<_Vp>();
3962 subrange<iterator_t<_Vp>> _M_next = subrange<iterator_t<_Vp>>();
3963 bool _M_trailing_empty = false;
3964
3965 friend struct _Sentinel;
3966
3967 public:
3968 using iterator_concept = forward_iterator_tag;
3969 using iterator_category = input_iterator_tag;
3970 using value_type = subrange<iterator_t<_Vp>>;
3971 using difference_type = range_difference_t<_Vp>;
3972
3973 _Iterator() = default;
3974
3975 constexpr
3976 _Iterator(split_view* __parent,
3977 iterator_t<_Vp> __current,
3978 subrange<iterator_t<_Vp>> __next)
3979 : _M_parent(__parent),
3980 _M_cur(std::move(__current)),
3981 _M_next(std::move(__next))
3982 { }
3983
3984 constexpr iterator_t<_Vp>
3985 base() const
3986 { return _M_cur; }
3987
3988 constexpr value_type
3989 operator*() const
3990 { return {_M_cur, _M_next.begin()}; }
3991
3992 constexpr _Iterator&
3993 operator++()
3994 {
3995 _M_cur = _M_next.begin();
3996 if (_M_cur != ranges::end(_M_parent->_M_base))
3997 {
3998 _M_cur = _M_next.end();
3999 if (_M_cur == ranges::end(_M_parent->_M_base))
4000 {
4001 _M_trailing_empty = true;
4002 _M_next = {_M_cur, _M_cur};
4003 }
4004 else
4005 _M_next = _M_parent->_M_find_next(_M_cur);
4006 }
4007 else
4008 _M_trailing_empty = false;
4009 return *this;
4010 }
4011
4012 constexpr _Iterator
4013 operator++(int)
4014 {
4015 auto __tmp = *this;
4016 ++*this;
4017 return __tmp;
4018 }
4019
4020 friend constexpr bool
4021 operator==(const _Iterator& __x, const _Iterator& __y)
4022 {
4023 return __x._M_cur == __y._M_cur
4024 && __x._M_trailing_empty == __y._M_trailing_empty;
4025 }
4026 };
4027
4028 struct _Sentinel
4029 {
4030 private:
4031 sentinel_t<_Vp> _M_end = sentinel_t<_Vp>();
4032
4033 public:
4034 _Sentinel() = default;
4035
4036 constexpr explicit
4037 _Sentinel(split_view* __parent)
4038 : _M_end(ranges::end(__parent->_M_base))
4039 { }
4040
4041 friend constexpr bool
4042 operator==(const _Iterator& __x, const _Sentinel& __y)
4043 { return __x._M_cur == __y._M_end && !__x._M_trailing_empty; }
4044 };
4045 };
4046
4047 template<typename _Range, typename _Pattern>
4048 split_view(_Range&&, _Pattern&&)
4049 -> split_view<views::all_t<_Range>, views::all_t<_Pattern>>;
4050
4051 template<forward_range _Range>
4052 split_view(_Range&&, range_value_t<_Range>)
4053 -> split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
4054
4055 namespace views
4056 {
4057 namespace __detail
4058 {
4059 template<typename _Range, typename _Pattern>
4060 concept __can_split_view
4061 = requires { split_view(std::declval<_Range>(), std::declval<_Pattern>()); };
4062 } // namespace __detail
4063
4064 struct _Split : __adaptor::_RangeAdaptor<_Split>
4065 {
4066 template<viewable_range _Range, typename _Pattern>
4067 requires __detail::__can_split_view<_Range, _Pattern>
4068 constexpr auto
4069 operator() [[nodiscard]] (_Range&& __r, _Pattern&& __f) const
4070 {
4071 return split_view(std::forward<_Range>(__r), std::forward<_Pattern>(__f));
4072 }
4073
4074 using _RangeAdaptor<_Split>::operator();
4075 static constexpr int _S_arity = 2;
4076 template<typename _Pattern>
4077 static constexpr bool _S_has_simple_extra_args
4078 = _LazySplit::_S_has_simple_extra_args<_Pattern>;
4079 };
4080
4081 inline constexpr _Split split;
4082 } // namespace views
4083
4084 namespace views
4085 {
4086 struct _Counted
4087 {
4088 template<input_or_output_iterator _Iter>
4089 constexpr auto
4090 operator() [[nodiscard]] (_Iter __i, iter_difference_t<_Iter> __n) const
4091 {
4092 if constexpr (contiguous_iterator<_Iter>)
4093 return span(std::to_address(__i), __n);
4094 else if constexpr (random_access_iterator<_Iter>)
4095 return subrange(__i, __i + __n);
4096 else
4097 return subrange(counted_iterator(std::move(__i), __n),
4098 default_sentinel);
4099 }
4100 };
4101
4102 inline constexpr _Counted counted{};
4103 } // namespace views
4104
4105 template<view _Vp>
4106 requires (!common_range<_Vp>) && copyable<iterator_t<_Vp>>
4107 class common_view : public view_interface<common_view<_Vp>>
4108 {
4109 private:
4110 _Vp _M_base = _Vp();
4111
4112 public:
4113 common_view() requires default_initializable<_Vp> = default;
4114
4115 constexpr explicit
4116 common_view(_Vp __r)
4117 : _M_base(std::move(__r))
4118 { }
4119
4120 constexpr _Vp
4121 base() const& requires copy_constructible<_Vp>
4122 { return _M_base; }
4123
4124 constexpr _Vp
4125 base() &&
4126 { return std::move(_M_base); }
4127
4128 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4129 // 4012. common_view::begin/end are missing the simple-view check
4130 constexpr auto
4131 begin() requires (!__detail::__simple_view<_Vp>)
4132 {
4133 if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
4134 return ranges::begin(_M_base);
4135 else
4136 return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
4137 (ranges::begin(_M_base));
4138 }
4139
4140 constexpr auto
4141 begin() const requires range<const _Vp>
4142 {
4143 if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
4144 return ranges::begin(_M_base);
4145 else
4146 return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
4147 (ranges::begin(_M_base));
4148 }
4149
4150 constexpr auto
4151 end() requires (!__detail::__simple_view<_Vp>)
4152 {
4153 if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
4154 return ranges::begin(_M_base) + ranges::size(_M_base);
4155 else
4156 return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
4157 (ranges::end(_M_base));
4158 }
4159
4160 constexpr auto
4161 end() const requires range<const _Vp>
4162 {
4163 if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
4164 return ranges::begin(_M_base) + ranges::size(_M_base);
4165 else
4166 return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
4167 (ranges::end(_M_base));
4168 }
4169
4170 constexpr auto
4171 size() requires sized_range<_Vp>
4172 { return ranges::size(_M_base); }
4173
4174 constexpr auto
4175 size() const requires sized_range<const _Vp>
4176 { return ranges::size(_M_base); }
4177 };
4178
4179 template<typename _Range>
4180 common_view(_Range&&) -> common_view<views::all_t<_Range>>;
4181
4182 template<typename _Tp>
4183 inline constexpr bool enable_borrowed_range<common_view<_Tp>>
4184 = enable_borrowed_range<_Tp>;
4185
4186 namespace views
4187 {
4188 namespace __detail
4189 {
4190 template<typename _Range>
4191 concept __already_common = common_range<_Range>
4192 && requires { views::all(std::declval<_Range>()); };
4193
4194 template<typename _Range>
4195 concept __can_common_view
4196 = requires { common_view{std::declval<_Range>()}; };
4197 } // namespace __detail
4198
4199 struct _Common : __adaptor::_RangeAdaptorClosure<_Common>
4200 {
4201 template<viewable_range _Range>
4202 requires __detail::__already_common<_Range>
4203 || __detail::__can_common_view<_Range>
4204 constexpr auto
4205 operator() [[nodiscard]] (_Range&& __r) const
4206 {
4207 if constexpr (__detail::__already_common<_Range>)
4208 return views::all(std::forward<_Range>(__r));
4209 else
4210 return common_view{std::forward<_Range>(__r)};
4211 }
4212
4213 static constexpr bool _S_has_simple_call_op = true;
4214 };
4215
4216 inline constexpr _Common common;
4217 } // namespace views
4218
4219 template<view _Vp>
4220 requires bidirectional_range<_Vp>
4221 class reverse_view : public view_interface<reverse_view<_Vp>>
4222 {
4223 private:
4224 static constexpr bool _S_needs_cached_begin
4225 = !common_range<_Vp> && !(random_access_range<_Vp>
4226 && sized_sentinel_for<sentinel_t<_Vp>,
4227 iterator_t<_Vp>>);
4228
4229 _Vp _M_base = _Vp();
4230 [[no_unique_address]]
4231 __detail::__maybe_present_t<_S_needs_cached_begin,
4232 __detail::_CachedPosition<_Vp>>
4233 _M_cached_begin;
4234
4235 public:
4236 reverse_view() requires default_initializable<_Vp> = default;
4237
4238 constexpr explicit
4239 reverse_view(_Vp __r)
4240 : _M_base(std::move(__r))
4241 { }
4242
4243 constexpr _Vp
4244 base() const& requires copy_constructible<_Vp>
4245 { return _M_base; }
4246
4247 constexpr _Vp
4248 base() &&
4249 { return std::move(_M_base); }
4250
4251 constexpr reverse_iterator<iterator_t<_Vp>>
4252 begin()
4253 {
4254 if constexpr (_S_needs_cached_begin)
4255 if (_M_cached_begin._M_has_value())
4256 return std::make_reverse_iterator(_M_cached_begin._M_get(_M_base));
4257
4258 auto __it = ranges::next(ranges::begin(_M_base), ranges::end(_M_base));
4259 if constexpr (_S_needs_cached_begin)
4260 _M_cached_begin._M_set(_M_base, __it);
4261 return std::make_reverse_iterator(std::move(__it));
4262 }
4263
4264 constexpr auto
4265 begin() requires common_range<_Vp>
4266 { return std::make_reverse_iterator(ranges::end(_M_base)); }
4267
4268 constexpr auto
4269 begin() const requires common_range<const _Vp>
4270 { return std::make_reverse_iterator(ranges::end(_M_base)); }
4271
4272 constexpr reverse_iterator<iterator_t<_Vp>>
4273 end()
4274 { return std::make_reverse_iterator(ranges::begin(_M_base)); }
4275
4276 constexpr auto
4277 end() const requires common_range<const _Vp>
4278 { return std::make_reverse_iterator(ranges::begin(_M_base)); }
4279
4280 constexpr auto
4281 size() requires sized_range<_Vp>
4282 { return ranges::size(_M_base); }
4283
4284 constexpr auto
4285 size() const requires sized_range<const _Vp>
4286 { return ranges::size(_M_base); }
4287 };
4288
4289 template<typename _Range>
4290 reverse_view(_Range&&) -> reverse_view<views::all_t<_Range>>;
4291
4292 template<typename _Tp>
4293 inline constexpr bool enable_borrowed_range<reverse_view<_Tp>>
4294 = enable_borrowed_range<_Tp>;
4295
4296 namespace views
4297 {
4298 namespace __detail
4299 {
4300 template<typename>
4301 inline constexpr bool __is_reversible_subrange = false;
4302
4303 template<typename _Iter, subrange_kind _Kind>
4304 inline constexpr bool
4305 __is_reversible_subrange<subrange<reverse_iterator<_Iter>,
4306 reverse_iterator<_Iter>,
4307 _Kind>> = true;
4308
4309 template<typename>
4310 inline constexpr bool __is_reverse_view = false;
4311
4312 template<typename _Vp>
4313 inline constexpr bool __is_reverse_view<reverse_view<_Vp>> = true;
4314
4315 template<typename _Range>
4316 concept __can_reverse_view
4317 = requires { reverse_view{std::declval<_Range>()}; };
4318 } // namespace __detail
4319
4320 struct _Reverse : __adaptor::_RangeAdaptorClosure<_Reverse>
4321 {
4322 template<viewable_range _Range>
4323 requires __detail::__is_reverse_view<remove_cvref_t<_Range>>
4324 || __detail::__is_reversible_subrange<remove_cvref_t<_Range>>
4325 || __detail::__can_reverse_view<_Range>
4326 constexpr auto
4327 operator() [[nodiscard]] (_Range&& __r) const
4328 {
4329 using _Tp = remove_cvref_t<_Range>;
4330 if constexpr (__detail::__is_reverse_view<_Tp>)
4331 return std::forward<_Range>(__r).base();
4332#ifdef __cpp_lib_optional_range_support // >= C++26
4333 else if constexpr (__is_optional_v<_Tp> && view<_Tp>)
4334 return std::forward<_Range>(__r);
4335#endif
4336 else if constexpr (__detail::__is_reversible_subrange<_Tp>)
4337 {
4338 using _Iter = decltype(ranges::begin(__r).base());
4339 if constexpr (sized_range<_Tp>)
4340 return subrange<_Iter, _Iter, subrange_kind::sized>
4341 {__r.end().base(), __r.begin().base(), __r.size()};
4342 else
4343 return subrange<_Iter, _Iter, subrange_kind::unsized>
4344 {__r.end().base(), __r.begin().base()};
4345 }
4346 else
4347 return reverse_view{std::forward<_Range>(__r)};
4348 }
4349
4350 static constexpr bool _S_has_simple_call_op = true;
4351 };
4352
4353 inline constexpr _Reverse reverse;
4354 } // namespace views
4355
4356 namespace __detail
4357 {
4358#if __cpp_lib_tuple_like // >= C++23
4359 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4360 // 3797. elements_view insufficiently constrained
4361 template<typename _Tp, size_t _Nm>
4362 concept __has_tuple_element = __tuple_like<_Tp> && _Nm < tuple_size_v<_Tp>
4363 && requires(_Tp __t)
4364 {
4365 { std::get<_Nm>(__t) }
4366 -> convertible_to<const tuple_element_t<_Nm, _Tp>&>;
4367 };
4368#else
4369 template<typename _Tp, size_t _Nm>
4370 concept __has_tuple_element = requires(_Tp __t)
4371 {
4372 typename tuple_size<_Tp>::type;
4373 requires _Nm < tuple_size_v<_Tp>;
4374 typename tuple_element_t<_Nm, _Tp>;
4375 { std::get<_Nm>(__t) }
4376 -> convertible_to<const tuple_element_t<_Nm, _Tp>&>;
4377 };
4378#endif
4379
4380 template<typename _Tp, size_t _Nm>
4381 concept __returnable_element
4382 = is_reference_v<_Tp> || move_constructible<tuple_element_t<_Nm, _Tp>>;
4383 }
4384
4385 template<input_range _Vp, size_t _Nm>
4386 requires view<_Vp>
4387 && __detail::__has_tuple_element<range_value_t<_Vp>, _Nm>
4388 && __detail::__has_tuple_element<remove_reference_t<range_reference_t<_Vp>>,
4389 _Nm>
4390 && __detail::__returnable_element<range_reference_t<_Vp>, _Nm>
4391 class elements_view : public view_interface<elements_view<_Vp, _Nm>>
4392 {
4393 public:
4394 elements_view() requires default_initializable<_Vp> = default;
4395
4396 constexpr explicit
4397 elements_view(_Vp __base)
4398 : _M_base(std::move(__base))
4399 { }
4400
4401 constexpr _Vp
4402 base() const& requires copy_constructible<_Vp>
4403 { return _M_base; }
4404
4405 constexpr _Vp
4406 base() &&
4407 { return std::move(_M_base); }
4408
4409 constexpr auto
4410 begin() requires (!__detail::__simple_view<_Vp>)
4411 { return _Iterator<false>(ranges::begin(_M_base)); }
4412
4413 constexpr auto
4414 begin() const requires range<const _Vp>
4415 { return _Iterator<true>(ranges::begin(_M_base)); }
4416
4417 constexpr auto
4418 end() requires (!__detail::__simple_view<_Vp> && !common_range<_Vp>)
4419 { return _Sentinel<false>{ranges::end(_M_base)}; }
4420
4421 constexpr auto
4422 end() requires (!__detail::__simple_view<_Vp> && common_range<_Vp>)
4423 { return _Iterator<false>{ranges::end(_M_base)}; }
4424
4425 constexpr auto
4426 end() const requires range<const _Vp>
4427 { return _Sentinel<true>{ranges::end(_M_base)}; }
4428
4429 constexpr auto
4430 end() const requires common_range<const _Vp>
4431 { return _Iterator<true>{ranges::end(_M_base)}; }
4432
4433 constexpr auto
4434 size() requires sized_range<_Vp>
4435 { return ranges::size(_M_base); }
4436
4437 constexpr auto
4438 size() const requires sized_range<const _Vp>
4439 { return ranges::size(_M_base); }
4440
4441 private:
4442 template<bool _Const>
4443 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
4444
4445 template<bool _Const>
4446 struct __iter_cat
4447 { };
4448
4449 template<bool _Const>
4450 requires forward_range<_Base<_Const>>
4451 struct __iter_cat<_Const>
4452 {
4453 private:
4454 static auto _S_iter_cat()
4455 {
4456 using _Base = elements_view::_Base<_Const>;
4457 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
4458 using _Res = decltype((std::get<_Nm>(*std::declval<iterator_t<_Base>>())));
4459 if constexpr (!is_lvalue_reference_v<_Res>)
4460 return input_iterator_tag{};
4461 else if constexpr (derived_from<_Cat, random_access_iterator_tag>)
4462 return random_access_iterator_tag{};
4463 else
4464 return _Cat{};
4465 }
4466 public:
4467 using iterator_category = decltype(_S_iter_cat());
4468 };
4469
4470 template<bool _Const>
4471 struct _Sentinel;
4472
4473 template<bool _Const>
4474 struct _Iterator : __iter_cat<_Const>
4475 {
4476 private:
4477 using _Base = elements_view::_Base<_Const>;
4478
4479 iterator_t<_Base> _M_current = iterator_t<_Base>();
4480
4481 static constexpr decltype(auto)
4482 _S_get_element(const iterator_t<_Base>& __i)
4483 {
4484 if constexpr (is_reference_v<range_reference_t<_Base>>)
4485 return std::get<_Nm>(*__i);
4486 else
4487 {
4488 using _Et = remove_cv_t<tuple_element_t<_Nm, range_reference_t<_Base>>>;
4489 return static_cast<_Et>(std::get<_Nm>(*__i));
4490 }
4491 }
4492
4493 static auto
4494 _S_iter_concept()
4495 {
4496 if constexpr (random_access_range<_Base>)
4497 return random_access_iterator_tag{};
4498 else if constexpr (bidirectional_range<_Base>)
4499 return bidirectional_iterator_tag{};
4500 else if constexpr (forward_range<_Base>)
4501 return forward_iterator_tag{};
4502 else
4503 return input_iterator_tag{};
4504 }
4505
4506 friend _Iterator<!_Const>;
4507
4508 public:
4509 using iterator_concept = decltype(_S_iter_concept());
4510 // iterator_category defined in elements_view::__iter_cat
4511 using value_type
4512 = remove_cvref_t<tuple_element_t<_Nm, range_value_t<_Base>>>;
4513 using difference_type = range_difference_t<_Base>;
4514
4515 _Iterator() requires default_initializable<iterator_t<_Base>> = default;
4516
4517 constexpr explicit
4518 _Iterator(iterator_t<_Base> __current)
4519 : _M_current(std::move(__current))
4520 { }
4521
4522 constexpr
4523 _Iterator(_Iterator<!_Const> __i)
4524 requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
4525 : _M_current(std::move(__i._M_current))
4526 { }
4527
4528 constexpr const iterator_t<_Base>&
4529 base() const& noexcept
4530 { return _M_current; }
4531
4532 constexpr iterator_t<_Base>
4533 base() &&
4534 { return std::move(_M_current); }
4535
4536 constexpr decltype(auto)
4537 operator*() const
4538 { return _S_get_element(_M_current); }
4539
4540 constexpr _Iterator&
4541 operator++()
4542 {
4543 ++_M_current;
4544 return *this;
4545 }
4546
4547 constexpr void
4548 operator++(int)
4549 { ++_M_current; }
4550
4551 constexpr _Iterator
4552 operator++(int) requires forward_range<_Base>
4553 {
4554 auto __tmp = *this;
4555 ++_M_current;
4556 return __tmp;
4557 }
4558
4559 constexpr _Iterator&
4560 operator--() requires bidirectional_range<_Base>
4561 {
4562 --_M_current;
4563 return *this;
4564 }
4565
4566 constexpr _Iterator
4567 operator--(int) requires bidirectional_range<_Base>
4568 {
4569 auto __tmp = *this;
4570 --_M_current;
4571 return __tmp;
4572 }
4573
4574 constexpr _Iterator&
4575 operator+=(difference_type __n)
4576 requires random_access_range<_Base>
4577 {
4578 _M_current += __n;
4579 return *this;
4580 }
4581
4582 constexpr _Iterator&
4583 operator-=(difference_type __n)
4584 requires random_access_range<_Base>
4585 {
4586 _M_current -= __n;
4587 return *this;
4588 }
4589
4590 constexpr decltype(auto)
4591 operator[](difference_type __n) const
4592 requires random_access_range<_Base>
4593 { return _S_get_element(_M_current + __n); }
4594
4595 friend constexpr bool
4596 operator==(const _Iterator& __x, const _Iterator& __y)
4597 requires equality_comparable<iterator_t<_Base>>
4598 { return __x._M_current == __y._M_current; }
4599
4600 friend constexpr bool
4601 operator<(const _Iterator& __x, const _Iterator& __y)
4602 requires random_access_range<_Base>
4603 { return __x._M_current < __y._M_current; }
4604
4605 friend constexpr bool
4606 operator>(const _Iterator& __x, const _Iterator& __y)
4607 requires random_access_range<_Base>
4608 { return __y._M_current < __x._M_current; }
4609
4610 friend constexpr bool
4611 operator<=(const _Iterator& __x, const _Iterator& __y)
4612 requires random_access_range<_Base>
4613 { return !(__y._M_current > __x._M_current); }
4614
4615 friend constexpr bool
4616 operator>=(const _Iterator& __x, const _Iterator& __y)
4617 requires random_access_range<_Base>
4618 { return !(__x._M_current > __y._M_current); }
4619
4620#ifdef __cpp_lib_three_way_comparison
4621 friend constexpr auto
4622 operator<=>(const _Iterator& __x, const _Iterator& __y)
4623 requires random_access_range<_Base>
4624 && three_way_comparable<iterator_t<_Base>>
4625 { return __x._M_current <=> __y._M_current; }
4626#endif
4627
4628 friend constexpr _Iterator
4629 operator+(const _Iterator& __x, difference_type __y)
4630 requires random_access_range<_Base>
4631 { return _Iterator{__x} += __y; }
4632
4633 friend constexpr _Iterator
4634 operator+(difference_type __x, const _Iterator& __y)
4635 requires random_access_range<_Base>
4636 { return __y + __x; }
4637
4638 friend constexpr _Iterator
4639 operator-(const _Iterator& __x, difference_type __y)
4640 requires random_access_range<_Base>
4641 { return _Iterator{__x} -= __y; }
4642
4643 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4644 // 3483. transform_view::iterator's difference is overconstrained
4645 friend constexpr difference_type
4646 operator-(const _Iterator& __x, const _Iterator& __y)
4647 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
4648 { return __x._M_current - __y._M_current; }
4649
4650 template <bool> friend struct _Sentinel;
4651 };
4652
4653 template<bool _Const>
4654 struct _Sentinel
4655 {
4656 private:
4657 using _Base = elements_view::_Base<_Const>;
4658 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
4659
4660 public:
4661 _Sentinel() = default;
4662
4663 constexpr explicit
4664 _Sentinel(sentinel_t<_Base> __end)
4665 : _M_end(std::move(__end))
4666 { }
4667
4668 constexpr
4669 _Sentinel(_Sentinel<!_Const> __other)
4670 requires _Const
4671 && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
4672 : _M_end(std::move(__other._M_end))
4673 { }
4674
4675 constexpr sentinel_t<_Base>
4676 base() const
4677 { return _M_end; }
4678
4679 template<bool _Const2>
4680 requires sentinel_for<sentinel_t<_Base>,
4681 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
4682 friend constexpr bool
4683 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
4684 { return __x._M_current == __y._M_end; }
4685
4686 template<bool _Const2,
4687 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
4688 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
4689 friend constexpr range_difference_t<_Base2>
4690 operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
4691 { return -(__y._M_end - __x._M_current); }
4692
4693 template<bool _Const2,
4694 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
4695 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
4696 friend constexpr range_difference_t<_Base2>
4697 operator-(const _Sentinel& __x, const _Iterator<_Const2>& __y)
4698 { return __x._M_end - __y._M_current; }
4699
4700 friend _Sentinel<!_Const>;
4701 };
4702
4703 _Vp _M_base = _Vp();
4704 };
4705
4706 template<typename _Tp, size_t _Nm>
4707 inline constexpr bool enable_borrowed_range<elements_view<_Tp, _Nm>>
4708 = enable_borrowed_range<_Tp>;
4709
4710 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4711 // 3563. keys_view example is broken
4712 template<typename _Range>
4713 using keys_view = elements_view<_Range, 0>;
4714
4715 template<typename _Range>
4716 using values_view = elements_view<_Range, 1>;
4717
4718 namespace views
4719 {
4720 namespace __detail
4721 {
4722 template<size_t _Nm, typename _Range>
4723 concept __can_elements_view
4724 = requires { elements_view<all_t<_Range>, _Nm>{std::declval<_Range>()}; };
4725 } // namespace __detail
4726
4727 template<size_t _Nm>
4728 struct _Elements : __adaptor::_RangeAdaptorClosure<_Elements<_Nm>>
4729 {
4730 template<viewable_range _Range>
4731 requires __detail::__can_elements_view<_Nm, _Range>
4732 constexpr auto
4733 operator() [[nodiscard]] (_Range&& __r) const
4734 {
4735 return elements_view<all_t<_Range>, _Nm>{std::forward<_Range>(__r)};
4736 }
4737
4738 static constexpr bool _S_has_simple_call_op = true;
4739 };
4740
4741 template<size_t _Nm>
4742 inline constexpr _Elements<_Nm> elements;
4743 inline constexpr auto keys = elements<0>;
4744 inline constexpr auto values = elements<1>;
4745 } // namespace views
4746
4747#ifdef __cpp_lib_ranges_zip // C++ >= 23
4748 namespace __detail
4749 {
4750 template<typename... _Rs>
4751 concept __zip_is_common = (sizeof...(_Rs) == 1 && (common_range<_Rs> && ...))
4752 || (!(bidirectional_range<_Rs> && ...) && (common_range<_Rs> && ...))
4753 || ((random_access_range<_Rs> && ...) && (sized_range<_Rs> && ...));
4754
4755 template<typename _Fp, typename _Tuple>
4756 constexpr auto
4757 __tuple_transform(_Fp&& __f, _Tuple&& __tuple)
4758 {
4759 return std::apply([&]<typename... _Ts>(_Ts&&... __elts) {
4760 return tuple<invoke_result_t<_Fp&, _Ts>...>
4761 (std::__invoke(__f, std::forward<_Ts>(__elts))...);
4762 }, std::forward<_Tuple>(__tuple));
4763 }
4764
4765 template<typename _Fp, typename _Tuple>
4766 constexpr void
4767 __tuple_for_each(_Fp&& __f, _Tuple&& __tuple)
4768 {
4769 std::apply([&]<typename... _Ts>(_Ts&&... __elts) {
4770 (std::__invoke(__f, std::forward<_Ts>(__elts)), ...);
4771 }, std::forward<_Tuple>(__tuple));
4772 }
4773 } // namespace __detail
4774
4775 template<input_range... _Vs>
4776 requires (view<_Vs> && ...) && (sizeof...(_Vs) > 0)
4777 class zip_view : public view_interface<zip_view<_Vs...>>
4778 {
4779 tuple<_Vs...> _M_views;
4780
4781 template<bool> class _Iterator;
4782 template<bool> class _Sentinel;
4783
4784 public:
4785 zip_view() = default;
4786
4787 constexpr explicit
4788 zip_view(_Vs... __views)
4789 : _M_views(std::move(__views)...)
4790 { }
4791
4792 constexpr auto
4793 begin() requires (!(__detail::__simple_view<_Vs> && ...))
4794 { return _Iterator<false>(__detail::__tuple_transform(ranges::begin, _M_views)); }
4795
4796 constexpr auto
4797 begin() const requires (range<const _Vs> && ...)
4798 { return _Iterator<true>(__detail::__tuple_transform(ranges::begin, _M_views)); }
4799
4800 constexpr auto
4801 end() requires (!(__detail::__simple_view<_Vs> && ...))
4802 {
4803 if constexpr (!__detail::__zip_is_common<_Vs...>)
4804 return _Sentinel<false>(__detail::__tuple_transform(ranges::end, _M_views));
4805 else if constexpr ((random_access_range<_Vs> && ...))
4806 return begin() + iter_difference_t<_Iterator<false>>(size());
4807 else
4808 return _Iterator<false>(__detail::__tuple_transform(ranges::end, _M_views));
4809 }
4810
4811 constexpr auto
4812 end() const requires (range<const _Vs> && ...)
4813 {
4814 if constexpr (!__detail::__zip_is_common<const _Vs...>)
4815 return _Sentinel<true>(__detail::__tuple_transform(ranges::end, _M_views));
4816 else if constexpr ((random_access_range<const _Vs> && ...))
4817 return begin() + iter_difference_t<_Iterator<true>>(size());
4818 else
4819 return _Iterator<true>(__detail::__tuple_transform(ranges::end, _M_views));
4820 }
4821
4822 constexpr auto
4823 size() requires (sized_range<_Vs> && ...)
4824 {
4825 return std::apply([](auto... __sizes) {
4826 using _CT = __detail::__make_unsigned_like_t<common_type_t<decltype(__sizes)...>>;
4827 return ranges::min({_CT(__sizes)...});
4828 }, __detail::__tuple_transform(ranges::size, _M_views));
4829 }
4830
4831 constexpr auto
4832 size() const requires (sized_range<const _Vs> && ...)
4833 {
4834 return std::apply([](auto... __sizes) {
4835 using _CT = __detail::__make_unsigned_like_t<common_type_t<decltype(__sizes)...>>;
4836 return ranges::min({_CT(__sizes)...});
4837 }, __detail::__tuple_transform(ranges::size, _M_views));
4838 }
4839 };
4840
4841 template<typename... _Rs>
4842 zip_view(_Rs&&...) -> zip_view<views::all_t<_Rs>...>;
4843
4844 template<typename... _Views>
4845 inline constexpr bool enable_borrowed_range<zip_view<_Views...>>
4846 = (enable_borrowed_range<_Views> && ...);
4847
4848 namespace __detail
4849 {
4850 template<bool _Const, typename... _Vs>
4851 concept __all_random_access
4852 = (random_access_range<__maybe_const_t<_Const, _Vs>> && ...);
4853
4854 template<bool _Const, typename... _Vs>
4855 concept __all_bidirectional
4856 = (bidirectional_range<__maybe_const_t<_Const, _Vs>> && ...);
4857
4858 template<bool _Const, typename... _Vs>
4859 concept __all_forward
4860 = (forward_range<__maybe_const_t<_Const, _Vs>> && ...);
4861
4862 template<bool _Const, typename... _Views>
4863 struct __zip_view_iter_cat
4864 { };
4865
4866 template<bool _Const, typename... _Views>
4867 requires __all_forward<_Const, _Views...>
4868 struct __zip_view_iter_cat<_Const, _Views...>
4869 { using iterator_category = input_iterator_tag; };
4870 } // namespace __detail
4871
4872 template<input_range... _Vs>
4873 requires (view<_Vs> && ...) && (sizeof...(_Vs) > 0)
4874 template<bool _Const>
4875 class zip_view<_Vs...>::_Iterator
4876 : public __detail::__zip_view_iter_cat<_Const, _Vs...>
4877 {
4878#ifdef _GLIBCXX_CLANG // LLVM-61763 workaround
4879 public:
4880#endif
4881 tuple<iterator_t<__detail::__maybe_const_t<_Const, _Vs>>...> _M_current;
4882
4883 constexpr explicit
4884 _Iterator(decltype(_M_current) __current)
4885 : _M_current(std::move(__current))
4886 { }
4887
4888 static auto
4889 _S_iter_concept()
4890 {
4891 if constexpr (__detail::__all_random_access<_Const, _Vs...>)
4892 return random_access_iterator_tag{};
4893 else if constexpr (__detail::__all_bidirectional<_Const, _Vs...>)
4894 return bidirectional_iterator_tag{};
4895 else if constexpr (__detail::__all_forward<_Const, _Vs...>)
4896 return forward_iterator_tag{};
4897 else
4898 return input_iterator_tag{};
4899 }
4900
4901#ifndef _GLIBCXX_CLANG // LLVM-61763 workaround
4902 template<move_constructible _Fp, input_range... _Ws>
4903 requires (view<_Ws> && ...) && (sizeof...(_Ws) > 0) && is_object_v<_Fp>
4904 && regular_invocable<_Fp&, range_reference_t<_Ws>...>
4905 && std::__detail::__can_reference<invoke_result_t<_Fp&, range_reference_t<_Ws>...>>
4906 friend class zip_transform_view;
4907#endif
4908
4909 public:
4910 // iterator_category defined in __zip_view_iter_cat
4911 using iterator_concept = decltype(_S_iter_concept());
4912 using value_type
4913 = tuple<range_value_t<__detail::__maybe_const_t<_Const, _Vs>>...>;
4914 using difference_type
4915 = common_type_t<range_difference_t<__detail::__maybe_const_t<_Const, _Vs>>...>;
4916
4917 _Iterator() = default;
4918
4919 constexpr
4920 _Iterator(_Iterator<!_Const> __i)
4921 requires _Const
4922 && (convertible_to<iterator_t<_Vs>,
4923 iterator_t<__detail::__maybe_const_t<_Const, _Vs>>> && ...)
4924 : _M_current(std::move(__i._M_current))
4925 { }
4926
4927 constexpr auto
4928 operator*() const
4929 {
4930 auto __f = [](auto& __i) -> decltype(auto) {
4931 return *__i;
4932 };
4933 return __detail::__tuple_transform(__f, _M_current);
4934 }
4935
4936 constexpr _Iterator&
4937 operator++()
4938 {
4939 __detail::__tuple_for_each([](auto& __i) { ++__i; }, _M_current);
4940 return *this;
4941 }
4942
4943 constexpr void
4944 operator++(int)
4945 { ++*this; }
4946
4947 constexpr _Iterator
4948 operator++(int)
4949 requires __detail::__all_forward<_Const, _Vs...>
4950 {
4951 auto __tmp = *this;
4952 ++*this;
4953 return __tmp;
4954 }
4955
4956 constexpr _Iterator&
4957 operator--()
4958 requires __detail::__all_bidirectional<_Const, _Vs...>
4959 {
4960 __detail::__tuple_for_each([](auto& __i) { --__i; }, _M_current);
4961 return *this;
4962 }
4963
4964 constexpr _Iterator
4965 operator--(int)
4966 requires __detail::__all_bidirectional<_Const, _Vs...>
4967 {
4968 auto __tmp = *this;
4969 --*this;
4970 return __tmp;
4971 }
4972
4973 constexpr _Iterator&
4974 operator+=(difference_type __x)
4975 requires __detail::__all_random_access<_Const, _Vs...>
4976 {
4977 auto __f = [&]<typename _It>(_It& __i) {
4978 __i += iter_difference_t<_It>(__x);
4979 };
4980 __detail::__tuple_for_each(__f, _M_current);
4981 return *this;
4982 }
4983
4984 constexpr _Iterator&
4985 operator-=(difference_type __x)
4986 requires __detail::__all_random_access<_Const, _Vs...>
4987 {
4988 auto __f = [&]<typename _It>(_It& __i) {
4989 __i -= iter_difference_t<_It>(__x);
4990 };
4991 __detail::__tuple_for_each(__f, _M_current);
4992 return *this;
4993 }
4994
4995 constexpr auto
4996 operator[](difference_type __n) const
4997 requires __detail::__all_random_access<_Const, _Vs...>
4998 {
4999 auto __f = [&]<typename _It>(_It& __i) -> decltype(auto) {
5000 return __i[iter_difference_t<_It>(__n)];
5001 };
5002 return __detail::__tuple_transform(__f, _M_current);
5003 }
5004
5005 friend constexpr bool
5006 operator==(const _Iterator& __x, const _Iterator& __y)
5007 requires (equality_comparable<iterator_t<__detail::__maybe_const_t<_Const, _Vs>>> && ...)
5008 {
5009 if constexpr (__detail::__all_bidirectional<_Const, _Vs...>)
5010 return __x._M_current == __y._M_current;
5011 else
5012 return [&]<size_t... _Is>(index_sequence<_Is...>) {
5013 return ((std::get<_Is>(__x._M_current) == std::get<_Is>(__y._M_current)) || ...);
5014 }(make_index_sequence<sizeof...(_Vs)>{});
5015 }
5016
5017 friend constexpr auto
5018 operator<=>(const _Iterator& __x, const _Iterator& __y)
5019 requires __detail::__all_random_access<_Const, _Vs...>
5020 { return __x._M_current <=> __y._M_current; }
5021
5022 friend constexpr _Iterator
5023 operator+(const _Iterator& __i, difference_type __n)
5024 requires __detail::__all_random_access<_Const, _Vs...>
5025 {
5026 auto __r = __i;
5027 __r += __n;
5028 return __r;
5029 }
5030
5031 friend constexpr _Iterator
5032 operator+(difference_type __n, const _Iterator& __i)
5033 requires __detail::__all_random_access<_Const, _Vs...>
5034 {
5035 auto __r = __i;
5036 __r += __n;
5037 return __r;
5038 }
5039
5040 friend constexpr _Iterator
5041 operator-(const _Iterator& __i, difference_type __n)
5042 requires __detail::__all_random_access<_Const, _Vs...>
5043 {
5044 auto __r = __i;
5045 __r -= __n;
5046 return __r;
5047 }
5048
5049 friend constexpr difference_type
5050 operator-(const _Iterator& __x, const _Iterator& __y)
5051 requires (sized_sentinel_for<iterator_t<__detail::__maybe_const_t<_Const, _Vs>>,
5052 iterator_t<__detail::__maybe_const_t<_Const, _Vs>>> && ...)
5053 {
5054 return [&]<size_t... _Is>(index_sequence<_Is...>) {
5055 return ranges::min({difference_type(std::get<_Is>(__x._M_current)
5056 - std::get<_Is>(__y._M_current))...},
5057 ranges::less{},
5058 [](difference_type __i) {
5059 return __detail::__to_unsigned_like(__i < 0 ? -__i : __i);
5060 });
5061 }(make_index_sequence<sizeof...(_Vs)>{});
5062 }
5063
5064 friend constexpr auto
5065 iter_move(const _Iterator& __i)
5066 { return __detail::__tuple_transform(ranges::iter_move, __i._M_current); }
5067
5068 friend constexpr void
5069 iter_swap(const _Iterator& __l, const _Iterator& __r)
5070 requires (indirectly_swappable<iterator_t<__detail::__maybe_const_t<_Const, _Vs>>> && ...)
5071 {
5072 [&]<size_t... _Is>(index_sequence<_Is...>) {
5073 (ranges::iter_swap(std::get<_Is>(__l._M_current), std::get<_Is>(__r._M_current)), ...);
5074 }(make_index_sequence<sizeof...(_Vs)>{});
5075 }
5076
5077 friend class zip_view;
5078 };
5079
5080 template<input_range... _Vs>
5081 requires (view<_Vs> && ...) && (sizeof...(_Vs) > 0)
5082 template<bool _Const>
5083 class zip_view<_Vs...>::_Sentinel
5084 {
5085 tuple<sentinel_t<__detail::__maybe_const_t<_Const, _Vs>>...> _M_end;
5086
5087 constexpr explicit
5088 _Sentinel(decltype(_M_end) __end)
5089 : _M_end(__end)
5090 { }
5091
5092 friend class zip_view;
5093
5094 public:
5095 _Sentinel() = default;
5096
5097 constexpr
5098 _Sentinel(_Sentinel<!_Const> __i)
5099 requires _Const
5100 && (convertible_to<sentinel_t<_Vs>,
5101 sentinel_t<__detail::__maybe_const_t<_Const, _Vs>>> && ...)
5102 : _M_end(std::move(__i._M_end))
5103 { }
5104
5105 template<bool _OtherConst>
5106 requires (sentinel_for<sentinel_t<__detail::__maybe_const_t<_Const, _Vs>>,
5107 iterator_t<__detail::__maybe_const_t<_OtherConst, _Vs>>> && ...)
5108 friend constexpr bool
5109 operator==(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
5110 {
5111 return [&]<size_t... _Is>(index_sequence<_Is...>) {
5112 return ((std::get<_Is>(__x._M_current) == std::get<_Is>(__y._M_end)) || ...);
5113 }(make_index_sequence<sizeof...(_Vs)>{});
5114 }
5115
5116 template<bool _OtherConst>
5117 requires (sized_sentinel_for<sentinel_t<__detail::__maybe_const_t<_Const, _Vs>>,
5118 iterator_t<__detail::__maybe_const_t<_OtherConst, _Vs>>> && ...)
5119 friend constexpr auto
5120 operator-(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
5121 {
5122 using _Ret
5123 = common_type_t<range_difference_t<__detail::__maybe_const_t<_OtherConst, _Vs>>...>;
5124 return [&]<size_t... _Is>(index_sequence<_Is...>) {
5125 return ranges::min({_Ret(std::get<_Is>(__x._M_current) - std::get<_Is>(__y._M_end))...},
5126 ranges::less{},
5127 [](_Ret __i) {
5128 return __detail::__to_unsigned_like(__i < 0 ? -__i : __i);
5129 });
5130 }(make_index_sequence<sizeof...(_Vs)>{});
5131 }
5132
5133 template<bool _OtherConst>
5134 requires (sized_sentinel_for<sentinel_t<__detail::__maybe_const_t<_Const, _Vs>>,
5135 iterator_t<__detail::__maybe_const_t<_OtherConst, _Vs>>> && ...)
5136 friend constexpr auto
5137 operator-(const _Sentinel& __y, const _Iterator<_OtherConst>& __x)
5138 { return -(__x - __y); }
5139 };
5140
5141 namespace views
5142 {
5143 namespace __detail
5144 {
5145 template<typename... _Ts>
5146 concept __can_zip_view
5147 = requires { zip_view<all_t<_Ts>...>(std::declval<_Ts>()...); };
5148 }
5149
5150 struct _Zip
5151 {
5152 template<typename... _Ts>
5153 requires (sizeof...(_Ts) == 0 || __detail::__can_zip_view<_Ts...>)
5154 constexpr auto
5155 operator() [[nodiscard]] (_Ts&&... __ts) const
5156 {
5157 if constexpr (sizeof...(_Ts) == 0)
5158 return views::empty<tuple<>>;
5159 else
5160 return zip_view<all_t<_Ts>...>(std::forward<_Ts>(__ts)...);
5161 }
5162 };
5163
5164 inline constexpr _Zip zip;
5165 }
5166
5167 namespace __detail
5168 {
5169 template<typename _Range, bool _Const>
5170 using __range_iter_cat
5171 = typename iterator_traits<iterator_t<__maybe_const_t<_Const, _Range>>>::iterator_category;
5172 }
5173
5174 template<move_constructible _Fp, input_range... _Vs>
5175 requires (view<_Vs> && ...) && (sizeof...(_Vs) > 0) && is_object_v<_Fp>
5176 && regular_invocable<_Fp&, range_reference_t<_Vs>...>
5177 && std::__detail::__can_reference<invoke_result_t<_Fp&, range_reference_t<_Vs>...>>
5178 class zip_transform_view : public view_interface<zip_transform_view<_Fp, _Vs...>>
5179 {
5180 [[no_unique_address]] __detail::__box<_Fp> _M_fun;
5181 zip_view<_Vs...> _M_zip;
5182
5183 using _InnerView = zip_view<_Vs...>;
5184
5185 template<bool _Const>
5186 using __ziperator = iterator_t<__detail::__maybe_const_t<_Const, _InnerView>>;
5187
5188 template<bool _Const>
5189 using __zentinel = sentinel_t<__detail::__maybe_const_t<_Const, _InnerView>>;
5190
5191 template<bool _Const>
5192 using _Base = __detail::__maybe_const_t<_Const, _InnerView>;
5193
5194 template<bool _Const>
5195 struct __iter_cat
5196 { };
5197
5198 template<bool _Const>
5199 requires forward_range<_Base<_Const>>
5200 struct __iter_cat<_Const>
5201 {
5202 private:
5203 static auto
5204 _S_iter_cat()
5205 {
5206 using __detail::__maybe_const_t;
5207 using __detail::__range_iter_cat;
5208 using _Res = invoke_result_t<__maybe_const_t<_Const, _Fp>&,
5209 range_reference_t<__maybe_const_t<_Const, _Vs>>...>;
5210 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5211 // 3798. Rvalue reference and iterator_category
5212 if constexpr (!is_reference_v<_Res>)
5213 return input_iterator_tag{};
5214 else if constexpr ((derived_from<__range_iter_cat<_Vs, _Const>,
5215 random_access_iterator_tag> && ...))
5216 return random_access_iterator_tag{};
5217 else if constexpr ((derived_from<__range_iter_cat<_Vs, _Const>,
5218 bidirectional_iterator_tag> && ...))
5219 return bidirectional_iterator_tag{};
5220 else if constexpr ((derived_from<__range_iter_cat<_Vs, _Const>,
5221 forward_iterator_tag> && ...))
5222 return forward_iterator_tag{};
5223 else
5224 return input_iterator_tag{};
5225 }
5226 public:
5227 using iterator_category = decltype(_S_iter_cat());
5228 };
5229
5230 template<bool> class _Iterator;
5231 template<bool> class _Sentinel;
5232
5233 public:
5234 zip_transform_view() = default;
5235
5236 constexpr explicit
5237 zip_transform_view(_Fp __fun, _Vs... __views)
5238 : _M_fun(std::move(__fun)), _M_zip(std::move(__views)...)
5239 { }
5240
5241 constexpr auto
5242 begin()
5243 { return _Iterator<false>(*this, _M_zip.begin()); }
5244
5245 constexpr auto
5246 begin() const
5247 requires range<const _InnerView>
5248 && regular_invocable<const _Fp&, range_reference_t<const _Vs>...>
5249 { return _Iterator<true>(*this, _M_zip.begin()); }
5250
5251 constexpr auto
5252 end()
5253 {
5254 if constexpr (common_range<_InnerView>)
5255 return _Iterator<false>(*this, _M_zip.end());
5256 else
5257 return _Sentinel<false>(_M_zip.end());
5258 }
5259
5260 constexpr auto
5261 end() const
5262 requires range<const _InnerView>
5263 && regular_invocable<const _Fp&, range_reference_t<const _Vs>...>
5264 {
5265 if constexpr (common_range<const _InnerView>)
5266 return _Iterator<true>(*this, _M_zip.end());
5267 else
5268 return _Sentinel<true>(_M_zip.end());
5269 }
5270
5271 constexpr auto
5272 size() requires sized_range<_InnerView>
5273 { return _M_zip.size(); }
5274
5275 constexpr auto
5276 size() const requires sized_range<const _InnerView>
5277 { return _M_zip.size(); }
5278 };
5279
5280 template<class _Fp, class... _Rs>
5281 zip_transform_view(_Fp, _Rs&&...)
5282 -> zip_transform_view<_Fp, views::all_t<_Rs>...>;
5283
5284 template<move_constructible _Fp, input_range... _Vs>
5285 requires (view<_Vs> && ...) && (sizeof...(_Vs) > 0) && is_object_v<_Fp>
5286 && regular_invocable<_Fp&, range_reference_t<_Vs>...>
5287 && std::__detail::__can_reference<invoke_result_t<_Fp&, range_reference_t<_Vs>...>>
5288 template<bool _Const>
5289 class zip_transform_view<_Fp, _Vs...>::_Iterator : public __iter_cat<_Const>
5290 {
5291 using _Parent = __detail::__maybe_const_t<_Const, zip_transform_view>;
5292 using _Fun_handle = __detail::__func_handle_t<
5293 __detail::__maybe_const_t<_Const, _Fp>,
5294 iterator_t<__detail::__maybe_const_t<_Const, _Vs>>...>;
5295
5296 [[no_unique_address]] _Fun_handle _M_fun;
5297 __ziperator<_Const> _M_inner;
5298
5299 constexpr
5300 _Iterator(_Fun_handle __fun, __ziperator<_Const> __inner)
5301 : _M_fun(__fun), _M_inner(std::move(__inner))
5302 { }
5303
5304 constexpr
5305 _Iterator(_Parent& __parent, __ziperator<_Const> __inner)
5306 : _M_fun(*__parent._M_fun), _M_inner(std::move(__inner))
5307 { }
5308
5309 friend class zip_transform_view;
5310
5311 public:
5312 // iterator_category defined in zip_transform_view::__iter_cat
5313 using iterator_concept = typename __ziperator<_Const>::iterator_concept;
5314 using value_type
5315 = remove_cvref_t<invoke_result_t<__detail::__maybe_const_t<_Const, _Fp>&,
5316 range_reference_t<__detail::__maybe_const_t<_Const, _Vs>>...>>;
5317 using difference_type = range_difference_t<_Base<_Const>>;
5318
5319 _Iterator() = default;
5320
5321 constexpr
5322 _Iterator(_Iterator<!_Const> __i)
5323 requires _Const && convertible_to<__ziperator<false>, __ziperator<_Const>>
5324 : _M_fun(__i._M_fun), _M_inner(std::move(__i._M_inner))
5325 { }
5326
5327 constexpr decltype(auto)
5328 operator*() const
5329 {
5330 return std::apply([&](const auto&... __iters) -> decltype(auto) {
5331 return _M_fun._M_call_deref(__iters...);
5332 }, _M_inner._M_current);
5333 }
5334
5335 constexpr _Iterator&
5336 operator++()
5337 {
5338 ++_M_inner;
5339 return *this;
5340 }
5341
5342 constexpr void
5343 operator++(int)
5344 { ++*this; }
5345
5346 constexpr _Iterator
5347 operator++(int) requires forward_range<_Base<_Const>>
5348 {
5349 auto __tmp = *this;
5350 ++*this;
5351 return __tmp;
5352 }
5353
5354 constexpr _Iterator&
5355 operator--() requires bidirectional_range<_Base<_Const>>
5356 {
5357 --_M_inner;
5358 return *this;
5359 }
5360
5361 constexpr _Iterator
5362 operator--(int) requires bidirectional_range<_Base<_Const>>
5363 {
5364 auto __tmp = *this;
5365 --*this;
5366 return __tmp;
5367 }
5368
5369 constexpr _Iterator&
5370 operator+=(difference_type __x) requires random_access_range<_Base<_Const>>
5371 {
5372 _M_inner += __x;
5373 return *this;
5374 }
5375
5376 constexpr _Iterator&
5377 operator-=(difference_type __x) requires random_access_range<_Base<_Const>>
5378 {
5379 _M_inner -= __x;
5380 return *this;
5381 }
5382
5383 constexpr decltype(auto)
5384 operator[](difference_type __n) const requires random_access_range<_Base<_Const>>
5385 {
5386 return std::apply([&]<typename... _Is>(const _Is&... __iters) -> decltype(auto) {
5387 return _M_fun._M_call_subscript(__n, __iters...);
5388 }, _M_inner._M_current);
5389 }
5390
5391 friend constexpr bool
5392 operator==(const _Iterator& __x, const _Iterator& __y)
5393 requires equality_comparable<__ziperator<_Const>>
5394 { return __x._M_inner == __y._M_inner; }
5395
5396 friend constexpr auto
5397 operator<=>(const _Iterator& __x, const _Iterator& __y)
5398 requires random_access_range<_Base<_Const>>
5399 { return __x._M_inner <=> __y._M_inner; }
5400
5401 friend constexpr _Iterator
5402 operator+(const _Iterator& __i, difference_type __n)
5403 requires random_access_range<_Base<_Const>>
5404 { return _Iterator(__i._M_fun, __i._M_inner + __n); }
5405
5406 friend constexpr _Iterator
5407 operator+(difference_type __n, const _Iterator& __i)
5408 requires random_access_range<_Base<_Const>>
5409 { return _Iterator(__i._M_fun, __i._M_inner + __n); }
5410
5411 friend constexpr _Iterator
5412 operator-(const _Iterator& __i, difference_type __n)
5413 requires random_access_range<_Base<_Const>>
5414 { return _Iterator(__i._M_fun, __i._M_inner - __n); }
5415
5416 friend constexpr difference_type
5417 operator-(const _Iterator& __x, const _Iterator& __y)
5418 requires sized_sentinel_for<__ziperator<_Const>, __ziperator<_Const>>
5419 { return __x._M_inner - __y._M_inner; }
5420 };
5421
5422 template<move_constructible _Fp, input_range... _Vs>
5423 requires (view<_Vs> && ...) && (sizeof...(_Vs) > 0) && is_object_v<_Fp>
5424 && regular_invocable<_Fp&, range_reference_t<_Vs>...>
5425 && std::__detail::__can_reference<invoke_result_t<_Fp&, range_reference_t<_Vs>...>>
5426 template<bool _Const>
5427 class zip_transform_view<_Fp, _Vs...>::_Sentinel
5428 {
5429 __zentinel<_Const> _M_inner;
5430
5431 constexpr explicit
5432 _Sentinel(__zentinel<_Const> __inner)
5433 : _M_inner(__inner)
5434 { }
5435
5436 friend class zip_transform_view;
5437
5438 public:
5439 _Sentinel() = default;
5440
5441 constexpr
5442 _Sentinel(_Sentinel<!_Const> __i)
5443 requires _Const && convertible_to<__zentinel<false>, __zentinel<_Const>>
5444 : _M_inner(std::move(__i._M_inner))
5445 { }
5446
5447 template<bool _OtherConst>
5448 requires sentinel_for<__zentinel<_Const>, __ziperator<_OtherConst>>
5449 friend constexpr bool
5450 operator==(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
5451 { return __x._M_inner == __y._M_inner; }
5452
5453 template<bool _OtherConst>
5454 requires sized_sentinel_for<__zentinel<_Const>, __ziperator<_OtherConst>>
5455 friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _InnerView>>
5456 operator-(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
5457 { return __x._M_inner - __y._M_inner; }
5458
5459 template<bool _OtherConst>
5460 requires sized_sentinel_for<__zentinel<_Const>, __ziperator<_OtherConst>>
5461 friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _InnerView>>
5462 operator-(const _Sentinel& __x, const _Iterator<_OtherConst>& __y)
5463 { return __x._M_inner - __y._M_inner; }
5464 };
5465
5466 namespace views
5467 {
5468 namespace __detail
5469 {
5470 template<typename _Fp, typename... _Ts>
5471 concept __can_zip_transform_view
5472 = requires { zip_transform_view(std::declval<_Fp>(), std::declval<_Ts>()...); };
5473 }
5474
5475 struct _ZipTransform
5476 {
5477 template<typename _Fp>
5478 requires move_constructible<decay_t<_Fp>> && regular_invocable<decay_t<_Fp>&>
5479 && is_object_v<decay_t<invoke_result_t<decay_t<_Fp>&>>>
5480 constexpr auto
5481 operator() [[nodiscard]] (_Fp&&) const
5482 {
5483 return views::empty<decay_t<invoke_result_t<decay_t<_Fp>&>>>;
5484 }
5485
5486 template<typename _Fp, typename... _Ts>
5487 requires (sizeof...(_Ts) != 0) && __detail::__can_zip_transform_view<_Fp, _Ts...>
5488 constexpr auto
5489 operator() [[nodiscard]] (_Fp&& __f, _Ts&&... __ts) const
5490 {
5491 return zip_transform_view(std::forward<_Fp>(__f), std::forward<_Ts>(__ts)...);
5492 }
5493 };
5494
5495 inline constexpr _ZipTransform zip_transform;
5496 }
5497
5498 template<forward_range _Vp, size_t _Nm>
5499 requires view<_Vp> && (_Nm > 0)
5500 class adjacent_view : public view_interface<adjacent_view<_Vp, _Nm>>
5501 {
5502 _Vp _M_base = _Vp();
5503
5504 template<bool> class _Iterator;
5505 template<bool> class _Sentinel;
5506
5507 struct __as_sentinel
5508 { };
5509
5510 public:
5511 adjacent_view() requires default_initializable<_Vp> = default;
5512
5513 constexpr explicit
5514 adjacent_view(_Vp __base)
5515 : _M_base(std::move(__base))
5516 { }
5517
5518 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5519 // 3848. adjacent_view, adjacent_transform_view and slide_view missing base accessor
5520 constexpr _Vp
5521 base() const & requires copy_constructible<_Vp>
5522 { return _M_base; }
5523
5524 constexpr _Vp
5525 base() &&
5526 { return std::move(_M_base); }
5527
5528 constexpr auto
5529 begin() requires (!__detail::__simple_view<_Vp>)
5530 { return _Iterator<false>(ranges::begin(_M_base), ranges::end(_M_base)); }
5531
5532 constexpr auto
5533 begin() const requires range<const _Vp>
5534 { return _Iterator<true>(ranges::begin(_M_base), ranges::end(_M_base)); }
5535
5536 constexpr auto
5537 end() requires (!__detail::__simple_view<_Vp>)
5538 {
5539 if constexpr (common_range<_Vp>)
5540 return _Iterator<false>(__as_sentinel{}, ranges::begin(_M_base), ranges::end(_M_base));
5541 else
5542 return _Sentinel<false>(ranges::end(_M_base));
5543 }
5544
5545 constexpr auto
5546 end() const requires range<const _Vp>
5547 {
5548 if constexpr (common_range<const _Vp>)
5549 return _Iterator<true>(__as_sentinel{}, ranges::begin(_M_base), ranges::end(_M_base));
5550 else
5551 return _Sentinel<true>(ranges::end(_M_base));
5552 }
5553
5554 constexpr auto
5555 size() requires sized_range<_Vp>
5556 {
5557 using _ST = decltype(ranges::size(_M_base));
5558 using _CT = common_type_t<_ST, size_t>;
5559 auto __sz = static_cast<_CT>(ranges::size(_M_base));
5560 __sz -= std::min<_CT>(__sz, _Nm - 1);
5561 return static_cast<_ST>(__sz);
5562 }
5563
5564 constexpr auto
5565 size() const requires sized_range<const _Vp>
5566 {
5567 using _ST = decltype(ranges::size(_M_base));
5568 using _CT = common_type_t<_ST, size_t>;
5569 auto __sz = static_cast<_CT>(ranges::size(_M_base));
5570 __sz -= std::min<_CT>(__sz, _Nm - 1);
5571 return static_cast<_ST>(__sz);
5572 }
5573 };
5574
5575 template<typename _Vp, size_t _Nm>
5576 inline constexpr bool enable_borrowed_range<adjacent_view<_Vp, _Nm>>
5577 = enable_borrowed_range<_Vp>;
5578
5579 namespace __detail
5580 {
5581 // Yields tuple<_Tp, ..., _Tp> with _Nm elements.
5582 template<typename _Tp, size_t _Nm>
5583 using __repeated_tuple = typename __make_tuple<array<_Tp, _Nm>>::__type;
5584
5585 // For a functor F that is callable with N arguments, the expression
5586 // declval<__unarize<F, N>>(x) is equivalent to declval<F>(x, ..., x).
5587 template<typename _Fp, size_t _Nm>
5588 struct __unarize
5589 {
5590 template<typename... _Ts>
5591 static invoke_result_t<_Fp, _Ts...>
5592 __tuple_apply(const tuple<_Ts...>&); // not defined
5593
5594 template<typename _Tp>
5595 decltype(__tuple_apply(std::declval<__repeated_tuple<_Tp, _Nm>>()))
5596 operator()(_Tp&&); // not defined
5597 };
5598 }
5599
5600 template<forward_range _Vp, size_t _Nm>
5601 requires view<_Vp> && (_Nm > 0)
5602 template<bool _Const>
5603 class adjacent_view<_Vp, _Nm>::_Iterator
5604 {
5605#ifdef _GLIBCXX_CLANG // LLVM-61763 workaround
5606 public:
5607#endif
5608 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
5609 array<iterator_t<_Base>, _Nm> _M_current = array<iterator_t<_Base>, _Nm>();
5610
5611 constexpr
5612 _Iterator(iterator_t<_Base> __first, sentinel_t<_Base> __last)
5613 {
5614 for (auto& __i : _M_current)
5615 {
5616 __i = __first;
5617 ranges::advance(__first, 1, __last);
5618 }
5619 }
5620
5621 constexpr
5622 _Iterator(__as_sentinel, iterator_t<_Base> __first, iterator_t<_Base> __last)
5623 {
5624 if constexpr (!bidirectional_range<_Base>)
5625 for (auto& __it : _M_current)
5626 __it = __last;
5627 else
5628 for (size_t __i = 0; __i < _Nm; ++__i)
5629 {
5630 _M_current[_Nm - 1 - __i] = __last;
5631 ranges::advance(__last, -1, __first);
5632 }
5633 }
5634
5635 static auto
5636 _S_iter_concept()
5637 {
5638 if constexpr (random_access_range<_Base>)
5639 return random_access_iterator_tag{};
5640 else if constexpr (bidirectional_range<_Base>)
5641 return bidirectional_iterator_tag{};
5642 else
5643 return forward_iterator_tag{};
5644 }
5645
5646 friend class adjacent_view;
5647
5648#ifndef _GLIBCXX_CLANG // LLVM-61763 workaround
5649 template<forward_range _Wp, move_constructible _Fp, size_t _Mm>
5650 requires view<_Wp> && (_Mm > 0) && is_object_v<_Fp>
5651 && regular_invocable<__detail::__unarize<_Fp&, _Mm>, range_reference_t<_Wp>>
5652 && std::__detail::__can_reference<invoke_result_t<__detail::__unarize<_Fp&, _Mm>,
5653 range_reference_t<_Wp>>>
5654 friend class adjacent_transform_view;
5655#endif
5656
5657 public:
5658 using iterator_category = input_iterator_tag;
5659 using iterator_concept = decltype(_S_iter_concept());
5660 using value_type = __detail::__repeated_tuple<range_value_t<_Base>, _Nm>;
5661 using difference_type = range_difference_t<_Base>;
5662
5663 _Iterator() = default;
5664
5665 constexpr
5666 _Iterator(_Iterator<!_Const> __i)
5667 requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
5668 {
5669 for (size_t __j = 0; __j < _Nm; ++__j)
5670 _M_current[__j] = std::move(__i._M_current[__j]);
5671 }
5672
5673 constexpr auto
5674 operator*() const
5675 {
5676 auto __f = [](auto& __i) -> decltype(auto) { return *__i; };
5677 return __detail::__tuple_transform(__f, _M_current);
5678 }
5679
5680 constexpr _Iterator&
5681 operator++()
5682 {
5683 for (auto& __i : _M_current)
5684 ++__i;
5685 return *this;
5686 }
5687
5688 constexpr _Iterator
5689 operator++(int)
5690 {
5691 auto __tmp = *this;
5692 ++*this;
5693 return __tmp;
5694 }
5695
5696 constexpr _Iterator&
5697 operator--() requires bidirectional_range<_Base>
5698 {
5699 for (auto& __i : _M_current)
5700 --__i;
5701 return *this;
5702 }
5703
5704 constexpr _Iterator
5705 operator--(int) requires bidirectional_range<_Base>
5706 {
5707 auto __tmp = *this;
5708 --*this;
5709 return __tmp;
5710 }
5711
5712 constexpr _Iterator&
5713 operator+=(difference_type __x)
5714 requires random_access_range<_Base>
5715 {
5716 for (auto& __i : _M_current)
5717 __i += __x;
5718 return *this;
5719 }
5720
5721 constexpr _Iterator&
5722 operator-=(difference_type __x)
5723 requires random_access_range<_Base>
5724 {
5725 for (auto& __i : _M_current)
5726 __i -= __x;
5727 return *this;
5728 }
5729
5730 constexpr auto
5731 operator[](difference_type __n) const
5732 requires random_access_range<_Base>
5733 {
5734 auto __f = [&](auto& __i) -> decltype(auto) { return __i[__n]; };
5735 return __detail::__tuple_transform(__f, _M_current);
5736 }
5737
5738 friend constexpr bool
5739 operator==(const _Iterator& __x, const _Iterator& __y)
5740 { return __x._M_current.back() == __y._M_current.back(); }
5741
5742 friend constexpr bool
5743 operator<(const _Iterator& __x, const _Iterator& __y)
5744 requires random_access_range<_Base>
5745 { return __x._M_current.back() < __y._M_current.back(); }
5746
5747 friend constexpr bool
5748 operator>(const _Iterator& __x, const _Iterator& __y)
5749 requires random_access_range<_Base>
5750 { return __y < __x; }
5751
5752 friend constexpr bool
5753 operator<=(const _Iterator& __x, const _Iterator& __y)
5754 requires random_access_range<_Base>
5755 { return !(__y < __x); }
5756
5757 friend constexpr bool
5758 operator>=(const _Iterator& __x, const _Iterator& __y)
5759 requires random_access_range<_Base>
5760 { return !(__x < __y); }
5761
5762 friend constexpr auto
5763 operator<=>(const _Iterator& __x, const _Iterator& __y)
5764 requires random_access_range<_Base>
5765 && three_way_comparable<iterator_t<_Base>>
5766 { return __x._M_current.back() <=> __y._M_current.back(); }
5767
5768 friend constexpr _Iterator
5769 operator+(const _Iterator& __i, difference_type __n)
5770 requires random_access_range<_Base>
5771 {
5772 auto __r = __i;
5773 __r += __n;
5774 return __r;
5775 }
5776
5777 friend constexpr _Iterator
5778 operator+(difference_type __n, const _Iterator& __i)
5779 requires random_access_range<_Base>
5780 {
5781 auto __r = __i;
5782 __r += __n;
5783 return __r;
5784 }
5785
5786 friend constexpr _Iterator
5787 operator-(const _Iterator& __i, difference_type __n)
5788 requires random_access_range<_Base>
5789 {
5790 auto __r = __i;
5791 __r -= __n;
5792 return __r;
5793 }
5794
5795 friend constexpr difference_type
5796 operator-(const _Iterator& __x, const _Iterator& __y)
5797 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
5798 { return __x._M_current.back() - __y._M_current.back(); }
5799
5800 friend constexpr auto
5801 iter_move(const _Iterator& __i)
5802 { return __detail::__tuple_transform(ranges::iter_move, __i._M_current); }
5803
5804 friend constexpr void
5805 iter_swap(const _Iterator& __l, const _Iterator& __r)
5806 requires indirectly_swappable<iterator_t<_Base>>
5807 {
5808 for (size_t __i = 0; __i < _Nm; __i++)
5809 ranges::iter_swap(__l._M_current[__i], __r._M_current[__i]);
5810 }
5811 };
5812
5813 template<forward_range _Vp, size_t _Nm>
5814 requires view<_Vp> && (_Nm > 0)
5815 template<bool _Const>
5816 class adjacent_view<_Vp, _Nm>::_Sentinel
5817 {
5818 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
5819
5820 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
5821
5822 constexpr explicit
5823 _Sentinel(sentinel_t<_Base> __end)
5824 : _M_end(__end)
5825 { }
5826
5827 friend class adjacent_view;
5828
5829 public:
5830 _Sentinel() = default;
5831
5832 constexpr
5833 _Sentinel(_Sentinel<!_Const> __i)
5834 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
5835 : _M_end(std::move(__i._M_end))
5836 { }
5837
5838 template<bool _OtherConst>
5839 requires sentinel_for<sentinel_t<_Base>,
5840 iterator_t<__detail::__maybe_const_t<_OtherConst, _Vp>>>
5841 friend constexpr bool
5842 operator==(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
5843 { return __x._M_current.back() == __y._M_end; }
5844
5845 template<bool _OtherConst>
5846 requires sized_sentinel_for<sentinel_t<_Base>,
5847 iterator_t<__detail::__maybe_const_t<_OtherConst, _Vp>>>
5848 friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _Vp>>
5849 operator-(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
5850 { return __x._M_current.back() - __y._M_end; }
5851
5852 template<bool _OtherConst>
5853 requires sized_sentinel_for<sentinel_t<_Base>,
5854 iterator_t<__detail::__maybe_const_t<_OtherConst, _Vp>>>
5855 friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _Vp>>
5856 operator-(const _Sentinel& __y, const _Iterator<_OtherConst>& __x)
5857 { return __y._M_end - __x._M_current.back(); }
5858 };
5859
5860 namespace views
5861 {
5862 namespace __detail
5863 {
5864 template<size_t _Nm, typename _Range>
5865 concept __can_adjacent_view
5866 = requires { adjacent_view<all_t<_Range>, _Nm>(std::declval<_Range>()); };
5867 }
5868
5869 template<size_t _Nm>
5870 struct _Adjacent : __adaptor::_RangeAdaptorClosure<_Adjacent<_Nm>>
5871 {
5872 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5873 // 4098. views::adjacent<0> should reject non-forward ranges
5874 template<viewable_range _Range>
5875 requires ((_Nm == 0) && forward_range<_Range>)
5876 || __detail::__can_adjacent_view<_Nm, _Range>
5877 constexpr auto
5878 operator() [[nodiscard]] (_Range&& __r) const
5879 {
5880 if constexpr (_Nm == 0)
5881 return views::empty<tuple<>>;
5882 else
5883 return adjacent_view<all_t<_Range>, _Nm>(std::forward<_Range>(__r));
5884 }
5885 };
5886
5887 template<size_t _Nm>
5888 inline constexpr _Adjacent<_Nm> adjacent;
5889
5890 inline constexpr auto pairwise = adjacent<2>;
5891 }
5892
5893 template<forward_range _Vp, move_constructible _Fp, size_t _Nm>
5894 requires view<_Vp> && (_Nm > 0) && is_object_v<_Fp>
5895 && regular_invocable<__detail::__unarize<_Fp&, _Nm>, range_reference_t<_Vp>>
5896 && std::__detail::__can_reference<invoke_result_t<__detail::__unarize<_Fp&, _Nm>,
5897 range_reference_t<_Vp>>>
5898 class adjacent_transform_view : public view_interface<adjacent_transform_view<_Vp, _Fp, _Nm>>
5899 {
5900 [[no_unique_address]] __detail::__box<_Fp> _M_fun;
5901 adjacent_view<_Vp, _Nm> _M_inner;
5902
5903 using _InnerView = adjacent_view<_Vp, _Nm>;
5904
5905 template<bool _Const>
5906 using _InnerIter = iterator_t<__detail::__maybe_const_t<_Const, _InnerView>>;
5907
5908 template<bool _Const>
5909 using _InnerSent = sentinel_t<__detail::__maybe_const_t<_Const, _InnerView>>;
5910
5911 template<bool> class _Iterator;
5912 template<bool> class _Sentinel;
5913
5914 public:
5915 adjacent_transform_view() = default;
5916
5917 constexpr explicit
5918 adjacent_transform_view(_Vp __base, _Fp __fun)
5919 : _M_fun(std::move(__fun)), _M_inner(std::move(__base))
5920 { }
5921
5922 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5923 // 3848. adjacent_view, adjacent_transform_view and slide_view missing base accessor
5924 // 3947. Unexpected constraints on adjacent_transform_view::base()
5925 constexpr _Vp
5926 base() const & requires copy_constructible<_Vp>
5927 { return _M_inner.base(); }
5928
5929 constexpr _Vp
5930 base() &&
5931 { return std::move(_M_inner.base()); }
5932
5933 constexpr auto
5934 begin()
5935 { return _Iterator<false>(*this, _M_inner.begin()); }
5936
5937 constexpr auto
5938 begin() const
5939 requires range<const _InnerView>
5940 && regular_invocable<__detail::__unarize<const _Fp&, _Nm>,
5941 range_reference_t<const _Vp>>
5942 { return _Iterator<true>(*this, _M_inner.begin()); }
5943
5944 constexpr auto
5945 end()
5946 {
5947 if constexpr (common_range<_InnerView>)
5948 return _Iterator<false>(*this, _M_inner.end());
5949 else
5950 return _Sentinel<false>(_M_inner.end());
5951 }
5952
5953 constexpr auto
5954 end() const
5955 requires range<const _InnerView>
5956 && regular_invocable<__detail::__unarize<const _Fp&, _Nm>,
5957 range_reference_t<const _Vp>>
5958 {
5959 if constexpr (common_range<const _InnerView>)
5960 return _Iterator<true>(*this, _M_inner.end());
5961 else
5962 return _Sentinel<true>(_M_inner.end());
5963 }
5964
5965 constexpr auto
5966 size() requires sized_range<_InnerView>
5967 { return _M_inner.size(); }
5968
5969 constexpr auto
5970 size() const requires sized_range<const _InnerView>
5971 { return _M_inner.size(); }
5972 };
5973
5974 template<forward_range _Vp, move_constructible _Fp, size_t _Nm>
5975 requires view<_Vp> && (_Nm > 0) && is_object_v<_Fp>
5976 && regular_invocable<__detail::__unarize<_Fp&, _Nm>, range_reference_t<_Vp>>
5977 && std::__detail::__can_reference<invoke_result_t<__detail::__unarize<_Fp&, _Nm>,
5978 range_reference_t<_Vp>>>
5979 template<bool _Const>
5980 class adjacent_transform_view<_Vp, _Fp, _Nm>::_Iterator
5981 {
5982 using _Parent = __detail::__maybe_const_t<_Const, adjacent_transform_view>;
5983 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
5984 using _Fun_handle = decltype([]<size_t... _Ids>(std::index_sequence<_Ids...>) {
5985 return __detail::__func_handle_t<
5986 __detail::__maybe_const_t<_Const, _Fp>,
5987 iterator_t<__detail::__maybe_const_t<(_Ids, _Const), _Vp>>...>();
5988 }(make_index_sequence<_Nm>()));
5989
5990 [[no_unique_address]] _Fun_handle _M_fun;
5991 _InnerIter<_Const> _M_inner;
5992
5993 constexpr
5994 _Iterator(_Fun_handle __fun, _InnerIter<_Const> __inner)
5995 : _M_fun(__fun), _M_inner(std::move(__inner))
5996 { }
5997
5998 constexpr
5999 _Iterator(_Parent& __parent, _InnerIter<_Const> __inner)
6000 : _M_fun(*__parent._M_fun), _M_inner(std::move(__inner))
6001 { }
6002
6003 static auto
6004 _S_iter_cat()
6005 {
6006 using __detail::__maybe_const_t;
6007 using __detail::__unarize;
6008 using _Res = invoke_result_t<__unarize<__maybe_const_t<_Const, _Fp>&, _Nm>,
6009 range_reference_t<_Base>>;
6010 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
6011 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6012 // 3798. Rvalue reference and iterator_category
6013 if constexpr (!is_reference_v<_Res>)
6014 return input_iterator_tag{};
6015 else if constexpr (derived_from<_Cat, random_access_iterator_tag>)
6016 return random_access_iterator_tag{};
6017 else if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
6018 return bidirectional_iterator_tag{};
6019 else if constexpr (derived_from<_Cat, forward_iterator_tag>)
6020 return forward_iterator_tag{};
6021 else
6022 return input_iterator_tag{};
6023 }
6024
6025 friend class adjacent_transform_view;
6026
6027 public:
6028 using iterator_category = decltype(_S_iter_cat());
6029 using iterator_concept = typename _InnerIter<_Const>::iterator_concept;
6030 using value_type
6031 = remove_cvref_t<invoke_result_t
6032 <__detail::__unarize<__detail::__maybe_const_t<_Const, _Fp>&, _Nm>,
6033 range_reference_t<_Base>>>;
6034 using difference_type = range_difference_t<_Base>;
6035
6036 _Iterator() = default;
6037
6038 constexpr
6039 _Iterator(_Iterator<!_Const> __i)
6040 requires _Const && convertible_to<_InnerIter<false>, _InnerIter<_Const>>
6041 : _M_fun(__i._M_fun), _M_inner(std::move(__i._M_inner))
6042 { }
6043
6044 constexpr decltype(auto)
6045 operator*() const
6046 {
6047 return std::apply([&](const auto&... __iters) -> decltype(auto) {
6048 return _M_fun._M_call_deref(__iters...);
6049 }, _M_inner._M_current);
6050 }
6051
6052 constexpr _Iterator&
6053 operator++()
6054 {
6055 ++_M_inner;
6056 return *this;
6057 }
6058
6059 constexpr _Iterator
6060 operator++(int)
6061 {
6062 auto __tmp = *this;
6063 ++*this;
6064 return __tmp;
6065 }
6066
6067 constexpr _Iterator&
6068 operator--() requires bidirectional_range<_Base>
6069 {
6070 --_M_inner;
6071 return *this;
6072 }
6073
6074 constexpr _Iterator
6075 operator--(int) requires bidirectional_range<_Base>
6076 {
6077 auto __tmp = *this;
6078 --*this;
6079 return __tmp;
6080 }
6081
6082 constexpr _Iterator&
6083 operator+=(difference_type __x) requires random_access_range<_Base>
6084 {
6085 _M_inner += __x;
6086 return *this;
6087 }
6088
6089 constexpr _Iterator&
6090 operator-=(difference_type __x) requires random_access_range<_Base>
6091 {
6092 _M_inner -= __x;
6093 return *this;
6094 }
6095
6096 constexpr decltype(auto)
6097 operator[](difference_type __n) const requires random_access_range<_Base>
6098 {
6099 return std::apply([&](const auto&... __iters) -> decltype(auto) {
6100 return _M_fun._M_call_subscript(__n, __iters...);
6101 }, _M_inner._M_current);
6102 }
6103
6104 friend constexpr bool
6105 operator==(const _Iterator& __x, const _Iterator& __y)
6106 { return __x._M_inner == __y._M_inner; }
6107
6108 friend constexpr bool
6109 operator<(const _Iterator& __x, const _Iterator& __y)
6110 requires random_access_range<_Base>
6111 { return __x._M_inner < __y._M_inner; }
6112
6113 friend constexpr bool
6114 operator>(const _Iterator& __x, const _Iterator& __y)
6115 requires random_access_range<_Base>
6116 { return __x._M_inner > __y._M_inner; }
6117
6118 friend constexpr bool
6119 operator<=(const _Iterator& __x, const _Iterator& __y)
6120 requires random_access_range<_Base>
6121 { return __x._M_inner <= __y._M_inner; }
6122
6123 friend constexpr bool
6124 operator>=(const _Iterator& __x, const _Iterator& __y)
6125 requires random_access_range<_Base>
6126 { return __x._M_inner >= __y._M_inner; }
6127
6128 friend constexpr auto
6129 operator<=>(const _Iterator& __x, const _Iterator& __y)
6130 requires random_access_range<_Base> &&
6131 three_way_comparable<_InnerIter<_Const>>
6132 { return __x._M_inner <=> __y._M_inner; }
6133
6134 friend constexpr _Iterator
6135 operator+(const _Iterator& __i, difference_type __n)
6136 requires random_access_range<_Base>
6137 { return _Iterator(__i._M_fun, __i._M_inner + __n); }
6138
6139 friend constexpr _Iterator
6140 operator+(difference_type __n, const _Iterator& __i)
6141 requires random_access_range<_Base>
6142 { return _Iterator(__i._M_fun, __i._M_inner + __n); }
6143
6144 friend constexpr _Iterator
6145 operator-(const _Iterator& __i, difference_type __n)
6146 requires random_access_range<_Base>
6147 { return _Iterator(__i._M_fun, __i._M_inner - __n); }
6148
6149 friend constexpr difference_type
6150 operator-(const _Iterator& __x, const _Iterator& __y)
6151 requires sized_sentinel_for<_InnerIter<_Const>, _InnerIter<_Const>>
6152 { return __x._M_inner - __y._M_inner; }
6153 };
6154
6155 template<forward_range _Vp, move_constructible _Fp, size_t _Nm>
6156 requires view<_Vp> && (_Nm > 0) && is_object_v<_Fp>
6157 && regular_invocable<__detail::__unarize<_Fp&, _Nm>, range_reference_t<_Vp>>
6158 && std::__detail::__can_reference<invoke_result_t<__detail::__unarize<_Fp&, _Nm>,
6159 range_reference_t<_Vp>>>
6160 template<bool _Const>
6161 class adjacent_transform_view<_Vp, _Fp, _Nm>::_Sentinel
6162 {
6163 _InnerSent<_Const> _M_inner;
6164
6165 constexpr explicit
6166 _Sentinel(_InnerSent<_Const> __inner)
6167 : _M_inner(__inner)
6168 { }
6169
6170 friend class adjacent_transform_view;
6171
6172 public:
6173 _Sentinel() = default;
6174
6175 constexpr
6176 _Sentinel(_Sentinel<!_Const> __i)
6177 requires _Const && convertible_to<_InnerSent<false>, _InnerSent<_Const>>
6178 : _M_inner(std::move(__i._M_inner))
6179 { }
6180
6181 template<bool _OtherConst>
6182 requires sentinel_for<_InnerSent<_Const>, _InnerIter<_OtherConst>>
6183 friend constexpr bool
6184 operator==(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
6185 { return __x._M_inner == __y._M_inner; }
6186
6187 template<bool _OtherConst>
6188 requires sized_sentinel_for<_InnerSent<_Const>, _InnerIter<_OtherConst>>
6189 friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _InnerView>>
6190 operator-(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
6191 { return __x._M_inner - __y._M_inner; }
6192
6193 template<bool _OtherConst>
6194 requires sized_sentinel_for<_InnerSent<_Const>, _InnerIter<_OtherConst>>
6195 friend constexpr range_difference_t<__detail::__maybe_const_t<_OtherConst, _InnerView>>
6196 operator-(const _Sentinel& __x, const _Iterator<_OtherConst>& __y)
6197 { return __x._M_inner - __y._M_inner; }
6198 };
6199
6200 namespace views
6201 {
6202 namespace __detail
6203 {
6204 template<size_t _Nm, typename _Range, typename _Fp>
6205 concept __can_adjacent_transform_view
6206 = requires { adjacent_transform_view<all_t<_Range>, decay_t<_Fp>, _Nm>
6207 (std::declval<_Range>(), std::declval<_Fp>()); };
6208 }
6209
6210 template<size_t _Nm>
6211 struct _AdjacentTransform : __adaptor::_RangeAdaptor<_AdjacentTransform<_Nm>>
6212 {
6213 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6214 // 4098. views::adjacent<0> should reject non-forward ranges
6215 template<viewable_range _Range, typename _Fp>
6216 requires ((_Nm == 0) && forward_range<_Range>)
6217 || __detail::__can_adjacent_transform_view<_Nm, _Range, _Fp>
6218 constexpr auto
6219 operator() [[nodiscard]] (_Range&& __r, _Fp&& __f) const
6220 {
6221 if constexpr (_Nm == 0)
6222 return zip_transform(std::forward<_Fp>(__f));
6223 else
6224 return adjacent_transform_view<all_t<_Range>, decay_t<_Fp>, _Nm>
6225 (std::forward<_Range>(__r), std::forward<_Fp>(__f));
6226 }
6227
6228 using __adaptor::_RangeAdaptor<_AdjacentTransform>::operator();
6229 static constexpr int _S_arity = 2;
6230 static constexpr bool _S_has_simple_extra_args = true;
6231 };
6232
6233 template<size_t _Nm>
6234 inline constexpr _AdjacentTransform<_Nm> adjacent_transform;
6235
6236 inline constexpr auto pairwise_transform = adjacent_transform<2>;
6237 }
6238#endif // __cpp_lib_ranges_zip
6239
6240#ifdef __cpp_lib_ranges_chunk // C++ >= 23
6241 namespace __detail
6242 {
6243 template<typename _Tp>
6244 constexpr _Tp __div_ceil(_Tp __num, _Tp __denom)
6245 {
6246 _Tp __r = __num / __denom;
6247 if (__num % __denom)
6248 ++__r;
6249 return __r;
6250 }
6251 }
6252
6253 template<view _Vp>
6254 requires input_range<_Vp>
6255 class chunk_view : public view_interface<chunk_view<_Vp>>
6256 {
6257 _Vp _M_base;
6258 range_difference_t<_Vp> _M_n;
6259 range_difference_t<_Vp> _M_remainder = 0;
6260 __detail::__non_propagating_cache<iterator_t<_Vp>> _M_current;
6261
6262 class _OuterIter;
6263 class _InnerIter;
6264
6265 public:
6266 constexpr explicit
6267 chunk_view(_Vp __base, range_difference_t<_Vp> __n)
6268 : _M_base(std::move(__base)), _M_n(__n)
6269 { __glibcxx_assert(__n >= 0); }
6270
6271 constexpr _Vp
6272 base() const & requires copy_constructible<_Vp>
6273 { return _M_base; }
6274
6275 constexpr _Vp
6276 base() &&
6277 { return std::move(_M_base); }
6278
6279 constexpr _OuterIter
6280 begin()
6281 {
6282 _M_current = ranges::begin(_M_base);
6283 _M_remainder = _M_n;
6284 return _OuterIter(*this);
6285 }
6286
6287 constexpr default_sentinel_t
6288 end() const noexcept
6289 { return default_sentinel; }
6290
6291 constexpr auto
6292 size() requires sized_range<_Vp>
6293 {
6294 return __detail::__to_unsigned_like(__detail::__div_ceil
6295 (ranges::distance(_M_base), _M_n));
6296 }
6297
6298 constexpr auto
6299 size() const requires sized_range<const _Vp>
6300 {
6301 return __detail::__to_unsigned_like(__detail::__div_ceil
6302 (ranges::distance(_M_base), _M_n));
6303 }
6304 };
6305
6306 template<typename _Range>
6307 chunk_view(_Range&&, range_difference_t<_Range>) -> chunk_view<views::all_t<_Range>>;
6308
6309 template<view _Vp>
6310 requires input_range<_Vp>
6311 class chunk_view<_Vp>::_OuterIter
6312 {
6313 chunk_view* _M_parent;
6314
6315 constexpr explicit
6316 _OuterIter(chunk_view& __parent) noexcept
6317 : _M_parent(std::__addressof(__parent))
6318 { }
6319
6320 friend chunk_view;
6321
6322 public:
6323 using iterator_concept = input_iterator_tag;
6324 using difference_type = range_difference_t<_Vp>;
6325
6326 struct value_type;
6327
6328 _OuterIter(_OuterIter&&) = default;
6329 _OuterIter& operator=(_OuterIter&&) = default;
6330
6331 constexpr value_type
6332 operator*() const
6333 {
6334 __glibcxx_assert(*this != default_sentinel);
6335 return value_type(*_M_parent);
6336 }
6337
6338 constexpr _OuterIter&
6339 operator++()
6340 {
6341 __glibcxx_assert(*this != default_sentinel);
6342 ranges::advance(*_M_parent->_M_current, _M_parent->_M_remainder,
6343 ranges::end(_M_parent->_M_base));
6344 _M_parent->_M_remainder = _M_parent->_M_n;
6345 return *this;
6346 }
6347
6348 constexpr void
6349 operator++(int)
6350 { ++*this; }
6351
6352 friend constexpr bool
6353 operator==(const _OuterIter& __x, default_sentinel_t)
6354 {
6355 return *__x._M_parent->_M_current == ranges::end(__x._M_parent->_M_base)
6356 && __x._M_parent->_M_remainder != 0;
6357 }
6358
6359 friend constexpr difference_type
6360 operator-(default_sentinel_t, const _OuterIter& __x)
6361 requires sized_sentinel_for<sentinel_t<_Vp>, iterator_t<_Vp>>
6362 {
6363 const auto __dist = ranges::end(__x._M_parent->_M_base) - *__x._M_parent->_M_current;
6364
6365 if (__dist < __x._M_parent->_M_remainder)
6366 return __dist == 0 ? 0 : 1;
6367
6368 return 1 + __detail::__div_ceil(__dist - __x._M_parent->_M_remainder,
6369 __x._M_parent->_M_n);
6370 }
6371
6372 friend constexpr difference_type
6373 operator-(const _OuterIter& __x, default_sentinel_t __y)
6374 requires sized_sentinel_for<sentinel_t<_Vp>, iterator_t<_Vp>>
6375 { return -(__y - __x); }
6376 };
6377
6378 template<view _Vp>
6379 requires input_range<_Vp>
6380 struct chunk_view<_Vp>::_OuterIter::value_type : view_interface<value_type>
6381 {
6382 private:
6383 chunk_view* _M_parent;
6384
6385 constexpr explicit
6386 value_type(chunk_view& __parent) noexcept
6387 : _M_parent(std::__addressof(__parent))
6388 { }
6389
6390 friend _OuterIter;
6391
6392 public:
6393 constexpr _InnerIter
6394 begin() const noexcept
6395 { return _InnerIter(*_M_parent); }
6396
6397 constexpr default_sentinel_t
6398 end() const noexcept
6399 { return default_sentinel; }
6400
6401 constexpr auto
6402 size() const
6403 requires sized_sentinel_for<sentinel_t<_Vp>, iterator_t<_Vp>>
6404 {
6405 return __detail::__to_unsigned_like
6406 (ranges::min(_M_parent->_M_remainder,
6407 ranges::end(_M_parent->_M_base) - *_M_parent->_M_current));
6408 }
6409 };
6410
6411 template<view _Vp>
6412 requires input_range<_Vp>
6413 class chunk_view<_Vp>::_InnerIter
6414 {
6415 chunk_view* _M_parent;
6416
6417 constexpr explicit
6418 _InnerIter(chunk_view& __parent) noexcept
6419 : _M_parent(std::__addressof(__parent))
6420 { }
6421
6422 friend _OuterIter::value_type;
6423
6424 public:
6425 using iterator_concept = input_iterator_tag;
6426 using difference_type = range_difference_t<_Vp>;
6427 using value_type = range_value_t<_Vp>;
6428
6429 _InnerIter(_InnerIter&&) = default;
6430 _InnerIter& operator=(_InnerIter&&) = default;
6431
6432 constexpr const iterator_t<_Vp>&
6433 base() const &
6434 { return *_M_parent->_M_current; }
6435
6436 constexpr range_reference_t<_Vp>
6437 operator*() const
6438 {
6439 __glibcxx_assert(*this != default_sentinel);
6440 return **_M_parent->_M_current;
6441 }
6442
6443 constexpr _InnerIter&
6444 operator++()
6445 {
6446 __glibcxx_assert(*this != default_sentinel);
6447 ++*_M_parent->_M_current;
6448 if (*_M_parent->_M_current == ranges::end(_M_parent->_M_base))
6449 _M_parent->_M_remainder = 0;
6450 else
6451 --_M_parent->_M_remainder;
6452 return *this;
6453 }
6454
6455 constexpr void
6456 operator++(int)
6457 { ++*this; }
6458
6459 friend constexpr bool
6460 operator==(const _InnerIter& __x, default_sentinel_t) noexcept
6461 { return __x._M_parent->_M_remainder == 0; }
6462
6463 friend constexpr difference_type
6464 operator-(default_sentinel_t, const _InnerIter& __x)
6465 requires sized_sentinel_for<sentinel_t<_Vp>, iterator_t<_Vp>>
6466 {
6467 return ranges::min(__x._M_parent->_M_remainder,
6468 ranges::end(__x._M_parent->_M_base) - *__x._M_parent->_M_current);
6469 }
6470
6471 friend constexpr difference_type
6472 operator-(const _InnerIter& __x, default_sentinel_t __y)
6473 requires sized_sentinel_for<sentinel_t<_Vp>, iterator_t<_Vp>>
6474 { return -(__y - __x); }
6475
6476 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6477 // 3851. chunk_view::inner-iterator missing custom iter_move and iter_swap
6478 friend constexpr range_rvalue_reference_t<_Vp>
6479 iter_move(const _InnerIter& __i)
6480 noexcept(noexcept(ranges::iter_move(*__i._M_parent->_M_current)))
6481 { return ranges::iter_move(*__i._M_parent->_M_current); }
6482
6483 friend constexpr void
6484 iter_swap(const _InnerIter& __x, const _InnerIter& __y)
6485 noexcept(noexcept(ranges::iter_swap(*__x._M_parent->_M_current,
6486 *__x._M_parent->_M_current)))
6487 requires indirectly_swappable<iterator_t<_Vp>>
6488 { return ranges::iter_swap(*__x._M_parent->_M_current, *__y._M_parent->_M_current); }
6489 };
6490
6491 template<view _Vp>
6492 requires forward_range<_Vp>
6493 class chunk_view<_Vp> : public view_interface<chunk_view<_Vp>>
6494 {
6495 _Vp _M_base;
6496 range_difference_t<_Vp> _M_n;
6497 template<bool> class _Iterator;
6498
6499 public:
6500 constexpr explicit
6501 chunk_view(_Vp __base, range_difference_t<_Vp> __n)
6502 : _M_base(std::move(__base)), _M_n(__n)
6503 { __glibcxx_assert(__n > 0); }
6504
6505 constexpr _Vp
6506 base() const & requires copy_constructible<_Vp>
6507 { return _M_base; }
6508
6509 constexpr _Vp
6510 base() &&
6511 { return std::move(_M_base); }
6512
6513 constexpr auto
6514 begin() requires (!__detail::__simple_view<_Vp>)
6515 { return _Iterator<false>(this, ranges::begin(_M_base)); }
6516
6517 constexpr auto
6518 begin() const requires forward_range<const _Vp>
6519 { return _Iterator<true>(this, ranges::begin(_M_base)); }
6520
6521 constexpr auto
6522 end() requires (!__detail::__simple_view<_Vp>)
6523 {
6524 if constexpr (common_range<_Vp> && sized_range<_Vp>)
6525 {
6526 auto __missing = (_M_n - ranges::distance(_M_base) % _M_n) % _M_n;
6527 return _Iterator<false>(this, ranges::end(_M_base), __missing);
6528 }
6529 else if constexpr (common_range<_Vp> && !bidirectional_range<_Vp>)
6530 return _Iterator<false>(this, ranges::end(_M_base));
6531 else
6532 return default_sentinel;
6533 }
6534
6535 constexpr auto
6536 end() const requires forward_range<const _Vp>
6537 {
6538 if constexpr (common_range<const _Vp> && sized_range<const _Vp>)
6539 {
6540 auto __missing = (_M_n - ranges::distance(_M_base) % _M_n) % _M_n;
6541 return _Iterator<true>(this, ranges::end(_M_base), __missing);
6542 }
6543 else if constexpr (common_range<const _Vp> && !bidirectional_range<const _Vp>)
6544 return _Iterator<true>(this, ranges::end(_M_base));
6545 else
6546 return default_sentinel;
6547 }
6548
6549 constexpr auto
6550 size() requires sized_range<_Vp>
6551 {
6552 return __detail::__to_unsigned_like(__detail::__div_ceil
6553 (ranges::distance(_M_base), _M_n));
6554 }
6555
6556 constexpr auto
6557 size() const requires sized_range<const _Vp>
6558 {
6559 return __detail::__to_unsigned_like(__detail::__div_ceil
6560 (ranges::distance(_M_base), _M_n));
6561 }
6562 };
6563
6564 template<typename _Vp>
6565 inline constexpr bool enable_borrowed_range<chunk_view<_Vp>>
6566 = forward_range<_Vp> && enable_borrowed_range<_Vp>;
6567
6568 template<view _Vp>
6569 requires forward_range<_Vp>
6570 template<bool _Const>
6571 class chunk_view<_Vp>::_Iterator
6572 {
6573 using _Parent = __detail::__maybe_const_t<_Const, chunk_view>;
6574 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
6575
6576 iterator_t<_Base> _M_current = iterator_t<_Base>();
6577 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
6578 range_difference_t<_Base> _M_n = 0;
6579 range_difference_t<_Base> _M_missing = 0;
6580
6581 constexpr
6582 _Iterator(_Parent* __parent, iterator_t<_Base> __current,
6583 range_difference_t<_Base> __missing = 0)
6584 : _M_current(__current), _M_end(ranges::end(__parent->_M_base)),
6585 _M_n(__parent->_M_n), _M_missing(__missing)
6586 { }
6587
6588 static auto
6589 _S_iter_cat()
6590 {
6591 if constexpr (random_access_range<_Base>)
6592 return random_access_iterator_tag{};
6593 else if constexpr (bidirectional_range<_Base>)
6594 return bidirectional_iterator_tag{};
6595 else
6596 return forward_iterator_tag{};
6597 }
6598
6599 friend chunk_view;
6600
6601 public:
6602 using iterator_category = input_iterator_tag;
6603 using iterator_concept = decltype(_S_iter_cat());
6604 using value_type = decltype(views::take(subrange(_M_current, _M_end), _M_n));
6605 using difference_type = range_difference_t<_Base>;
6606
6607 _Iterator() = default;
6608
6609 constexpr _Iterator(_Iterator<!_Const> __i)
6610 requires _Const
6611 && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
6612 && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
6613 : _M_current(std::move(__i._M_current)), _M_end(std::move(__i._M_end)),
6614 _M_n(__i._M_n), _M_missing(__i._M_missing)
6615 { }
6616
6617 constexpr iterator_t<_Base>
6618 base() const
6619 { return _M_current; }
6620
6621 constexpr value_type
6622 operator*() const
6623 {
6624 __glibcxx_assert(_M_current != _M_end);
6625 return views::take(subrange(_M_current, _M_end), _M_n);
6626 }
6627
6628 constexpr _Iterator&
6629 operator++()
6630 {
6631 __glibcxx_assert(_M_current != _M_end);
6632 _M_missing = ranges::advance(_M_current, _M_n, _M_end);
6633 return *this;
6634 }
6635
6636 constexpr _Iterator
6637 operator++(int)
6638 {
6639 auto __tmp = *this;
6640 ++*this;
6641 return __tmp;
6642 }
6643
6644 constexpr _Iterator&
6645 operator--() requires bidirectional_range<_Base>
6646 {
6647 ranges::advance(_M_current, _M_missing - _M_n);
6648 _M_missing = 0;
6649 return *this;
6650 }
6651
6652 constexpr _Iterator
6653 operator--(int) requires bidirectional_range<_Base>
6654 {
6655 auto __tmp = *this;
6656 --*this;
6657 return __tmp;
6658 }
6659
6660 constexpr _Iterator&
6661 operator+=(difference_type __x)
6662 requires random_access_range<_Base>
6663 {
6664 if (__x > 0)
6665 {
6666 __glibcxx_assert(ranges::distance(_M_current, _M_end) > _M_n * (__x - 1));
6667 _M_missing = ranges::advance(_M_current, _M_n * __x, _M_end);
6668 }
6669 else if (__x < 0)
6670 {
6671 ranges::advance(_M_current, _M_n * __x + _M_missing);
6672 _M_missing = 0;
6673 }
6674 return *this;
6675 }
6676
6677 constexpr _Iterator&
6678 operator-=(difference_type __x)
6679 requires random_access_range<_Base>
6680 { return *this += -__x; }
6681
6682 constexpr value_type
6683 operator[](difference_type __n) const
6684 requires random_access_range<_Base>
6685 { return *(*this + __n); }
6686
6687 friend constexpr bool
6688 operator==(const _Iterator& __x, const _Iterator& __y)
6689 { return __x._M_current == __y._M_current; }
6690
6691 friend constexpr bool
6692 operator==(const _Iterator& __x, default_sentinel_t)
6693 { return __x._M_current == __x._M_end; }
6694
6695 friend constexpr bool
6696 operator<(const _Iterator& __x, const _Iterator& __y)
6697 requires random_access_range<_Base>
6698 { return __x._M_current > __y._M_current; }
6699
6700 friend constexpr bool
6701 operator>(const _Iterator& __x, const _Iterator& __y)
6702 requires random_access_range<_Base>
6703 { return __y < __x; }
6704
6705 friend constexpr bool
6706 operator<=(const _Iterator& __x, const _Iterator& __y)
6707 requires random_access_range<_Base>
6708 { return !(__y < __x); }
6709
6710 friend constexpr bool
6711 operator>=(const _Iterator& __x, const _Iterator& __y)
6712 requires random_access_range<_Base>
6713 { return !(__x < __y); }
6714
6715 friend constexpr auto
6716 operator<=>(const _Iterator& __x, const _Iterator& __y)
6717 requires random_access_range<_Base>
6718 && three_way_comparable<iterator_t<_Base>>
6719 { return __x._M_current <=> __y._M_current; }
6720
6721 friend constexpr _Iterator
6722 operator+(const _Iterator& __i, difference_type __n)
6723 requires random_access_range<_Base>
6724 {
6725 auto __r = __i;
6726 __r += __n;
6727 return __r;
6728 }
6729
6730 friend constexpr _Iterator
6731 operator+(difference_type __n, const _Iterator& __i)
6732 requires random_access_range<_Base>
6733 {
6734 auto __r = __i;
6735 __r += __n;
6736 return __r;
6737 }
6738
6739 friend constexpr _Iterator
6740 operator-(const _Iterator& __i, difference_type __n)
6741 requires random_access_range<_Base>
6742 {
6743 auto __r = __i;
6744 __r -= __n;
6745 return __r;
6746 }
6747
6748 friend constexpr difference_type
6749 operator-(const _Iterator& __x, const _Iterator& __y)
6750 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
6751 {
6752 return (__x._M_current - __y._M_current
6753 + __x._M_missing - __y._M_missing) / __x._M_n;
6754 }
6755
6756 friend constexpr difference_type
6757 operator-(default_sentinel_t, const _Iterator& __x)
6758 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base>>
6759 { return __detail::__div_ceil(__x._M_end - __x._M_current, __x._M_n); }
6760
6761 friend constexpr difference_type
6762 operator-(const _Iterator& __x, default_sentinel_t __y)
6763 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base>>
6764 { return -(__y - __x); }
6765 };
6766
6767 namespace views
6768 {
6769 namespace __detail
6770 {
6771 template<typename _Range, typename _Dp>
6772 concept __can_chunk_view
6773 = requires { chunk_view(std::declval<_Range>(), std::declval<_Dp>()); };
6774 }
6775
6776 struct _Chunk : __adaptor::_RangeAdaptor<_Chunk>
6777 {
6778 template<viewable_range _Range, typename _Dp = range_difference_t<_Range>>
6779 requires __detail::__can_chunk_view<_Range, _Dp>
6780 constexpr auto
6781 operator() [[nodiscard]] (_Range&& __r, type_identity_t<_Dp> __n) const
6782 { return chunk_view(std::forward<_Range>(__r), __n); }
6783
6784 using __adaptor::_RangeAdaptor<_Chunk>::operator();
6785 static constexpr int _S_arity = 2;
6786 static constexpr bool _S_has_simple_extra_args = true;
6787 };
6788
6789 inline constexpr _Chunk chunk;
6790 }
6791#endif // __cpp_lib_ranges_chunk
6792
6793#ifdef __cpp_lib_ranges_slide // C++ >= 23
6794 namespace __detail
6795 {
6796 template<typename _Vp>
6797 concept __slide_caches_nothing = random_access_range<_Vp> && sized_range<_Vp>;
6798
6799 template<typename _Vp>
6800 concept __slide_caches_last
6801 = !__slide_caches_nothing<_Vp> && bidirectional_range<_Vp> && common_range<_Vp>;
6802
6803 template<typename _Vp>
6804 concept __slide_caches_first
6805 = !__slide_caches_nothing<_Vp> && !__slide_caches_last<_Vp>;
6806 }
6807
6808 template<forward_range _Vp>
6809 requires view<_Vp>
6810 class slide_view : public view_interface<slide_view<_Vp>>
6811 {
6812 _Vp _M_base;
6813 range_difference_t<_Vp> _M_n;
6814 [[no_unique_address]]
6815 __detail::__maybe_present_t<__detail::__slide_caches_first<_Vp>,
6816 __detail::_CachedPosition<_Vp>, 0> _M_cached_begin;
6817 [[no_unique_address]]
6818 __detail::__maybe_present_t<__detail::__slide_caches_last<_Vp>,
6819 __detail::_CachedPosition<_Vp>, 1> _M_cached_end;
6820
6821 template<bool> class _Iterator;
6822 class _Sentinel;
6823
6824 public:
6825 constexpr explicit
6826 slide_view(_Vp __base, range_difference_t<_Vp> __n)
6827 : _M_base(std::move(__base)), _M_n(__n)
6828 { __glibcxx_assert(__n > 0); }
6829
6830 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6831 // 3848. adjacent_view, adjacent_transform_view and slide_view missing base accessor
6832 constexpr _Vp
6833 base() const & requires copy_constructible<_Vp>
6834 { return _M_base; }
6835
6836 constexpr _Vp
6837 base() &&
6838 { return std::move(_M_base); }
6839
6840 constexpr auto
6841 begin() requires (!(__detail::__simple_view<_Vp>
6842 && __detail::__slide_caches_nothing<const _Vp>))
6843 {
6844 if constexpr (__detail::__slide_caches_first<_Vp>)
6845 {
6846 iterator_t<_Vp> __it;
6847 if (_M_cached_begin._M_has_value())
6848 __it = _M_cached_begin._M_get(_M_base);
6849 else
6850 {
6851 __it = ranges::next(ranges::begin(_M_base), _M_n - 1, ranges::end(_M_base));
6852 _M_cached_begin._M_set(_M_base, __it);
6853 }
6854 return _Iterator<false>(ranges::begin(_M_base), std::move(__it), _M_n);
6855 }
6856 else
6857 return _Iterator<false>(ranges::begin(_M_base), _M_n);
6858 }
6859
6860 constexpr auto
6861 begin() const requires __detail::__slide_caches_nothing<const _Vp>
6862 { return _Iterator<true>(ranges::begin(_M_base), _M_n); }
6863
6864 constexpr auto
6865 end() requires (!(__detail::__simple_view<_Vp>
6866 && __detail::__slide_caches_nothing<const _Vp>))
6867 {
6868 if constexpr (__detail::__slide_caches_nothing<_Vp>)
6869 return _Iterator<false>(ranges::begin(_M_base) + range_difference_t<_Vp>(size()),
6870 _M_n);
6871 else if constexpr (__detail::__slide_caches_last<_Vp>)
6872 {
6873 iterator_t<_Vp> __it;
6874 if (_M_cached_end._M_has_value())
6875 __it = _M_cached_end._M_get(_M_base);
6876 else
6877 {
6878 __it = ranges::prev(ranges::end(_M_base), _M_n - 1, ranges::begin(_M_base));
6879 _M_cached_end._M_set(_M_base, __it);
6880 }
6881 return _Iterator<false>(std::move(__it), _M_n);
6882 }
6883 else if constexpr (common_range<_Vp>)
6884 return _Iterator<false>(ranges::end(_M_base), ranges::end(_M_base), _M_n);
6885 else
6886 return _Sentinel(ranges::end(_M_base));
6887 }
6888
6889 constexpr auto
6890 end() const requires __detail::__slide_caches_nothing<const _Vp>
6891 { return begin() + range_difference_t<const _Vp>(size()); }
6892
6893 constexpr auto
6894 size() requires sized_range<_Vp>
6895 {
6896 auto __sz = ranges::distance(_M_base) - _M_n + 1;
6897 if (__sz < 0)
6898 __sz = 0;
6899 return __detail::__to_unsigned_like(__sz);
6900 }
6901
6902 constexpr auto
6903 size() const requires sized_range<const _Vp>
6904 {
6905 auto __sz = ranges::distance(_M_base) - _M_n + 1;
6906 if (__sz < 0)
6907 __sz = 0;
6908 return __detail::__to_unsigned_like(__sz);
6909 }
6910 };
6911
6912 template<typename _Range>
6913 slide_view(_Range&&, range_difference_t<_Range>) -> slide_view<views::all_t<_Range>>;
6914
6915 template<typename _Vp>
6916 inline constexpr bool enable_borrowed_range<slide_view<_Vp>>
6917 = enable_borrowed_range<_Vp>;
6918
6919 template<forward_range _Vp>
6920 requires view<_Vp>
6921 template<bool _Const>
6922 class slide_view<_Vp>::_Iterator
6923 {
6924 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
6925 static constexpr bool _S_last_elt_present
6926 = __detail::__slide_caches_first<_Base>;
6927
6928 iterator_t<_Base> _M_current = iterator_t<_Base>();
6929 [[no_unique_address]]
6930 __detail::__maybe_present_t<_S_last_elt_present, iterator_t<_Base>>
6931 _M_last_elt = decltype(_M_last_elt)();
6932 range_difference_t<_Base> _M_n = 0;
6933
6934 constexpr
6935 _Iterator(iterator_t<_Base> __current, range_difference_t<_Base> __n)
6936 requires (!_S_last_elt_present)
6937 : _M_current(__current), _M_n(__n)
6938 { }
6939
6940 constexpr
6941 _Iterator(iterator_t<_Base> __current, iterator_t<_Base> __last_elt,
6942 range_difference_t<_Base> __n)
6943 requires _S_last_elt_present
6944 : _M_current(__current), _M_last_elt(__last_elt), _M_n(__n)
6945 { }
6946
6947 static auto
6948 _S_iter_concept()
6949 {
6950 if constexpr (random_access_range<_Base>)
6951 return random_access_iterator_tag{};
6952 else if constexpr (bidirectional_range<_Base>)
6953 return bidirectional_iterator_tag{};
6954 else
6955 return forward_iterator_tag{};
6956 }
6957
6958 friend slide_view;
6959 friend slide_view::_Sentinel;
6960
6961 public:
6962 using iterator_category = input_iterator_tag;
6963 using iterator_concept = decltype(_S_iter_concept());
6964 using value_type = decltype(views::counted(_M_current, _M_n));
6965 using difference_type = range_difference_t<_Base>;
6966
6967 _Iterator() = default;
6968
6969 constexpr
6970 _Iterator(_Iterator<!_Const> __i)
6971 requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
6972 : _M_current(std::move(__i._M_current)), _M_n(__i._M_n)
6973 { }
6974
6975 constexpr auto
6976 operator*() const
6977 { return views::counted(_M_current, _M_n); }
6978
6979 constexpr _Iterator&
6980 operator++()
6981 {
6982 ++_M_current;
6983 if constexpr (_S_last_elt_present)
6984 ++_M_last_elt;
6985 return *this;
6986 }
6987
6988 constexpr _Iterator
6989 operator++(int)
6990 {
6991 auto __tmp = *this;
6992 ++*this;
6993 return __tmp;
6994 }
6995
6996 constexpr _Iterator&
6997 operator--() requires bidirectional_range<_Base>
6998 {
6999 --_M_current;
7000 if constexpr (_S_last_elt_present)
7001 --_M_last_elt;
7002 return *this;
7003 }
7004
7005 constexpr _Iterator
7006 operator--(int) requires bidirectional_range<_Base>
7007 {
7008 auto __tmp = *this;
7009 --*this;
7010 return __tmp;
7011 }
7012
7013 constexpr _Iterator&
7014 operator+=(difference_type __x)
7015 requires random_access_range<_Base>
7016 {
7017 _M_current += __x;
7018 if constexpr (_S_last_elt_present)
7019 _M_last_elt += __x;
7020 return *this;
7021 }
7022
7023 constexpr _Iterator&
7024 operator-=(difference_type __x)
7025 requires random_access_range<_Base>
7026 {
7027 _M_current -= __x;
7028 if constexpr (_S_last_elt_present)
7029 _M_last_elt -= __x;
7030 return *this;
7031 }
7032
7033 constexpr auto
7034 operator[](difference_type __n) const
7035 requires random_access_range<_Base>
7036 { return views::counted(_M_current + __n, _M_n); }
7037
7038 friend constexpr bool
7039 operator==(const _Iterator& __x, const _Iterator& __y)
7040 {
7041 if constexpr (_S_last_elt_present)
7042 return __x._M_last_elt == __y._M_last_elt;
7043 else
7044 return __x._M_current == __y._M_current;
7045 }
7046
7047 friend constexpr bool
7048 operator<(const _Iterator& __x, const _Iterator& __y)
7049 requires random_access_range<_Base>
7050 { return __x._M_current < __y._M_current; }
7051
7052 friend constexpr bool
7053 operator>(const _Iterator& __x, const _Iterator& __y)
7054 requires random_access_range<_Base>
7055 { return __y < __x; }
7056
7057 friend constexpr bool
7058 operator<=(const _Iterator& __x, const _Iterator& __y)
7059 requires random_access_range<_Base>
7060 { return !(__y < __x); }
7061
7062 friend constexpr bool
7063 operator>=(const _Iterator& __x, const _Iterator& __y)
7064 requires random_access_range<_Base>
7065 { return !(__x < __y); }
7066
7067 friend constexpr auto
7068 operator<=>(const _Iterator& __x, const _Iterator& __y)
7069 requires random_access_range<_Base>
7070 && three_way_comparable<iterator_t<_Base>>
7071 { return __x._M_current <=> __y._M_current; }
7072
7073 friend constexpr _Iterator
7074 operator+(const _Iterator& __i, difference_type __n)
7075 requires random_access_range<_Base>
7076 {
7077 auto __r = __i;
7078 __r += __n;
7079 return __r;
7080 }
7081
7082 friend constexpr _Iterator
7083 operator+(difference_type __n, const _Iterator& __i)
7084 requires random_access_range<_Base>
7085 {
7086 auto __r = __i;
7087 __r += __n;
7088 return __r;
7089 }
7090
7091 friend constexpr _Iterator
7092 operator-(const _Iterator& __i, difference_type __n)
7093 requires random_access_range<_Base>
7094 {
7095 auto __r = __i;
7096 __r -= __n;
7097 return __r;
7098 }
7099
7100 friend constexpr difference_type
7101 operator-(const _Iterator& __x, const _Iterator& __y)
7102 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
7103 {
7104 if constexpr (_S_last_elt_present)
7105 return __x._M_last_elt - __y._M_last_elt;
7106 else
7107 return __x._M_current - __y._M_current;
7108 }
7109 };
7110
7111 template<forward_range _Vp>
7112 requires view<_Vp>
7113 class slide_view<_Vp>::_Sentinel
7114 {
7115 sentinel_t<_Vp> _M_end = sentinel_t<_Vp>();
7116
7117 constexpr explicit
7118 _Sentinel(sentinel_t<_Vp> __end)
7119 : _M_end(__end)
7120 { }
7121
7122 friend slide_view;
7123
7124 public:
7125 _Sentinel() = default;
7126
7127 friend constexpr bool
7128 operator==(const _Iterator<false>& __x, const _Sentinel& __y)
7129 { return __x._M_last_elt == __y._M_end; }
7130
7131 friend constexpr range_difference_t<_Vp>
7132 operator-(const _Iterator<false>& __x, const _Sentinel& __y)
7133 requires sized_sentinel_for<sentinel_t<_Vp>, iterator_t<_Vp>>
7134 { return __x._M_last_elt - __y._M_end; }
7135
7136 friend constexpr range_difference_t<_Vp>
7137 operator-(const _Sentinel& __y, const _Iterator<false>& __x)
7138 requires sized_sentinel_for<sentinel_t<_Vp>, iterator_t<_Vp>>
7139 { return __y._M_end -__x._M_last_elt; }
7140 };
7141
7142 namespace views
7143 {
7144 namespace __detail
7145 {
7146 template<typename _Range, typename _Dp>
7147 concept __can_slide_view
7148 = requires { slide_view(std::declval<_Range>(), std::declval<_Dp>()); };
7149 }
7150
7151 struct _Slide : __adaptor::_RangeAdaptor<_Slide>
7152 {
7153 template<viewable_range _Range, typename _Dp = range_difference_t<_Range>>
7154 requires __detail::__can_slide_view<_Range, _Dp>
7155 constexpr auto
7156 operator() [[nodiscard]] (_Range&& __r, type_identity_t<_Dp> __n) const
7157 { return slide_view(std::forward<_Range>(__r), __n); }
7158
7159 using __adaptor::_RangeAdaptor<_Slide>::operator();
7160 static constexpr int _S_arity = 2;
7161 static constexpr bool _S_has_simple_extra_args = true;
7162 };
7163
7164 inline constexpr _Slide slide;
7165 }
7166#endif // __cpp_lib_ranges_slide
7167
7168#ifdef __cpp_lib_ranges_chunk_by // C++ >= 23
7169 template<forward_range _Vp,
7170 indirect_binary_predicate<iterator_t<_Vp>, iterator_t<_Vp>> _Pred>
7171 requires view<_Vp> && is_object_v<_Pred>
7172 class chunk_by_view : public view_interface<chunk_by_view<_Vp, _Pred>>
7173 {
7174 _Vp _M_base = _Vp();
7175 __detail::__box<_Pred> _M_pred;
7176 __detail::_CachedPosition<_Vp> _M_cached_begin;
7177
7178 constexpr iterator_t<_Vp>
7179 _M_find_next(iterator_t<_Vp> __current)
7180 {
7181 __glibcxx_assert(_M_pred.has_value());
7182 auto __pred = [this]<typename _Tp, typename _Up>(_Tp&& __x, _Up&& __y) {
7183 return !bool((*_M_pred)(std::forward<_Tp>(__x), std::forward<_Up>(__y)));
7184 };
7185 auto __it = ranges::adjacent_find(__current, ranges::end(_M_base), __pred);
7186 return ranges::next(__it, 1, ranges::end(_M_base));
7187 }
7188
7189 constexpr iterator_t<_Vp>
7190 _M_find_prev(iterator_t<_Vp> __current) requires bidirectional_range<_Vp>
7191 {
7192 __glibcxx_assert(_M_pred.has_value());
7193 auto __pred = [this]<typename _Tp, typename _Up>(_Tp&& __x, _Up&& __y) {
7194 return !bool((*_M_pred)(std::forward<_Up>(__y), std::forward<_Tp>(__x)));
7195 };
7196 auto __rbegin = std::make_reverse_iterator(__current);
7197 auto __rend = std::make_reverse_iterator(ranges::begin(_M_base));
7198 __glibcxx_assert(__rbegin != __rend);
7199 auto __it = ranges::adjacent_find(__rbegin, __rend, __pred).base();
7200 return ranges::prev(__it, 1, ranges::begin(_M_base));
7201 }
7202
7203 class _Iterator;
7204
7205 public:
7206 chunk_by_view() requires (default_initializable<_Vp>
7207 && default_initializable<_Pred>)
7208 = default;
7209
7210 constexpr explicit
7211 chunk_by_view(_Vp __base, _Pred __pred)
7212 : _M_base(std::move(__base)), _M_pred(std::move(__pred))
7213 { }
7214
7215 constexpr _Vp
7216 base() const & requires copy_constructible<_Vp>
7217 { return _M_base; }
7218
7219 constexpr _Vp
7220 base() &&
7221 { return std::move(_M_base); }
7222
7223 constexpr const _Pred&
7224 pred() const
7225 { return *_M_pred; }
7226
7227 constexpr _Iterator
7228 begin()
7229 {
7230 __glibcxx_assert(_M_pred.has_value());
7231 iterator_t<_Vp> __it;
7232 if (_M_cached_begin._M_has_value())
7233 __it = _M_cached_begin._M_get(_M_base);
7234 else
7235 {
7236 __it = _M_find_next(ranges::begin(_M_base));
7237 _M_cached_begin._M_set(_M_base, __it);
7238 }
7239 return _Iterator(*this, ranges::begin(_M_base), __it);
7240 }
7241
7242 constexpr auto
7243 end()
7244 {
7245 if constexpr (common_range<_Vp>)
7246 return _Iterator(*this, ranges::end(_M_base), ranges::end(_M_base));
7247 else
7248 return default_sentinel;
7249 }
7250 };
7251
7252 template<typename _Range, typename _Pred>
7253 chunk_by_view(_Range&&, _Pred) -> chunk_by_view<views::all_t<_Range>, _Pred>;
7254
7255 template<forward_range _Vp,
7256 indirect_binary_predicate<iterator_t<_Vp>, iterator_t<_Vp>> _Pred>
7257 requires view<_Vp> && is_object_v<_Pred>
7258 class chunk_by_view<_Vp, _Pred>::_Iterator
7259 {
7260 chunk_by_view* _M_parent = nullptr;
7261 iterator_t<_Vp> _M_current = iterator_t<_Vp>();
7262 iterator_t<_Vp> _M_next = iterator_t<_Vp>();
7263
7264 constexpr
7265 _Iterator(chunk_by_view& __parent, iterator_t<_Vp> __current, iterator_t<_Vp> __next)
7266 : _M_parent(std::__addressof(__parent)), _M_current(__current), _M_next(__next)
7267 { }
7268
7269 static auto
7270 _S_iter_concept()
7271 {
7272 if constexpr (bidirectional_range<_Vp>)
7273 return bidirectional_iterator_tag{};
7274 else
7275 return forward_iterator_tag{};
7276 }
7277
7278 friend chunk_by_view;
7279
7280 public:
7281 using value_type = subrange<iterator_t<_Vp>>;
7282 using difference_type = range_difference_t<_Vp>;
7283 using iterator_category = input_iterator_tag;
7284 using iterator_concept = decltype(_S_iter_concept());
7285
7286 _Iterator() = default;
7287
7288 constexpr value_type
7289 operator*() const
7290 {
7291 __glibcxx_assert(_M_current != _M_next);
7292 return ranges::subrange(_M_current, _M_next);
7293 }
7294
7295 constexpr _Iterator&
7296 operator++()
7297 {
7298 __glibcxx_assert(_M_current != _M_next);
7299 _M_current = _M_next;
7300 _M_next = _M_parent->_M_find_next(_M_current);
7301 return *this;
7302 }
7303
7304 constexpr _Iterator
7305 operator++(int)
7306 {
7307 auto __tmp = *this;
7308 ++*this;
7309 return __tmp;
7310 }
7311
7312 constexpr _Iterator&
7313 operator--() requires bidirectional_range<_Vp>
7314 {
7315 _M_next = _M_current;
7316 _M_current = _M_parent->_M_find_prev(_M_next);
7317 return *this;
7318 }
7319
7320 constexpr _Iterator
7321 operator--(int) requires bidirectional_range<_Vp>
7322 {
7323 auto __tmp = *this;
7324 --*this;
7325 return __tmp;
7326 }
7327
7328 friend constexpr bool
7329 operator==(const _Iterator& __x, const _Iterator& __y)
7330 { return __x._M_current == __y._M_current; }
7331
7332 friend constexpr bool
7333 operator==(const _Iterator& __x, default_sentinel_t)
7334 { return __x._M_current == __x._M_next; }
7335 };
7336
7337 namespace views
7338 {
7339 namespace __detail
7340 {
7341 template<typename _Range, typename _Pred>
7342 concept __can_chunk_by_view
7343 = requires { chunk_by_view(std::declval<_Range>(), std::declval<_Pred>()); };
7344 }
7345
7346 struct _ChunkBy : __adaptor::_RangeAdaptor<_ChunkBy>
7347 {
7348 template<viewable_range _Range, typename _Pred>
7349 requires __detail::__can_chunk_by_view<_Range, _Pred>
7350 constexpr auto
7351 operator() [[nodiscard]] (_Range&& __r, _Pred&& __pred) const
7352 { return chunk_by_view(std::forward<_Range>(__r), std::forward<_Pred>(__pred)); }
7353
7354 using __adaptor::_RangeAdaptor<_ChunkBy>::operator();
7355 static constexpr int _S_arity = 2;
7356 static constexpr bool _S_has_simple_extra_args = true;
7357 };
7358
7359 inline constexpr _ChunkBy chunk_by;
7360 }
7361#endif // __cpp_lib_ranges_chunk_by
7362
7363#ifdef __cpp_lib_ranges_join_with // C++ >= 23
7364 namespace __detail
7365 {
7366 template<typename _Range, typename _Pattern>
7367 concept __compatible_joinable_ranges
7368 = common_with<range_value_t<_Range>, range_value_t<_Pattern>>
7369 && common_reference_with<range_reference_t<_Range>,
7370 range_reference_t<_Pattern>>
7371 && common_reference_with<range_rvalue_reference_t<_Range>,
7372 range_rvalue_reference_t<_Pattern>>;
7373
7374 template<typename _Range>
7375 concept __bidirectional_common = bidirectional_range<_Range> && common_range<_Range>;
7376 }
7377
7378 template<input_range _Vp, forward_range _Pattern>
7379 requires view<_Vp> && view<_Pattern>
7380 && input_range<range_reference_t<_Vp>>
7381 && __detail::__compatible_joinable_ranges<range_reference_t<_Vp>, _Pattern>
7382 class join_with_view : public view_interface<join_with_view<_Vp, _Pattern>>
7383 {
7384 using _InnerRange = range_reference_t<_Vp>;
7385
7386 _Vp _M_base = _Vp();
7387 [[no_unique_address]]
7388 __detail::__maybe_present_t<!forward_range<_Vp>,
7389 __detail::__non_propagating_cache<iterator_t<_Vp>>> _M_outer_it;
7390 __detail::__non_propagating_cache<remove_cv_t<_InnerRange>> _M_inner;
7391 _Pattern _M_pattern = _Pattern();
7392
7393 template<bool _Const> using _Base = __detail::__maybe_const_t<_Const, _Vp>;
7394 template<bool _Const> using _InnerBase = range_reference_t<_Base<_Const>>;
7395 template<bool _Const> using _PatternBase = __detail::__maybe_const_t<_Const, _Pattern>;
7396
7397 template<bool _Const> using _OuterIter = iterator_t<_Base<_Const>>;
7398 template<bool _Const> using _InnerIter = iterator_t<_InnerBase<_Const>>;
7399 template<bool _Const> using _PatternIter = iterator_t<_PatternBase<_Const>>;
7400
7401 template<bool _Const>
7402 static constexpr bool _S_ref_is_glvalue = is_reference_v<_InnerBase<_Const>>;
7403
7404 template<bool _Const>
7405 struct __iter_cat
7406 { };
7407
7408 template<bool _Const>
7409 requires _S_ref_is_glvalue<_Const>
7410 && forward_range<_Base<_Const>>
7411 && forward_range<_InnerBase<_Const>>
7412 struct __iter_cat<_Const>
7413 {
7414 private:
7415 static auto
7416 _S_iter_cat()
7417 {
7418 using _OuterIter = join_with_view::_OuterIter<_Const>;
7419 using _InnerIter = join_with_view::_InnerIter<_Const>;
7420 using _PatternIter = join_with_view::_PatternIter<_Const>;
7421 using _OuterCat = typename iterator_traits<_OuterIter>::iterator_category;
7422 using _InnerCat = typename iterator_traits<_InnerIter>::iterator_category;
7423 using _PatternCat = typename iterator_traits<_PatternIter>::iterator_category;
7424 // _GLIBCXX_RESOLVE_LIB_DEFECTS
7425 // 3798. Rvalue reference and iterator_category
7426 if constexpr (!is_reference_v<common_reference_t<iter_reference_t<_InnerIter>,
7427 iter_reference_t<_PatternIter>>>)
7428 return input_iterator_tag{};
7429 else if constexpr (derived_from<_OuterCat, bidirectional_iterator_tag>
7430 && derived_from<_InnerCat, bidirectional_iterator_tag>
7431 && derived_from<_PatternCat, bidirectional_iterator_tag>
7432 && common_range<_InnerBase<_Const>>
7433 && common_range<_PatternBase<_Const>>)
7434 return bidirectional_iterator_tag{};
7435 else if constexpr (derived_from<_OuterCat, forward_iterator_tag>
7436 && derived_from<_InnerCat, forward_iterator_tag>
7437 && derived_from<_PatternCat, forward_iterator_tag>)
7438 return forward_iterator_tag{};
7439 else
7440 return input_iterator_tag{};
7441 }
7442 public:
7443 using iterator_category = decltype(_S_iter_cat());
7444 };
7445
7446 template<bool> class _Iterator;
7447 template<bool> class _Sentinel;
7448
7449 public:
7450 join_with_view() requires (default_initializable<_Vp>
7451 && default_initializable<_Pattern>)
7452 = default;
7453
7454 constexpr explicit
7455 join_with_view(_Vp __base, _Pattern __pattern)
7456 : _M_base(std::move(__base)), _M_pattern(std::move(__pattern))
7457 { }
7458
7459 template<input_range _Range>
7460 requires constructible_from<_Vp, views::all_t<_Range>>
7461 && constructible_from<_Pattern, single_view<range_value_t<_InnerRange>>>
7462 constexpr explicit
7463 join_with_view(_Range&& __r, range_value_t<_InnerRange> __e)
7464 : _M_base(views::all(std::forward<_Range>(__r))),
7465 _M_pattern(views::single(std::move(__e)))
7466 { }
7467
7468 constexpr _Vp
7469 base() const& requires copy_constructible<_Vp>
7470 { return _M_base; }
7471
7472 constexpr _Vp
7473 base() &&
7474 { return std::move(_M_base); }
7475
7476 constexpr auto
7477 begin()
7478 {
7479 if constexpr (forward_range<_Vp>)
7480 {
7481 constexpr bool __use_const = is_reference_v<_InnerRange>
7482 && __detail::__simple_view<_Vp> && __detail::__simple_view<_Pattern>;
7483 return _Iterator<__use_const>{*this, ranges::begin(_M_base)};
7484 }
7485 else
7486 {
7487 _M_outer_it = ranges::begin(_M_base);
7488 return _Iterator<false>{*this};
7489 }
7490 }
7491
7492 constexpr auto
7493 begin() const
7494 requires forward_range<const _Vp>
7495 && forward_range<const _Pattern>
7496 && is_reference_v<range_reference_t<const _Vp>>
7497 && input_range<range_reference_t<const _Vp>>
7498 { return _Iterator<true>{*this, ranges::begin(_M_base)}; }
7499
7500 constexpr auto
7501 end()
7502 {
7503 constexpr bool __use_const
7504 = __detail::__simple_view<_Vp> && __detail::__simple_view<_Pattern>;
7505 if constexpr (is_reference_v<_InnerRange>
7506 && forward_range<_Vp> && common_range<_Vp>
7507 && forward_range<_InnerRange> && common_range<_InnerRange>)
7508 return _Iterator<__use_const>{*this, ranges::end(_M_base)};
7509 else
7510 return _Sentinel<__use_const>{*this};
7511 }
7512
7513 constexpr auto
7514 end() const
7515 requires forward_range<const _Vp>
7516 && forward_range<const _Pattern>
7517 && is_reference_v<range_reference_t<const _Vp>>
7518 && input_range<range_reference_t<const _Vp>>
7519 {
7520 using _InnerConstRange = range_reference_t<const _Vp>;
7521 if constexpr (forward_range<_InnerConstRange>
7522 && common_range<const _Vp>
7523 && common_range<_InnerConstRange>)
7524 return _Iterator<true>{*this, ranges::end(_M_base)};
7525 else
7526 return _Sentinel<true>{*this};
7527 }
7528 };
7529
7530 template<typename _Range, typename _Pattern>
7531 join_with_view(_Range&&, _Pattern&&)
7532 -> join_with_view<views::all_t<_Range>, views::all_t<_Pattern>>;
7533
7534 template<input_range _Range>
7535 join_with_view(_Range&&, range_value_t<range_reference_t<_Range>>)
7536 -> join_with_view<views::all_t<_Range>,
7537 single_view<range_value_t<range_reference_t<_Range>>>>;
7538
7539 template<input_range _Vp, forward_range _Pattern>
7540 requires view<_Vp> && view<_Pattern>
7541 && input_range<range_reference_t<_Vp>>
7542 && __detail::__compatible_joinable_ranges<range_reference_t<_Vp>, _Pattern>
7543 template<bool _Const>
7544 class join_with_view<_Vp, _Pattern>::_Iterator : public __iter_cat<_Const>
7545 {
7546 using _Parent = __detail::__maybe_const_t<_Const, join_with_view>;
7547 using _Base = join_with_view::_Base<_Const>;
7548 using _InnerBase = join_with_view::_InnerBase<_Const>;
7549 using _PatternBase = join_with_view::_PatternBase<_Const>;
7550
7551 using _OuterIter = join_with_view::_OuterIter<_Const>;
7552 using _InnerIter = join_with_view::_InnerIter<_Const>;
7553 using _PatternIter = join_with_view::_PatternIter<_Const>;
7554
7555 static constexpr bool _S_ref_is_glvalue = join_with_view::_S_ref_is_glvalue<_Const>;
7556
7557 _Parent* _M_parent = nullptr;
7558 [[no_unique_address]]
7559 __detail::__maybe_present_t<forward_range<_Base>, _OuterIter> _M_outer_it
7560 = decltype(_M_outer_it)();
7561 variant<_PatternIter, _InnerIter> _M_inner_it;
7562
7563 constexpr _OuterIter&
7564 _M_get_outer()
7565 {
7566 if constexpr (forward_range<_Base>)
7567 return _M_outer_it;
7568 else
7569 return *_M_parent->_M_outer_it;
7570 }
7571
7572 constexpr const _OuterIter&
7573 _M_get_outer() const
7574 {
7575 if constexpr (forward_range<_Base>)
7576 return _M_outer_it;
7577 else
7578 return *_M_parent->_M_outer_it;
7579 }
7580
7581 constexpr
7582 _Iterator(_Parent& __parent, _OuterIter __outer)
7583 requires forward_range<_Base>
7584 : _M_parent(std::__addressof(__parent)), _M_outer_it(std::move(__outer))
7585 {
7586 if (_M_get_outer() != ranges::end(_M_parent->_M_base))
7587 {
7588 auto&& __inner = _M_update_inner();
7589 _M_inner_it.template emplace<1>(ranges::begin(__inner));
7590 _M_satisfy();
7591 }
7592 }
7593
7594 constexpr
7595 _Iterator(_Parent& __parent)
7596 requires (!forward_range<_Base>)
7597 : _M_parent(std::__addressof(__parent))
7598 {
7599 if (_M_get_outer() != ranges::end(_M_parent->_M_base))
7600 {
7601 auto&& __inner = _M_update_inner();
7602 _M_inner_it.template emplace<1>(ranges::begin(__inner));
7603 _M_satisfy();
7604 }
7605 }
7606
7607 constexpr auto&
7608 _M_update_inner()
7609 {
7610 _OuterIter& __outer = _M_get_outer();
7611 if constexpr (_S_ref_is_glvalue)
7612 return __detail::__as_lvalue(*__outer);
7613 else
7614 return _M_parent->_M_inner._M_emplace_deref(__outer);
7615 }
7616
7617 constexpr auto&
7618 _M_get_inner()
7619 {
7620 if constexpr (_S_ref_is_glvalue)
7621 return __detail::__as_lvalue(*_M_get_outer());
7622 else
7623 return *_M_parent->_M_inner;
7624 }
7625
7626 constexpr void
7627 _M_satisfy()
7628 {
7629 while (true)
7630 {
7631 if (_M_inner_it.index() == 0)
7632 {
7633 if (std::get<0>(_M_inner_it) != ranges::end(_M_parent->_M_pattern))
7634 break;
7635
7636 auto&& __inner = _M_update_inner();
7637 _M_inner_it.template emplace<1>(ranges::begin(__inner));
7638 }
7639 else
7640 {
7641 auto&& __inner = _M_get_inner();
7642 if (std::get<1>(_M_inner_it) != ranges::end(__inner))
7643 break;
7644
7645 if (++_M_get_outer() == ranges::end(_M_parent->_M_base))
7646 {
7647 if constexpr (_S_ref_is_glvalue)
7648 _M_inner_it.template emplace<0>();
7649 break;
7650 }
7651
7652 _M_inner_it.template emplace<0>(ranges::begin(_M_parent->_M_pattern));
7653 }
7654 }
7655 }
7656
7657 static auto
7658 _S_iter_concept()
7659 {
7660 if constexpr (_S_ref_is_glvalue
7661 && bidirectional_range<_Base>
7662 && __detail::__bidirectional_common<_InnerBase>
7663 && __detail::__bidirectional_common<_PatternBase>)
7664 return bidirectional_iterator_tag{};
7665 else if constexpr (_S_ref_is_glvalue
7666 && forward_range<_Base>
7667 && forward_range<_InnerBase>)
7668 return forward_iterator_tag{};
7669 else
7670 return input_iterator_tag{};
7671 }
7672
7673 friend join_with_view;
7674
7675 public:
7676 using iterator_concept = decltype(_S_iter_concept());
7677 // iterator_category defined in join_with_view::__iter_cat
7678 using value_type = common_type_t<iter_value_t<_InnerIter>,
7679 iter_value_t<_PatternIter>>;
7680 using difference_type = common_type_t<iter_difference_t<_OuterIter>,
7681 iter_difference_t<_InnerIter>,
7682 iter_difference_t<_PatternIter>>;
7683
7684 _Iterator() = default;
7685
7686 constexpr
7687 _Iterator(_Iterator<!_Const> __i)
7688 requires _Const
7689 && convertible_to<iterator_t<_Vp>, _OuterIter>
7690 && convertible_to<iterator_t<_InnerRange>, _InnerIter>
7691 && convertible_to<iterator_t<_Pattern>, _PatternIter>
7692 : _M_parent(__i._M_parent),
7693 _M_outer_it(std::move(__i._M_outer_it))
7694 {
7695 if (__i._M_inner_it.index() == 0)
7696 _M_inner_it.template emplace<0>(std::get<0>(std::move(__i._M_inner_it)));
7697 else
7698 _M_inner_it.template emplace<1>(std::get<1>(std::move(__i._M_inner_it)));
7699 }
7700
7701 constexpr common_reference_t<iter_reference_t<_InnerIter>,
7702 iter_reference_t<_PatternIter>>
7703 operator*() const
7704 {
7705 if (_M_inner_it.index() == 0)
7706 return *std::get<0>(_M_inner_it);
7707 else
7708 return *std::get<1>(_M_inner_it);
7709 }
7710
7711 constexpr _Iterator&
7712 operator++()
7713 {
7714 if (_M_inner_it.index() == 0)
7715 ++std::get<0>(_M_inner_it);
7716 else
7717 ++std::get<1>(_M_inner_it);
7718 _M_satisfy();
7719 return *this;
7720 }
7721
7722 constexpr void
7723 operator++(int)
7724 { ++*this; }
7725
7726 constexpr _Iterator
7727 operator++(int)
7728 requires _S_ref_is_glvalue
7729 && forward_iterator<_OuterIter> && forward_iterator<_InnerIter>
7730 {
7731 _Iterator __tmp = *this;
7732 ++*this;
7733 return __tmp;
7734 }
7735
7736 constexpr _Iterator&
7737 operator--()
7738 requires _S_ref_is_glvalue
7739 && bidirectional_range<_Base>
7740 && __detail::__bidirectional_common<_InnerBase>
7741 && __detail::__bidirectional_common<_PatternBase>
7742 {
7743 if (_M_outer_it == ranges::end(_M_parent->_M_base))
7744 {
7745 auto&& __inner = *--_M_outer_it;
7746 _M_inner_it.template emplace<1>(ranges::end(__inner));
7747 }
7748
7749 while (true)
7750 {
7751 if (_M_inner_it.index() == 0)
7752 {
7753 auto& __it = std::get<0>(_M_inner_it);
7754 if (__it == ranges::begin(_M_parent->_M_pattern))
7755 {
7756 auto&& __inner = *--_M_outer_it;
7757 _M_inner_it.template emplace<1>(ranges::end(__inner));
7758 }
7759 else
7760 break;
7761 }
7762 else
7763 {
7764 auto& __it = std::get<1>(_M_inner_it);
7765 auto&& __inner = *_M_outer_it;
7766 if (__it == ranges::begin(__inner))
7767 _M_inner_it.template emplace<0>(ranges::end(_M_parent->_M_pattern));
7768 else
7769 break;
7770 }
7771 }
7772
7773 if (_M_inner_it.index() == 0)
7774 --std::get<0>(_M_inner_it);
7775 else
7776 --std::get<1>(_M_inner_it);
7777 return *this;
7778 }
7779
7780 constexpr _Iterator
7781 operator--(int)
7782 requires _S_ref_is_glvalue && bidirectional_range<_Base>
7783 && __detail::__bidirectional_common<_InnerBase>
7784 && __detail::__bidirectional_common<_PatternBase>
7785 {
7786 _Iterator __tmp = *this;
7787 --*this;
7788 return __tmp;
7789 }
7790
7791 friend constexpr bool
7792 operator==(const _Iterator& __x, const _Iterator& __y)
7793 requires _S_ref_is_glvalue
7794 && forward_range<_Base> && equality_comparable<_InnerIter>
7795 { return __x._M_outer_it == __y._M_outer_it && __x._M_inner_it ==__y._M_inner_it; }
7796
7797 friend constexpr common_reference_t<iter_rvalue_reference_t<_InnerIter>,
7798 iter_rvalue_reference_t<_PatternIter>>
7799 iter_move(const _Iterator& __x)
7800 {
7801 if (__x._M_inner_it.index() == 0)
7802 return ranges::iter_move(std::get<0>(__x._M_inner_it));
7803 else
7804 return ranges::iter_move(std::get<1>(__x._M_inner_it));
7805 }
7806
7807 friend constexpr void
7808 iter_swap(const _Iterator& __x, const _Iterator& __y)
7809 requires indirectly_swappable<_InnerIter, _PatternIter>
7810 {
7811 if (__x._M_inner_it.index() == 0)
7812 {
7813 if (__y._M_inner_it.index() == 0)
7814 ranges::iter_swap(std::get<0>(__x._M_inner_it), std::get<0>(__y._M_inner_it));
7815 else
7816 ranges::iter_swap(std::get<0>(__x._M_inner_it), std::get<1>(__y._M_inner_it));
7817 }
7818 else
7819 {
7820 if (__y._M_inner_it.index() == 0)
7821 ranges::iter_swap(std::get<1>(__x._M_inner_it), std::get<0>(__y._M_inner_it));
7822 else
7823 ranges::iter_swap(std::get<1>(__x._M_inner_it), std::get<1>(__y._M_inner_it));
7824 }
7825 }
7826 };
7827
7828 template<input_range _Vp, forward_range _Pattern>
7829 requires view<_Vp> && view<_Pattern>
7830 && input_range<range_reference_t<_Vp>>
7831 && __detail::__compatible_joinable_ranges<range_reference_t<_Vp>, _Pattern>
7832 template<bool _Const>
7833 class join_with_view<_Vp, _Pattern>::_Sentinel
7834 {
7835 using _Parent = __detail::__maybe_const_t<_Const, join_with_view>;
7836 using _Base = join_with_view::_Base<_Const>;
7837
7838 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
7839
7840 constexpr explicit
7841 _Sentinel(_Parent& __parent)
7842 : _M_end(ranges::end(__parent._M_base))
7843 { }
7844
7845 friend join_with_view;
7846
7847 public:
7848 _Sentinel() = default;
7849
7850 constexpr
7851 _Sentinel(_Sentinel<!_Const> __s)
7852 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
7853 : _M_end(std::move(__s._M_end))
7854 { }
7855
7856 template<bool _OtherConst>
7857 requires sentinel_for<sentinel_t<_Base>,
7858 iterator_t<__detail::__maybe_const_t<_OtherConst, _Vp>>>
7859 friend constexpr bool
7860 operator==(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
7861 { return __x._M_get_outer() == __y._M_end; }
7862 };
7863
7864 namespace views
7865 {
7866 namespace __detail
7867 {
7868 template<typename _Range, typename _Pattern>
7869 concept __can_join_with_view
7870 = requires { join_with_view(std::declval<_Range>(), std::declval<_Pattern>()); };
7871 } // namespace __detail
7872
7873 struct _JoinWith : __adaptor::_RangeAdaptor<_JoinWith>
7874 {
7875 template<viewable_range _Range, typename _Pattern>
7876 requires __detail::__can_join_with_view<_Range, _Pattern>
7877 constexpr auto
7878 operator() [[nodiscard]] (_Range&& __r, _Pattern&& __f) const
7879 {
7880 return join_with_view(std::forward<_Range>(__r), std::forward<_Pattern>(__f));
7881 }
7882
7883 using _RangeAdaptor<_JoinWith>::operator();
7884 static constexpr int _S_arity = 2;
7885 template<typename _Pattern>
7886 static constexpr bool _S_has_simple_extra_args
7887 = _LazySplit::_S_has_simple_extra_args<_Pattern>;
7888 };
7889
7890 inline constexpr _JoinWith join_with;
7891 } // namespace views
7892#endif // __cpp_lib_ranges_join_with
7893
7894#ifdef __cpp_lib_ranges_repeat // C++ >= 23
7895 template<move_constructible _Tp, semiregular _Bound = unreachable_sentinel_t>
7896 requires is_object_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>
7897 && (__detail::__is_integer_like<_Bound> || same_as<_Bound, unreachable_sentinel_t>)
7898 class repeat_view : public view_interface<repeat_view<_Tp, _Bound>>
7899 {
7900 [[no_unique_address]] __detail::__box<_Tp> _M_value;
7901 [[no_unique_address]] _Bound _M_bound = _Bound();
7902
7903 class _Iterator;
7904
7905 template<typename _Range>
7906 friend constexpr auto
7907 views::__detail::__take_of_repeat_view(_Range&&, range_difference_t<_Range>);
7908
7909 template<typename _Range>
7910 friend constexpr auto
7911 views::__detail::__drop_of_repeat_view(_Range&&, range_difference_t<_Range>);
7912
7913 public:
7914 repeat_view() requires default_initializable<_Tp> = default;
7915
7916 constexpr explicit
7917 repeat_view(const _Tp& __value, _Bound __bound = _Bound())
7918 requires copy_constructible<_Tp>
7919 : _M_value(__value), _M_bound(__bound)
7920 {
7921 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
7922 __glibcxx_assert(__bound >= 0);
7923 }
7924
7925 constexpr explicit
7926 repeat_view(_Tp&& __value, _Bound __bound = _Bound())
7927 : _M_value(std::move(__value)), _M_bound(__bound)
7928 { }
7929
7930 template<typename... _Args, typename... _BoundArgs>
7931 requires constructible_from<_Tp, _Args...>
7932 && constructible_from<_Bound, _BoundArgs...>
7933 constexpr explicit
7934 repeat_view(piecewise_construct_t,
7935 tuple<_Args...> __args,
7936 tuple<_BoundArgs...> __bound_args = tuple<>{})
7937 : _M_value(std::make_from_tuple<_Tp>(std::move(__args))),
7938 _M_bound(std::make_from_tuple<_Bound>(std::move(__bound_args)))
7939 { }
7940
7941 constexpr _Iterator
7942 begin() const
7943 { return _Iterator(std::__addressof(*_M_value)); }
7944
7945 constexpr _Iterator
7946 end() const requires (!same_as<_Bound, unreachable_sentinel_t>)
7947 { return _Iterator(std::__addressof(*_M_value), _M_bound); }
7948
7949 constexpr unreachable_sentinel_t
7950 end() const noexcept
7951 { return unreachable_sentinel; }
7952
7953 constexpr auto
7954 size() const requires (!same_as<_Bound, unreachable_sentinel_t>)
7955 { return __detail::__to_unsigned_like(_M_bound); }
7956 };
7957
7958 // _GLIBCXX_RESOLVE_LIB_DEFECTS
7959 // 4053. Unary call to std::views::repeat does not decay the argument
7960 template<typename _Tp, typename _Bound = unreachable_sentinel_t>
7961 repeat_view(_Tp, _Bound = _Bound()) -> repeat_view<_Tp, _Bound>;
7962
7963 template<move_constructible _Tp, semiregular _Bound>
7964 requires is_object_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>
7965 && (__detail::__is_integer_like<_Bound> || same_as<_Bound, unreachable_sentinel_t>)
7966 class repeat_view<_Tp, _Bound>::_Iterator
7967 {
7968 using __index_type
7969 = __conditional_t<same_as<_Bound, unreachable_sentinel_t>, ptrdiff_t, _Bound>;
7970
7971 const _Tp* _M_value = nullptr;
7972 __index_type _M_current = __index_type();
7973
7974 constexpr explicit
7975 _Iterator(const _Tp* __value, __index_type __bound = __index_type())
7976 : _M_value(__value), _M_current(__bound)
7977 {
7978 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
7979 __glibcxx_assert(__bound >= 0);
7980 }
7981
7982 friend repeat_view;
7983
7984 public:
7985 using iterator_concept = random_access_iterator_tag;
7986 using iterator_category = random_access_iterator_tag;
7987 using value_type = _Tp;
7988 using difference_type = __conditional_t<__detail::__is_signed_integer_like<__index_type>,
7989 __index_type,
7990 __detail::__iota_diff_t<__index_type>>;
7991
7992 _Iterator() = default;
7993
7994 constexpr const _Tp&
7995 operator*() const noexcept
7996 { return *_M_value; }
7997
7998 constexpr _Iterator&
7999 operator++()
8000 {
8001 ++_M_current;
8002 return *this;
8003 }
8004
8005 constexpr _Iterator
8006 operator++(int)
8007 {
8008 auto __tmp = *this;
8009 ++*this;
8010 return __tmp;
8011 }
8012
8013 constexpr _Iterator&
8014 operator--()
8015 {
8016 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
8017 __glibcxx_assert(_M_current > 0);
8018 --_M_current;
8019 return *this;
8020 }
8021
8022 constexpr _Iterator
8023 operator--(int)
8024 {
8025 auto __tmp = *this;
8026 --*this;
8027 return __tmp;
8028 }
8029
8030 constexpr _Iterator&
8031 operator+=(difference_type __n)
8032 {
8033 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
8034 __glibcxx_assert(_M_current + __n >= 0);
8035 _M_current += __n;
8036 return *this;
8037 }
8038
8039 constexpr _Iterator&
8040 operator-=(difference_type __n)
8041 {
8042 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
8043 __glibcxx_assert(_M_current - __n >= 0);
8044 _M_current -= __n;
8045 return *this;
8046 }
8047
8048 constexpr const _Tp&
8049 operator[](difference_type __n) const noexcept
8050 { return *(*this + __n); }
8051
8052 friend constexpr bool
8053 operator==(const _Iterator& __x, const _Iterator& __y)
8054 { return __x._M_current == __y._M_current; }
8055
8056 friend constexpr auto
8057 operator<=>(const _Iterator& __x, const _Iterator& __y)
8058 { return __x._M_current <=> __y._M_current; }
8059
8060 friend constexpr _Iterator
8061 operator+(_Iterator __i, difference_type __n)
8062 {
8063 __i += __n;
8064 return __i;
8065 }
8066
8067 friend constexpr _Iterator
8068 operator+(difference_type __n, _Iterator __i)
8069 { return __i + __n; }
8070
8071 friend constexpr _Iterator
8072 operator-(_Iterator __i, difference_type __n)
8073 {
8074 __i -= __n;
8075 return __i;
8076 }
8077
8078 friend constexpr difference_type
8079 operator-(const _Iterator& __x, const _Iterator& __y)
8080 {
8081 return (static_cast<difference_type>(__x._M_current)
8082 - static_cast<difference_type>(__y._M_current));
8083 }
8084 };
8085
8086 namespace views
8087 {
8088 namespace __detail
8089 {
8090 template<typename _Tp, typename _Bound>
8091 inline constexpr bool __is_repeat_view<repeat_view<_Tp, _Bound>> = true;
8092
8093 template<typename _Tp>
8094 concept __can_repeat_view
8095 = requires { repeat_view(std::declval<_Tp>()); };
8096
8097 template<typename _Tp, typename _Bound>
8098 concept __can_bounded_repeat_view
8099 = requires { repeat_view(std::declval<_Tp>(), std::declval<_Bound>()); };
8100 }
8101
8102 struct _Repeat
8103 {
8104 template<typename _Tp>
8105 requires __detail::__can_repeat_view<_Tp>
8106 constexpr auto
8107 operator() [[nodiscard]] (_Tp&& __value) const
8108 {
8109 // _GLIBCXX_RESOLVE_LIB_DEFECTS
8110 // 4054. Repeating a repeat_view should repeat the view
8111 return repeat_view<decay_t<_Tp>>(std::forward<_Tp>(__value));
8112 }
8113
8114 template<typename _Tp, typename _Bound>
8115 requires __detail::__can_bounded_repeat_view<_Tp, _Bound>
8116 constexpr auto
8117 operator() [[nodiscard]] (_Tp&& __value, _Bound __bound) const
8118 { return repeat_view(std::forward<_Tp>(__value), __bound); }
8119 };
8120
8121 inline constexpr _Repeat repeat;
8122
8123 namespace __detail
8124 {
8125 template<typename _Range>
8126 constexpr auto
8127 __take_of_repeat_view(_Range&& __r, range_difference_t<_Range> __n)
8128 {
8129 using _Tp = remove_cvref_t<_Range>;
8130 static_assert(__is_repeat_view<_Tp>);
8131 if constexpr (sized_range<_Tp>)
8132 return views::repeat(*std::forward<_Range>(__r)._M_value,
8133 std::min(ranges::distance(__r), __n));
8134 else
8135 return views::repeat(*std::forward<_Range>(__r)._M_value, __n);
8136 }
8137
8138 template<typename _Range>
8139 constexpr auto
8140 __drop_of_repeat_view(_Range&& __r, range_difference_t<_Range> __n)
8141 {
8142 using _Tp = remove_cvref_t<_Range>;
8143 static_assert(__is_repeat_view<_Tp>);
8144 if constexpr (sized_range<_Tp>)
8145 {
8146 auto __sz = ranges::distance(__r);
8147 return views::repeat(*std::forward<_Range>(__r)._M_value,
8148 __sz - std::min(__sz, __n));
8149 }
8150 else
8151 return __r;
8152 }
8153 }
8154 }
8155#endif // __cpp_lib_ranges_repeat
8156
8157#ifdef __cpp_lib_ranges_stride // C++ >= 23
8158 template<input_range _Vp>
8159 requires view<_Vp>
8160 class stride_view : public view_interface<stride_view<_Vp>>
8161 {
8162 _Vp _M_base;
8163 range_difference_t<_Vp> _M_stride;
8164
8165 template<bool _Const> using _Base = __detail::__maybe_const_t<_Const, _Vp>;
8166
8167 template<bool _Const>
8168 struct __iter_cat
8169 { };
8170
8171 template<bool _Const>
8172 requires forward_range<_Base<_Const>>
8173 struct __iter_cat<_Const>
8174 {
8175 private:
8176 static auto
8177 _S_iter_cat()
8178 {
8179 using _Cat = typename iterator_traits<iterator_t<_Base<_Const>>>::iterator_category;
8180 if constexpr (derived_from<_Cat, random_access_iterator_tag>)
8181 return random_access_iterator_tag{};
8182 else
8183 return _Cat{};
8184 }
8185 public:
8186 using iterator_category = decltype(_S_iter_cat());
8187 };
8188
8189 template<bool> class _Iterator;
8190
8191 public:
8192 constexpr explicit
8193 stride_view(_Vp __base, range_difference_t<_Vp> __stride)
8194 : _M_base(std::move(__base)), _M_stride(__stride)
8195 { __glibcxx_assert(__stride > 0); }
8196
8197 constexpr _Vp
8198 base() const& requires copy_constructible<_Vp>
8199 { return _M_base; }
8200
8201 constexpr _Vp
8202 base() &&
8203 { return std::move(_M_base); }
8204
8205 constexpr range_difference_t<_Vp>
8206 stride() const noexcept
8207 { return _M_stride; }
8208
8209 constexpr auto
8210 begin() requires (!__detail::__simple_view<_Vp>)
8211 { return _Iterator<false>(this, ranges::begin(_M_base)); }
8212
8213 constexpr auto
8214 begin() const requires range<const _Vp>
8215 { return _Iterator<true>(this, ranges::begin(_M_base)); }
8216
8217 constexpr auto
8218 end() requires (!__detail::__simple_view<_Vp>)
8219 {
8220 if constexpr (common_range<_Vp> && sized_range<_Vp> && forward_range<_Vp>)
8221 {
8222 auto __missing = (_M_stride - ranges::distance(_M_base) % _M_stride) % _M_stride;
8223 return _Iterator<false>(this, ranges::end(_M_base), __missing);
8224 }
8225 else if constexpr (common_range<_Vp> && !bidirectional_range<_Vp>)
8226 return _Iterator<false>(this, ranges::end(_M_base));
8227 else
8228 return default_sentinel;
8229 }
8230
8231 constexpr auto
8232 end() const requires range<const _Vp>
8233 {
8234 if constexpr (common_range<const _Vp> && sized_range<const _Vp>
8235 && forward_range<const _Vp>)
8236 {
8237 auto __missing = (_M_stride - ranges::distance(_M_base) % _M_stride) % _M_stride;
8238 return _Iterator<true>(this, ranges::end(_M_base), __missing);
8239 }
8240 else if constexpr (common_range<const _Vp> && !bidirectional_range<const _Vp>)
8241 return _Iterator<true>(this, ranges::end(_M_base));
8242 else
8243 return default_sentinel;
8244 }
8245
8246 constexpr auto
8247 size() requires sized_range<_Vp>
8248 {
8249 return __detail::__to_unsigned_like
8250 (__detail::__div_ceil(ranges::distance(_M_base), _M_stride));
8251 }
8252
8253 constexpr auto
8254 size() const requires sized_range<const _Vp>
8255 {
8256 return __detail::__to_unsigned_like
8257 (__detail::__div_ceil(ranges::distance(_M_base), _M_stride));
8258 }
8259 };
8260
8261 template<typename _Range>
8262 stride_view(_Range&&, range_difference_t<_Range>) -> stride_view<views::all_t<_Range>>;
8263
8264 template<typename _Vp>
8265 inline constexpr bool enable_borrowed_range<stride_view<_Vp>>
8266 = enable_borrowed_range<_Vp>;
8267
8268 template<input_range _Vp>
8269 requires view<_Vp>
8270 template<bool _Const>
8271 class stride_view<_Vp>::_Iterator : public __iter_cat<_Const>
8272 {
8273 using _Parent = __detail::__maybe_const_t<_Const, stride_view>;
8274 using _Base = stride_view::_Base<_Const>;
8275
8276 iterator_t<_Base> _M_current = iterator_t<_Base>();
8277 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
8278 range_difference_t<_Base> _M_stride = 0;
8279 range_difference_t<_Base> _M_missing = 0;
8280
8281 constexpr
8282 _Iterator(_Parent* __parent, iterator_t<_Base> __current,
8283 range_difference_t<_Base> __missing = 0)
8284 : _M_current(std::move(__current)), _M_end(ranges::end(__parent->_M_base)),
8285 _M_stride(__parent->_M_stride), _M_missing(__missing)
8286 { }
8287
8288 static auto
8289 _S_iter_concept()
8290 {
8291 if constexpr (random_access_range<_Base>)
8292 return random_access_iterator_tag{};
8293 else if constexpr (bidirectional_range<_Base>)
8294 return bidirectional_iterator_tag{};
8295 else if constexpr (forward_range<_Base>)
8296 return forward_iterator_tag{};
8297 else
8298 return input_iterator_tag{};
8299 }
8300
8301 friend stride_view;
8302
8303 public:
8304 using difference_type = range_difference_t<_Base>;
8305 using value_type = range_value_t<_Base>;
8306 using iterator_concept = decltype(_S_iter_concept());
8307 // iterator_category defined in stride_view::__iter_cat
8308
8309 _Iterator() requires default_initializable<iterator_t<_Base>> = default;
8310
8311 constexpr
8312 _Iterator(_Iterator<!_Const> __other)
8313 requires _Const
8314 && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
8315 && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
8316 : _M_current(std::move(__other._M_current)), _M_end(std::move(__other._M_end)),
8317 _M_stride(__other._M_stride), _M_missing(__other._M_missing)
8318 { }
8319
8320 constexpr iterator_t<_Base>
8321 base() &&
8322 { return std::move(_M_current); }
8323
8324 constexpr const iterator_t<_Base>&
8325 base() const & noexcept
8326 { return _M_current; }
8327
8328 constexpr decltype(auto)
8329 operator*() const
8330 { return *_M_current; }
8331
8332 constexpr _Iterator&
8333 operator++()
8334 {
8335 __glibcxx_assert(_M_current != _M_end);
8336 _M_missing = ranges::advance(_M_current, _M_stride, _M_end);
8337 return *this;
8338 }
8339
8340 constexpr void
8341 operator++(int)
8342 { ++*this; }
8343
8344 constexpr _Iterator
8345 operator++(int) requires forward_range<_Base>
8346 {
8347 auto __tmp = *this;
8348 ++*this;
8349 return __tmp;
8350 }
8351
8352 constexpr _Iterator&
8353 operator--() requires bidirectional_range<_Base>
8354 {
8355 ranges::advance(_M_current, _M_missing - _M_stride);
8356 _M_missing = 0;
8357 return *this;
8358 }
8359
8360 constexpr _Iterator
8361 operator--(int) requires bidirectional_range<_Base>
8362 {
8363 auto __tmp = *this;
8364 --*this;
8365 return __tmp;
8366 }
8367
8368 constexpr _Iterator&
8369 operator+=(difference_type __n) requires random_access_range<_Base>
8370 {
8371 if (__n > 0)
8372 {
8373 __glibcxx_assert(ranges::distance(_M_current, _M_end) > _M_stride * (__n - 1));
8374 _M_missing = ranges::advance(_M_current, _M_stride * __n, _M_end);
8375 }
8376 else if (__n < 0)
8377 {
8378 ranges::advance(_M_current, _M_stride * __n + _M_missing);
8379 _M_missing = 0;
8380 }
8381 return *this;
8382 }
8383
8384 constexpr _Iterator&
8385 operator-=(difference_type __n) requires random_access_range<_Base>
8386 { return *this += -__n; }
8387
8388 constexpr decltype(auto) operator[](difference_type __n) const
8389 requires random_access_range<_Base>
8390 { return *(*this + __n); }
8391
8392 friend constexpr bool
8393 operator==(const _Iterator& __x, default_sentinel_t)
8394 { return __x._M_current == __x._M_end; }
8395
8396 friend constexpr bool
8397 operator==(const _Iterator& __x, const _Iterator& __y)
8398 requires equality_comparable<iterator_t<_Base>>
8399 { return __x._M_current == __y._M_current; }
8400
8401 friend constexpr bool
8402 operator<(const _Iterator& __x, const _Iterator& __y)
8403 requires random_access_range<_Base>
8404 { return __x._M_current < __y._M_current; }
8405
8406 friend constexpr bool
8407 operator>(const _Iterator& __x, const _Iterator& __y)
8408 requires random_access_range<_Base>
8409 { return __y._M_current < __x._M_current; }
8410
8411 friend constexpr bool
8412 operator<=(const _Iterator& __x, const _Iterator& __y)
8413 requires random_access_range<_Base>
8414 { return !(__y._M_current < __x._M_current); }
8415
8416 friend constexpr bool
8417 operator>=(const _Iterator& __x, const _Iterator& __y)
8418 requires random_access_range<_Base>
8419 { return !(__x._M_current < __y._M_current); }
8420
8421 friend constexpr auto
8422 operator<=>(const _Iterator& __x, const _Iterator& __y)
8423 requires random_access_range<_Base> && three_way_comparable<iterator_t<_Base>>
8424 { return __x._M_current <=> __y._M_current; }
8425
8426 friend constexpr _Iterator
8427 operator+(const _Iterator& __i, difference_type __n)
8428 requires random_access_range<_Base>
8429 {
8430 auto __r = __i;
8431 __r += __n;
8432 return __r;
8433 }
8434
8435 friend constexpr _Iterator
8436 operator+(difference_type __n, const _Iterator& __i)
8437 requires random_access_range<_Base>
8438 { return __i + __n; }
8439
8440 friend constexpr _Iterator
8441 operator-(const _Iterator& __i, difference_type __n)
8442 requires random_access_range<_Base>
8443 {
8444 auto __r = __i;
8445 __r -= __n;
8446 return __r;
8447 }
8448
8449 friend constexpr difference_type
8450 operator-(const _Iterator& __x, const _Iterator& __y)
8451 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
8452 {
8453 auto __n = __x._M_current - __y._M_current;
8454 if constexpr (forward_range<_Base>)
8455 return (__n + __x._M_missing - __y._M_missing) / __x._M_stride;
8456 else if (__n < 0)
8457 return -__detail::__div_ceil(-__n, __x._M_stride);
8458 else
8459 return __detail::__div_ceil(__n, __x._M_stride);
8460 }
8461
8462 friend constexpr difference_type
8463 operator-(default_sentinel_t, const _Iterator& __x)
8464 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base>>
8465 { return __detail::__div_ceil(__x._M_end - __x._M_current, __x._M_stride); }
8466
8467 friend constexpr difference_type
8468 operator-(const _Iterator& __x, default_sentinel_t __y)
8469 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base>>
8470 { return -(__y - __x); }
8471
8472 friend constexpr range_rvalue_reference_t<_Base>
8473 iter_move(const _Iterator& __i)
8474 noexcept(noexcept(ranges::iter_move(__i._M_current)))
8475 { return ranges::iter_move(__i._M_current); }
8476
8477 friend constexpr void
8478 iter_swap(const _Iterator& __x, const _Iterator& __y)
8479 noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
8480 requires indirectly_swappable<iterator_t<_Base>>
8481 { ranges::iter_swap(__x._M_current, __y._M_current); }
8482 };
8483
8484 namespace views
8485 {
8486 namespace __detail
8487 {
8488 template<typename _Range, typename _Dp>
8489 concept __can_stride_view
8490 = requires { stride_view(std::declval<_Range>(), std::declval<_Dp>()); };
8491 }
8492
8493 struct _Stride : __adaptor::_RangeAdaptor<_Stride>
8494 {
8495 template<viewable_range _Range, typename _Dp = range_difference_t<_Range>>
8496 requires __detail::__can_stride_view<_Range, _Dp>
8497 constexpr auto
8498 operator() [[nodiscard]] (_Range&& __r, type_identity_t<_Dp> __n) const
8499 { return stride_view(std::forward<_Range>(__r), __n); }
8500
8501 using __adaptor::_RangeAdaptor<_Stride>::operator();
8502 static constexpr int _S_arity = 2;
8503 static constexpr bool _S_has_simple_extra_args = true;
8504 };
8505
8506 inline constexpr _Stride stride;
8507 }
8508#endif // __cpp_lib_ranges_stride
8509
8510#ifdef __cpp_lib_ranges_cartesian_product // C++ >= 23
8511 namespace __detail
8512 {
8513 template<bool _Const, typename _First, typename... _Vs>
8514 concept __cartesian_product_is_random_access
8515 = (random_access_range<__maybe_const_t<_Const, _First>>
8516 && ...
8517 && (random_access_range<__maybe_const_t<_Const, _Vs>>
8518 && sized_range<__maybe_const_t<_Const, _Vs>>));
8519
8520 template<typename _Range>
8521 concept __cartesian_product_common_arg
8522 = common_range<_Range> || (sized_range<_Range> && random_access_range<_Range>);
8523
8524 template<bool _Const, typename _First, typename... _Vs>
8525 concept __cartesian_product_is_bidirectional
8526 = (bidirectional_range<__maybe_const_t<_Const, _First>>
8527 && ...
8528 && (bidirectional_range<__maybe_const_t<_Const, _Vs>>
8529 && __cartesian_product_common_arg<__maybe_const_t<_Const, _Vs>>));
8530
8531 template<typename _First, typename... _Vs>
8532 concept __cartesian_product_is_common = __cartesian_product_common_arg<_First>;
8533
8534 template<typename... _Vs>
8535 concept __cartesian_product_is_sized = (sized_range<_Vs> && ...);
8536
8537 template<bool _Const, template<typename> class _FirstSent,
8538 typename _First, typename... _Vs>
8539 concept __cartesian_is_sized_sentinel
8540 = (sized_sentinel_for<_FirstSent<__maybe_const_t<_Const, _First>>,
8541 iterator_t<__maybe_const_t<_Const, _First>>>
8542 && ...
8543 && (sized_range<__maybe_const_t<_Const, _Vs>>
8544 && sized_sentinel_for<iterator_t<__maybe_const_t<_Const, _Vs>>,
8545 iterator_t<__maybe_const_t<_Const, _Vs>>>));
8546
8547 template<__cartesian_product_common_arg _Range>
8548 constexpr auto
8549 __cartesian_common_arg_end(_Range& __r)
8550 {
8551 if constexpr (common_range<_Range>)
8552 return ranges::end(__r);
8553 else
8554 return ranges::begin(__r) + ranges::distance(__r);
8555 }
8556 } // namespace __detail
8557
8558 template<input_range _First, forward_range... _Vs>
8559 requires (view<_First> && ... && view<_Vs>)
8560 class cartesian_product_view : public view_interface<cartesian_product_view<_First, _Vs...>>
8561 {
8562 tuple<_First, _Vs...> _M_bases;
8563
8564 template<bool> class _Iterator;
8565
8566 static auto
8567 _S_difference_type()
8568 {
8569 // TODO: Implement the recommended practice of using the smallest
8570 // sufficiently wide type according to the maximum sizes of the
8571 // underlying ranges?
8572 return common_type_t<ptrdiff_t,
8573 range_difference_t<_First>,
8574 range_difference_t<_Vs>...>{};
8575 }
8576
8577 public:
8578 cartesian_product_view() = default;
8579
8580 constexpr explicit
8581 cartesian_product_view(_First __first, _Vs... __rest)
8582 : _M_bases(std::move(__first), std::move(__rest)...)
8583 { }
8584
8585 constexpr _Iterator<false>
8586 begin() requires (!__detail::__simple_view<_First> || ... || !__detail::__simple_view<_Vs>)
8587 { return _Iterator<false>(*this, __detail::__tuple_transform(ranges::begin, _M_bases)); }
8588
8589 constexpr _Iterator<true>
8590 begin() const requires (range<const _First> && ... && range<const _Vs>)
8591 { return _Iterator<true>(*this, __detail::__tuple_transform(ranges::begin, _M_bases)); }
8592
8593 constexpr _Iterator<false>
8594 end() requires ((!__detail::__simple_view<_First> || ... || !__detail::__simple_view<_Vs>)
8595 && __detail::__cartesian_product_is_common<_First, _Vs...>)
8596 {
8597 auto __its = [this]<size_t... _Is>(index_sequence<_Is...>) {
8598 using _Ret = tuple<iterator_t<_First>, iterator_t<_Vs>...>;
8599 bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
8600 auto& __first = std::get<0>(_M_bases);
8601 return _Ret{(__empty_tail
8602 ? ranges::begin(__first)
8603 : __detail::__cartesian_common_arg_end(__first)),
8604 ranges::begin(std::get<1 + _Is>(_M_bases))...};
8605 }(make_index_sequence<sizeof...(_Vs)>{});
8606
8607 return _Iterator<false>{*this, std::move(__its)};
8608 }
8609
8610 constexpr _Iterator<true>
8611 end() const requires __detail::__cartesian_product_is_common<const _First, const _Vs...>
8612 {
8613 auto __its = [this]<size_t... _Is>(index_sequence<_Is...>) {
8614 using _Ret = tuple<iterator_t<const _First>, iterator_t<const _Vs>...>;
8615 bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
8616 auto& __first = std::get<0>(_M_bases);
8617 return _Ret{(__empty_tail
8618 ? ranges::begin(__first)
8619 : __detail::__cartesian_common_arg_end(__first)),
8620 ranges::begin(std::get<1 + _Is>(_M_bases))...};
8621 }(make_index_sequence<sizeof...(_Vs)>{});
8622
8623 return _Iterator<true>{*this, std::move(__its)};
8624 }
8625
8626 constexpr default_sentinel_t
8627 end() const noexcept
8628 { return default_sentinel; }
8629
8630 constexpr auto
8631 size() requires __detail::__cartesian_product_is_sized<_First, _Vs...>
8632 {
8633 using _ST = __detail::__make_unsigned_like_t<decltype(_S_difference_type())>;
8634 return [&]<size_t... _Is>(index_sequence<_Is...>) {
8635 auto __size = static_cast<_ST>(1);
8636#ifdef _GLIBCXX_ASSERTIONS
8637 if constexpr (integral<_ST>)
8638 {
8639 bool __overflow
8640 = (__builtin_mul_overflow(__size,
8641 static_cast<_ST>(ranges::size(std::get<_Is>(_M_bases))),
8642 &__size)
8643 || ...);
8644 __glibcxx_assert(!__overflow);
8645 }
8646 else
8647#endif
8648 __size = (static_cast<_ST>(ranges::size(std::get<_Is>(_M_bases))) * ...);
8649 return __size;
8650 }(make_index_sequence<1 + sizeof...(_Vs)>{});
8651 }
8652
8653 constexpr auto
8654 size() const requires __detail::__cartesian_product_is_sized<const _First, const _Vs...>
8655 {
8656 using _ST = __detail::__make_unsigned_like_t<decltype(_S_difference_type())>;
8657 return [&]<size_t... _Is>(index_sequence<_Is...>) {
8658 auto __size = static_cast<_ST>(1);
8659#ifdef _GLIBCXX_ASSERTIONS
8660 if constexpr (integral<_ST>)
8661 {
8662 bool __overflow
8663 = (__builtin_mul_overflow(__size,
8664 static_cast<_ST>(ranges::size(std::get<_Is>(_M_bases))),
8665 &__size)
8666 || ...);
8667 __glibcxx_assert(!__overflow);
8668 }
8669 else
8670#endif
8671 __size = (static_cast<_ST>(ranges::size(std::get<_Is>(_M_bases))) * ...);
8672 return __size;
8673 }(make_index_sequence<1 + sizeof...(_Vs)>{});
8674 }
8675 };
8676
8677 template<typename... _Vs>
8678 cartesian_product_view(_Vs&&...) -> cartesian_product_view<views::all_t<_Vs>...>;
8679
8680 template<input_range _First, forward_range... _Vs>
8681 requires (view<_First> && ... && view<_Vs>)
8682 template<bool _Const>
8683 class cartesian_product_view<_First, _Vs...>::_Iterator
8684 {
8685 using _Parent = __maybe_const_t<_Const, cartesian_product_view>;
8686 _Parent* _M_parent = nullptr;
8687 tuple<iterator_t<__maybe_const_t<_Const, _First>>,
8688 iterator_t<__maybe_const_t<_Const, _Vs>>...> _M_current;
8689
8690 constexpr
8691 _Iterator(_Parent& __parent, decltype(_M_current) __current)
8692 : _M_parent(std::__addressof(__parent)),
8693 _M_current(std::move(__current))
8694 { }
8695
8696 static auto
8697 _S_iter_concept()
8698 {
8699 if constexpr (__detail::__cartesian_product_is_random_access<_Const, _First, _Vs...>)
8700 return random_access_iterator_tag{};
8701 else if constexpr (__detail::__cartesian_product_is_bidirectional<_Const, _First, _Vs...>)
8702 return bidirectional_iterator_tag{};
8703 else if constexpr (forward_range<__maybe_const_t<_Const, _First>>)
8704 return forward_iterator_tag{};
8705 else
8706 return input_iterator_tag{};
8707 }
8708
8709 friend cartesian_product_view;
8710
8711 public:
8712 using iterator_category = input_iterator_tag;
8713 using iterator_concept = decltype(_S_iter_concept());
8714 using value_type
8715 = tuple<range_value_t<__maybe_const_t<_Const, _First>>,
8716 range_value_t<__maybe_const_t<_Const, _Vs>>...>;
8717 using reference
8718 = tuple<range_reference_t<__maybe_const_t<_Const, _First>>,
8719 range_reference_t<__maybe_const_t<_Const, _Vs>>...>;
8720 using difference_type = decltype(cartesian_product_view::_S_difference_type());
8721
8722 _Iterator() = default;
8723
8724 constexpr
8725 _Iterator(_Iterator<!_Const> __i)
8726 requires _Const
8727 && (convertible_to<iterator_t<_First>, iterator_t<const _First>>
8728 && ... && convertible_to<iterator_t<_Vs>, iterator_t<const _Vs>>)
8729 : _M_parent(std::__addressof(__i._M_parent)),
8730 _M_current(std::move(__i._M_current))
8731 { }
8732
8733 constexpr auto
8734 operator*() const
8735 {
8736 auto __f = [](auto& __i) -> decltype(auto) {
8737 return *__i;
8738 };
8739 return __detail::__tuple_transform(__f, _M_current);
8740 }
8741
8742 constexpr _Iterator&
8743 operator++()
8744 {
8745 _M_next();
8746 return *this;
8747 }
8748
8749 constexpr void
8750 operator++(int)
8751 { ++*this; }
8752
8753 constexpr _Iterator
8754 operator++(int) requires forward_range<__maybe_const_t<_Const, _First>>
8755 {
8756 auto __tmp = *this;
8757 ++*this;
8758 return __tmp;
8759 }
8760
8761 constexpr _Iterator&
8762 operator--()
8763 requires __detail::__cartesian_product_is_bidirectional<_Const, _First, _Vs...>
8764 {
8765 _M_prev();
8766 return *this;
8767 }
8768
8769 constexpr _Iterator
8770 operator--(int)
8771 requires __detail::__cartesian_product_is_bidirectional<_Const, _First, _Vs...>
8772 {
8773 auto __tmp = *this;
8774 --*this;
8775 return __tmp;
8776 }
8777
8778 constexpr _Iterator&
8779 operator+=(difference_type __x)
8780 requires __detail::__cartesian_product_is_random_access<_Const, _First, _Vs...>
8781 {
8782 _M_advance(__x);
8783 return *this;
8784 }
8785
8786 constexpr _Iterator&
8787 operator-=(difference_type __x)
8788 requires __detail::__cartesian_product_is_random_access<_Const, _First, _Vs...>
8789 { return *this += -__x; }
8790
8791 constexpr reference
8792 operator[](difference_type __n) const
8793 requires __detail::__cartesian_product_is_random_access<_Const, _First, _Vs...>
8794 { return *((*this) + __n); }
8795
8796 friend constexpr bool
8797 operator==(const _Iterator& __x, const _Iterator& __y)
8798 requires equality_comparable<iterator_t<__maybe_const_t<_Const, _First>>>
8799 { return __x._M_current == __y._M_current; }
8800
8801 friend constexpr bool
8802 operator==(const _Iterator& __x, default_sentinel_t)
8803 {
8804 return [&]<size_t... _Is>(index_sequence<_Is...>) {
8805 return ((std::get<_Is>(__x._M_current)
8806 == ranges::end(std::get<_Is>(__x._M_parent->_M_bases)))
8807 || ...);
8808 }(make_index_sequence<1 + sizeof...(_Vs)>{});
8809 }
8810
8811 friend constexpr auto
8812 operator<=>(const _Iterator& __x, const _Iterator& __y)
8813 requires __detail::__all_random_access<_Const, _First, _Vs...>
8814 { return __x._M_current <=> __y._M_current; }
8815
8816 friend constexpr _Iterator
8817 operator+(_Iterator __x, difference_type __y)
8818 requires __detail::__cartesian_product_is_random_access<_Const, _First, _Vs...>
8819 { return __x += __y; }
8820
8821 friend constexpr _Iterator
8822 operator+(difference_type __x, _Iterator __y)
8823 requires __detail::__cartesian_product_is_random_access<_Const, _First, _Vs...>
8824 { return __y += __x; }
8825
8826 friend constexpr _Iterator
8827 operator-(_Iterator __x, difference_type __y)
8828 requires __detail::__cartesian_product_is_random_access<_Const, _First, _Vs...>
8829 { return __x -= __y; }
8830
8831 friend constexpr difference_type
8832 operator-(const _Iterator& __x, const _Iterator& __y)
8833 requires __detail::__cartesian_is_sized_sentinel<_Const, iterator_t, _First, _Vs...>
8834 { return __x._M_distance_from(__y._M_current); }
8835
8836 friend constexpr difference_type
8837 operator-(const _Iterator& __i, default_sentinel_t)
8838 requires __detail::__cartesian_is_sized_sentinel<_Const, sentinel_t, _First, _Vs...>
8839 {
8840 tuple __end_tuple = [&]<size_t... _Is>(index_sequence<_Is...>) {
8841 return tuple{ranges::end(std::get<0>(__i._M_parent->_M_bases)),
8842 ranges::begin(std::get<1 + _Is>(__i._M_parent->_M_bases))...};
8843 }(make_index_sequence<sizeof...(_Vs)>{});
8844 return __i._M_distance_from(__end_tuple);
8845 }
8846
8847 friend constexpr difference_type
8848 operator-(default_sentinel_t, const _Iterator& __i)
8849 requires __detail::__cartesian_is_sized_sentinel<_Const, sentinel_t, _First, _Vs...>
8850 { return -(__i - default_sentinel); }
8851
8852 friend constexpr auto
8853 iter_move(const _Iterator& __i)
8854 { return __detail::__tuple_transform(ranges::iter_move, __i._M_current); }
8855
8856 friend constexpr void
8857 iter_swap(const _Iterator& __l, const _Iterator& __r)
8858 requires (indirectly_swappable<iterator_t<__maybe_const_t<_Const, _First>>>
8859 && ...
8860 && indirectly_swappable<iterator_t<__maybe_const_t<_Const, _Vs>>>)
8861 {
8862 [&]<size_t... _Is>(index_sequence<_Is...>) {
8863 (ranges::iter_swap(std::get<_Is>(__l._M_current), std::get<_Is>(__r._M_current)), ...);
8864 }(make_index_sequence<1 + sizeof...(_Vs)>{});
8865 }
8866
8867 private:
8868 template<size_t _Nm = sizeof...(_Vs)>
8869 constexpr void
8870 _M_next()
8871 {
8872 auto& __it = std::get<_Nm>(_M_current);
8873 ++__it;
8874 if constexpr (_Nm > 0)
8875 if (__it == ranges::end(std::get<_Nm>(_M_parent->_M_bases)))
8876 {
8877 __it = ranges::begin(std::get<_Nm>(_M_parent->_M_bases));
8878 _M_next<_Nm - 1>();
8879 }
8880 }
8881
8882 template<size_t _Nm = sizeof...(_Vs)>
8883 constexpr void
8884 _M_prev()
8885 {
8886 auto& __it = std::get<_Nm>(_M_current);
8887 if constexpr (_Nm > 0)
8888 if (__it == ranges::begin(std::get<_Nm>(_M_parent->_M_bases)))
8889 {
8890 __it = __detail::__cartesian_common_arg_end(std::get<_Nm>(_M_parent->_M_bases));
8891 _M_prev<_Nm - 1>();
8892 }
8893 --__it;
8894 }
8895
8896 template<size_t _Nm = sizeof...(_Vs)>
8897 constexpr void
8898 _M_advance(difference_type __x)
8899 requires __detail::__cartesian_product_is_random_access<_Const, _First, _Vs...>
8900 {
8901 if (__x == 1)
8902 _M_next<_Nm>();
8903 else if (__x == -1)
8904 _M_prev<_Nm>();
8905 else if (__x != 0)
8906 {
8907 // Constant time iterator advancement.
8908 auto& __r = std::get<_Nm>(_M_parent->_M_bases);
8909 auto& __it = std::get<_Nm>(_M_current);
8910 if constexpr (_Nm == 0)
8911 {
8912#ifdef _GLIBCXX_ASSERTIONS
8913 if constexpr (sized_range<__maybe_const_t<_Const, _First>>)
8914 {
8915 auto __size = ranges::ssize(__r);
8916 auto __begin = ranges::begin(__r);
8917 auto __offset = __it - __begin;
8918 __glibcxx_assert(__offset + __x >= 0 && __offset + __x <= __size);
8919 }
8920#endif
8921 __it += __x;
8922 }
8923 else
8924 {
8925 auto __size = ranges::ssize(__r);
8926 auto __begin = ranges::begin(__r);
8927 auto __offset = __it - __begin;
8928 __offset += __x;
8929 __x = __offset / __size;
8930 __offset %= __size;
8931 if (__offset < 0)
8932 {
8933 __offset = __size + __offset;
8934 --__x;
8935 }
8936 __it = __begin + __offset;
8937 _M_advance<_Nm - 1>(__x);
8938 }
8939 }
8940 }
8941
8942 template<typename _Tuple>
8943 constexpr difference_type
8944 _M_distance_from(const _Tuple& __t) const
8945 {
8946 return [&]<size_t... _Is>(index_sequence<_Is...>) {
8947 auto __sum = static_cast<difference_type>(0);
8948#ifdef _GLIBCXX_ASSERTIONS
8949 if constexpr (integral<difference_type>)
8950 {
8951 bool __overflow
8952 = (__builtin_add_overflow(__sum, _M_scaled_distance<_Is>(__t), &__sum)
8953 || ...);
8954 __glibcxx_assert(!__overflow);
8955 }
8956 else
8957#endif
8958 __sum = (_M_scaled_distance<_Is>(__t) + ...);
8959 return __sum;
8960 }(make_index_sequence<1 + sizeof...(_Vs)>{});
8961 }
8962
8963 template<size_t _Nm, typename _Tuple>
8964 constexpr difference_type
8965 _M_scaled_distance(const _Tuple& __t) const
8966 {
8967 auto __dist = static_cast<difference_type>(std::get<_Nm>(_M_current)
8968 - std::get<_Nm>(__t));
8969#ifdef _GLIBCXX_ASSERTIONS
8970 if constexpr (integral<difference_type>)
8971 {
8972 bool __overflow = __builtin_mul_overflow(__dist, _M_scaled_size<_Nm+1>(), &__dist);
8973 __glibcxx_assert(!__overflow);
8974 }
8975 else
8976#endif
8977 __dist *= _M_scaled_size<_Nm+1>();
8978 return __dist;
8979 }
8980
8981 template<size_t _Nm>
8982 constexpr difference_type
8983 _M_scaled_size() const
8984 {
8985 if constexpr (_Nm <= sizeof...(_Vs))
8986 {
8987 auto __size = static_cast<difference_type>(ranges::size
8988 (std::get<_Nm>(_M_parent->_M_bases)));
8989#ifdef _GLIBCXX_ASSERTIONS
8990 if constexpr (integral<difference_type>)
8991 {
8992 bool __overflow = __builtin_mul_overflow(__size, _M_scaled_size<_Nm+1>(), &__size);
8993 __glibcxx_assert(!__overflow);
8994 }
8995 else
8996#endif
8997 __size *= _M_scaled_size<_Nm+1>();
8998 return __size;
8999 }
9000 else
9001 return static_cast<difference_type>(1);
9002 }
9003 };
9004
9005 namespace views
9006 {
9007 namespace __detail
9008 {
9009 template<typename... _Ts>
9010 concept __can_cartesian_product_view
9011 = requires { cartesian_product_view<all_t<_Ts>...>(std::declval<_Ts>()...); };
9012 }
9013
9014 struct _CartesianProduct
9015 {
9016 template<typename... _Ts>
9017 requires (sizeof...(_Ts) == 0 || __detail::__can_cartesian_product_view<_Ts...>)
9018 constexpr auto
9019 operator() [[nodiscard]] (_Ts&&... __ts) const
9020 {
9021 if constexpr (sizeof...(_Ts) == 0)
9022 return views::single(tuple{});
9023 else
9024 return cartesian_product_view<all_t<_Ts>...>(std::forward<_Ts>(__ts)...);
9025 }
9026 };
9027
9028 inline constexpr _CartesianProduct cartesian_product;
9029 }
9030#endif // __cpp_lib_ranges_cartesian_product
9031
9032#ifdef __cpp_lib_ranges_as_rvalue // C++ >= 23
9033 template<input_range _Vp>
9034 requires view<_Vp>
9035 class as_rvalue_view : public view_interface<as_rvalue_view<_Vp>>
9036 {
9037 _Vp _M_base = _Vp();
9038
9039 public:
9040 as_rvalue_view() requires default_initializable<_Vp> = default;
9041
9042 constexpr explicit
9043 as_rvalue_view(_Vp __base)
9044 : _M_base(std::move(__base))
9045 { }
9046
9047 constexpr _Vp
9048 base() const& requires copy_constructible<_Vp>
9049 { return _M_base; }
9050
9051 constexpr _Vp
9052 base() &&
9053 { return std::move(_M_base); }
9054
9055 constexpr auto
9056 begin() requires (!__detail::__simple_view<_Vp>)
9057 { return move_iterator(ranges::begin(_M_base)); }
9058
9059 constexpr auto
9060 begin() const requires range<const _Vp>
9061 { return move_iterator(ranges::begin(_M_base)); }
9062
9063 constexpr auto
9064 end() requires (!__detail::__simple_view<_Vp>)
9065 {
9066 if constexpr (common_range<_Vp>)
9067 return move_iterator(ranges::end(_M_base));
9068 else
9069 return move_sentinel(ranges::end(_M_base));
9070 }
9071
9072 constexpr auto
9073 end() const requires range<const _Vp>
9074 {
9075 if constexpr (common_range<const _Vp>)
9076 return move_iterator(ranges::end(_M_base));
9077 else
9078 return move_sentinel(ranges::end(_M_base));
9079 }
9080
9081 constexpr auto
9082 size() requires sized_range<_Vp>
9083 { return ranges::size(_M_base); }
9084
9085 constexpr auto
9086 size() const requires sized_range<const _Vp>
9087 { return ranges::size(_M_base); }
9088 };
9089
9090 template<typename _Range>
9091 as_rvalue_view(_Range&&) -> as_rvalue_view<views::all_t<_Range>>;
9092
9093 template<typename _Tp>
9094 inline constexpr bool enable_borrowed_range<as_rvalue_view<_Tp>>
9095 = enable_borrowed_range<_Tp>;
9096
9097 namespace views
9098 {
9099 namespace __detail
9100 {
9101 template<typename _Tp>
9102 concept __can_as_rvalue_view = requires { as_rvalue_view(std::declval<_Tp>()); };
9103 }
9104
9105 struct _AsRvalue : __adaptor::_RangeAdaptorClosure<_AsRvalue>
9106 {
9107 template<viewable_range _Range>
9108 requires __detail::__can_as_rvalue_view<_Range>
9109 constexpr auto
9110 operator() [[nodiscard]] (_Range&& __r) const
9111 {
9112 // _GLIBCXX_RESOLVE_LIB_DEFECTS
9113 // 4083. views::as_rvalue should reject non-input ranges
9114 // input_range<_Range> is implied by __detail::__can_as_rvalue_view<_Range>
9115 if constexpr (same_as<range_rvalue_reference_t<_Range>,
9116 range_reference_t<_Range>>)
9117 return views::all(std::forward<_Range>(__r));
9118 else
9119 return as_rvalue_view(std::forward<_Range>(__r));
9120 }
9121 };
9122
9123 inline constexpr _AsRvalue as_rvalue;
9124 }
9125#endif // __cpp_lib_as_rvalue
9126
9127#ifdef __cpp_lib_ranges_enumerate // C++ >= 23
9128 namespace __detail
9129 {
9130 template<typename _Range>
9131 concept __range_with_movable_reference = input_range<_Range>
9132 && move_constructible<range_reference_t<_Range>>
9133 && move_constructible<range_rvalue_reference_t<_Range>>;
9134 }
9135
9136 template<view _Vp>
9137 requires __detail::__range_with_movable_reference<_Vp>
9138 class enumerate_view : public view_interface<enumerate_view<_Vp>>
9139 {
9140 _Vp _M_base = _Vp();
9141
9142 template<bool _Const> class _Iterator;
9143 template<bool _Const> class _Sentinel;
9144
9145 public:
9146 enumerate_view() requires default_initializable<_Vp> = default;
9147
9148 constexpr explicit
9149 enumerate_view(_Vp __base)
9150 : _M_base(std::move(__base))
9151 { }
9152
9153 constexpr auto
9154 begin() requires (!__detail::__simple_view<_Vp>)
9155 { return _Iterator<false>(ranges::begin(_M_base), 0); }
9156
9157 constexpr auto
9158 begin() const requires __detail::__range_with_movable_reference<const _Vp>
9159 { return _Iterator<true>(ranges::begin(_M_base), 0); }
9160
9161 constexpr auto
9162 end() requires (!__detail::__simple_view<_Vp>)
9163 {
9164 if constexpr (common_range<_Vp> && sized_range<_Vp>)
9165 return _Iterator<false>(ranges::end(_M_base), ranges::distance(_M_base));
9166 else
9167 return _Sentinel<false>(ranges::end(_M_base));
9168 }
9169
9170 constexpr auto
9171 end() const requires __detail::__range_with_movable_reference<const _Vp>
9172 {
9173 if constexpr (common_range<const _Vp> && sized_range<const _Vp>)
9174 return _Iterator<true>(ranges::end(_M_base), ranges::distance(_M_base));
9175 else
9176 return _Sentinel<true>(ranges::end(_M_base));
9177 }
9178
9179 constexpr auto
9180 size() requires sized_range<_Vp>
9181 { return ranges::size(_M_base); }
9182
9183 constexpr auto
9184 size() const requires sized_range<const _Vp>
9185 { return ranges::size(_M_base); }
9186
9187 constexpr _Vp
9188 base() const & requires copy_constructible<_Vp>
9189 { return _M_base; }
9190
9191 constexpr _Vp
9192 base() &&
9193 { return std::move(_M_base); }
9194 };
9195
9196 template<typename _Range>
9197 enumerate_view(_Range&&) -> enumerate_view<views::all_t<_Range>>;
9198
9199 template<typename _Tp>
9200 inline constexpr bool enable_borrowed_range<enumerate_view<_Tp>>
9201 = enable_borrowed_range<_Tp>;
9202
9203 template<view _Vp>
9204 requires __detail::__range_with_movable_reference<_Vp>
9205 template<bool _Const>
9206 class enumerate_view<_Vp>::_Iterator
9207 {
9208 using _Base = __maybe_const_t<_Const, _Vp>;
9209
9210 static auto
9211 _S_iter_concept()
9212 {
9213 if constexpr (random_access_range<_Base>)
9214 return random_access_iterator_tag{};
9215 else if constexpr (bidirectional_range<_Base>)
9216 return bidirectional_iterator_tag{};
9217 else if constexpr (forward_range<_Base>)
9218 return forward_iterator_tag{};
9219 else
9220 return input_iterator_tag{};
9221 }
9222
9223 friend enumerate_view;
9224
9225 public:
9226 using iterator_category = input_iterator_tag;
9227 using iterator_concept = decltype(_S_iter_concept());
9228 using difference_type = range_difference_t<_Base>;
9229 using value_type = tuple<difference_type, range_value_t<_Base>>;
9230
9231 private:
9232 using __reference_type = tuple<difference_type, range_reference_t<_Base>>;
9233
9234 iterator_t<_Base> _M_current = iterator_t<_Base>();
9235 difference_type _M_pos = 0;
9236
9237 constexpr explicit
9238 _Iterator(iterator_t<_Base> __current, difference_type __pos)
9239 : _M_current(std::move(__current)), _M_pos(__pos)
9240 { }
9241
9242 public:
9243 _Iterator() requires default_initializable<iterator_t<_Base>> = default;
9244
9245 constexpr
9246 _Iterator(_Iterator<!_Const> __i)
9247 requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
9248 : _M_current(std::move(__i._M_current)), _M_pos(__i._M_pos)
9249 { }
9250
9251 constexpr const iterator_t<_Base> &
9252 base() const & noexcept
9253 { return _M_current; }
9254
9255 constexpr iterator_t<_Base>
9256 base() &&
9257 { return std::move(_M_current); }
9258
9259 constexpr difference_type
9260 index() const noexcept
9261 { return _M_pos; }
9262
9263 constexpr auto
9264 operator*() const
9265 { return __reference_type(_M_pos, *_M_current); }
9266
9267 constexpr _Iterator&
9268 operator++()
9269 {
9270 ++_M_current;
9271 ++_M_pos;
9272 return *this;
9273 }
9274
9275 constexpr void
9276 operator++(int)
9277 { ++*this; }
9278
9279 constexpr _Iterator
9280 operator++(int) requires forward_range<_Base>
9281 {
9282 auto __tmp = *this;
9283 ++*this;
9284 return __tmp;
9285 }
9286
9287 constexpr _Iterator&
9288 operator--() requires bidirectional_range<_Base>
9289 {
9290 --_M_current;
9291 --_M_pos;
9292 return *this;
9293 }
9294
9295 constexpr _Iterator
9296 operator--(int) requires bidirectional_range<_Base>
9297 {
9298 auto __tmp = *this;
9299 --*this;
9300 return __tmp;
9301 }
9302
9303 constexpr _Iterator&
9304 operator+=(difference_type __n) requires random_access_range<_Base>
9305 {
9306 _M_current += __n;
9307 _M_pos += __n;
9308 return *this;
9309 }
9310
9311 constexpr _Iterator&
9312 operator-=(difference_type __n) requires random_access_range<_Base>
9313 {
9314 _M_current -= __n;
9315 _M_pos -= __n;
9316 return *this;
9317 }
9318
9319 constexpr auto
9320 operator[](difference_type __n) const requires random_access_range<_Base>
9321 { return __reference_type(_M_pos + __n, _M_current[__n]); }
9322
9323 friend constexpr bool
9324 operator==(const _Iterator& __x, const _Iterator& __y) noexcept
9325 { return __x._M_pos == __y._M_pos; }
9326
9327 friend constexpr strong_ordering
9328 operator<=>(const _Iterator& __x, const _Iterator& __y) noexcept
9329 { return __x._M_pos <=> __y._M_pos; }
9330
9331 friend constexpr _Iterator
9332 operator+(const _Iterator& __x, difference_type __y)
9333 requires random_access_range<_Base>
9334 { return (auto(__x) += __y); }
9335
9336 friend constexpr _Iterator
9337 operator+(difference_type __x, const _Iterator& __y)
9338 requires random_access_range<_Base>
9339 { return auto(__y) += __x; }
9340
9341 friend constexpr _Iterator
9342 operator-(const _Iterator& __x, difference_type __y)
9343 requires random_access_range<_Base>
9344 { return auto(__x) -= __y; }
9345
9346 friend constexpr difference_type
9347 operator-(const _Iterator& __x, const _Iterator& __y) noexcept
9348 { return __x._M_pos - __y._M_pos; }
9349
9350 friend constexpr auto
9351 iter_move(const _Iterator& __i)
9352 noexcept(noexcept(ranges::iter_move(__i._M_current))
9353 && is_nothrow_move_constructible_v<range_rvalue_reference_t<_Base>>)
9354 {
9355 return tuple<difference_type, range_rvalue_reference_t<_Base>>
9356 (__i._M_pos, ranges::iter_move(__i._M_current));
9357 }
9358 };
9359
9360 template<view _Vp>
9361 requires __detail::__range_with_movable_reference<_Vp>
9362 template<bool _Const>
9363 class enumerate_view<_Vp>::_Sentinel
9364 {
9365 using _Base = __maybe_const_t<_Const, _Vp>;
9366
9367 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
9368
9369 constexpr explicit
9370 _Sentinel(sentinel_t<_Base> __end)
9371 : _M_end(std::move(__end))
9372 { }
9373
9374 friend enumerate_view;
9375
9376 public:
9377 _Sentinel() = default;
9378
9379 constexpr
9380 _Sentinel(_Sentinel<!_Const> __other)
9381 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
9382 : _M_end(std::move(__other._M_end))
9383 { }
9384
9385 constexpr sentinel_t<_Base>
9386 base() const
9387 { return _M_end; }
9388
9389 template<bool _OtherConst>
9390 requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const_t<_OtherConst, _Vp>>>
9391 friend constexpr bool
9392 operator==(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
9393 { return __x._M_current == __y._M_end; }
9394
9395 template<bool _OtherConst>
9396 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const_t<_OtherConst, _Vp>>>
9397 friend constexpr range_difference_t<__maybe_const_t<_OtherConst, _Vp>>
9398 operator-(const _Iterator<_OtherConst>& __x, const _Sentinel& __y)
9399 { return __x._M_current - __y._M_end; }
9400
9401 template<bool _OtherConst>
9402 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const_t<_OtherConst, _Vp>>>
9403 friend constexpr range_difference_t<__maybe_const_t<_OtherConst, _Vp>>
9404 operator-(const _Sentinel& __x, const _Iterator<_OtherConst>& __y)
9405 { return __x._M_end - __y._M_current; }
9406 };
9407
9408 namespace views
9409 {
9410 namespace __detail
9411 {
9412 template<typename _Tp>
9413 concept __can_enumerate_view
9414 = requires { enumerate_view<all_t<_Tp>>(std::declval<_Tp>()); };
9415 }
9416
9417 struct _Enumerate : __adaptor::_RangeAdaptorClosure<_Enumerate>
9418 {
9419 template<viewable_range _Range>
9420 requires __detail::__can_enumerate_view<_Range>
9421 constexpr auto
9422 operator() [[nodiscard]] (_Range&& __r) const
9423 { return enumerate_view<all_t<_Range>>(std::forward<_Range>(__r)); }
9424 };
9425
9426 inline constexpr _Enumerate enumerate;
9427 }
9428#endif // __cpp_lib_ranges_enumerate
9429
9430#ifdef __cpp_lib_ranges_as_const // C++ >= 23
9431 template<view _Vp>
9432 requires input_range<_Vp>
9433 class as_const_view : public view_interface<as_const_view<_Vp>>
9434 {
9435 _Vp _M_base = _Vp();
9436
9437 public:
9438 as_const_view() requires default_initializable<_Vp> = default;
9439
9440 constexpr explicit
9441 as_const_view(_Vp __base)
9442 noexcept(is_nothrow_move_constructible_v<_Vp>)
9443 : _M_base(std::move(__base))
9444 { }
9445
9446 constexpr _Vp
9447 base() const &
9448 noexcept(is_nothrow_copy_constructible_v<_Vp>)
9449 requires copy_constructible<_Vp>
9450 { return _M_base; }
9451
9452 constexpr _Vp
9453 base() &&
9454 noexcept(is_nothrow_move_constructible_v<_Vp>)
9455 { return std::move(_M_base); }
9456
9457 constexpr auto
9458 begin() requires (!__detail::__simple_view<_Vp>)
9459 { return ranges::cbegin(_M_base); }
9460
9461 constexpr auto
9462 begin() const requires range<const _Vp>
9463 { return ranges::cbegin(_M_base); }
9464
9465 constexpr auto
9466 end() requires (!__detail::__simple_view<_Vp>)
9467 { return ranges::cend(_M_base); }
9468
9469 constexpr auto
9470 end() const requires range<const _Vp>
9471 { return ranges::cend(_M_base); }
9472
9473 constexpr auto
9474 size() requires sized_range<_Vp>
9475 { return ranges::size(_M_base); }
9476
9477 constexpr auto
9478 size() const requires sized_range<const _Vp>
9479 { return ranges::size(_M_base); }
9480 };
9481
9482 template<typename _Range>
9483 as_const_view(_Range&&) -> as_const_view<views::all_t<_Range>>;
9484
9485 template<typename _Tp>
9486 inline constexpr bool enable_borrowed_range<as_const_view<_Tp>>
9487 = enable_borrowed_range<_Tp>;
9488
9489 namespace views
9490 {
9491 namespace __detail
9492 {
9493 template<typename _Tp>
9494 inline constexpr bool __is_constable_ref_view = false;
9495
9496 template<typename _Range>
9497 inline constexpr bool __is_constable_ref_view<ref_view<_Range>>
9498 = constant_range<const _Range>;
9499
9500 template<typename _Range>
9501 concept __can_as_const_view = requires { as_const_view(std::declval<_Range>()); };
9502 }
9503
9504 struct _AsConst : __adaptor::_RangeAdaptorClosure<_AsConst>
9505 {
9506 template<viewable_range _Range>
9507 constexpr auto
9508 operator()(_Range&& __r) const
9509 noexcept(noexcept(as_const_view(std::declval<_Range>())))
9510 requires __detail::__can_as_const_view<_Range>
9511 {
9512 using _Tp = remove_cvref_t<_Range>;
9513 using element_type = remove_reference_t<range_reference_t<_Range>>;
9514 if constexpr (constant_range<views::all_t<_Range>>)
9515 return views::all(std::forward<_Range>(__r));
9516 else if constexpr (__detail::__is_empty_view<_Tp>)
9517 return views::empty<const element_type>;
9518#if __cpp_lib_optional >= 202506L && __cpp_lib_optional_range_support // >= C++26
9519 else if constexpr (__is_optional_ref_v<_Tp>)
9520 return optional<const typename _Tp::value_type&>(__r);
9521#endif
9522 else if constexpr (std::__detail::__is_span<_Tp>)
9523 return span<const element_type, _Tp::extent>(std::forward<_Range>(__r));
9524 else if constexpr (__detail::__is_constable_ref_view<_Tp>)
9525 return ref_view(std::as_const(std::forward<_Range>(__r).base()));
9526 else if constexpr (is_lvalue_reference_v<_Range>
9527 && constant_range<const _Tp>
9528 && !view<_Tp>)
9529 return ref_view(static_cast<const _Tp&>(__r));
9530 else
9531 return as_const_view(std::forward<_Range>(__r));
9532 }
9533 };
9534
9535 inline constexpr _AsConst as_const;
9536 }
9537#endif // __cpp_lib_as_const
9538} // namespace ranges
9539
9540 namespace views = ranges::views;
9541
9542#if __cpp_lib_ranges_to_container // C++ >= 23
9543namespace ranges
9544{
9545/// @cond undocumented
9546namespace __detail
9547{
9548 template<typename _Container>
9549 constexpr bool __reservable_container
9550 = sized_range<_Container>
9551 && requires(_Container& __c, range_size_t<_Container> __n) {
9552 __c.reserve(__n);
9553 { __c.capacity() } -> same_as<decltype(__n)>;
9554 { __c.max_size() } -> same_as<decltype(__n)>;
9555 };
9556
9557 template<typename _Cont, typename _Range>
9558 constexpr bool __toable = requires {
9559 requires (!input_range<_Cont>
9560 || convertible_to<range_reference_t<_Range>,
9561 range_value_t<_Cont>>);
9562 };
9563} // namespace __detail
9564/// @endcond
9565
9566 /// Convert a range to a container.
9567 /**
9568 * @tparam _Cont A container type.
9569 * @param __r A range that models the `input_range` concept.
9570 * @param __args... Arguments to pass to the container constructor.
9571 * @since C++23
9572 *
9573 * This function converts a range to the `_Cont` type.
9574 *
9575 * For example, `std::ranges::to<std::vector<int>>(some_view)`
9576 * will convert the view to `std::vector<int>`.
9577 *
9578 * Additional constructor arguments for the container can be supplied after
9579 * the input range argument, e.g.
9580 * `std::ranges::to<std::vector<int, Alloc<int>>>(a_range, an_allocator)`.
9581 */
9582 template<typename _Cont, input_range _Rg, typename... _Args>
9583 requires (!view<_Cont>)
9584 constexpr _Cont
9585 to [[nodiscard]] (_Rg&& __r, _Args&&... __args)
9586 {
9587 static_assert(!is_const_v<_Cont> && !is_volatile_v<_Cont>);
9588 static_assert(is_class_v<_Cont> || is_union_v<_Cont>);
9589
9590 if constexpr (__detail::__toable<_Cont, _Rg>)
9591 {
9592 if constexpr (constructible_from<_Cont, _Rg, _Args...>)
9593 return _Cont(std::forward<_Rg>(__r),
9594 std::forward<_Args>(__args)...);
9595 else if constexpr (constructible_from<_Cont, from_range_t, _Rg, _Args...>)
9596 return _Cont(from_range, std::forward<_Rg>(__r),
9597 std::forward<_Args>(__args)...);
9598 else if constexpr (requires { requires common_range<_Rg>;
9599 typename __iter_category_t<iterator_t<_Rg>>;
9600 requires derived_from<__iter_category_t<iterator_t<_Rg>>,
9601 input_iterator_tag>;
9602 requires constructible_from<_Cont, iterator_t<_Rg>,
9603 sentinel_t<_Rg>, _Args...>;
9604 })
9605 return _Cont(ranges::begin(__r), ranges::end(__r),
9606 std::forward<_Args>(__args)...);
9607 else
9608 {
9609 static_assert(constructible_from<_Cont, _Args...>);
9610 _Cont __c(std::forward<_Args>(__args)...);
9611 if constexpr (sized_range<_Rg>
9612 && __detail::__reservable_container<_Cont>)
9613 __c.reserve(static_cast<range_size_t<_Cont>>(ranges::size(__r)));
9614 // _GLIBCXX_RESOLVE_LIB_DEFECTS
9615 // 4016. container-insertable checks do not match what
9616 // container-inserter does
9617 auto __it = ranges::begin(__r);
9618 const auto __sent = ranges::end(__r);
9619 while (__it != __sent)
9620 {
9621 if constexpr (requires { __c.emplace_back(*__it); })
9622 __c.emplace_back(*__it);
9623 else if constexpr (requires { __c.push_back(*__it); })
9624 __c.push_back(*__it);
9625 else if constexpr (requires { __c.emplace(__c.end(), *__it); })
9626 __c.emplace(__c.end(), *__it);
9627 else
9628 __c.insert(__c.end(), *__it);
9629 ++__it;
9630 }
9631 return __c;
9632 }
9633 }
9634 else
9635 {
9636 static_assert(input_range<range_reference_t<_Rg>>);
9637 // _GLIBCXX_RESOLVE_LIB_DEFECTS
9638 // 3984. ranges::to's recursion branch may be ill-formed
9639 return ranges::to<_Cont>(ref_view(__r) | views::transform(
9640 []<typename _Elt>(_Elt&& __elem) {
9641 using _ValT = range_value_t<_Cont>;
9642 return ranges::to<_ValT>(std::forward<_Elt>(__elem));
9643 }), std::forward<_Args>(__args)...);
9644 }
9645 }
9646
9647/// @cond undocumented
9648namespace __detail
9649{
9650 template<typename _Rg>
9651 struct _InputIter
9652 {
9653 using iterator_category = input_iterator_tag;
9654 using value_type = range_value_t<_Rg>;
9655 using difference_type = ptrdiff_t;
9656 using pointer = add_pointer_t<range_reference_t<_Rg>>;
9657 using reference = range_reference_t<_Rg>;
9658 reference operator*() const;
9659 pointer operator->() const;
9660 _InputIter& operator++();
9661 _InputIter operator++(int);
9662 bool operator==(const _InputIter&) const;
9663 };
9664
9665 template<template<typename...> typename _Cont, input_range _Rg,
9666 typename... _Args>
9667 using _DeduceExpr1
9668 = decltype(_Cont(std::declval<_Rg>(), std::declval<_Args>()...));
9669
9670 template<template<typename...> typename _Cont, input_range _Rg,
9671 typename... _Args>
9672 using _DeduceExpr2
9673 = decltype(_Cont(from_range, std::declval<_Rg>(),
9674 std::declval<_Args>()...));
9675
9676 template<template<typename...> typename _Cont, input_range _Rg,
9677 typename... _Args>
9678 using _DeduceExpr3
9679 = decltype(_Cont(std::declval<_InputIter<_Rg>>(),
9680 std::declval<_InputIter<_Rg>>(),
9681 std::declval<_Args>()...));
9682
9683} // namespace __detail
9684/// @endcond
9685
9686 template<template<typename...> typename _Cont, input_range _Rg,
9687 typename... _Args>
9688 constexpr auto
9689 to [[nodiscard]] (_Rg&& __r, _Args&&... __args)
9690 {
9691 using __detail::_DeduceExpr1;
9692 using __detail::_DeduceExpr2;
9693 using __detail::_DeduceExpr3;
9694 if constexpr (requires { typename _DeduceExpr1<_Cont, _Rg, _Args...>; })
9695 return ranges::to<_DeduceExpr1<_Cont, _Rg, _Args...>>(
9696 std::forward<_Rg>(__r), std::forward<_Args>(__args)...);
9697 else if constexpr (requires { typename _DeduceExpr2<_Cont, _Rg, _Args...>; })
9698 return ranges::to<_DeduceExpr2<_Cont, _Rg, _Args...>>(
9699 std::forward<_Rg>(__r), std::forward<_Args>(__args)...);
9700 else if constexpr (requires { typename _DeduceExpr3<_Cont, _Rg, _Args...>; })
9701 return ranges::to<_DeduceExpr3<_Cont, _Rg, _Args...>>(
9702 std::forward<_Rg>(__r), std::forward<_Args>(__args)...);
9703 else
9704 static_assert(false); // Cannot deduce container specialization.
9705 }
9706
9707/// @cond undocumented
9708namespace __detail
9709{
9710 template<typename _Cont>
9711 struct _To
9712 {
9713 template<typename _Range, typename... _Args>
9714 requires requires { ranges::to<_Cont>(std::declval<_Range>(),
9715 std::declval<_Args>()...); }
9716 constexpr auto
9717 operator()(_Range&& __r, _Args&&... __args) const
9718 {
9719 return ranges::to<_Cont>(std::forward<_Range>(__r),
9720 std::forward<_Args>(__args)...);
9721 }
9722 };
9723} // namespace __detail
9724/// @endcond
9725
9726 /// ranges::to adaptor for converting a range to a container type
9727 /**
9728 * @tparam _Cont A container type.
9729 * @param __args... Arguments to pass to the container constructor.
9730 * @since C++23
9731 *
9732 * This range adaptor returns a range adaptor closure object that converts
9733 * a range to the `_Cont` type.
9734 *
9735 * For example, `some_view | std::ranges::to<std::vector<int>>()`
9736 * will convert the view to `std::vector<int>`.
9737 *
9738 * Additional constructor arguments for the container can be supplied, e.g.
9739 * `r | std::ranges::to<std::vector<int, Alloc<int>>>(an_allocator)`.
9740 */
9741 template<typename _Cont, typename... _Args>
9742 requires (!view<_Cont>)
9743 constexpr auto
9744 to [[nodiscard]] (_Args&&... __args)
9745 {
9746 static_assert(!is_const_v<_Cont> && !is_volatile_v<_Cont>);
9747 static_assert(is_class_v<_Cont> || is_union_v<_Cont>);
9748
9749 using __detail::_To;
9750 using views::__adaptor::_Partial;
9751 return _Partial<_To<_Cont>, decay_t<_Args>...>{0, std::forward<_Args>(__args)...};
9752 }
9753
9754/// @cond undocumented
9755namespace __detail
9756{
9757 template<template<typename...> typename _Cont>
9758 struct _To2
9759 {
9760 template<typename _Range, typename... _Args>
9761 requires requires { ranges::to<_Cont>(std::declval<_Range>(),
9762 std::declval<_Args>()...); }
9763 constexpr auto
9764 operator()(_Range&& __r, _Args&&... __args) const
9765 {
9766 return ranges::to<_Cont>(std::forward<_Range>(__r),
9767 std::forward<_Args>(__args)...);
9768 }
9769 };
9770} // namespace __detail
9771/// @endcond
9772
9773 /// ranges::to adaptor for converting a range to a deduced container type.
9774 /**
9775 * @tparam _Cont A container template.
9776 * @param __args... Arguments to pass to the container constructor.
9777 * @since C++23
9778 *
9779 * This range adaptor returns a range adaptor closure object that converts
9780 * a range to a specialization of the `_Cont` class template. The specific
9781 * specialization of `_Cont` to be used is deduced automatically.
9782 *
9783 * For example, `some_view | std::ranges::to<std::vector>(Alloc<int>{})`
9784 * will convert the view to `std::vector<T, Alloc<T>>`, where `T` is the
9785 * view's value type, i.e. `std::ranges::range_value_t<decltype(some_view)>`.
9786 *
9787 * Additional constructor arguments for the container can be supplied, e.g.
9788 * `r | std::ranges::to<std::vector>(an_allocator)`.
9789 */
9790 template<template<typename...> typename _Cont, typename... _Args>
9791 constexpr auto
9792 to [[nodiscard]] (_Args&&... __args)
9793 {
9794 using __detail::_To2;
9795 using views::__adaptor::_Partial;
9796 return _Partial<_To2<_Cont>, decay_t<_Args>...>{0, std::forward<_Args>(__args)...};
9797 }
9798
9799} // namespace ranges
9800#endif // __cpp_lib_ranges_to_container
9801
9802#if __cpp_lib_ranges_concat // C++ >= C++26
9803namespace ranges
9804{
9805 namespace __detail
9806 {
9807 template<typename... _Rs>
9808 using __concat_reference_t = common_reference_t<range_reference_t<_Rs>...>;
9809
9810 template<typename... _Rs>
9811 using __concat_value_t = common_type_t<range_value_t<_Rs>...>;
9812
9813 template<typename... _Rs>
9814 using __concat_rvalue_reference_t
9815 = common_reference_t<range_rvalue_reference_t<_Rs>...>;
9816
9817 template<typename _Ref, typename _RRef, typename _It>
9818 concept __concat_indirectly_readable_impl = requires(const _It __it) {
9819 { *__it } -> convertible_to<_Ref>;
9820 { ranges::iter_move(__it) } -> convertible_to<_RRef>;
9821 };
9822
9823 template<typename... _Rs>
9824 concept __concat_indirectly_readable
9825 = common_reference_with<__concat_reference_t<_Rs...>&&, __concat_value_t<_Rs...>&>
9826 && common_reference_with<__concat_reference_t<_Rs...>&&,
9827 __concat_rvalue_reference_t<_Rs...>&&>
9828 && common_reference_with<__concat_rvalue_reference_t<_Rs...>&&,
9829 __concat_value_t<_Rs...> const&>
9830 && (__concat_indirectly_readable_impl<__concat_reference_t<_Rs...>,
9831 __concat_rvalue_reference_t<_Rs...>,
9832 iterator_t<_Rs>>
9833 && ...);
9834
9835 template<typename... _Rs>
9836 concept __concatable = requires {
9837 typename __concat_reference_t<_Rs...>;
9838 typename __concat_value_t<_Rs...>;
9839 typename __concat_rvalue_reference_t<_Rs...>;
9840 } && __concat_indirectly_readable<_Rs...>;
9841
9842 template<bool _Const, typename _Range, typename... _Rs>
9843 struct __all_but_last_common
9844 {
9845 static inline constexpr bool value
9846 = requires { requires (common_range<__maybe_const_t<_Const, _Range>>
9847 && __all_but_last_common<_Const, _Rs...>::value); };
9848 };
9849
9850 template<bool _Const, typename _Range>
9851 struct __all_but_last_common<_Const, _Range>
9852 { static inline constexpr bool value = true; };
9853
9854 template<bool _Const, typename... _Rs>
9855 concept __concat_is_random_access = __all_random_access<_Const, _Rs...>
9856 && __all_but_last_common<_Const, _Rs...>::value;
9857
9858 template<bool _Const, typename... _Rs>
9859 concept __concat_is_bidirectional = __all_bidirectional<_Const, _Rs...>
9860 && __all_but_last_common<_Const, _Rs...>::value;
9861
9862 template<typename _Range, typename... _Rs>
9863 struct __all_but_first_sized
9864 { static inline constexpr bool value = (sized_range<_Rs> && ...); };
9865 } // namespace __detail
9866
9867 template<input_range... _Vs>
9868 requires (view<_Vs> && ...) && (sizeof...(_Vs) > 0) && __detail::__concatable<_Vs...>
9869 class concat_view : public view_interface<concat_view<_Vs...>>
9870 {
9871 tuple<_Vs...> _M_views;
9872
9873 template<bool _Const> class _Iterator;
9874
9875 public:
9876 constexpr concat_view() = default;
9877
9878 constexpr explicit
9879 concat_view(_Vs... __views)
9880 : _M_views(std::move(__views)...)
9881 { }
9882
9883 constexpr _Iterator<false>
9884 begin() requires (!(__detail::__simple_view<_Vs> && ...))
9885 {
9886 _Iterator<false> __it(this, in_place_index<0>, ranges::begin(std::get<0>(_M_views)));
9887 __it.template _M_satisfy<0>();
9888 return __it;
9889 }
9890
9891 constexpr _Iterator<true>
9892 begin() const requires (range<const _Vs> && ...) && __detail::__concatable<const _Vs...>
9893 {
9894 _Iterator<true> __it(this, in_place_index<0>, ranges::begin(std::get<0>(_M_views)));
9895 __it.template _M_satisfy<0>();
9896 return __it;
9897 }
9898
9899 constexpr auto
9900 end() requires (!(__detail::__simple_view<_Vs> && ...))
9901 {
9902 constexpr auto __n = sizeof...(_Vs);
9903 if constexpr (__detail::__all_forward<false, _Vs...>
9904 && common_range<_Vs...[__n - 1]>)
9905 return _Iterator<false>(this, in_place_index<__n - 1>,
9906 ranges::end(std::get<__n - 1>(_M_views)));
9907 else
9908 return default_sentinel;
9909 }
9910
9911 constexpr auto
9912 end() const requires (range<const _Vs> && ...) && __detail::__concatable<const _Vs...>
9913 {
9914 constexpr auto __n = sizeof...(_Vs);
9915 if constexpr (__detail::__all_forward<true, _Vs...>
9916 && common_range<const _Vs...[__n - 1]>)
9917 return _Iterator<true>(this, in_place_index<__n - 1>,
9918 ranges::end(std::get<__n - 1>(_M_views)));
9919 else
9920 return default_sentinel;
9921 }
9922
9923 constexpr auto
9924 size() requires (sized_range<_Vs>&&...)
9925 {
9926 return std::apply([](auto... __sizes) {
9927 using _CT = __detail::__make_unsigned_like_t<common_type_t<decltype(__sizes)...>>;
9928 return (_CT(__sizes) + ...);
9929 }, __detail::__tuple_transform(ranges::size, _M_views));
9930 }
9931
9932 constexpr auto
9933 size() const requires (sized_range<const _Vs>&&...)
9934 {
9935 return std::apply([](auto... __sizes) {
9936 using _CT = __detail::__make_unsigned_like_t<common_type_t<decltype(__sizes)...>>;
9937 return (_CT(__sizes) + ...);
9938 }, __detail::__tuple_transform(ranges::size, _M_views));
9939 }
9940 };
9941
9942 template<typename... _Rs>
9943 concat_view(_Rs&&...) -> concat_view<views::all_t<_Rs>...>;
9944
9945 namespace __detail
9946 {
9947 template<bool _Const, typename... _Vs>
9948 struct __concat_view_iter_cat
9949 { };
9950
9951 template<bool _Const, typename... _Vs>
9952 requires __detail::__all_forward<_Const, _Vs...>
9953 struct __concat_view_iter_cat<_Const, _Vs...>
9954 {
9955 static auto
9956 _S_iter_cat()
9957 {
9958 if constexpr (!is_reference_v<__concat_reference_t<__maybe_const_t<_Const, _Vs>...>>)
9959 return input_iterator_tag{};
9960 else
9961 return []<typename... _Cats>(_Cats... __cats) {
9962 if constexpr ((derived_from<_Cats, random_access_iterator_tag> && ...)
9963 && __concat_is_random_access<_Const, _Vs...>)
9964 return random_access_iterator_tag{};
9965 else if constexpr ((derived_from<_Cats, bidirectional_iterator_tag> && ...)
9966 && __concat_is_bidirectional<_Const, _Vs...>)
9967 return bidirectional_iterator_tag{};
9968 else if constexpr ((derived_from<_Cats, forward_iterator_tag> && ...))
9969 return forward_iterator_tag{};
9970 else
9971 return input_iterator_tag{};
9972 }(typename iterator_traits<iterator_t<__maybe_const_t<_Const, _Vs>>>
9973 ::iterator_category{}...);
9974 }
9975 };
9976 }
9977
9978 template<input_range... _Vs>
9979 requires (view<_Vs> && ...) && (sizeof...(_Vs) > 0) && __detail::__concatable<_Vs...>
9980 template<bool _Const>
9981 class concat_view<_Vs...>::_Iterator
9982 : public __detail::__concat_view_iter_cat<_Const, _Vs...>
9983 {
9984 static auto
9985 _S_iter_concept()
9986 {
9987 if constexpr (__detail::__concat_is_random_access<_Const, _Vs...>)
9988 return random_access_iterator_tag{};
9989 else if constexpr (__detail::__concat_is_bidirectional<_Const, _Vs...>)
9990 return bidirectional_iterator_tag{};
9991 else if constexpr (__detail::__all_forward<_Const, _Vs...>)
9992 return forward_iterator_tag{};
9993 else
9994 return input_iterator_tag{};
9995 }
9996
9997 friend concat_view;
9998 friend _Iterator<!_Const>;
9999
10000 public:
10001 // iterator_category defined in __concat_view_iter_cat
10002 using iterator_concept = decltype(_S_iter_concept());
10003 using value_type = __detail::__concat_value_t<__maybe_const_t<_Const, _Vs>...>;
10004 using difference_type = common_type_t<range_difference_t<__maybe_const_t<_Const, _Vs>>...>;
10005
10006 private:
10007 using __base_iter = variant<iterator_t<__maybe_const_t<_Const, _Vs>>...>;
10008
10009 __maybe_const_t<_Const, concat_view>* _M_parent = nullptr;
10010 __base_iter _M_it;
10011
10012 template<size_t _Nm>
10013 constexpr void
10014 _M_satisfy()
10015 {
10016 if constexpr (_Nm < (sizeof...(_Vs) - 1))
10017 {
10018 if (std::get<_Nm>(_M_it) == ranges::end(std::get<_Nm>(_M_parent->_M_views)))
10019 {
10020 _M_it.template emplace<_Nm + 1>(ranges::begin
10021 (std::get<_Nm + 1>(_M_parent->_M_views)));
10022 _M_satisfy<_Nm + 1>();
10023 }
10024 }
10025 }
10026
10027 template<size_t _Nm>
10028 constexpr void
10029 _M_prev()
10030 {
10031 if constexpr (_Nm == 0)
10032 --std::get<0>(_M_it);
10033 else
10034 {
10035 if (std::get<_Nm>(_M_it) == ranges::begin(std::get<_Nm>(_M_parent->_M_views)))
10036 {
10037 _M_it.template emplace<_Nm - 1>(ranges::end
10038 (std::get<_Nm - 1>(_M_parent->_M_views)));
10039 _M_prev<_Nm - 1>();
10040 }
10041 else
10042 --std::get<_Nm>(_M_it);
10043 }
10044 }
10045
10046 template<size_t _Nm>
10047 constexpr void
10048 _M_advance_fwd(difference_type __offset, difference_type __steps)
10049 {
10050 using _Dt = iter_difference_t<variant_alternative_t<_Nm, __base_iter>>;
10051 if constexpr (_Nm == sizeof...(_Vs) - 1)
10052 std::get<_Nm>(_M_it) += static_cast<_Dt>(__steps);
10053 else
10054 {
10055 auto __n_size = ranges::distance(std::get<_Nm>(_M_parent->_M_views));
10056 if (__offset + __steps < __n_size)
10057 std::get<_Nm>(_M_it) += static_cast<_Dt>(__steps);
10058 else
10059 {
10060 _M_it.template emplace<_Nm + 1>(ranges::begin
10061 (std::get<_Nm + 1>(_M_parent->_M_views)));
10062 _M_advance_fwd<_Nm + 1>(0, __offset + __steps - __n_size);
10063 }
10064 }
10065 }
10066
10067 template<size_t _Nm>
10068 constexpr void
10069 _M_advance_bwd(difference_type __offset, difference_type __steps)
10070 {
10071 using _Dt = iter_difference_t<variant_alternative_t<_Nm, __base_iter>>;
10072 if constexpr (_Nm == 0)
10073 std::get<_Nm>(_M_it) -= static_cast<_Dt>(__steps);
10074 else {
10075 if (__offset >= __steps)
10076 std::get<_Nm>(_M_it) -= static_cast<_Dt>(__steps);
10077 else
10078 {
10079 auto __prev_size = ranges::distance(std::get<_Nm - 1>(_M_parent->_M_views));
10080 _M_it.template emplace<_Nm - 1>(ranges::end
10081 (std::get<_Nm - 1>(_M_parent->_M_views)));
10082 _M_advance_bwd<_Nm - 1>(__prev_size, __steps - __offset);
10083 }
10084 }
10085 }
10086
10087 // Invoke the function object __f, which has a call operator with a size_t
10088 // template parameter (corresponding to an index into the pack of views),
10089 // using the runtime value of __index as the template argument.
10090 template<typename _Fp>
10091 static constexpr auto
10092 _S_invoke_with_runtime_index(_Fp&& __f, size_t __index)
10093 {
10094 return [&__f, __index]<size_t _Idx>(this auto&& __self) {
10095 if (_Idx == __index)
10096 return __f.template operator()<_Idx>();
10097 if constexpr (_Idx + 1 < sizeof...(_Vs))
10098 return __self.template operator()<_Idx + 1>();
10099 __builtin_unreachable();
10100 }.template operator()<0>();
10101 }
10102
10103 template<typename _Fp>
10104 constexpr auto
10105 _M_invoke_with_runtime_index(_Fp&& __f)
10106 { return _S_invoke_with_runtime_index(std::forward<_Fp>(__f), _M_it.index()); }
10107
10108 template<typename... _Args>
10109 explicit constexpr
10110 _Iterator(__maybe_const_t<_Const, concat_view>* __parent, _Args&&... __args)
10111 requires constructible_from<__base_iter, _Args&&...>
10112 : _M_parent(__parent), _M_it(std::forward<_Args>(__args)...)
10113 { }
10114
10115 public:
10116 _Iterator() = default;
10117
10118 constexpr
10119 _Iterator(_Iterator<!_Const> __it)
10120 requires _Const && (convertible_to<iterator_t<_Vs>, iterator_t<const _Vs>> && ...)
10121 : _M_parent(__it._M_parent),
10122 _M_it(_S_invoke_with_runtime_index([this, &__it]<size_t _Idx>() {
10123 return __base_iter(in_place_index<_Idx>,
10124 std::get<_Idx>(std::move(__it._M_it)));
10125 }, __it._M_it.index()))
10126 { }
10127
10128 constexpr decltype(auto)
10129 operator*() const
10130 {
10131 __glibcxx_assert(!_M_it.valueless_by_exception());
10132 using reference = __detail::__concat_reference_t<__maybe_const_t<_Const, _Vs>...>;
10133 return std::visit([](auto&& __it) -> reference { return *__it; }, _M_it);
10134 }
10135
10136 constexpr _Iterator&
10137 operator++()
10138 {
10139 _M_invoke_with_runtime_index([this]<size_t _Idx>() {
10140 ++std::get<_Idx>(_M_it);
10141 _M_satisfy<_Idx>();
10142 });
10143 return *this;
10144 }
10145
10146 constexpr void
10147 operator++(int)
10148 { ++*this; }
10149
10150 constexpr _Iterator
10151 operator++(int)
10152 requires __detail::__all_forward<_Const, _Vs...>
10153 {
10154 auto __tmp = *this;
10155 ++*this;
10156 return __tmp;
10157 }
10158
10159 constexpr _Iterator&
10160 operator--()
10161 requires __detail::__concat_is_bidirectional<_Const, _Vs...>
10162 {
10163 __glibcxx_assert(!_M_it.valueless_by_exception());
10164 _M_invoke_with_runtime_index([this]<size_t _Idx>() {
10165 _M_prev<_Idx>();
10166 });
10167 return *this;
10168 }
10169
10170 constexpr _Iterator
10171 operator--(int)
10172 requires __detail::__concat_is_bidirectional<_Const, _Vs...>
10173 {
10174 auto __tmp = *this;
10175 --*this;
10176 return __tmp;
10177 }
10178
10179 constexpr _Iterator&
10180 operator+=(difference_type __n)
10181 requires __detail::__concat_is_random_access<_Const, _Vs...>
10182 {
10183 __glibcxx_assert(!_M_it.valueless_by_exception());
10184 _M_invoke_with_runtime_index([this, __n]<size_t _Idx>() {
10185 auto __begin = ranges::begin(std::get<_Idx>(_M_parent->_M_views));
10186 if (__n > 0)
10187 _M_advance_fwd<_Idx>(std::get<_Idx>(_M_it) - __begin, __n);
10188 else if (__n < 0)
10189 _M_advance_bwd<_Idx>(std::get<_Idx>(_M_it) - __begin, -__n);
10190 });
10191 return *this;
10192 }
10193
10194 constexpr _Iterator&
10195 operator-=(difference_type __n)
10196 requires __detail::__concat_is_random_access<_Const, _Vs...>
10197 {
10198 *this += -__n;
10199 return *this;
10200 }
10201
10202 constexpr decltype(auto)
10203 operator[](difference_type __n) const
10204 requires __detail::__concat_is_random_access<_Const, _Vs...>
10205 { return *((*this) + __n); }
10206
10207 friend constexpr bool
10208 operator==(const _Iterator& __x, const _Iterator& __y)
10209 requires (equality_comparable<iterator_t<__maybe_const_t<_Const, _Vs>>> && ...)
10210 {
10211 __glibcxx_assert(!__x._M_it.valueless_by_exception());
10212 __glibcxx_assert(!__y._M_it.valueless_by_exception());
10213 return __x._M_it == __y._M_it;
10214 }
10215
10216 friend constexpr bool
10217 operator==(const _Iterator& __it, default_sentinel_t)
10218 {
10219 __glibcxx_assert(!__it._M_it.valueless_by_exception());
10220 constexpr auto __last_idx = sizeof...(_Vs) - 1;
10221 return (__it._M_it.index() == __last_idx
10222 && (std::get<__last_idx>(__it._M_it)
10223 == ranges::end(std::get<__last_idx>(__it._M_parent->_M_views))));
10224 }
10225
10226 friend constexpr bool
10227 operator<(const _Iterator& __x, const _Iterator& __y)
10228 requires __detail::__all_random_access<_Const, _Vs...>
10229 { return __x._M_it < __y._M_it; }
10230
10231 friend constexpr bool
10232 operator>(const _Iterator& __x, const _Iterator& __y)
10233 requires __detail::__all_random_access<_Const, _Vs...>
10234 { return __x._M_it > __y._M_it; }
10235
10236 friend constexpr bool
10237 operator<=(const _Iterator& __x, const _Iterator& __y)
10238 requires __detail::__all_random_access<_Const, _Vs...>
10239 { return __x._M_it <= __y._M_it; }
10240
10241 friend constexpr bool
10242 operator>=(const _Iterator& __x, const _Iterator& __y)
10243 requires __detail::__all_random_access<_Const, _Vs...>
10244 { return __x._M_it >= __y._M_it; }
10245
10246 friend constexpr auto
10247 operator<=>(const _Iterator& __x, const _Iterator& __y)
10248 requires __detail::__all_random_access<_Const, _Vs...>
10249 && (three_way_comparable<iterator_t<__maybe_const_t<_Const, _Vs>>> && ...)
10250 { return __x._M_it <=> __y._M_it; }
10251
10252 friend constexpr _Iterator
10253 operator+(const _Iterator& __it, difference_type __n)
10254 requires __detail::__concat_is_random_access<_Const, _Vs...>
10255 { return auto(__it) += __n; }
10256
10257 friend constexpr _Iterator
10258 operator+(difference_type __n, const _Iterator& __it)
10259 requires __detail::__concat_is_random_access<_Const, _Vs...>
10260 { return __it + __n; }
10261
10262 friend constexpr _Iterator
10263 operator-(const _Iterator& __it, difference_type __n)
10264 requires __detail::__concat_is_random_access<_Const, _Vs...>
10265 { return auto(__it) -= __n; }
10266
10267 friend constexpr difference_type
10268 operator-(const _Iterator& __x, const _Iterator& __y)
10269 requires __detail::__concat_is_random_access<_Const, _Vs...>
10270 {
10271 return _S_invoke_with_runtime_index([&]<size_t _Ix>() -> difference_type {
10272 return _S_invoke_with_runtime_index([&]<size_t _Iy>() -> difference_type {
10273 if constexpr (_Ix > _Iy)
10274 {
10275 auto __dy = ranges::distance(std::get<_Iy>(__y._M_it),
10276 ranges::end(std::get<_Iy>(__y._M_parent
10277 ->_M_views)));
10278 auto __dx = ranges::distance(ranges::begin(std::get<_Ix>(__x._M_parent
10279 ->_M_views)),
10280 std::get<_Ix>(__x._M_it));
10281 difference_type __s = 0;
10282 [&]<size_t _Idx = _Iy + 1>(this auto&& __self) {
10283 if constexpr (_Idx < _Ix)
10284 {
10285 __s += ranges::size(std::get<_Idx>(__x._M_parent->_M_views));
10286 __self.template operator()<_Idx + 1>();
10287 }
10288 }();
10289 return __dy + __s + __dx;
10290 }
10291 else if constexpr (_Ix < _Iy)
10292 return -(__y - __x);
10293 else
10294 return std::get<_Ix>(__x._M_it) - std::get<_Iy>(__y._M_it);
10295 }, __y._M_it.index());
10296 }, __x._M_it.index());
10297 }
10298
10299 friend constexpr difference_type
10300 operator-(const _Iterator& __x, default_sentinel_t)
10301 requires (sized_sentinel_for<sentinel_t<__maybe_const_t<_Const, _Vs>>,
10302 iterator_t<__maybe_const_t<_Const, _Vs>>> && ...)
10303 && __detail::__all_but_first_sized<__maybe_const_t<_Const, _Vs>...>::value
10304 {
10305 return _S_invoke_with_runtime_index([&]<size_t _Ix>() -> difference_type {
10306 auto __dx = ranges::distance(std::get<_Ix>(__x._M_it),
10307 ranges::end(std::get<_Ix>(__x._M_parent->_M_views)));
10308 difference_type __s = 0;
10309 [&]<size_t _Idx = _Ix + 1>(this auto&& __self) {
10310 if constexpr (_Idx < sizeof...(_Vs))
10311 {
10312 __s += ranges::size(std::get<_Idx>(__x._M_parent->_M_views));
10313 __self.template operator()<_Idx + 1>();
10314 }
10315 }();
10316 return -(__dx + __s);
10317 }, __x._M_it.index());
10318 }
10319
10320 friend constexpr difference_type
10321 operator-(default_sentinel_t, const _Iterator& __x)
10322 requires (sized_sentinel_for<sentinel_t<__maybe_const_t<_Const, _Vs>>,
10323 iterator_t<__maybe_const_t<_Const, _Vs>>> && ...)
10324 && __detail::__all_but_first_sized<__maybe_const_t<_Const, _Vs>...>::value
10325 { return -(__x - default_sentinel); }
10326
10327 friend constexpr decltype(auto)
10328 iter_move(const _Iterator& __it)
10329 {
10330 using _Res = __detail::__concat_rvalue_reference_t<__maybe_const_t<_Const, _Vs>...>;
10331 return std::visit([](const auto& __i) -> _Res {
10332 return ranges::iter_move(__i);
10333 }, __it._M_it);
10334 }
10335
10336 friend constexpr void
10337 iter_swap(const _Iterator& __x, const _Iterator& __y)
10338 requires swappable_with<iter_reference_t<_Iterator>, iter_reference_t<_Iterator>>
10339 && (... && indirectly_swappable<iterator_t<__maybe_const_t<_Const, _Vs>>>)
10340 {
10341 std::visit([&]<typename _Tp, typename _Up>(const _Tp& __it1, const _Up& __it2) {
10342 if constexpr (is_same_v<_Tp, _Up>)
10343 ranges::iter_swap(__it1, __it2);
10344 else
10345 ranges::swap(*__it1, *__it2);
10346 }, __x._M_it, __y._M_it);
10347 }
10348 };
10349
10350 namespace views
10351 {
10352 namespace __detail
10353 {
10354 template<typename... _Ts>
10355 concept __can_concat_view = requires { concat_view(std::declval<_Ts>()...); };
10356 }
10357
10358 struct _Concat
10359 {
10360 template<typename... _Ts>
10361 requires __detail::__can_concat_view<_Ts...>
10362 constexpr auto
10363 operator() [[nodiscard]] (_Ts&&... __ts) const
10364 { return concat_view(std::forward<_Ts>(__ts)...); }
10365
10366 template<input_range _Range>
10367 constexpr auto
10368 operator() [[nodiscard]] (_Range&& __t) const
10369 { return views::all(std::forward<_Range>(__t)); }
10370 };
10371
10372 inline constexpr _Concat concat;
10373 }
10374
10375} // namespace ranges
10376#endif // __cpp_lib_ranges_concat
10377
10378#if __cpp_lib_ranges_cache_latest // C++ >= 26
10379namespace ranges
10380{
10381 template<input_range _Vp>
10382 requires view<_Vp>
10383 class cache_latest_view : public view_interface<cache_latest_view<_Vp>>
10384 {
10385 _Vp _M_base = _Vp();
10386
10387 using __cache_t = __conditional_t<is_reference_v<range_reference_t<_Vp>>,
10388 add_pointer_t<range_reference_t<_Vp>>,
10389 range_reference_t<_Vp>>;
10390 __detail::__non_propagating_cache<__cache_t> _M_cache;
10391
10392 class _Iterator;
10393 class _Sentinel;
10394
10395 public:
10396 cache_latest_view() requires default_initializable<_Vp> = default;
10397
10398 constexpr explicit
10399 cache_latest_view(_Vp __base)
10400 : _M_base(std::move(__base))
10401 { }
10402
10403 constexpr _Vp
10404 base() const & requires copy_constructible<_Vp>
10405 { return _M_base; }
10406
10407 constexpr _Vp
10408 base() &&
10409 { return std::move(_M_base); }
10410
10411 constexpr auto
10412 begin()
10413 { return _Iterator(*this); }
10414
10415 constexpr auto
10416 end()
10417 { return _Sentinel(*this); }
10418
10419 constexpr auto
10420 size() requires sized_range<_Vp>
10421 { return ranges::size(_M_base); }
10422
10423 constexpr auto
10424 size() const requires sized_range<const _Vp>
10425 { return ranges::size(_M_base); }
10426 };
10427
10428 template<typename _Range>
10429 cache_latest_view(_Range&&) -> cache_latest_view<views::all_t<_Range>>;
10430
10431 template<input_range _Vp>
10432 requires view<_Vp>
10433 class cache_latest_view<_Vp>::_Iterator
10434 {
10435 cache_latest_view* _M_parent;
10436 iterator_t<_Vp> _M_current;
10437
10438 constexpr explicit
10439 _Iterator(cache_latest_view& __parent)
10440 : _M_parent(std::__addressof(__parent)),
10441 _M_current(ranges::begin(__parent._M_base))
10442 { }
10443
10444 friend class cache_latest_view;
10445
10446 public:
10447 using difference_type = range_difference_t<_Vp>;
10448 using value_type = range_value_t<_Vp>;
10449 using iterator_concept = input_iterator_tag;
10450
10451 _Iterator(_Iterator&&) = default;
10452
10453 _Iterator&
10454 operator=(_Iterator&&) = default;
10455
10456 constexpr iterator_t<_Vp>
10457 base() &&
10458 { return std::move(_M_current); }
10459
10460 constexpr const iterator_t<_Vp>&
10461 base() const & noexcept
10462 { return _M_current; }
10463
10464 constexpr range_reference_t<_Vp>&
10465 operator*() const
10466 {
10467 if constexpr (is_reference_v<range_reference_t<_Vp>>)
10468 {
10469 if (!_M_parent->_M_cache)
10470 _M_parent->_M_cache = std::__addressof(__detail::__as_lvalue(*_M_current));
10471 return **_M_parent->_M_cache;
10472 }
10473 else
10474 {
10475 if (!_M_parent->_M_cache)
10476 _M_parent->_M_cache._M_emplace_deref(_M_current);
10477 return *_M_parent->_M_cache;
10478 }
10479 }
10480
10481 constexpr _Iterator&
10482 operator++()
10483 {
10484 _M_parent->_M_cache._M_reset();
10485 ++_M_current;
10486 return *this;
10487 }
10488
10489 constexpr void
10490 operator++(int)
10491 { ++*this; }
10492
10493 friend constexpr range_rvalue_reference_t<_Vp>
10494 iter_move(const _Iterator& __i)
10495 noexcept(noexcept(ranges::iter_move(__i._M_current)))
10496 { return ranges::iter_move(__i._M_current); }
10497
10498 friend constexpr void
10499 iter_swap(const _Iterator& __x, const _Iterator& __y)
10500 noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
10501 requires indirectly_swappable<iterator_t<_Vp>>
10502 { ranges::iter_swap(__x._M_current, __y._M_current); }
10503 };
10504
10505 template<input_range _Vp>
10506 requires view<_Vp>
10507 class cache_latest_view<_Vp>::_Sentinel
10508 {
10509 sentinel_t<_Vp> _M_end = sentinel_t<_Vp>();
10510
10511 constexpr explicit
10512 _Sentinel(cache_latest_view& __parent)
10513 : _M_end(ranges::end(__parent._M_base))
10514 { }
10515
10516 friend class cache_latest_view;
10517
10518 public:
10519 _Sentinel() = default;
10520
10521 constexpr sentinel_t<_Vp>
10522 base() const
10523 { return _M_end; }
10524
10525 friend constexpr bool
10526 operator==(const _Iterator& __x, const _Sentinel& __y)
10527 { return __x._M_current == __y._M_end; }
10528
10529 friend constexpr range_difference_t<_Vp>
10530 operator-(const _Iterator& __x, const _Sentinel& __y)
10531 requires sized_sentinel_for<sentinel_t<_Vp>, iterator_t<_Vp>>
10532 { return __x._M_current - __y._M_end; }
10533
10534 friend constexpr range_difference_t<_Vp>
10535 operator-(const _Sentinel& __x, const _Iterator& __y)
10536 requires sized_sentinel_for<sentinel_t<_Vp>, iterator_t<_Vp>>
10537 { return __x._M_end - __y._M_current; }
10538 };
10539
10540 namespace views
10541 {
10542 namespace __detail
10543 {
10544 template<typename _Tp>
10545 concept __can_cache_latest = requires { cache_latest_view(std::declval<_Tp>()); };
10546 }
10547
10548 struct _CacheLatest : __adaptor::_RangeAdaptorClosure<_CacheLatest>
10549 {
10550 template<viewable_range _Range>
10551 requires __detail::__can_cache_latest<_Range>
10552 constexpr auto
10553 operator() [[nodiscard]] (_Range&& __r) const
10554 { return cache_latest_view(std::forward<_Range>(__r)); }
10555
10556 static constexpr bool _S_has_simple_call_op = true;
10557 };
10558
10559 inline constexpr _CacheLatest cache_latest;
10560 }
10561} // namespace ranges
10562#endif // __cpp_lib_ranges_cache_latest
10563
10564#if __cpp_lib_ranges_as_input // C++ >= 26
10565namespace ranges
10566{
10567 template<input_range _Vp>
10568 requires view<_Vp>
10569 class as_input_view : public view_interface<as_input_view<_Vp>>
10570 {
10571 _Vp _M_base = _Vp();
10572
10573 template<bool _Const>
10574 class _Iterator;
10575
10576 public:
10577 as_input_view() requires default_initializable<_Vp> = default;
10578
10579 constexpr explicit
10580 as_input_view(_Vp __base)
10581 : _M_base(std::move(__base))
10582 { }
10583
10584 constexpr _Vp
10585 base() const & requires copy_constructible<_Vp>
10586 { return _M_base; }
10587
10588 constexpr _Vp
10589 base() &&
10590 { return std::move(_M_base); }
10591
10592 constexpr auto
10593 begin() requires (!__detail::__simple_view<_Vp>)
10594 { return _Iterator<false>(ranges::begin(_M_base)); }
10595
10596 constexpr auto
10597 begin() const requires range<const _Vp>
10598 { return _Iterator<true>(ranges::begin(_M_base)); }
10599
10600 constexpr auto
10601 end() requires (!__detail::__simple_view<_Vp>)
10602 { return ranges::end(_M_base); }
10603
10604 constexpr auto
10605 end() const requires range<const _Vp>
10606 { return ranges::end(_M_base); }
10607
10608 constexpr auto
10609 size() requires sized_range<_Vp>
10610 { return ranges::size(_M_base); }
10611
10612 constexpr auto
10613 size() const requires sized_range<const _Vp>
10614 { return ranges::size(_M_base); }
10615 };
10616
10617 template<typename _Range>
10618 as_input_view(_Range&&) -> as_input_view<views::all_t<_Range>>;
10619
10620 template<input_range _Vp>
10621 requires view<_Vp>
10622 template<bool _Const>
10623 class as_input_view<_Vp>::_Iterator
10624 {
10625 using _Base = __maybe_const_t<_Const, _Vp>;
10626
10627 iterator_t<_Base> _M_current = iterator_t<_Base>();
10628
10629 constexpr explicit
10630 _Iterator(iterator_t<_Base> __current)
10631 : _M_current(std::move(__current))
10632 { }
10633
10634 friend as_input_view;
10635 friend _Iterator<!_Const>;
10636
10637 public:
10638 using difference_type = range_difference_t<_Base>;
10639 using value_type = range_value_t<_Base>;
10640 using iterator_concept = input_iterator_tag;
10641
10642 _Iterator() requires default_initializable<iterator_t<_Base>> = default;
10643
10644 _Iterator(_Iterator&&) = default;
10645 _Iterator& operator=(_Iterator&&) = default;
10646
10647 constexpr
10648 _Iterator(_Iterator<!_Const> __i)
10649 requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
10650 : _M_current(std::move(__i._M_current))
10651 { }
10652
10653 constexpr iterator_t<_Base>
10654 base() &&
10655 { return std::move(_M_current); }
10656
10657 constexpr const iterator_t<_Base>&
10658 base() const & noexcept
10659 { return _M_current; }
10660
10661 constexpr decltype(auto)
10662 operator*() const
10663 { return *_M_current; }
10664
10665 constexpr _Iterator&
10666 operator++()
10667 {
10668 ++_M_current;
10669 return *this;
10670 }
10671
10672 constexpr void
10673 operator++(int)
10674 { ++*this; }
10675
10676 friend constexpr bool
10677 operator==(const _Iterator& __x, const sentinel_t<_Base>& __y)
10678 { return __x._M_current == __y; }
10679
10680 friend constexpr difference_type
10681 operator-(const sentinel_t<_Base>& __y, const _Iterator& __x)
10682 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base>>
10683 { return __y - __x._M_current; }
10684
10685 friend constexpr difference_type
10686 operator-(const _Iterator& __x, const sentinel_t<_Base>& __y)
10687 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base>>
10688 { return __x._M_current - __y; }
10689
10690 friend constexpr range_rvalue_reference_t<_Base>
10691 iter_move(const _Iterator& __i)
10692 noexcept(noexcept(ranges::iter_move(__i._M_current)))
10693 { return ranges::iter_move(__i._M_current); }
10694
10695 friend constexpr void
10696 iter_swap(const _Iterator& __x, const _Iterator& __y)
10697 noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
10698 requires indirectly_swappable<iterator_t<_Base>>
10699 { ranges::iter_swap(__x._M_current, __y._M_current); }
10700 };
10701
10702 namespace views
10703 {
10704 namespace __detail
10705 {
10706 template<typename _Tp>
10707 concept __can_as_input = requires { as_input_view(std::declval<_Tp>()); };
10708 }
10709
10710 struct _AsInput : __adaptor::_RangeAdaptorClosure<_AsInput>
10711 {
10712 template<viewable_range _Range>
10713 requires __detail::__can_as_input<_Range>
10714 constexpr auto
10715 operator() [[nodiscard]] (_Range&& __r) const
10716 {
10717 if constexpr (input_range<_Range>
10718 && !common_range<_Range>
10719 && !forward_range<_Range>)
10720 return views::all(std::forward<_Range>(__r));
10721 else
10722 return as_input_view(std::forward<_Range>(__r));
10723 }
10724
10725 static constexpr bool _S_has_simple_call_op = true;
10726 };
10727
10728 inline constexpr _AsInput as_input;
10729 }
10730} // namespace ranges
10731#endif // __cpp_lib_ranges_as_input
10732
10733_GLIBCXX_END_NAMESPACE_VERSION
10734} // namespace std
10735#endif // library concepts
10736#endif // C++2a
10737#endif /* _GLIBCXX_RANGES */
compare
initializer_list
concepts
iterator
optional
span
string_view
tuple
utility
variant
ranges_util.h
refwrap.h
version.h
std::chrono::operator<=
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:863
std::chrono::operator>=
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:877
std::chrono::operator<
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:830
std::chrono::operator>
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:870
std::operator*
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition complex:434
std::operator-
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
Definition complex:404
std::operator+
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition complex:374
std::to_address
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:234
std::remove_reference_t
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1913
std::common_type_t
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
Definition type_traits:2975
std::make_unsigned_t
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition type_traits:2273
std::decay_t
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2963
std::declval
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2741
std::addressof
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition move.h:176
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
std::__invoke
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
std::__addressof
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
std::forward
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
std::min
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition stl_algobase.h:232
std::make_reverse_iterator
constexpr reverse_iterator< _Iterator > make_reverse_iterator(_Iterator __i)
Generator function for reverse_iterator.
Definition bits/stl_iterator.h:647
std
ISO C++ entities toplevel namespace is std.
std::make_index_sequence
make_integer_sequence< size_t, _Num > make_index_sequence
Alias template make_index_sequence.
Definition utility.h:528
std::index_sequence
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
Definition utility.h:524
std::end
constexpr auto end(_Container &__cont) noexcept(noexcept(__cont.end())) -> decltype(__cont.end())
Return an iterator pointing to one past the last element of the container.
Definition range_access.h:80
std::empty
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
Definition range_access.h:318
std::size
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition range_access.h:294
std::default_sentinel
constexpr default_sentinel_t default_sentinel
A default sentinel value.
Definition iterator_concepts.h:63
std::data
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
Definition range_access.h:354
std::begin
constexpr auto begin(_Container &__cont) noexcept(noexcept(__cont.begin())) -> decltype(__cont.begin())
Return an iterator pointing to the first element of the container.
Definition range_access.h:55
__gnu_debug::__base
constexpr _Iterator __base(_Iterator __it)
Definition helper_functions.h:339
std::ranges::empty_view
A view that contains no elements.
Definition ranges:110
std::ranges::single_view
A view that contains exactly one element.
Definition ranges:435
std::reference_wrapper::ref
constexpr reference_wrapper< _Tp > ref(_Tp &__t) noexcept
Denotes a reference should be taken to a variable.
Definition refwrap.h:428
std::reference_wrapper::cref
constexpr reference_wrapper< const _Tp > cref(const _Tp &__t) noexcept
Denotes a const reference should be taken to a variable.
Definition refwrap.h:435
std::ranges::view_interface
The ranges::view_interface class template.
Definition ranges_util.h:71
std::constructible_from
[concept.constructible], concept constructible_from
Definition concepts:162
std::default_initializable
[concept.defaultinitializable], concept default_initializable
Definition concepts:166
std::move_constructible
[concept.moveconstructible], concept move_constructible
Definition concepts:176
std::copy_constructible
[concept.copyconstructible], concept copy_constructible
Definition concepts:181
std::ranges::range
[range.range] The range concept.
Definition ranges_base.h:510
std::ranges::sized_range
[range.sized] The sized_range concept.
Definition ranges_base.h:553
std::ranges::view
[range.view] The ranges::view concept.
Definition ranges_base.h:608
std::ranges::input_range
A range for which ranges::begin returns an input iterator.
Definition ranges_base.h:619
std::ranges::forward_range
A range for which ranges::begin returns a forward iterator.
Definition ranges_base.h:624
std::ranges::bidirectional_range
A range for which ranges::begin returns a bidirectional iterator.
Definition ranges_base.h:629
std::ranges::random_access_range
A range for which ranges::begin returns a random access iterator.
Definition ranges_base.h:634
std::ranges::contiguous_range
A range for which ranges::begin returns a contiguous iterator.
Definition ranges_base.h:639
std::ranges::common_range
A range for which ranges::begin and ranges::end return the same type.
Definition ranges_base.h:648
binders.h