1 // <ranges> -*- C++ -*-
3 // Copyright (C) 2019-2020 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file include/ranges
26 * This is a Standard C++ Library header.
30 #ifndef _GLIBCXX_RANGES
31 #define _GLIBCXX_RANGES 1
33 #if __cplusplus > 201703L
35 #pragma GCC system_header
39 #if __cpp_lib_concepts
41 #include <bits/refwrap.h>
43 #include <initializer_list>
49 * @defgroup ranges Ranges
51 * Components for dealing with ranges of elements.
54 namespace std _GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 // [range.range] The range concept.
60 // [range.sized] The sized_range concept.
61 // Defined in <bits/range_access.h>
63 // [range.refinements]
64 // Defined in <bits/range_access.h>
68 template<typename _Tp>
69 inline constexpr bool enable_view = derived_from<_Tp, view_base>;
71 template<typename _Tp>
73 = range<_Tp> && movable<_Tp> && enable_view<_Tp>;
75 /// A range which can be safely converted to a view.
76 template<typename _Tp>
77 concept viewable_range = range<_Tp>
78 && (borrowed_range<_Tp> || view<remove_cvref_t<_Tp>>);
82 template<typename _Range>
83 concept __simple_view = view<_Range> && range<const _Range>
84 && same_as<iterator_t<_Range>, iterator_t<const _Range>>
85 && same_as<sentinel_t<_Range>, sentinel_t<const _Range>>;
87 template<typename _It>
88 concept __has_arrow = input_iterator<_It>
89 && (is_pointer_v<_It> || requires(_It __it) { __it.operator->(); });
91 template<typename _Tp, typename _Up>
93 = !same_as<remove_cvref_t<_Tp>, remove_cvref_t<_Up>>;
94 } // namespace __detail
96 template<typename _Derived>
97 requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
98 class view_interface : public view_base
101 constexpr _Derived& _M_derived() noexcept
103 static_assert(derived_from<_Derived, view_interface<_Derived>>);
104 static_assert(view<_Derived>);
105 return static_cast<_Derived&>(*this);
108 constexpr const _Derived& _M_derived() const noexcept
110 static_assert(derived_from<_Derived, view_interface<_Derived>>);
111 static_assert(view<_Derived>);
112 return static_cast<const _Derived&>(*this);
117 empty() requires forward_range<_Derived>
118 { return ranges::begin(_M_derived()) == ranges::end(_M_derived()); }
121 empty() const requires forward_range<const _Derived>
122 { return ranges::begin(_M_derived()) == ranges::end(_M_derived()); }
125 operator bool() requires requires { ranges::empty(_M_derived()); }
126 { return !ranges::empty(_M_derived()); }
129 operator bool() const requires requires { ranges::empty(_M_derived()); }
130 { return !ranges::empty(_M_derived()); }
133 data() requires contiguous_iterator<iterator_t<_Derived>>
134 { return to_address(ranges::begin(_M_derived())); }
138 requires range<const _Derived>
139 && contiguous_iterator<iterator_t<const _Derived>>
140 { return to_address(ranges::begin(_M_derived())); }
144 requires forward_range<_Derived>
145 && sized_sentinel_for<sentinel_t<_Derived>, iterator_t<_Derived>>
146 { return ranges::end(_M_derived()) - ranges::begin(_M_derived()); }
150 requires forward_range<const _Derived>
151 && sized_sentinel_for<sentinel_t<const _Derived>,
152 iterator_t<const _Derived>>
153 { return ranges::end(_M_derived()) - ranges::begin(_M_derived()); }
155 constexpr decltype(auto)
156 front() requires forward_range<_Derived>
158 __glibcxx_assert(!empty());
159 return *ranges::begin(_M_derived());
162 constexpr decltype(auto)
163 front() const requires forward_range<const _Derived>
165 __glibcxx_assert(!empty());
166 return *ranges::begin(_M_derived());
169 constexpr decltype(auto)
171 requires bidirectional_range<_Derived> && common_range<_Derived>
173 __glibcxx_assert(!empty());
174 return *ranges::prev(ranges::end(_M_derived()));
177 constexpr decltype(auto)
179 requires bidirectional_range<const _Derived>
180 && common_range<const _Derived>
182 __glibcxx_assert(!empty());
183 return *ranges::prev(ranges::end(_M_derived()));
186 template<random_access_range _Range = _Derived>
187 constexpr decltype(auto)
188 operator[](range_difference_t<_Range> __n)
189 { return ranges::begin(_M_derived())[__n]; }
191 template<random_access_range _Range = const _Derived>
192 constexpr decltype(auto)
193 operator[](range_difference_t<_Range> __n) const
194 { return ranges::begin(_M_derived())[__n]; }
199 template<class _From, class _To>
200 concept __convertible_to_non_slicing = convertible_to<_From, _To>
201 && !(is_pointer_v<decay_t<_From>> && is_pointer_v<decay_t<_To>>
202 && __not_same_as<remove_pointer_t<decay_t<_From>>,
203 remove_pointer_t<decay_t<_To>>>);
205 template<typename _Tp>
207 = !is_reference_v<_Tp> && requires(_Tp __t)
209 typename tuple_size<_Tp>::type;
210 requires derived_from<tuple_size<_Tp>, integral_constant<size_t, 2>>;
211 typename tuple_element_t<0, remove_const_t<_Tp>>;
212 typename tuple_element_t<1, remove_const_t<_Tp>>;
213 { get<0>(__t) } -> convertible_to<const tuple_element_t<0, _Tp>&>;
214 { get<1>(__t) } -> convertible_to<const tuple_element_t<1, _Tp>&>;
217 template<typename _Tp, typename _Up, typename _Vp>
218 concept __pair_like_convertible_from
219 = !range<_Tp> && __pair_like<_Tp>
220 && constructible_from<_Tp, _Up, _Vp>
221 && __convertible_to_non_slicing<_Up, tuple_element_t<0, _Tp>>
222 && convertible_to<_Vp, tuple_element_t<1, _Tp>>;
224 } // namespace __detail
226 enum class subrange_kind : bool { unsized, sized };
228 template<input_or_output_iterator _It, sentinel_for<_It> _Sent = _It,
229 subrange_kind _Kind = sized_sentinel_for<_Sent, _It>
230 ? subrange_kind::sized : subrange_kind::unsized>
231 requires (_Kind == subrange_kind::sized || !sized_sentinel_for<_Sent, _It>)
232 class subrange : public view_interface<subrange<_It, _Sent, _Kind>>
235 // XXX: gcc complains when using constexpr here
236 static const bool _S_store_size
237 = _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _It>;
239 _It _M_begin = _It();
240 _Sent _M_end = _Sent();
242 template<typename, bool = _S_store_size>
246 template<typename _Tp>
247 struct _Size<_Tp, true>
248 { __detail::__make_unsigned_like_t<_Tp> _M_size; };
250 [[no_unique_address]] _Size<iter_difference_t<_It>> _M_size = {};
253 subrange() requires default_initializable<_It> = default;
256 subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s)
257 requires (!_S_store_size)
258 : _M_begin(std::move(__i)), _M_end(__s)
262 subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s,
263 __detail::__make_unsigned_like_t<iter_difference_t<_It>> __n)
264 requires (_Kind == subrange_kind::sized)
265 : _M_begin(std::move(__i)), _M_end(__s)
267 using __detail::__to_unsigned_like;
268 __glibcxx_assert(__n == __to_unsigned_like(ranges::distance(__i, __s)));
269 if constexpr (_S_store_size)
270 _M_size._M_size = __n;
273 template<__detail::__not_same_as<subrange> _Rng>
274 requires borrowed_range<_Rng>
275 && __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
276 && convertible_to<sentinel_t<_Rng>, _Sent>
278 subrange(_Rng&& __r) requires _S_store_size && sized_range<_Rng>
279 : subrange(__r, ranges::size(__r))
282 template<__detail::__not_same_as<subrange> _Rng>
283 requires borrowed_range<_Rng>
284 && __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
285 && convertible_to<sentinel_t<_Rng>, _Sent>
287 subrange(_Rng&& __r) requires (!_S_store_size)
288 : subrange{ranges::begin(__r), ranges::end(__r)}
291 template<borrowed_range _Rng>
292 requires __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
293 && convertible_to<sentinel_t<_Rng>, _Sent>
296 __detail::__make_unsigned_like_t<iter_difference_t<_It>> __n)
297 requires (_Kind == subrange_kind::sized)
298 : subrange{ranges::begin(__r), ranges::end(__r), __n}
301 template<__detail::__not_same_as<subrange> _PairLike>
302 requires __detail::__pair_like_convertible_from<_PairLike, const _It&,
305 operator _PairLike() const
306 { return _PairLike(_M_begin, _M_end); }
309 begin() const requires copyable<_It>
312 [[nodiscard]] constexpr _It
313 begin() requires (!copyable<_It>)
314 { return std::move(_M_begin); }
316 constexpr _Sent end() const { return _M_end; }
318 constexpr bool empty() const { return _M_begin == _M_end; }
320 constexpr __detail::__make_unsigned_like_t<iter_difference_t<_It>>
321 size() const requires (_Kind == subrange_kind::sized)
323 if constexpr (_S_store_size)
324 return _M_size._M_size;
326 return __detail::__to_unsigned_like(_M_end - _M_begin);
329 [[nodiscard]] constexpr subrange
330 next(iter_difference_t<_It> __n = 1) const &
331 requires forward_iterator<_It>
338 [[nodiscard]] constexpr subrange
339 next(iter_difference_t<_It> __n = 1) &&
342 return std::move(*this);
345 [[nodiscard]] constexpr subrange
346 prev(iter_difference_t<_It> __n = 1) const
347 requires bidirectional_iterator<_It>
355 advance(iter_difference_t<_It> __n)
357 // _GLIBCXX_RESOLVE_LIB_DEFECTS
358 // 3433. subrange::advance(n) has UB when n < 0
359 if constexpr (bidirectional_iterator<_It>)
362 ranges::advance(_M_begin, __n);
363 if constexpr (_S_store_size)
364 _M_size._M_size += __detail::__to_unsigned_like(-__n);
368 __glibcxx_assert(__n >= 0);
369 auto __d = __n - ranges::advance(_M_begin, __n, _M_end);
370 if constexpr (_S_store_size)
371 _M_size._M_size -= __detail::__to_unsigned_like(__d);
376 template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
377 subrange(_It, _Sent) -> subrange<_It, _Sent>;
379 template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
381 __detail::__make_unsigned_like_t<iter_difference_t<_It>>)
382 -> subrange<_It, _Sent, subrange_kind::sized>;
384 template<borrowed_range _Rng>
386 -> subrange<iterator_t<_Rng>, sentinel_t<_Rng>,
388 || sized_sentinel_for<sentinel_t<_Rng>, iterator_t<_Rng>>)
389 ? subrange_kind::sized : subrange_kind::unsized>;
391 template<borrowed_range _Rng>
393 __detail::__make_unsigned_like_t<range_difference_t<_Rng>>)
394 -> subrange<iterator_t<_Rng>, sentinel_t<_Rng>, subrange_kind::sized>;
396 template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
399 get(const subrange<_It, _Sent, _Kind>& __r)
401 if constexpr (_Num == 0)
407 template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
410 get(subrange<_It, _Sent, _Kind>&& __r)
412 if constexpr (_Num == 0)
418 template<input_or_output_iterator _It, sentinel_for<_It> _Sent,
420 inline constexpr bool
421 enable_borrowed_range<subrange<_It, _Sent, _Kind>> = true;
423 } // namespace ranges
429 /// Type returned by algorithms instead of a dangling iterator or subrange.
432 constexpr dangling() noexcept = default;
433 template<typename... _Args>
434 constexpr dangling(_Args&&...) noexcept { }
437 template<range _Range>
438 using borrowed_iterator_t = conditional_t<borrowed_range<_Range>,
442 template<range _Range>
443 using borrowed_subrange_t = conditional_t<borrowed_range<_Range>,
444 subrange<iterator_t<_Range>>,
447 template<typename _Tp> requires is_object_v<_Tp>
449 : public view_interface<empty_view<_Tp>>
452 static constexpr _Tp* begin() noexcept { return nullptr; }
453 static constexpr _Tp* end() noexcept { return nullptr; }
454 static constexpr _Tp* data() noexcept { return nullptr; }
455 static constexpr size_t size() noexcept { return 0; }
456 static constexpr bool empty() noexcept { return true; }
459 template<typename _Tp>
460 inline constexpr bool enable_borrowed_range<empty_view<_Tp>> = true;
464 template<copy_constructible _Tp> requires is_object_v<_Tp>
465 struct __box : std::optional<_Tp>
467 using std::optional<_Tp>::optional;
471 noexcept(is_nothrow_default_constructible_v<_Tp>)
472 requires default_initializable<_Tp>
473 : std::optional<_Tp>{std::in_place}
476 __box(const __box&) = default;
477 __box(__box&&) = default;
479 using std::optional<_Tp>::operator=;
481 // _GLIBCXX_RESOLVE_LIB_DEFECTS
482 // 3477. Simplify constraints for semiregular-box
484 operator=(const __box& __that)
485 noexcept(is_nothrow_copy_constructible_v<_Tp>)
486 requires (!copyable<_Tp>)
488 if (this != std::__addressof(__that))
491 this->emplace(*__that);
499 operator=(__box&& __that)
500 noexcept(is_nothrow_move_constructible_v<_Tp>)
501 requires (!movable<_Tp>)
503 if (this != std::__addressof(__that))
506 this->emplace(std::move(*__that));
514 } // namespace __detail
516 /// A view that contains exactly one element.
517 template<copy_constructible _Tp> requires is_object_v<_Tp>
518 class single_view : public view_interface<single_view<_Tp>>
521 single_view() requires default_initializable<_Tp> = default;
524 single_view(const _Tp& __t)
529 single_view(_Tp&& __t)
530 : _M_value(std::move(__t))
533 // _GLIBCXX_RESOLVE_LIB_DEFECTS
534 // 3428. single_view's in place constructor should be explicit
535 template<typename... _Args>
536 requires constructible_from<_Tp, _Args...>
538 single_view(in_place_t, _Args&&... __args)
539 : _M_value{in_place, std::forward<_Args>(__args)...}
547 begin() const noexcept
552 { return data() + 1; }
556 { return data() + 1; }
558 static constexpr size_t
564 { return _M_value.operator->(); }
567 data() const noexcept
568 { return _M_value.operator->(); }
571 __detail::__box<_Tp> _M_value;
576 template<typename _Wp>
577 constexpr auto __to_signed_like(_Wp __w) noexcept
579 if constexpr (!integral<_Wp>)
580 return iter_difference_t<_Wp>();
581 else if constexpr (sizeof(iter_difference_t<_Wp>) > sizeof(_Wp))
582 return iter_difference_t<_Wp>(__w);
583 else if constexpr (sizeof(ptrdiff_t) > sizeof(_Wp))
584 return ptrdiff_t(__w);
585 else if constexpr (sizeof(long long) > sizeof(_Wp))
586 return (long long)(__w);
587 #ifdef __SIZEOF_INT128__
588 else if constexpr (__SIZEOF_INT128__ > sizeof(_Wp))
589 return __int128(__w);
592 return __max_diff_type(__w);
595 template<typename _Wp>
596 using __iota_diff_t = decltype(__to_signed_like(std::declval<_Wp>()));
598 template<typename _It>
599 concept __decrementable = incrementable<_It>
602 { --__i } -> same_as<_It&>;
603 { __i-- } -> same_as<_It>;
606 template<typename _It>
607 concept __advanceable = __decrementable<_It> && totally_ordered<_It>
608 && requires( _It __i, const _It __j, const __iota_diff_t<_It> __n)
610 { __i += __n } -> same_as<_It&>;
611 { __i -= __n } -> same_as<_It&>;
615 { __j - __j } -> convertible_to<__iota_diff_t<_It>>;
618 template<typename _Winc>
619 struct __iota_view_iter_cat
622 template<incrementable _Winc>
623 struct __iota_view_iter_cat<_Winc>
624 { using iterator_category = input_iterator_tag; };
625 } // namespace __detail
627 template<weakly_incrementable _Winc,
628 semiregular _Bound = unreachable_sentinel_t>
629 requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound>
631 class iota_view : public view_interface<iota_view<_Winc, _Bound>>
636 struct _Iterator : __detail::__iota_view_iter_cat<_Winc>
642 using namespace __detail;
643 if constexpr (__advanceable<_Winc>)
644 return random_access_iterator_tag{};
645 else if constexpr (__decrementable<_Winc>)
646 return bidirectional_iterator_tag{};
647 else if constexpr (incrementable<_Winc>)
648 return forward_iterator_tag{};
650 return input_iterator_tag{};
654 using iterator_concept = decltype(_S_iter_concept());
655 // iterator_category defined in __iota_view_iter_cat
656 using value_type = _Winc;
657 using difference_type = __detail::__iota_diff_t<_Winc>;
659 _Iterator() requires default_initializable<_Winc> = default;
662 _Iterator(_Winc __value)
663 : _M_value(__value) { }
666 operator*() const noexcept(is_nothrow_copy_constructible_v<_Winc>)
681 operator++(int) requires incrementable<_Winc>
689 operator--() requires __detail::__decrementable<_Winc>
696 operator--(int) requires __detail::__decrementable<_Winc>
704 operator+=(difference_type __n) requires __detail::__advanceable<_Winc>
706 using __detail::__is_integer_like;
707 using __detail::__is_signed_integer_like;
708 if constexpr (__is_integer_like<_Winc>
709 && !__is_signed_integer_like<_Winc>)
711 if (__n >= difference_type(0))
712 _M_value += static_cast<_Winc>(__n);
714 _M_value -= static_cast<_Winc>(-__n);
722 operator-=(difference_type __n) requires __detail::__advanceable<_Winc>
724 using __detail::__is_integer_like;
725 using __detail::__is_signed_integer_like;
726 if constexpr (__is_integer_like<_Winc>
727 && !__is_signed_integer_like<_Winc>)
729 if (__n >= difference_type(0))
730 _M_value -= static_cast<_Winc>(__n);
732 _M_value += static_cast<_Winc>(-__n);
740 operator[](difference_type __n) const
741 requires __detail::__advanceable<_Winc>
742 { return _Winc(_M_value + __n); }
744 friend constexpr bool
745 operator==(const _Iterator& __x, const _Iterator& __y)
746 requires equality_comparable<_Winc>
747 { return __x._M_value == __y._M_value; }
749 friend constexpr bool
750 operator<(const _Iterator& __x, const _Iterator& __y)
751 requires totally_ordered<_Winc>
752 { return __x._M_value < __y._M_value; }
754 friend constexpr bool
755 operator>(const _Iterator& __x, const _Iterator& __y)
756 requires totally_ordered<_Winc>
757 { return __y < __x; }
759 friend constexpr bool
760 operator<=(const _Iterator& __x, const _Iterator& __y)
761 requires totally_ordered<_Winc>
762 { return !(__y < __x); }
764 friend constexpr bool
765 operator>=(const _Iterator& __x, const _Iterator& __y)
766 requires totally_ordered<_Winc>
767 { return !(__x < __y); }
769 #ifdef __cpp_lib_three_way_comparison
770 friend constexpr auto
771 operator<=>(const _Iterator& __x, const _Iterator& __y)
772 requires totally_ordered<_Winc> && three_way_comparable<_Winc>
773 { return __x._M_value <=> __y._M_value; }
776 friend constexpr _Iterator
777 operator+(_Iterator __i, difference_type __n)
778 requires __detail::__advanceable<_Winc>
779 { return __i += __n; }
781 friend constexpr _Iterator
782 operator+(difference_type __n, _Iterator __i)
783 requires __detail::__advanceable<_Winc>
784 { return __i += __n; }
786 friend constexpr _Iterator
787 operator-(_Iterator __i, difference_type __n)
788 requires __detail::__advanceable<_Winc>
789 { return __i -= __n; }
791 friend constexpr difference_type
792 operator-(const _Iterator& __x, const _Iterator& __y)
793 requires __detail::__advanceable<_Winc>
795 using __detail::__is_integer_like;
796 using __detail::__is_signed_integer_like;
797 using _Dt = difference_type;
798 if constexpr (__is_integer_like<_Winc>)
800 if constexpr (__is_signed_integer_like<_Winc>)
801 return _Dt(_Dt(__x._M_value) - _Dt(__y._M_value));
803 return (__y._M_value > __x._M_value)
804 ? _Dt(-_Dt(__y._M_value - __x._M_value))
805 : _Dt(__x._M_value - __y._M_value);
808 return __x._M_value - __y._M_value;
812 _Winc _M_value = _Winc();
821 _M_equal(const _Iterator& __x) const
822 { return __x._M_value == _M_bound; }
825 _M_distance_from(const _Iterator& __x) const
826 { return _M_bound - __x._M_value; }
828 _Bound _M_bound = _Bound();
831 _Sentinel() = default;
834 _Sentinel(_Bound __bound)
835 : _M_bound(__bound) { }
837 friend constexpr bool
838 operator==(const _Iterator& __x, const _Sentinel& __y)
839 { return __y._M_equal(__x); }
841 friend constexpr iter_difference_t<_Winc>
842 operator-(const _Iterator& __x, const _Sentinel& __y)
843 requires sized_sentinel_for<_Bound, _Winc>
844 { return -__y._M_distance_from(__x); }
846 friend constexpr iter_difference_t<_Winc>
847 operator-(const _Sentinel& __x, const _Iterator& __y)
848 requires sized_sentinel_for<_Bound, _Winc>
849 { return __x._M_distance_from(__y); }
852 _Winc _M_value = _Winc();
853 _Bound _M_bound = _Bound();
856 iota_view() requires default_initializable<_Winc> = default;
859 iota_view(_Winc __value)
864 iota_view(type_identity_t<_Winc> __value,
865 type_identity_t<_Bound> __bound)
866 : _M_value(__value), _M_bound(__bound)
868 if constexpr (totally_ordered_with<_Winc, _Bound>)
870 __glibcxx_assert( bool(__value <= __bound) );
875 begin() const { return _Iterator{_M_value}; }
880 if constexpr (same_as<_Bound, unreachable_sentinel_t>)
881 return unreachable_sentinel;
883 return _Sentinel{_M_bound};
887 end() const requires same_as<_Winc, _Bound>
888 { return _Iterator{_M_bound}; }
892 requires (same_as<_Winc, _Bound> && __detail::__advanceable<_Winc>)
893 || (integral<_Winc> && integral<_Bound>)
894 || sized_sentinel_for<_Bound, _Winc>
896 using __detail::__is_integer_like;
897 using __detail::__to_unsigned_like;
898 if constexpr (integral<_Winc> && integral<_Bound>)
900 using _Up = make_unsigned_t<decltype(_M_bound - _M_value)>;
901 return _Up(_M_bound) - _Up(_M_value);
903 else if constexpr (__is_integer_like<_Winc>)
904 return __to_unsigned_like(_M_bound) - __to_unsigned_like(_M_value);
906 return __to_unsigned_like(_M_bound - _M_value);
910 template<typename _Winc, typename _Bound>
911 requires (!__detail::__is_integer_like<_Winc>
912 || !__detail::__is_integer_like<_Bound>
913 || (__detail::__is_signed_integer_like<_Winc>
914 == __detail::__is_signed_integer_like<_Bound>))
915 iota_view(_Winc, _Bound) -> iota_view<_Winc, _Bound>;
917 template<weakly_incrementable _Winc, semiregular _Bound>
918 inline constexpr bool
919 enable_borrowed_range<iota_view<_Winc, _Bound>> = true;
923 template<typename _Tp>
924 inline constexpr empty_view<_Tp> empty{};
928 template<typename _Tp>
930 operator()(_Tp&& __e) const
931 { return single_view{std::forward<_Tp>(__e)}; }
934 inline constexpr _Single single{};
938 template<typename _Tp>
940 operator()(_Tp&& __e) const
941 { return iota_view{std::forward<_Tp>(__e)}; }
943 template<typename _Tp, typename _Up>
945 operator()(_Tp&& __e, _Up&& __f) const
946 { return iota_view{std::forward<_Tp>(__e), std::forward<_Up>(__f)}; }
949 inline constexpr _Iota iota{};
954 template<typename _Val, typename _CharT, typename _Traits>
955 concept __stream_extractable
956 = requires(basic_istream<_CharT, _Traits>& is, _Val& t) { is >> t; };
957 } // namespace __detail
959 template<movable _Val, typename _CharT,
960 typename _Traits = char_traits<_CharT>>
961 requires default_initializable<_Val>
962 && __detail::__stream_extractable<_Val, _CharT, _Traits>
963 class basic_istream_view
964 : public view_interface<basic_istream_view<_Val, _CharT, _Traits>>
967 basic_istream_view() = default;
970 basic_istream_view(basic_istream<_CharT, _Traits>& __stream)
971 : _M_stream(std::__addressof(__stream))
977 if (_M_stream != nullptr)
978 *_M_stream >> _M_object;
979 return _Iterator{this};
982 constexpr default_sentinel_t
984 { return default_sentinel; }
987 basic_istream<_CharT, _Traits>* _M_stream = nullptr;
988 _Val _M_object = _Val();
993 using iterator_concept = input_iterator_tag;
994 using difference_type = ptrdiff_t;
995 using value_type = _Val;
997 _Iterator() = default;
1000 _Iterator(basic_istream_view* __parent) noexcept
1001 : _M_parent(__parent)
1004 _Iterator(const _Iterator&) = delete;
1005 _Iterator(_Iterator&&) = default;
1006 _Iterator& operator=(const _Iterator&) = delete;
1007 _Iterator& operator=(_Iterator&&) = default;
1012 __glibcxx_assert(_M_parent->_M_stream != nullptr);
1013 *_M_parent->_M_stream >> _M_parent->_M_object;
1024 __glibcxx_assert(_M_parent->_M_stream != nullptr);
1025 return _M_parent->_M_object;
1029 operator==(const _Iterator& __x, default_sentinel_t)
1030 { return __x._M_at_end(); }
1033 basic_istream_view* _M_parent = nullptr;
1037 { return _M_parent == nullptr || !*_M_parent->_M_stream; }
1043 template<typename _Val, typename _CharT, typename _Traits>
1044 basic_istream_view<_Val, _CharT, _Traits>
1045 istream_view(basic_istream<_CharT, _Traits>& __s)
1046 { return basic_istream_view<_Val, _CharT, _Traits>{__s}; }
1052 // Alias for a type that is conditionally present
1053 // (and is an empty type otherwise).
1054 // Data members using this alias should use [[no_unique_address]] so that
1055 // they take no space when not needed.
1056 template<bool _Present, typename _Tp>
1057 using __maybe_present_t = conditional_t<_Present, _Tp, _Empty>;
1059 // Alias for a type that is conditionally const.
1060 template<bool _Const, typename _Tp>
1061 using __maybe_const_t = conditional_t<_Const, const _Tp, _Tp>;
1063 } // namespace __detail
1069 template<typename _Tp>
1070 inline constexpr auto
1071 __maybe_refwrap(_Tp& __arg)
1072 { return reference_wrapper<_Tp>{__arg}; }
1074 template<typename _Tp>
1075 inline constexpr auto
1076 __maybe_refwrap(const _Tp& __arg)
1077 { return reference_wrapper<const _Tp>{__arg}; }
1079 template<typename _Tp>
1080 inline constexpr decltype(auto)
1081 __maybe_refwrap(_Tp&& __arg)
1082 { return std::forward<_Tp>(__arg); }
1084 template<typename _Callable>
1085 struct _RangeAdaptorClosure;
1087 template<typename _Callable>
1088 struct _RangeAdaptor
1091 [[no_unique_address]]
1092 __detail::__maybe_present_t<!is_default_constructible_v<_Callable>,
1093 _Callable> _M_callable;
1097 _RangeAdaptor(const _Callable& = {})
1098 requires is_default_constructible_v<_Callable>
1102 _RangeAdaptor(_Callable __callable)
1103 requires (!is_default_constructible_v<_Callable>)
1104 : _M_callable(std::move(__callable))
1107 template<typename... _Args>
1108 requires (sizeof...(_Args) >= 1)
1110 operator()(_Args&&... __args) const
1112 // [range.adaptor.object]: If a range adaptor object accepts more
1113 // than one argument, then the following expressions are equivalent:
1115 // (1) adaptor(range, args...)
1116 // (2) adaptor(args...)(range)
1117 // (3) range | adaptor(args...)
1119 // In this case, adaptor(args...) is a range adaptor closure object.
1121 // We handle (1) and (2) here, and (3) is just a special case of a
1122 // more general case already handled by _RangeAdaptorClosure.
1123 if constexpr (is_invocable_v<_Callable, _Args...>)
1125 static_assert(sizeof...(_Args) != 1,
1126 "a _RangeAdaptor that accepts only one argument "
1127 "should be defined as a _RangeAdaptorClosure");
1128 // Here we handle adaptor(range, args...) -- just forward all
1129 // arguments to the underlying adaptor routine.
1130 return _Callable{}(std::forward<_Args>(__args)...);
1134 // Here we handle adaptor(args...)(range).
1135 // Given args..., we return a _RangeAdaptorClosure that takes a
1136 // range argument, such that (2) is equivalent to (1).
1138 // We need to be careful about how we capture args... in this
1139 // closure. By using __maybe_refwrap, we capture lvalue
1140 // references by reference (through a reference_wrapper) and
1141 // otherwise capture by value.
1143 = [...__args(__maybe_refwrap(std::forward<_Args>(__args)))]
1144 <typename _Range> (_Range&& __r) {
1145 // This static_cast has two purposes: it forwards a
1146 // reference_wrapper<T> capture as a T&, and otherwise
1147 // forwards the captured argument as an rvalue.
1148 return _Callable{}(std::forward<_Range>(__r),
1149 (static_cast<unwrap_reference_t
1150 <remove_const_t<decltype(__args)>>>
1153 using _ClosureType = decltype(__closure);
1154 return _RangeAdaptorClosure<_ClosureType>(std::move(__closure));
1159 template<typename _Callable>
1160 _RangeAdaptor(_Callable) -> _RangeAdaptor<_Callable>;
1162 template<typename _Callable>
1163 struct _RangeAdaptorClosure : public _RangeAdaptor<_Callable>
1165 using _RangeAdaptor<_Callable>::_RangeAdaptor;
1167 template<viewable_range _Range>
1168 requires requires { declval<_Callable>()(declval<_Range>()); }
1170 operator()(_Range&& __r) const
1172 if constexpr (is_default_constructible_v<_Callable>)
1173 return _Callable{}(std::forward<_Range>(__r));
1175 return this->_M_callable(std::forward<_Range>(__r));
1178 template<viewable_range _Range>
1179 requires requires { declval<_Callable>()(declval<_Range>()); }
1180 friend constexpr auto
1181 operator|(_Range&& __r, const _RangeAdaptorClosure& __o)
1182 { return __o(std::forward<_Range>(__r)); }
1184 template<typename _Tp>
1185 friend constexpr auto
1186 operator|(const _RangeAdaptorClosure<_Tp>& __x,
1187 const _RangeAdaptorClosure& __y)
1189 if constexpr (is_default_constructible_v<_Tp>
1190 && is_default_constructible_v<_Callable>)
1192 auto __closure = [] <typename _Up> (_Up&& __e) {
1193 return std::forward<_Up>(__e) | decltype(__x){} | decltype(__y){};
1195 return _RangeAdaptorClosure<decltype(__closure)>(__closure);
1197 else if constexpr (is_default_constructible_v<_Tp>
1198 && !is_default_constructible_v<_Callable>)
1200 auto __closure = [__y] <typename _Up> (_Up&& __e) {
1201 return std::forward<_Up>(__e) | decltype(__x){} | __y;
1203 return _RangeAdaptorClosure<decltype(__closure)>(__closure);
1205 else if constexpr (!is_default_constructible_v<_Tp>
1206 && is_default_constructible_v<_Callable>)
1208 auto __closure = [__x] <typename _Up> (_Up&& __e) {
1209 return std::forward<_Up>(__e) | __x | decltype(__y){};
1211 return _RangeAdaptorClosure<decltype(__closure)>(__closure);
1215 auto __closure = [__x, __y] <typename _Up> (_Up&& __e) {
1216 return std::forward<_Up>(__e) | __x | __y;
1218 return _RangeAdaptorClosure<decltype(__closure)>(__closure);
1223 template<typename _Callable>
1224 _RangeAdaptorClosure(_Callable) -> _RangeAdaptorClosure<_Callable>;
1225 } // namespace __adaptor
1226 } // namespace views
1228 template<range _Range> requires is_object_v<_Range>
1229 class ref_view : public view_interface<ref_view<_Range>>
1232 _Range* _M_r = nullptr;
1234 static void _S_fun(_Range&); // not defined
1235 static void _S_fun(_Range&&) = delete;
1239 ref_view() noexcept = default;
1241 template<__detail::__not_same_as<ref_view> _Tp>
1242 requires convertible_to<_Tp, _Range&>
1243 && requires { _S_fun(declval<_Tp>()); }
1246 : _M_r(std::__addressof(static_cast<_Range&>(std::forward<_Tp>(__t))))
1253 constexpr iterator_t<_Range>
1255 { return ranges::begin(*_M_r); }
1257 constexpr sentinel_t<_Range>
1259 { return ranges::end(*_M_r); }
1262 empty() const requires requires { ranges::empty(*_M_r); }
1263 { return ranges::empty(*_M_r); }
1266 size() const requires sized_range<_Range>
1267 { return ranges::size(*_M_r); }
1270 data() const requires contiguous_range<_Range>
1271 { return ranges::data(*_M_r); }
1274 template<typename _Range>
1275 ref_view(_Range&) -> ref_view<_Range>;
1277 template<typename _Tp>
1278 inline constexpr bool enable_borrowed_range<ref_view<_Tp>> = true;
1282 inline constexpr __adaptor::_RangeAdaptorClosure all
1283 = [] <viewable_range _Range> (_Range&& __r)
1285 if constexpr (view<decay_t<_Range>>)
1286 return std::forward<_Range>(__r);
1287 else if constexpr (requires { ref_view{std::forward<_Range>(__r)}; })
1288 return ref_view{std::forward<_Range>(__r)};
1290 return subrange{std::forward<_Range>(__r)};
1293 template<viewable_range _Range>
1294 using all_t = decltype(all(std::declval<_Range>()));
1296 } // namespace views
1298 // The following simple algos are transcribed from ranges_algo.h to avoid
1299 // having to include that entire header.
1302 template<typename _Iter, typename _Sent, typename _Tp>
1304 find(_Iter __first, _Sent __last, const _Tp& __value)
1306 while (__first != __last
1307 && !(bool)(*__first == __value))
1312 template<typename _Iter, typename _Sent, typename _Pred>
1314 find_if(_Iter __first, _Sent __last, _Pred __pred)
1316 while (__first != __last
1317 && !(bool)std::__invoke(__pred, *__first))
1322 template<typename _Iter, typename _Sent, typename _Pred>
1324 find_if_not(_Iter __first, _Sent __last, _Pred __pred)
1326 while (__first != __last
1327 && (bool)std::__invoke(__pred, *__first))
1332 template<typename _Iter1, typename _Sent1, typename _Iter2, typename _Sent2>
1333 constexpr pair<_Iter1, _Iter2>
1334 mismatch(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2)
1336 while (__first1 != __last1 && __first2 != __last2
1337 && (bool)ranges::equal_to{}(*__first1, *__first2))
1342 return { std::move(__first1), std::move(__first2) };
1344 } // namespace __detail
1348 template<range _Range>
1349 struct _CachedPosition
1352 _M_has_value() const
1355 constexpr iterator_t<_Range>
1356 _M_get(const _Range&) const
1358 __glibcxx_assert(false);
1359 __builtin_unreachable();
1363 _M_set(const _Range&, const iterator_t<_Range>&) const
1367 template<forward_range _Range>
1368 struct _CachedPosition<_Range>
1371 iterator_t<_Range> _M_iter{};
1375 _M_has_value() const
1376 { return _M_iter != iterator_t<_Range>{}; }
1378 constexpr iterator_t<_Range>
1379 _M_get(const _Range&) const
1381 __glibcxx_assert(_M_has_value());
1386 _M_set(const _Range&, const iterator_t<_Range>& __it)
1388 __glibcxx_assert(!_M_has_value());
1393 template<random_access_range _Range>
1394 requires (sizeof(range_difference_t<_Range>)
1395 <= sizeof(iterator_t<_Range>))
1396 struct _CachedPosition<_Range>
1399 range_difference_t<_Range> _M_offset = -1;
1403 _M_has_value() const
1404 { return _M_offset >= 0; }
1406 constexpr iterator_t<_Range>
1407 _M_get(_Range& __r) const
1409 __glibcxx_assert(_M_has_value());
1410 return ranges::begin(__r) + _M_offset;
1414 _M_set(_Range& __r, const iterator_t<_Range>& __it)
1416 __glibcxx_assert(!_M_has_value());
1417 _M_offset = __it - ranges::begin(__r);
1420 } // namespace __detail
1424 template<typename _Base>
1425 struct __filter_view_iter_cat
1428 template<forward_range _Base>
1429 struct __filter_view_iter_cat<_Base>
1435 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
1436 if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
1437 return bidirectional_iterator_tag{};
1438 else if constexpr (derived_from<_Cat, forward_iterator_tag>)
1439 return forward_iterator_tag{};
1444 using iterator_category = decltype(_S_iter_cat());
1446 } // namespace __detail
1448 template<input_range _Vp,
1449 indirect_unary_predicate<iterator_t<_Vp>> _Pred>
1450 requires view<_Vp> && is_object_v<_Pred>
1451 class filter_view : public view_interface<filter_view<_Vp, _Pred>>
1456 struct _Iterator : __detail::__filter_view_iter_cat<_Vp>
1459 static constexpr auto
1462 if constexpr (bidirectional_range<_Vp>)
1463 return bidirectional_iterator_tag{};
1464 else if constexpr (forward_range<_Vp>)
1465 return forward_iterator_tag{};
1467 return input_iterator_tag{};
1472 using _Vp_iter = iterator_t<_Vp>;
1474 _Vp_iter _M_current = _Vp_iter();
1475 filter_view* _M_parent = nullptr;
1478 using iterator_concept = decltype(_S_iter_concept());
1479 // iterator_category defined in __filter_view_iter_cat
1480 using value_type = range_value_t<_Vp>;
1481 using difference_type = range_difference_t<_Vp>;
1483 _Iterator() requires default_initializable<_Vp_iter> = default;
1486 _Iterator(filter_view* __parent, _Vp_iter __current)
1487 : _M_current(std::move(__current)),
1491 constexpr const _Vp_iter&
1492 base() const & noexcept
1493 { return _M_current; }
1497 { return std::move(_M_current); }
1499 constexpr range_reference_t<_Vp>
1501 { return *_M_current; }
1505 requires __detail::__has_arrow<_Vp_iter>
1506 && copyable<_Vp_iter>
1507 { return _M_current; }
1509 constexpr _Iterator&
1512 _M_current = __detail::find_if(std::move(++_M_current),
1513 ranges::end(_M_parent->_M_base),
1514 std::ref(*_M_parent->_M_pred));
1523 operator++(int) requires forward_range<_Vp>
1530 constexpr _Iterator&
1531 operator--() requires bidirectional_range<_Vp>
1535 while (!std::__invoke(*_M_parent->_M_pred, *_M_current));
1540 operator--(int) requires bidirectional_range<_Vp>
1547 friend constexpr bool
1548 operator==(const _Iterator& __x, const _Iterator& __y)
1549 requires equality_comparable<_Vp_iter>
1550 { return __x._M_current == __y._M_current; }
1552 friend constexpr range_rvalue_reference_t<_Vp>
1553 iter_move(const _Iterator& __i)
1554 noexcept(noexcept(ranges::iter_move(__i._M_current)))
1555 { return ranges::iter_move(__i._M_current); }
1557 friend constexpr void
1558 iter_swap(const _Iterator& __x, const _Iterator& __y)
1559 noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
1560 requires indirectly_swappable<_Vp_iter>
1561 { ranges::iter_swap(__x._M_current, __y._M_current); }
1567 sentinel_t<_Vp> _M_end = sentinel_t<_Vp>();
1570 __equal(const _Iterator& __i) const
1571 { return __i._M_current == _M_end; }
1574 _Sentinel() = default;
1577 _Sentinel(filter_view* __parent)
1578 : _M_end(ranges::end(__parent->_M_base))
1581 constexpr sentinel_t<_Vp>
1585 friend constexpr bool
1586 operator==(const _Iterator& __x, const _Sentinel& __y)
1587 { return __y.__equal(__x); }
1590 _Vp _M_base = _Vp();
1591 __detail::__box<_Pred> _M_pred;
1592 [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
1595 filter_view() requires (default_initializable<_Vp>
1596 && default_initializable<_Pred>)
1600 filter_view(_Vp __base, _Pred __pred)
1601 : _M_base(std::move(__base)), _M_pred(std::move(__pred))
1605 base() const& requires copy_constructible<_Vp>
1610 { return std::move(_M_base); }
1612 constexpr const _Pred&
1614 { return *_M_pred; }
1619 if (_M_cached_begin._M_has_value())
1620 return {this, _M_cached_begin._M_get(_M_base)};
1622 __glibcxx_assert(_M_pred.has_value());
1623 auto __it = __detail::find_if(ranges::begin(_M_base),
1624 ranges::end(_M_base),
1625 std::ref(*_M_pred));
1626 _M_cached_begin._M_set(_M_base, __it);
1627 return {this, std::move(__it)};
1633 if constexpr (common_range<_Vp>)
1634 return _Iterator{this, ranges::end(_M_base)};
1636 return _Sentinel{this};
1640 template<typename _Range, typename _Pred>
1641 filter_view(_Range&&, _Pred) -> filter_view<views::all_t<_Range>, _Pred>;
1645 inline constexpr __adaptor::_RangeAdaptor filter
1646 = [] <viewable_range _Range, typename _Pred> (_Range&& __r, _Pred&& __p)
1648 return filter_view{std::forward<_Range>(__r), std::forward<_Pred>(__p)};
1650 } // namespace views
1652 template<input_range _Vp, copy_constructible _Fp>
1653 requires view<_Vp> && is_object_v<_Fp>
1654 && regular_invocable<_Fp&, range_reference_t<_Vp>>
1655 && std::__detail::__can_reference<invoke_result_t<_Fp&,
1656 range_reference_t<_Vp>>>
1657 class transform_view : public view_interface<transform_view<_Vp, _Fp>>
1660 template<bool _Const>
1661 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
1663 template<bool _Const>
1667 template<bool _Const>
1668 requires forward_range<_Base<_Const>>
1669 struct __iter_cat<_Const>
1675 using _Base = transform_view::_Base<_Const>;
1676 using _Res = invoke_result_t<_Fp&, range_reference_t<_Base>>;
1677 if constexpr (is_lvalue_reference_v<_Res>)
1680 = typename iterator_traits<iterator_t<_Base>>::iterator_category;
1681 if constexpr (derived_from<_Cat, contiguous_iterator_tag>)
1682 return random_access_iterator_tag{};
1687 return input_iterator_tag{};
1690 using iterator_category = decltype(_S_iter_cat());
1693 template<bool _Const>
1696 template<bool _Const>
1697 struct _Iterator : __iter_cat<_Const>
1700 using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
1701 using _Base = transform_view::_Base<_Const>;
1706 if constexpr (random_access_range<_Base>)
1707 return random_access_iterator_tag{};
1708 else if constexpr (bidirectional_range<_Base>)
1709 return bidirectional_iterator_tag{};
1710 else if constexpr (forward_range<_Base>)
1711 return forward_iterator_tag{};
1713 return input_iterator_tag{};
1716 using _Base_iter = iterator_t<_Base>;
1718 _Base_iter _M_current = _Base_iter();
1719 _Parent* _M_parent = nullptr;
1722 using iterator_concept = decltype(_S_iter_concept());
1723 // iterator_category defined in __transform_view_iter_cat
1725 = remove_cvref_t<invoke_result_t<_Fp&, range_reference_t<_Base>>>;
1726 using difference_type = range_difference_t<_Base>;
1728 _Iterator() requires default_initializable<_Base_iter> = default;
1731 _Iterator(_Parent* __parent, _Base_iter __current)
1732 : _M_current(std::move(__current)),
1737 _Iterator(_Iterator<!_Const> __i)
1739 && convertible_to<iterator_t<_Vp>, _Base_iter>
1740 : _M_current(std::move(__i._M_current)), _M_parent(__i._M_parent)
1743 constexpr const _Base_iter&
1744 base() const & noexcept
1745 { return _M_current; }
1747 constexpr _Base_iter
1749 { return std::move(_M_current); }
1751 constexpr decltype(auto)
1753 noexcept(noexcept(std::__invoke(*_M_parent->_M_fun, *_M_current)))
1754 { return std::__invoke(*_M_parent->_M_fun, *_M_current); }
1756 constexpr _Iterator&
1768 operator++(int) requires forward_range<_Base>
1775 constexpr _Iterator&
1776 operator--() requires bidirectional_range<_Base>
1783 operator--(int) requires bidirectional_range<_Base>
1790 constexpr _Iterator&
1791 operator+=(difference_type __n) requires random_access_range<_Base>
1797 constexpr _Iterator&
1798 operator-=(difference_type __n) requires random_access_range<_Base>
1804 constexpr decltype(auto)
1805 operator[](difference_type __n) const
1806 requires random_access_range<_Base>
1807 { return std::__invoke(*_M_parent->_M_fun, _M_current[__n]); }
1809 friend constexpr bool
1810 operator==(const _Iterator& __x, const _Iterator& __y)
1811 requires equality_comparable<_Base_iter>
1812 { return __x._M_current == __y._M_current; }
1814 friend constexpr bool
1815 operator<(const _Iterator& __x, const _Iterator& __y)
1816 requires random_access_range<_Base>
1817 { return __x._M_current < __y._M_current; }
1819 friend constexpr bool
1820 operator>(const _Iterator& __x, const _Iterator& __y)
1821 requires random_access_range<_Base>
1822 { return __y < __x; }
1824 friend constexpr bool
1825 operator<=(const _Iterator& __x, const _Iterator& __y)
1826 requires random_access_range<_Base>
1827 { return !(__y < __x); }
1829 friend constexpr bool
1830 operator>=(const _Iterator& __x, const _Iterator& __y)
1831 requires random_access_range<_Base>
1832 { return !(__x < __y); }
1834 #ifdef __cpp_lib_three_way_comparison
1835 friend constexpr auto
1836 operator<=>(const _Iterator& __x, const _Iterator& __y)
1837 requires random_access_range<_Base>
1838 && three_way_comparable<_Base_iter>
1839 { return __x._M_current <=> __y._M_current; }
1842 friend constexpr _Iterator
1843 operator+(_Iterator __i, difference_type __n)
1844 requires random_access_range<_Base>
1845 { return {__i._M_parent, __i._M_current + __n}; }
1847 friend constexpr _Iterator
1848 operator+(difference_type __n, _Iterator __i)
1849 requires random_access_range<_Base>
1850 { return {__i._M_parent, __i._M_current + __n}; }
1852 friend constexpr _Iterator
1853 operator-(_Iterator __i, difference_type __n)
1854 requires random_access_range<_Base>
1855 { return {__i._M_parent, __i._M_current - __n}; }
1857 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1858 // 3483. transform_view::iterator's difference is overconstrained
1859 friend constexpr difference_type
1860 operator-(const _Iterator& __x, const _Iterator& __y)
1861 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
1862 { return __x._M_current - __y._M_current; }
1864 friend constexpr decltype(auto)
1865 iter_move(const _Iterator& __i) noexcept(noexcept(*__i))
1867 if constexpr (is_lvalue_reference_v<decltype(*__i)>)
1868 return std::move(*__i);
1873 friend _Iterator<!_Const>;
1874 template<bool> friend struct _Sentinel;
1877 template<bool _Const>
1881 using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
1882 using _Base = transform_view::_Base<_Const>;
1884 template<bool _Const2>
1886 __distance_from(const _Iterator<_Const2>& __i) const
1887 { return _M_end - __i._M_current; }
1889 template<bool _Const2>
1891 __equal(const _Iterator<_Const2>& __i) const
1892 { return __i._M_current == _M_end; }
1894 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
1897 _Sentinel() = default;
1900 _Sentinel(sentinel_t<_Base> __end)
1905 _Sentinel(_Sentinel<!_Const> __i)
1907 && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
1908 : _M_end(std::move(__i._M_end))
1911 constexpr sentinel_t<_Base>
1915 template<bool _Const2>
1916 requires sentinel_for<sentinel_t<_Base>,
1917 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
1918 friend constexpr bool
1919 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
1920 { return __y.__equal(__x); }
1922 template<bool _Const2,
1923 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
1924 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
1925 friend constexpr range_difference_t<_Base2>
1926 operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
1927 { return -__y.__distance_from(__x); }
1929 template<bool _Const2,
1930 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
1931 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
1932 friend constexpr range_difference_t<_Base2>
1933 operator-(const _Sentinel& __y, const _Iterator<_Const2>& __x)
1934 { return __y.__distance_from(__x); }
1936 friend _Sentinel<!_Const>;
1939 _Vp _M_base = _Vp();
1940 __detail::__box<_Fp> _M_fun;
1943 transform_view() requires (default_initializable<_Vp>
1944 && default_initializable<_Fp>)
1948 transform_view(_Vp __base, _Fp __fun)
1949 : _M_base(std::move(__base)), _M_fun(std::move(__fun))
1953 base() const& requires copy_constructible<_Vp>
1954 { return _M_base ; }
1958 { return std::move(_M_base); }
1960 constexpr _Iterator<false>
1962 { return _Iterator<false>{this, ranges::begin(_M_base)}; }
1964 constexpr _Iterator<true>
1966 requires range<const _Vp>
1967 && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
1968 { return _Iterator<true>{this, ranges::begin(_M_base)}; }
1970 constexpr _Sentinel<false>
1972 { return _Sentinel<false>{ranges::end(_M_base)}; }
1974 constexpr _Iterator<false>
1975 end() requires common_range<_Vp>
1976 { return _Iterator<false>{this, ranges::end(_M_base)}; }
1978 constexpr _Sentinel<true>
1980 requires range<const _Vp>
1981 && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
1982 { return _Sentinel<true>{ranges::end(_M_base)}; }
1984 constexpr _Iterator<true>
1986 requires common_range<const _Vp>
1987 && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
1988 { return _Iterator<true>{this, ranges::end(_M_base)}; }
1991 size() requires sized_range<_Vp>
1992 { return ranges::size(_M_base); }
1995 size() const requires sized_range<const _Vp>
1996 { return ranges::size(_M_base); }
1999 template<typename _Range, typename _Fp>
2000 transform_view(_Range&&, _Fp) -> transform_view<views::all_t<_Range>, _Fp>;
2004 inline constexpr __adaptor::_RangeAdaptor transform
2005 = [] <viewable_range _Range, typename _Fp> (_Range&& __r, _Fp&& __f)
2007 return transform_view{std::forward<_Range>(__r), std::forward<_Fp>(__f)};
2009 } // namespace views
2012 class take_view : public view_interface<take_view<_Vp>>
2015 template<bool _Const>
2016 using _CI = counted_iterator<
2017 iterator_t<__detail::__maybe_const_t<_Const, _Vp>>>;
2019 template<bool _Const>
2023 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2024 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
2027 _Sentinel() = default;
2030 _Sentinel(sentinel_t<_Base> __end)
2035 _Sentinel(_Sentinel<!_Const> __s)
2036 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
2037 : _M_end(std::move(__s._M_end))
2040 constexpr sentinel_t<_Base>
2044 friend constexpr bool
2045 operator==(const _CI<_Const>& __y, const _Sentinel& __x)
2046 { return __y.count() == 0 || __y.base() == __x._M_end; }
2048 template<bool _OtherConst = !_Const,
2049 typename _Base2 = __detail::__maybe_const_t<_OtherConst, _Vp>>
2050 requires sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
2051 friend constexpr bool
2052 operator==(const _CI<_OtherConst>& __y, const _Sentinel& __x)
2053 { return __y.count() == 0 || __y.base() == __x._M_end; }
2055 friend _Sentinel<!_Const>;
2058 _Vp _M_base = _Vp();
2059 range_difference_t<_Vp> _M_count = 0;
2062 take_view() requires default_initializable<_Vp> = default;
2065 take_view(_Vp base, range_difference_t<_Vp> __count)
2066 : _M_base(std::move(base)), _M_count(std::move(__count))
2070 base() const& requires copy_constructible<_Vp>
2075 { return std::move(_M_base); }
2078 begin() requires (!__detail::__simple_view<_Vp>)
2080 if constexpr (sized_range<_Vp>)
2082 if constexpr (random_access_range<_Vp>)
2083 return ranges::begin(_M_base);
2087 return counted_iterator{ranges::begin(_M_base), __sz};
2091 return counted_iterator{ranges::begin(_M_base), _M_count};
2095 begin() const requires range<const _Vp>
2097 if constexpr (sized_range<const _Vp>)
2099 if constexpr (random_access_range<const _Vp>)
2100 return ranges::begin(_M_base);
2104 return counted_iterator{ranges::begin(_M_base), __sz};
2108 return counted_iterator{ranges::begin(_M_base), _M_count};
2112 end() requires (!__detail::__simple_view<_Vp>)
2114 if constexpr (sized_range<_Vp>)
2116 if constexpr (random_access_range<_Vp>)
2117 return ranges::begin(_M_base) + size();
2119 return default_sentinel;
2122 return _Sentinel<false>{ranges::end(_M_base)};
2126 end() const requires range<const _Vp>
2128 if constexpr (sized_range<const _Vp>)
2130 if constexpr (random_access_range<const _Vp>)
2131 return ranges::begin(_M_base) + size();
2133 return default_sentinel;
2136 return _Sentinel<true>{ranges::end(_M_base)};
2140 size() requires sized_range<_Vp>
2142 auto __n = ranges::size(_M_base);
2143 return std::min(__n, static_cast<decltype(__n)>(_M_count));
2147 size() const requires sized_range<const _Vp>
2149 auto __n = ranges::size(_M_base);
2150 return std::min(__n, static_cast<decltype(__n)>(_M_count));
2154 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2155 // 3447. Deduction guides for take_view and drop_view have different
2157 template<typename _Range>
2158 take_view(_Range&&, range_difference_t<_Range>)
2159 -> take_view<views::all_t<_Range>>;
2161 template<typename _Tp>
2162 inline constexpr bool enable_borrowed_range<take_view<_Tp>>
2163 = enable_borrowed_range<_Tp>;
2167 inline constexpr __adaptor::_RangeAdaptor take
2168 = [] <viewable_range _Range, typename _Tp> (_Range&& __r, _Tp&& __n)
2170 return take_view{std::forward<_Range>(__r), std::forward<_Tp>(__n)};
2172 } // namespace views
2174 template<view _Vp, typename _Pred>
2175 requires input_range<_Vp> && is_object_v<_Pred>
2176 && indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
2177 class take_while_view : public view_interface<take_while_view<_Vp, _Pred>>
2179 template<bool _Const>
2183 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2185 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
2186 const _Pred* _M_pred = nullptr;
2189 _Sentinel() = default;
2192 _Sentinel(sentinel_t<_Base> __end, const _Pred* __pred)
2193 : _M_end(__end), _M_pred(__pred)
2197 _Sentinel(_Sentinel<!_Const> __s)
2198 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
2199 : _M_end(__s._M_end), _M_pred(__s._M_pred)
2202 constexpr sentinel_t<_Base>
2203 base() const { return _M_end; }
2205 friend constexpr bool
2206 operator==(const iterator_t<_Base>& __x, const _Sentinel& __y)
2207 { return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); }
2209 template<bool _OtherConst = !_Const,
2210 typename _Base2 = __detail::__maybe_const_t<_OtherConst, _Vp>>
2211 requires sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
2212 friend constexpr bool
2213 operator==(const iterator_t<_Base2>& __x, const _Sentinel& __y)
2214 { return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); }
2216 friend _Sentinel<!_Const>;
2219 _Vp _M_base = _Vp();
2220 __detail::__box<_Pred> _M_pred;
2223 take_while_view() requires (default_initializable<_Vp>
2224 && default_initializable<_Pred>)
2228 take_while_view(_Vp base, _Pred __pred)
2229 : _M_base(std::move(base)), _M_pred(std::move(__pred))
2234 base() const& requires copy_constructible<_Vp>
2239 { return std::move(_M_base); }
2241 constexpr const _Pred&
2243 { return *_M_pred; }
2246 begin() requires (!__detail::__simple_view<_Vp>)
2247 { return ranges::begin(_M_base); }
2250 begin() const requires range<const _Vp>
2251 && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>
2252 { return ranges::begin(_M_base); }
2255 end() requires (!__detail::__simple_view<_Vp>)
2256 { return _Sentinel<false>(ranges::end(_M_base),
2257 std::__addressof(*_M_pred)); }
2260 end() const requires range<const _Vp>
2261 && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>
2262 { return _Sentinel<true>(ranges::end(_M_base),
2263 std::__addressof(*_M_pred)); }
2266 template<typename _Range, typename _Pred>
2267 take_while_view(_Range&&, _Pred)
2268 -> take_while_view<views::all_t<_Range>, _Pred>;
2272 inline constexpr __adaptor::_RangeAdaptor take_while
2273 = [] <viewable_range _Range, typename _Pred> (_Range&& __r, _Pred&& __p)
2275 return take_while_view{std::forward<_Range>(__r), std::forward<_Pred>(__p)};
2277 } // namespace views
2280 class drop_view : public view_interface<drop_view<_Vp>>
2283 _Vp _M_base = _Vp();
2284 range_difference_t<_Vp> _M_count = 0;
2286 // ranges::next(begin(base), count, end(base)) is O(1) if _Vp satisfies
2287 // both random_access_range and sized_range. Otherwise, cache its result.
2288 static constexpr bool _S_needs_cached_begin
2289 = !(random_access_range<const _Vp> && sized_range<const _Vp>);
2290 [[no_unique_address]]
2291 __detail::__maybe_present_t<_S_needs_cached_begin,
2292 __detail::_CachedPosition<_Vp>>
2296 drop_view() requires default_initializable<_Vp> = default;
2299 drop_view(_Vp __base, range_difference_t<_Vp> __count)
2300 : _M_base(std::move(__base)), _M_count(__count)
2301 { __glibcxx_assert(__count >= 0); }
2304 base() const& requires copy_constructible<_Vp>
2309 { return std::move(_M_base); }
2311 // This overload is disabled for simple views with constant-time begin().
2314 requires (!(__detail::__simple_view<_Vp>
2315 && random_access_range<const _Vp>
2316 && sized_range<const _Vp>))
2318 if constexpr (_S_needs_cached_begin)
2319 if (_M_cached_begin._M_has_value())
2320 return _M_cached_begin._M_get(_M_base);
2322 auto __it = ranges::next(ranges::begin(_M_base),
2323 _M_count, ranges::end(_M_base));
2324 if constexpr (_S_needs_cached_begin)
2325 _M_cached_begin._M_set(_M_base, __it);
2329 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2330 // 3482. drop_view's const begin should additionally require sized_range
2333 requires random_access_range<const _Vp> && sized_range<const _Vp>
2335 return ranges::next(ranges::begin(_M_base), _M_count,
2336 ranges::end(_M_base));
2340 end() requires (!__detail::__simple_view<_Vp>)
2341 { return ranges::end(_M_base); }
2344 end() const requires range<const _Vp>
2345 { return ranges::end(_M_base); }
2348 size() requires sized_range<_Vp>
2350 const auto __s = ranges::size(_M_base);
2351 const auto __c = static_cast<decltype(__s)>(_M_count);
2352 return __s < __c ? 0 : __s - __c;
2356 size() const requires sized_range<const _Vp>
2358 const auto __s = ranges::size(_M_base);
2359 const auto __c = static_cast<decltype(__s)>(_M_count);
2360 return __s < __c ? 0 : __s - __c;
2364 template<typename _Range>
2365 drop_view(_Range&&, range_difference_t<_Range>)
2366 -> drop_view<views::all_t<_Range>>;
2368 template<typename _Tp>
2369 inline constexpr bool enable_borrowed_range<drop_view<_Tp>>
2370 = enable_borrowed_range<_Tp>;
2374 inline constexpr __adaptor::_RangeAdaptor drop
2375 = [] <viewable_range _Range, typename _Tp> (_Range&& __r, _Tp&& __n)
2377 return drop_view{std::forward<_Range>(__r), std::forward<_Tp>(__n)};
2379 } // namespace views
2381 template<view _Vp, typename _Pred>
2382 requires input_range<_Vp> && is_object_v<_Pred>
2383 && indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
2384 class drop_while_view : public view_interface<drop_while_view<_Vp, _Pred>>
2387 _Vp _M_base = _Vp();
2388 __detail::__box<_Pred> _M_pred;
2389 [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
2392 drop_while_view() requires (default_initializable<_Vp>
2393 && default_initializable<_Pred>)
2397 drop_while_view(_Vp __base, _Pred __pred)
2398 : _M_base(std::move(__base)), _M_pred(std::move(__pred))
2402 base() const& requires copy_constructible<_Vp>
2407 { return std::move(_M_base); }
2409 constexpr const _Pred&
2411 { return *_M_pred; }
2416 if (_M_cached_begin._M_has_value())
2417 return _M_cached_begin._M_get(_M_base);
2419 __glibcxx_assert(_M_pred.has_value());
2420 auto __it = __detail::find_if_not(ranges::begin(_M_base),
2421 ranges::end(_M_base),
2422 std::cref(*_M_pred));
2423 _M_cached_begin._M_set(_M_base, __it);
2429 { return ranges::end(_M_base); }
2432 template<typename _Range, typename _Pred>
2433 drop_while_view(_Range&&, _Pred)
2434 -> drop_while_view<views::all_t<_Range>, _Pred>;
2436 template<typename _Tp, typename _Pred>
2437 inline constexpr bool enable_borrowed_range<drop_while_view<_Tp, _Pred>>
2438 = enable_borrowed_range<_Tp>;
2442 inline constexpr __adaptor::_RangeAdaptor drop_while
2443 = [] <viewable_range _Range, typename _Pred> (_Range&& __r, _Pred&& __p)
2445 return drop_while_view{std::forward<_Range>(__r),
2446 std::forward<_Pred>(__p)};
2448 } // namespace views
2450 template<input_range _Vp>
2451 requires view<_Vp> && input_range<range_reference_t<_Vp>>
2452 && (is_reference_v<range_reference_t<_Vp>>
2453 || view<range_value_t<_Vp>>)
2454 class join_view : public view_interface<join_view<_Vp>>
2457 using _InnerRange = range_reference_t<_Vp>;
2459 template<bool _Const>
2460 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2462 template<bool _Const>
2463 using _Outer_iter = iterator_t<_Base<_Const>>;
2465 template<bool _Const>
2466 using _Inner_iter = iterator_t<range_reference_t<_Base<_Const>>>;
2468 template<bool _Const>
2469 static constexpr bool _S_ref_is_glvalue
2470 = is_reference_v<range_reference_t<_Base<_Const>>>;
2472 template<bool _Const>
2476 template<bool _Const>
2477 requires _S_ref_is_glvalue<_Const>
2478 && forward_range<_Base<_Const>>
2479 && forward_range<range_reference_t<_Base<_Const>>>
2480 struct __iter_cat<_Const>
2483 static constexpr auto
2486 using _Outer_iter = join_view::_Outer_iter<_Const>;
2487 using _Inner_iter = join_view::_Inner_iter<_Const>;
2488 using _OuterCat = typename iterator_traits<_Outer_iter>::iterator_category;
2489 using _InnerCat = typename iterator_traits<_Inner_iter>::iterator_category;
2490 if constexpr (derived_from<_OuterCat, bidirectional_iterator_tag>
2491 && derived_from<_InnerCat, bidirectional_iterator_tag>)
2492 return bidirectional_iterator_tag{};
2493 else if constexpr (derived_from<_OuterCat, forward_iterator_tag>
2494 && derived_from<_InnerCat, forward_iterator_tag>)
2495 return forward_iterator_tag{};
2497 return input_iterator_tag{};
2500 using iterator_category = decltype(_S_iter_cat());
2503 template<bool _Const>
2506 template<bool _Const>
2507 struct _Iterator : __iter_cat<_Const>
2510 using _Parent = __detail::__maybe_const_t<_Const, join_view>;
2511 using _Base = join_view::_Base<_Const>;
2513 static constexpr bool _S_ref_is_glvalue
2514 = join_view::_S_ref_is_glvalue<_Const>;
2519 auto __update_inner = [this] (range_reference_t<_Base> __x) -> auto&
2521 if constexpr (_S_ref_is_glvalue)
2524 return (_M_parent->_M_inner = views::all(std::move(__x)));
2527 for (; _M_outer != ranges::end(_M_parent->_M_base); ++_M_outer)
2529 auto& __inner = __update_inner(*_M_outer);
2530 _M_inner = ranges::begin(__inner);
2531 if (_M_inner != ranges::end(__inner))
2535 if constexpr (_S_ref_is_glvalue)
2536 _M_inner = _Inner_iter();
2539 static constexpr auto
2542 if constexpr (_S_ref_is_glvalue
2543 && bidirectional_range<_Base>
2544 && bidirectional_range<range_reference_t<_Base>>)
2545 return bidirectional_iterator_tag{};
2546 else if constexpr (_S_ref_is_glvalue
2547 && forward_range<_Base>
2548 && forward_range<range_reference_t<_Base>>)
2549 return forward_iterator_tag{};
2551 return input_iterator_tag{};
2554 using _Outer_iter = join_view::_Outer_iter<_Const>;
2555 using _Inner_iter = join_view::_Inner_iter<_Const>;
2557 _Outer_iter _M_outer = _Outer_iter();
2558 _Inner_iter _M_inner = _Inner_iter();
2559 _Parent* _M_parent = nullptr;
2562 using iterator_concept = decltype(_S_iter_concept());
2563 // iterator_category defined in __join_view_iter_cat
2564 using value_type = range_value_t<range_reference_t<_Base>>;
2565 using difference_type
2566 = common_type_t<range_difference_t<_Base>,
2567 range_difference_t<range_reference_t<_Base>>>;
2569 _Iterator() requires (default_initializable<_Outer_iter>
2570 && default_initializable<_Inner_iter>)
2574 _Iterator(_Parent* __parent, _Outer_iter __outer)
2575 : _M_outer(std::move(__outer)),
2580 _Iterator(_Iterator<!_Const> __i)
2582 && convertible_to<iterator_t<_Vp>, _Outer_iter>
2583 && convertible_to<iterator_t<_InnerRange>, _Inner_iter>
2584 : _M_outer(std::move(__i._M_outer)), _M_inner(std::move(__i._M_inner)),
2585 _M_parent(__i._M_parent)
2588 constexpr decltype(auto)
2590 { return *_M_inner; }
2592 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2593 // 3500. join_view::iterator::operator->() is bogus
2594 constexpr _Inner_iter
2596 requires __detail::__has_arrow<_Inner_iter>
2597 && copyable<_Inner_iter>
2598 { return _M_inner; }
2600 constexpr _Iterator&
2603 auto&& __inner_range = [this] () -> auto&& {
2604 if constexpr (_S_ref_is_glvalue)
2607 return _M_parent->_M_inner;
2609 if (++_M_inner == ranges::end(__inner_range))
2623 requires _S_ref_is_glvalue && forward_range<_Base>
2624 && forward_range<range_reference_t<_Base>>
2631 constexpr _Iterator&
2633 requires _S_ref_is_glvalue && bidirectional_range<_Base>
2634 && bidirectional_range<range_reference_t<_Base>>
2635 && common_range<range_reference_t<_Base>>
2637 if (_M_outer == ranges::end(_M_parent->_M_base))
2638 _M_inner = ranges::end(*--_M_outer);
2639 while (_M_inner == ranges::begin(*_M_outer))
2640 _M_inner = ranges::end(*--_M_outer);
2647 requires _S_ref_is_glvalue && bidirectional_range<_Base>
2648 && bidirectional_range<range_reference_t<_Base>>
2649 && common_range<range_reference_t<_Base>>
2656 friend constexpr bool
2657 operator==(const _Iterator& __x, const _Iterator& __y)
2658 requires _S_ref_is_glvalue
2659 && equality_comparable<_Outer_iter>
2660 && equality_comparable<_Inner_iter>
2662 return (__x._M_outer == __y._M_outer
2663 && __x._M_inner == __y._M_inner);
2666 friend constexpr decltype(auto)
2667 iter_move(const _Iterator& __i)
2668 noexcept(noexcept(ranges::iter_move(__i._M_inner)))
2669 { return ranges::iter_move(__i._M_inner); }
2671 friend constexpr void
2672 iter_swap(const _Iterator& __x, const _Iterator& __y)
2673 noexcept(noexcept(ranges::iter_swap(__x._M_inner, __y._M_inner)))
2674 requires indirectly_swappable<_Inner_iter>
2675 { return ranges::iter_swap(__x._M_inner, __y._M_inner); }
2677 friend _Iterator<!_Const>;
2678 template<bool> friend struct _Sentinel;
2681 template<bool _Const>
2685 using _Parent = __detail::__maybe_const_t<_Const, join_view>;
2686 using _Base = join_view::_Base<_Const>;
2688 template<bool _Const2>
2690 __equal(const _Iterator<_Const2>& __i) const
2691 { return __i._M_outer == _M_end; }
2693 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
2696 _Sentinel() = default;
2699 _Sentinel(_Parent* __parent)
2700 : _M_end(ranges::end(__parent->_M_base))
2704 _Sentinel(_Sentinel<!_Const> __s)
2705 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
2706 : _M_end(std::move(__s._M_end))
2709 template<bool _Const2>
2710 requires sentinel_for<sentinel_t<_Base>,
2711 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
2712 friend constexpr bool
2713 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
2714 { return __y.__equal(__x); }
2716 friend _Sentinel<!_Const>;
2719 _Vp _M_base = _Vp();
2721 // XXX: _M_inner is "present only when !is_reference_v<_InnerRange>"
2722 [[no_unique_address]]
2723 __detail::__maybe_present_t<!is_reference_v<_InnerRange>,
2724 views::all_t<_InnerRange>> _M_inner;
2727 join_view() requires default_initializable<_Vp> = default;
2730 join_view(_Vp __base)
2731 : _M_base(std::move(__base))
2735 base() const& requires copy_constructible<_Vp>
2740 { return std::move(_M_base); }
2745 constexpr bool __use_const
2746 = (__detail::__simple_view<_Vp>
2747 && is_reference_v<range_reference_t<_Vp>>);
2748 return _Iterator<__use_const>{this, ranges::begin(_M_base)};
2753 requires input_range<const _Vp>
2754 && is_reference_v<range_reference_t<const _Vp>>
2756 return _Iterator<true>{this, ranges::begin(_M_base)};
2762 if constexpr (forward_range<_Vp> && is_reference_v<_InnerRange>
2763 && forward_range<_InnerRange>
2764 && common_range<_Vp> && common_range<_InnerRange>)
2765 return _Iterator<__detail::__simple_view<_Vp>>{this,
2766 ranges::end(_M_base)};
2768 return _Sentinel<__detail::__simple_view<_Vp>>{this};
2773 requires input_range<const _Vp>
2774 && is_reference_v<range_reference_t<const _Vp>>
2776 if constexpr (forward_range<const _Vp>
2777 && is_reference_v<range_reference_t<const _Vp>>
2778 && forward_range<range_reference_t<const _Vp>>
2779 && common_range<const _Vp>
2780 && common_range<range_reference_t<const _Vp>>)
2781 return _Iterator<true>{this, ranges::end(_M_base)};
2783 return _Sentinel<true>{this};
2787 template<typename _Range>
2788 explicit join_view(_Range&&) -> join_view<views::all_t<_Range>>;
2792 inline constexpr __adaptor::_RangeAdaptorClosure join
2793 = [] <viewable_range _Range> (_Range&& __r)
2795 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2796 // 3474. Nesting join_views is broken because of CTAD
2797 return join_view<views::all_t<_Range>>{std::forward<_Range>(__r)};
2799 } // namespace views
2804 struct __require_constant;
2806 template<typename _Range>
2807 concept __tiny_range = sized_range<_Range>
2809 { typename __require_constant<remove_reference_t<_Range>::size()>; }
2810 && (remove_reference_t<_Range>::size() <= 1);
2812 template<typename _Base>
2813 struct __split_view_outer_iter_cat
2816 template<forward_range _Base>
2817 struct __split_view_outer_iter_cat<_Base>
2818 { using iterator_category = input_iterator_tag; };
2820 template<typename _Base>
2821 struct __split_view_inner_iter_cat
2824 template<forward_range _Base>
2825 struct __split_view_inner_iter_cat<_Base>
2828 static constexpr auto
2831 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
2832 if constexpr (derived_from<_Cat, forward_iterator_tag>)
2833 return forward_iterator_tag{};
2838 using iterator_category = decltype(_S_iter_cat());
2842 template<input_range _Vp, forward_range _Pattern>
2843 requires view<_Vp> && view<_Pattern>
2844 && indirectly_comparable<iterator_t<_Vp>, iterator_t<_Pattern>,
2846 && (forward_range<_Vp> || __detail::__tiny_range<_Pattern>)
2847 class split_view : public view_interface<split_view<_Vp, _Pattern>>
2850 template<bool _Const>
2851 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2853 template<bool _Const>
2856 template<bool _Const>
2858 : __detail::__split_view_outer_iter_cat<_Base<_Const>>
2861 using _Parent = __detail::__maybe_const_t<_Const, split_view>;
2862 using _Base = split_view::_Base<_Const>;
2866 { return __current() == ranges::end(_M_parent->_M_base); }
2868 // [range.split.outer] p1
2869 // Many of the following specifications refer to the notional member
2870 // current of outer-iterator. current is equivalent to current_ if
2871 // V models forward_range, and parent_->current_ otherwise.
2873 __current() noexcept
2875 if constexpr (forward_range<_Vp>)
2878 return _M_parent->_M_current;
2882 __current() const noexcept
2884 if constexpr (forward_range<_Vp>)
2887 return _M_parent->_M_current;
2890 _Parent* _M_parent = nullptr;
2892 // XXX: _M_current is present only if "V models forward_range"
2893 [[no_unique_address]]
2894 __detail::__maybe_present_t<forward_range<_Vp>,
2895 iterator_t<_Base>> _M_current;
2898 using iterator_concept = conditional_t<forward_range<_Base>,
2899 forward_iterator_tag,
2900 input_iterator_tag>;
2901 // iterator_category defined in __split_view_outer_iter_cat
2902 using difference_type = range_difference_t<_Base>;
2904 struct value_type : view_interface<value_type>
2907 _OuterIter _M_i = _OuterIter();
2910 value_type() = default;
2913 value_type(_OuterIter __i)
2914 : _M_i(std::move(__i))
2917 constexpr _InnerIter<_Const>
2919 { return _InnerIter<_Const>{_M_i}; }
2921 constexpr default_sentinel_t
2923 { return default_sentinel; }
2926 _OuterIter() = default;
2929 _OuterIter(_Parent* __parent) requires (!forward_range<_Base>)
2930 : _M_parent(__parent)
2934 _OuterIter(_Parent* __parent, iterator_t<_Base> __current)
2935 requires forward_range<_Base>
2936 : _M_parent(__parent),
2937 _M_current(std::move(__current))
2941 _OuterIter(_OuterIter<!_Const> __i)
2943 && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
2944 : _M_parent(__i._M_parent), _M_current(std::move(__i._M_current))
2947 constexpr value_type
2949 { return value_type{*this}; }
2951 constexpr _OuterIter&
2954 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2955 // 3505. split_view::outer-iterator::operator++ misspecified
2956 const auto __end = ranges::end(_M_parent->_M_base);
2957 if (__current() == __end)
2959 const auto [__pbegin, __pend] = subrange{_M_parent->_M_pattern};
2960 if (__pbegin == __pend)
2962 else if constexpr (__detail::__tiny_range<_Pattern>)
2964 __current() = __detail::find(std::move(__current()), __end,
2966 if (__current() != __end)
2973 = __detail::mismatch(__current(), __end, __pbegin, __pend);
2979 } while (++__current() != __end);
2983 constexpr decltype(auto)
2986 if constexpr (forward_range<_Base>)
2996 friend constexpr bool
2997 operator==(const _OuterIter& __x, const _OuterIter& __y)
2998 requires forward_range<_Base>
2999 { return __x._M_current == __y._M_current; }
3001 friend constexpr bool
3002 operator==(const _OuterIter& __x, default_sentinel_t)
3003 { return __x.__at_end(); };
3005 friend _OuterIter<!_Const>;
3006 friend _InnerIter<_Const>;
3009 template<bool _Const>
3011 : __detail::__split_view_inner_iter_cat<_Base<_Const>>
3014 using _Base = split_view::_Base<_Const>;
3019 auto [__pcur, __pend] = subrange{_M_i._M_parent->_M_pattern};
3020 auto __end = ranges::end(_M_i._M_parent->_M_base);
3021 if constexpr (__detail::__tiny_range<_Pattern>)
3023 const auto& __cur = _M_i_current();
3026 if (__pcur == __pend)
3027 return _M_incremented;
3028 return *__cur == *__pcur;
3032 auto __cur = _M_i_current();
3035 if (__pcur == __pend)
3036 return _M_incremented;
3039 if (*__cur != *__pcur)
3041 if (++__pcur == __pend)
3043 } while (++__cur != __end);
3049 _M_i_current() noexcept
3050 { return _M_i.__current(); }
3053 _M_i_current() const noexcept
3054 { return _M_i.__current(); }
3056 _OuterIter<_Const> _M_i = _OuterIter<_Const>();
3057 bool _M_incremented = false;
3060 using iterator_concept
3061 = typename _OuterIter<_Const>::iterator_concept;
3062 // iterator_category defined in __split_view_inner_iter_cat
3063 using value_type = range_value_t<_Base>;
3064 using difference_type = range_difference_t<_Base>;
3066 _InnerIter() = default;
3069 _InnerIter(_OuterIter<_Const> __i)
3070 : _M_i(std::move(__i))
3073 constexpr const iterator_t<_Base>&
3074 base() const& noexcept
3075 { return _M_i_current(); }
3077 constexpr iterator_t<_Base>
3079 { return std::move(_M_i_current()); }
3081 constexpr decltype(auto)
3083 { return *_M_i_current(); }
3085 constexpr _InnerIter&
3088 _M_incremented = true;
3089 if constexpr (!forward_range<_Base>)
3090 if constexpr (_Pattern::size() == 0)
3096 constexpr decltype(auto)
3099 if constexpr (forward_range<_Base>)
3109 friend constexpr bool
3110 operator==(const _InnerIter& __x, const _InnerIter& __y)
3111 requires forward_range<_Base>
3112 { return __x._M_i == __y._M_i; }
3114 friend constexpr bool
3115 operator==(const _InnerIter& __x, default_sentinel_t)
3116 { return __x.__at_end(); }
3118 friend constexpr decltype(auto)
3119 iter_move(const _InnerIter& __i)
3120 noexcept(noexcept(ranges::iter_move(__i._M_i_current())))
3121 { return ranges::iter_move(__i._M_i_current()); }
3123 friend constexpr void
3124 iter_swap(const _InnerIter& __x, const _InnerIter& __y)
3125 noexcept(noexcept(ranges::iter_swap(__x._M_i_current(),
3126 __y._M_i_current())))
3127 requires indirectly_swappable<iterator_t<_Base>>
3128 { ranges::iter_swap(__x._M_i_current(), __y._M_i_current()); }
3131 _Vp _M_base = _Vp();
3132 _Pattern _M_pattern = _Pattern();
3134 // XXX: _M_current is "present only if !forward_range<V>"
3135 [[no_unique_address]]
3136 __detail::__maybe_present_t<!forward_range<_Vp>, iterator_t<_Vp>>
3141 split_view() requires (default_initializable<_Vp>
3142 && default_initializable<_Pattern>
3143 && default_initializable<iterator_t<_Vp>>)
3147 split_view(_Vp __base, _Pattern __pattern)
3148 : _M_base(std::move(__base)), _M_pattern(std::move(__pattern))
3151 template<input_range _Range>
3152 requires constructible_from<_Vp, views::all_t<_Range>>
3153 && constructible_from<_Pattern, single_view<range_value_t<_Range>>>
3155 split_view(_Range&& __r, range_value_t<_Range> __e)
3156 : _M_base(views::all(std::forward<_Range>(__r))),
3157 _M_pattern(std::move(__e))
3161 base() const& requires copy_constructible<_Vp>
3166 { return std::move(_M_base); }
3171 if constexpr (forward_range<_Vp>)
3172 return _OuterIter<__detail::__simple_view<_Vp>>{
3173 this, ranges::begin(_M_base)};
3176 _M_current = ranges::begin(_M_base);
3177 return _OuterIter<false>{this};
3182 begin() const requires forward_range<_Vp> && forward_range<const _Vp>
3184 return _OuterIter<true>{this, ranges::begin(_M_base)};
3188 end() requires forward_range<_Vp> && common_range<_Vp>
3190 return _OuterIter<__detail::__simple_view<_Vp>>{
3191 this, ranges::end(_M_base)};
3197 if constexpr (forward_range<_Vp>
3198 && forward_range<const _Vp>
3199 && common_range<const _Vp>)
3200 return _OuterIter<true>{this, ranges::end(_M_base)};
3202 return default_sentinel;
3206 template<typename _Range, typename _Pred>
3207 split_view(_Range&&, _Pred&&)
3208 -> split_view<views::all_t<_Range>, views::all_t<_Pred>>;
3210 template<input_range _Range>
3211 split_view(_Range&&, range_value_t<_Range>)
3212 -> split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
3216 inline constexpr __adaptor::_RangeAdaptor split
3217 = [] <viewable_range _Range, typename _Fp> (_Range&& __r, _Fp&& __f)
3219 return split_view{std::forward<_Range>(__r), std::forward<_Fp>(__f)};
3221 } // namespace views
3227 template<input_or_output_iterator _Iter>
3229 operator()(_Iter __i, iter_difference_t<_Iter> __n) const
3231 if constexpr (random_access_iterator<_Iter>)
3232 return subrange{__i, __i + __n};
3234 return subrange{counted_iterator{std::move(__i), __n},
3239 inline constexpr _Counted counted{};
3240 } // namespace views
3243 requires (!common_range<_Vp>) && copyable<iterator_t<_Vp>>
3244 class common_view : public view_interface<common_view<_Vp>>
3247 _Vp _M_base = _Vp();
3250 common_view() requires default_initializable<_Vp> = default;
3253 common_view(_Vp __r)
3254 : _M_base(std::move(__r))
3257 /* XXX: LWG 3280 didn't remove this constructor, but I think it should?
3258 template<viewable_range _Range>
3259 requires (!common_range<_Range>)
3260 && constructible_from<_Vp, views::all_t<_Range>>
3262 common_view(_Range&& __r)
3263 : _M_base(views::all(std::forward<_Range>(__r)))
3268 base() const& requires copy_constructible<_Vp>
3273 { return std::move(_M_base); }
3278 if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
3279 return ranges::begin(_M_base);
3281 return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
3282 (ranges::begin(_M_base));
3286 begin() const requires range<const _Vp>
3288 if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
3289 return ranges::begin(_M_base);
3291 return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
3292 (ranges::begin(_M_base));
3298 if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
3299 return ranges::begin(_M_base) + ranges::size(_M_base);
3301 return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
3302 (ranges::end(_M_base));
3306 end() const requires range<const _Vp>
3308 if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
3309 return ranges::begin(_M_base) + ranges::size(_M_base);
3311 return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
3312 (ranges::end(_M_base));
3316 size() requires sized_range<_Vp>
3317 { return ranges::size(_M_base); }
3320 size() const requires sized_range<const _Vp>
3321 { return ranges::size(_M_base); }
3324 template<typename _Range>
3325 common_view(_Range&&) -> common_view<views::all_t<_Range>>;
3327 template<typename _Tp>
3328 inline constexpr bool enable_borrowed_range<common_view<_Tp>>
3329 = enable_borrowed_range<_Tp>;
3333 inline constexpr __adaptor::_RangeAdaptorClosure common
3334 = [] <viewable_range _Range> (_Range&& __r)
3336 if constexpr (common_range<_Range>
3337 && requires { views::all(std::forward<_Range>(__r)); })
3338 return views::all(std::forward<_Range>(__r));
3340 return common_view{std::forward<_Range>(__r)};
3343 } // namespace views
3346 requires bidirectional_range<_Vp>
3347 class reverse_view : public view_interface<reverse_view<_Vp>>
3350 _Vp _M_base = _Vp();
3352 static constexpr bool _S_needs_cached_begin
3353 = !common_range<_Vp> && !random_access_range<_Vp>;
3354 [[no_unique_address]]
3355 __detail::__maybe_present_t<_S_needs_cached_begin,
3356 __detail::_CachedPosition<_Vp>>
3360 reverse_view() requires default_initializable<_Vp> = default;
3363 reverse_view(_Vp __r)
3364 : _M_base(std::move(__r))
3368 base() const& requires copy_constructible<_Vp>
3373 { return std::move(_M_base); }
3375 constexpr reverse_iterator<iterator_t<_Vp>>
3378 if constexpr (_S_needs_cached_begin)
3379 if (_M_cached_begin._M_has_value())
3380 return std::make_reverse_iterator(_M_cached_begin._M_get(_M_base));
3382 auto __it = ranges::next(ranges::begin(_M_base), ranges::end(_M_base));
3383 if constexpr (_S_needs_cached_begin)
3384 _M_cached_begin._M_set(_M_base, __it);
3385 return std::make_reverse_iterator(std::move(__it));
3389 begin() requires common_range<_Vp>
3390 { return std::make_reverse_iterator(ranges::end(_M_base)); }
3393 begin() const requires common_range<const _Vp>
3394 { return std::make_reverse_iterator(ranges::end(_M_base)); }
3396 constexpr reverse_iterator<iterator_t<_Vp>>
3398 { return std::make_reverse_iterator(ranges::begin(_M_base)); }
3401 end() const requires common_range<const _Vp>
3402 { return std::make_reverse_iterator(ranges::begin(_M_base)); }
3405 size() requires sized_range<_Vp>
3406 { return ranges::size(_M_base); }
3409 size() const requires sized_range<const _Vp>
3410 { return ranges::size(_M_base); }
3413 template<typename _Range>
3414 reverse_view(_Range&&) -> reverse_view<views::all_t<_Range>>;
3416 template<typename _Tp>
3417 inline constexpr bool enable_borrowed_range<reverse_view<_Tp>>
3418 = enable_borrowed_range<_Tp>;
3425 inline constexpr bool __is_reversible_subrange = false;
3427 template<typename _Iter, subrange_kind _Kind>
3428 inline constexpr bool
3429 __is_reversible_subrange<subrange<reverse_iterator<_Iter>,
3430 reverse_iterator<_Iter>,
3434 inline constexpr bool __is_reverse_view = false;
3436 template<typename _Vp>
3437 inline constexpr bool __is_reverse_view<reverse_view<_Vp>> = true;
3440 inline constexpr __adaptor::_RangeAdaptorClosure reverse
3441 = [] <viewable_range _Range> (_Range&& __r)
3443 using _Tp = remove_cvref_t<_Range>;
3444 if constexpr (__detail::__is_reverse_view<_Tp>)
3445 return std::forward<_Range>(__r).base();
3446 else if constexpr (__detail::__is_reversible_subrange<_Tp>)
3448 using _Iter = decltype(ranges::begin(__r).base());
3449 if constexpr (sized_range<_Tp>)
3450 return subrange<_Iter, _Iter, subrange_kind::sized>
3451 (__r.end().base(), __r.begin().base(), __r.size());
3453 return subrange<_Iter, _Iter, subrange_kind::unsized>
3454 (__r.end().base(), __r.begin().base());
3457 return reverse_view{std::forward<_Range>(__r)};
3459 } // namespace views
3463 template<typename _Tp, size_t _Nm>
3464 concept __has_tuple_element = requires(_Tp __t)
3466 typename tuple_size<_Tp>::type;
3467 requires _Nm < tuple_size_v<_Tp>;
3468 typename tuple_element_t<_Nm, _Tp>;
3469 { std::get<_Nm>(__t) }
3470 -> convertible_to<const tuple_element_t<_Nm, _Tp>&>;
3473 template<typename _Tp, size_t _Nm>
3474 concept __returnable_element
3475 = is_reference_v<_Tp> || move_constructible<tuple_element_t<_Nm, _Tp>>;
3478 template<input_range _Vp, size_t _Nm>
3480 && __detail::__has_tuple_element<range_value_t<_Vp>, _Nm>
3481 && __detail::__has_tuple_element<remove_reference_t<range_reference_t<_Vp>>,
3483 && __detail::__returnable_element<range_reference_t<_Vp>, _Nm>
3484 class elements_view : public view_interface<elements_view<_Vp, _Nm>>
3487 elements_view() requires default_initializable<_Vp> = default;
3490 elements_view(_Vp base)
3491 : _M_base(std::move(base))
3495 base() const& requires copy_constructible<_Vp>
3500 { return std::move(_M_base); }
3503 begin() requires (!__detail::__simple_view<_Vp>)
3504 { return _Iterator<false>(ranges::begin(_M_base)); }
3507 begin() const requires range<const _Vp>
3508 { return _Iterator<true>(ranges::begin(_M_base)); }
3511 end() requires (!__detail::__simple_view<_Vp> && !common_range<_Vp>)
3512 { return _Sentinel<false>{ranges::end(_M_base)}; }
3515 end() requires (!__detail::__simple_view<_Vp> && common_range<_Vp>)
3516 { return _Iterator<false>{ranges::end(_M_base)}; }
3519 end() const requires range<const _Vp>
3520 { return _Sentinel<true>{ranges::end(_M_base)}; }
3523 end() const requires common_range<const _Vp>
3524 { return _Iterator<true>{ranges::end(_M_base)}; }
3527 size() requires sized_range<_Vp>
3528 { return ranges::size(_M_base); }
3531 size() const requires sized_range<const _Vp>
3532 { return ranges::size(_M_base); }
3535 template<bool _Const>
3536 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
3538 template<bool _Const>
3542 template<bool _Const>
3543 requires forward_range<_Base<_Const>>
3544 struct __iter_cat<_Const>
3547 static auto _S_iter_cat()
3549 using _Base = elements_view::_Base<_Const>;
3550 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
3551 using _Res = decltype((std::get<_Nm>(*std::declval<iterator_t<_Base>>())));
3552 if constexpr (!is_lvalue_reference_v<_Res>)
3553 return input_iterator_tag{};
3554 else if constexpr (derived_from<_Cat, random_access_iterator_tag>)
3555 return random_access_iterator_tag{};
3560 using iterator_category = decltype(_S_iter_cat());
3563 template<bool _Const>
3566 template<bool _Const>
3567 struct _Iterator : __iter_cat<_Const>
3570 using _Base = elements_view::_Base<_Const>;
3572 iterator_t<_Base> _M_current = iterator_t<_Base>();
3574 static constexpr decltype(auto)
3575 _S_get_element(const iterator_t<_Base>& __i)
3577 if constexpr (is_reference_v<range_reference_t<_Base>>)
3578 return std::get<_Nm>(*__i);
3581 using _Et = remove_cv_t<tuple_element_t<_Nm, range_reference_t<_Base>>>;
3582 return static_cast<_Et>(std::get<_Nm>(*__i));
3589 if constexpr (random_access_range<_Base>)
3590 return random_access_iterator_tag{};
3591 else if constexpr (bidirectional_range<_Base>)
3592 return bidirectional_iterator_tag{};
3593 else if constexpr (forward_range<_Base>)
3594 return forward_iterator_tag{};
3596 return input_iterator_tag{};
3599 friend _Iterator<!_Const>;
3602 using iterator_concept = decltype(_S_iter_concept());
3603 // iterator_category defined in elements_view::__iter_cat
3605 = remove_cvref_t<tuple_element_t<_Nm, range_value_t<_Base>>>;
3606 using difference_type = range_difference_t<_Base>;
3608 _Iterator() requires default_initializable<iterator_t<_Base>> = default;
3611 _Iterator(iterator_t<_Base> current)
3612 : _M_current(std::move(current))
3616 _Iterator(_Iterator<!_Const> i)
3617 requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
3618 : _M_current(std::move(i._M_current))
3621 constexpr const iterator_t<_Base>&
3622 base() const& noexcept
3623 { return _M_current; }
3625 constexpr iterator_t<_Base>
3627 { return std::move(_M_current); }
3629 constexpr decltype(auto)
3631 { return _S_get_element(_M_current); }
3633 constexpr _Iterator&
3645 operator++(int) requires forward_range<_Base>
3652 constexpr _Iterator&
3653 operator--() requires bidirectional_range<_Base>
3660 operator--(int) requires bidirectional_range<_Base>
3667 constexpr _Iterator&
3668 operator+=(difference_type __n)
3669 requires random_access_range<_Base>
3675 constexpr _Iterator&
3676 operator-=(difference_type __n)
3677 requires random_access_range<_Base>
3683 constexpr decltype(auto)
3684 operator[](difference_type __n) const
3685 requires random_access_range<_Base>
3686 { return _S_get_element(_M_current + __n); }
3688 friend constexpr bool
3689 operator==(const _Iterator& __x, const _Iterator& __y)
3690 requires equality_comparable<iterator_t<_Base>>
3691 { return __x._M_current == __y._M_current; }
3693 friend constexpr bool
3694 operator<(const _Iterator& __x, const _Iterator& __y)
3695 requires random_access_range<_Base>
3696 { return __x._M_current < __y._M_current; }
3698 friend constexpr bool
3699 operator>(const _Iterator& __x, const _Iterator& __y)
3700 requires random_access_range<_Base>
3701 { return __y._M_current < __x._M_current; }
3703 friend constexpr bool
3704 operator<=(const _Iterator& __x, const _Iterator& __y)
3705 requires random_access_range<_Base>
3706 { return !(__y._M_current > __x._M_current); }
3708 friend constexpr bool
3709 operator>=(const _Iterator& __x, const _Iterator& __y)
3710 requires random_access_range<_Base>
3711 { return !(__x._M_current > __y._M_current); }
3713 #ifdef __cpp_lib_three_way_comparison
3714 friend constexpr auto
3715 operator<=>(const _Iterator& __x, const _Iterator& __y)
3716 requires random_access_range<_Base>
3717 && three_way_comparable<iterator_t<_Base>>
3718 { return __x._M_current <=> __y._M_current; }
3721 friend constexpr _Iterator
3722 operator+(const _Iterator& __x, difference_type __y)
3723 requires random_access_range<_Base>
3724 { return _Iterator{__x} += __y; }
3726 friend constexpr _Iterator
3727 operator+(difference_type __x, const _Iterator& __y)
3728 requires random_access_range<_Base>
3729 { return __y + __x; }
3731 friend constexpr _Iterator
3732 operator-(const _Iterator& __x, difference_type __y)
3733 requires random_access_range<_Base>
3734 { return _Iterator{__x} -= __y; }
3736 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3737 // 3483. transform_view::iterator's difference is overconstrained
3738 friend constexpr difference_type
3739 operator-(const _Iterator& __x, const _Iterator& __y)
3740 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
3741 { return __x._M_current - __y._M_current; }
3743 template <bool> friend struct _Sentinel;
3746 template<bool _Const>
3750 template<bool _Const2>
3752 _M_equal(const _Iterator<_Const2>& __x) const
3753 { return __x._M_current == _M_end; }
3755 template<bool _Const2>
3757 _M_distance_from(const _Iterator<_Const2>& __i) const
3758 { return _M_end - __i._M_current; }
3760 using _Base = elements_view::_Base<_Const>;
3761 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
3764 _Sentinel() = default;
3767 _Sentinel(sentinel_t<_Base> __end)
3768 : _M_end(std::move(__end))
3772 _Sentinel(_Sentinel<!_Const> __other)
3774 && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
3775 : _M_end(std::move(__other._M_end))
3778 constexpr sentinel_t<_Base>
3782 template<bool _Const2>
3783 requires sentinel_for<sentinel_t<_Base>,
3784 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
3785 friend constexpr bool
3786 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
3787 { return __y._M_equal(__x); }
3789 template<bool _Const2,
3790 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
3791 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
3792 friend constexpr range_difference_t<_Base2>
3793 operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
3794 { return -__y._M_distance_from(__x); }
3796 template<bool _Const2,
3797 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
3798 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
3799 friend constexpr range_difference_t<_Base2>
3800 operator-(const _Sentinel& __x, const _Iterator<_Const2>& __y)
3801 { return __x._M_distance_from(__y); }
3803 friend _Sentinel<!_Const>;
3806 _Vp _M_base = _Vp();
3809 template<typename _Tp, size_t _Nm>
3810 inline constexpr bool enable_borrowed_range<elements_view<_Tp, _Nm>>
3811 = enable_borrowed_range<_Tp>;
3813 template<typename _Range>
3814 using keys_view = elements_view<views::all_t<_Range>, 0>;
3816 template<typename _Range>
3817 using values_view = elements_view<views::all_t<_Range>, 1>;
3821 template<size_t _Nm>
3822 inline constexpr __adaptor::_RangeAdaptorClosure elements
3823 = [] <viewable_range _Range> (_Range&& __r)
3825 using _El = elements_view<views::all_t<_Range>, _Nm>;
3826 return _El{std::forward<_Range>(__r)};
3829 inline constexpr __adaptor::_RangeAdaptorClosure keys = elements<0>;
3830 inline constexpr __adaptor::_RangeAdaptorClosure values = elements<1>;
3831 } // namespace views
3833 } // namespace ranges
3835 namespace views = ranges::views;
3837 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
3838 struct tuple_size<ranges::subrange<_Iter, _Sent, _Kind>>
3839 : integral_constant<size_t, 2>
3842 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
3843 struct tuple_element<0, ranges::subrange<_Iter, _Sent, _Kind>>
3844 { using type = _Iter; };
3846 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
3847 struct tuple_element<1, ranges::subrange<_Iter, _Sent, _Kind>>
3848 { using type = _Sent; };
3850 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
3851 struct tuple_element<0, const ranges::subrange<_Iter, _Sent, _Kind>>
3852 { using type = _Iter; };
3854 template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
3855 struct tuple_element<1, const ranges::subrange<_Iter, _Sent, _Kind>>
3856 { using type = _Sent; };
3858 _GLIBCXX_END_NAMESPACE_VERSION
3860 #endif // library concepts
3862 #endif /* _GLIBCXX_RANGES */