3// Copyright (C) 2019-2021 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
42#include <initializer_list>
46#include <bits/ranges_util.h>
47#include <bits/refwrap.h>
50 * @defgroup ranges Ranges
52 * Components for dealing with ranges of elements.
55namespace std _GLIBCXX_VISIBILITY(default)
57_GLIBCXX_BEGIN_NAMESPACE_VERSION
60 // [range.access] customization point objects
61 // [range.req] range and view concepts
62 // [range.dangling] dangling iterator handling
63 // Defined in <bits/ranges_base.h>
65 // [view.interface] View interface
66 // [range.subrange] Sub-ranges
67 // Defined in <bits/ranges_util.h>
69 // C++20 24.6 [range.factories] Range factories
71 /// A view that contains no elements.
72 template<typename _Tp> requires is_object_v<_Tp>
74 : public view_interface<empty_view<_Tp>>
77 static constexpr _Tp* begin() noexcept { return nullptr; }
78 static constexpr _Tp* end() noexcept { return nullptr; }
79 static constexpr _Tp* data() noexcept { return nullptr; }
80 static constexpr size_t size() noexcept { return 0; }
81 static constexpr bool empty() noexcept { return true; }
84 template<typename _Tp>
85 inline constexpr bool enable_borrowed_range<empty_view<_Tp>> = true;
89 template<typename _Tp>
90 concept __boxable = copy_constructible<_Tp> && is_object_v<_Tp>;
92 template<__boxable _Tp>
93 struct __box : std::optional<_Tp>
95 using std::optional<_Tp>::optional;
99 noexcept(is_nothrow_default_constructible_v<_Tp>)
100 requires default_initializable<_Tp>
101 : std::optional<_Tp>{std::in_place}
104 __box(const __box&) = default;
105 __box(__box&&) = default;
107 using std::optional<_Tp>::operator=;
109 // _GLIBCXX_RESOLVE_LIB_DEFECTS
110 // 3477. Simplify constraints for semiregular-box
112 operator=(const __box& __that)
113 noexcept(is_nothrow_copy_constructible_v<_Tp>)
114 requires (!copyable<_Tp>)
116 if (this != std::__addressof(__that))
119 this->emplace(*__that);
127 operator=(__box&& __that)
128 noexcept(is_nothrow_move_constructible_v<_Tp>)
129 requires (!movable<_Tp>)
131 if (this != std::__addressof(__that))
134 this->emplace(std::move(*__that));
142 // For types which are already copyable, this specialization of the
143 // copyable wrapper stores the object directly without going through
144 // std::optional. It provides just the subset of the primary template's
145 // API that we currently use.
146 template<__boxable _Tp>
147 requires copyable<_Tp>
151 [[no_unique_address]] _Tp _M_value = _Tp();
154 __box() requires default_initializable<_Tp> = default;
157 __box(const _Tp& __t)
158 noexcept(is_nothrow_copy_constructible_v<_Tp>)
164 noexcept(is_nothrow_move_constructible_v<_Tp>)
165 : _M_value(std::move(__t))
168 template<typename... _Args>
169 requires constructible_from<_Tp, _Args...>
171 __box(in_place_t, _Args&&... __args)
172 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
173 : _M_value(std::forward<_Args>(__args)...)
177 has_value() const noexcept
185 operator*() const noexcept
189 operator->() noexcept
190 { return std::__addressof(_M_value); }
193 operator->() const noexcept
194 { return std::__addressof(_M_value); }
196 } // namespace __detail
198 /// A view that contains exactly one element.
199 template<copy_constructible _Tp> requires is_object_v<_Tp>
200 class single_view : public view_interface<single_view<_Tp>>
203 single_view() = default;
206 single_view(const _Tp& __t)
211 single_view(_Tp&& __t)
212 : _M_value(std::move(__t))
215 // _GLIBCXX_RESOLVE_LIB_DEFECTS
216 // 3428. single_view's in place constructor should be explicit
217 template<typename... _Args>
218 requires constructible_from<_Tp, _Args...>
220 single_view(in_place_t, _Args&&... __args)
221 : _M_value{in_place, std::forward<_Args>(__args)...}
229 begin() const noexcept
234 { return data() + 1; }
238 { return data() + 1; }
240 static constexpr size_t
246 { return _M_value.operator->(); }
249 data() const noexcept
250 { return _M_value.operator->(); }
253 [[no_unique_address]] __detail::__box<_Tp> _M_value;
256 template<typename _Tp>
257 single_view(_Tp) -> single_view<_Tp>;
261 template<typename _Wp>
262 constexpr auto __to_signed_like(_Wp __w) noexcept
264 if constexpr (!integral<_Wp>)
265 return iter_difference_t<_Wp>();
266 else if constexpr (sizeof(iter_difference_t<_Wp>) > sizeof(_Wp))
267 return iter_difference_t<_Wp>(__w);
268 else if constexpr (sizeof(ptrdiff_t) > sizeof(_Wp))
269 return ptrdiff_t(__w);
270 else if constexpr (sizeof(long long) > sizeof(_Wp))
271 return (long long)(__w);
272#ifdef __SIZEOF_INT128__
273 else if constexpr (__SIZEOF_INT128__ > sizeof(_Wp))
274 return __int128(__w);
277 return __max_diff_type(__w);
280 template<typename _Wp>
281 using __iota_diff_t = decltype(__to_signed_like(std::declval<_Wp>()));
283 template<typename _It>
284 concept __decrementable = incrementable<_It>
287 { --__i } -> same_as<_It&>;
288 { __i-- } -> same_as<_It>;
291 template<typename _It>
292 concept __advanceable = __decrementable<_It> && totally_ordered<_It>
293 && requires( _It __i, const _It __j, const __iota_diff_t<_It> __n)
295 { __i += __n } -> same_as<_It&>;
296 { __i -= __n } -> same_as<_It&>;
300 { __j - __j } -> convertible_to<__iota_diff_t<_It>>;
303 template<typename _Winc>
304 struct __iota_view_iter_cat
307 template<incrementable _Winc>
308 struct __iota_view_iter_cat<_Winc>
309 { using iterator_category = input_iterator_tag; };
310 } // namespace __detail
312 template<weakly_incrementable _Winc,
313 semiregular _Bound = unreachable_sentinel_t>
314 requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound>
316 class iota_view : public view_interface<iota_view<_Winc, _Bound>>
321 struct _Iterator : __detail::__iota_view_iter_cat<_Winc>
327 using namespace __detail;
328 if constexpr (__advanceable<_Winc>)
329 return random_access_iterator_tag{};
330 else if constexpr (__decrementable<_Winc>)
331 return bidirectional_iterator_tag{};
332 else if constexpr (incrementable<_Winc>)
333 return forward_iterator_tag{};
335 return input_iterator_tag{};
339 using iterator_concept = decltype(_S_iter_concept());
340 // iterator_category defined in __iota_view_iter_cat
341 using value_type = _Winc;
342 using difference_type = __detail::__iota_diff_t<_Winc>;
344 _Iterator() requires default_initializable<_Winc> = default;
347 _Iterator(_Winc __value)
348 : _M_value(__value) { }
351 operator*() const noexcept(is_nothrow_copy_constructible_v<_Winc>)
366 operator++(int) requires incrementable<_Winc>
374 operator--() requires __detail::__decrementable<_Winc>
381 operator--(int) requires __detail::__decrementable<_Winc>
389 operator+=(difference_type __n) requires __detail::__advanceable<_Winc>
391 using __detail::__is_integer_like;
392 using __detail::__is_signed_integer_like;
393 if constexpr (__is_integer_like<_Winc>
394 && !__is_signed_integer_like<_Winc>)
396 if (__n >= difference_type(0))
397 _M_value += static_cast<_Winc>(__n);
399 _M_value -= static_cast<_Winc>(-__n);
407 operator-=(difference_type __n) requires __detail::__advanceable<_Winc>
409 using __detail::__is_integer_like;
410 using __detail::__is_signed_integer_like;
411 if constexpr (__is_integer_like<_Winc>
412 && !__is_signed_integer_like<_Winc>)
414 if (__n >= difference_type(0))
415 _M_value -= static_cast<_Winc>(__n);
417 _M_value += static_cast<_Winc>(-__n);
425 operator[](difference_type __n) const
426 requires __detail::__advanceable<_Winc>
427 { return _Winc(_M_value + __n); }
429 friend constexpr bool
430 operator==(const _Iterator& __x, const _Iterator& __y)
431 requires equality_comparable<_Winc>
432 { return __x._M_value == __y._M_value; }
434 friend constexpr bool
435 operator<(const _Iterator& __x, const _Iterator& __y)
436 requires totally_ordered<_Winc>
437 { return __x._M_value < __y._M_value; }
439 friend constexpr bool
440 operator>(const _Iterator& __x, const _Iterator& __y)
441 requires totally_ordered<_Winc>
442 { return __y < __x; }
444 friend constexpr bool
445 operator<=(const _Iterator& __x, const _Iterator& __y)
446 requires totally_ordered<_Winc>
447 { return !(__y < __x); }
449 friend constexpr bool
450 operator>=(const _Iterator& __x, const _Iterator& __y)
451 requires totally_ordered<_Winc>
452 { return !(__x < __y); }
454#ifdef __cpp_lib_three_way_comparison
455 friend constexpr auto
456 operator<=>(const _Iterator& __x, const _Iterator& __y)
457 requires totally_ordered<_Winc> && three_way_comparable<_Winc>
458 { return __x._M_value <=> __y._M_value; }
461 friend constexpr _Iterator
462 operator+(_Iterator __i, difference_type __n)
463 requires __detail::__advanceable<_Winc>
469 friend constexpr _Iterator
470 operator+(difference_type __n, _Iterator __i)
471 requires __detail::__advanceable<_Winc>
472 { return __i += __n; }
474 friend constexpr _Iterator
475 operator-(_Iterator __i, difference_type __n)
476 requires __detail::__advanceable<_Winc>
482 friend constexpr difference_type
483 operator-(const _Iterator& __x, const _Iterator& __y)
484 requires __detail::__advanceable<_Winc>
486 using __detail::__is_integer_like;
487 using __detail::__is_signed_integer_like;
488 using _Dt = difference_type;
489 if constexpr (__is_integer_like<_Winc>)
491 if constexpr (__is_signed_integer_like<_Winc>)
492 return _Dt(_Dt(__x._M_value) - _Dt(__y._M_value));
494 return (__y._M_value > __x._M_value)
495 ? _Dt(-_Dt(__y._M_value - __x._M_value))
496 : _Dt(__x._M_value - __y._M_value);
499 return __x._M_value - __y._M_value;
503 _Winc _M_value = _Winc();
513 _M_equal(const _Iterator& __x) const
514 { return __x._M_value == _M_bound; }
517 _M_distance_from(const _Iterator& __x) const
518 { return _M_bound - __x._M_value; }
520 _Bound _M_bound = _Bound();
523 _Sentinel() = default;
526 _Sentinel(_Bound __bound)
527 : _M_bound(__bound) { }
529 friend constexpr bool
530 operator==(const _Iterator& __x, const _Sentinel& __y)
531 { return __y._M_equal(__x); }
533 friend constexpr iter_difference_t<_Winc>
534 operator-(const _Iterator& __x, const _Sentinel& __y)
535 requires sized_sentinel_for<_Bound, _Winc>
536 { return -__y._M_distance_from(__x); }
538 friend constexpr iter_difference_t<_Winc>
539 operator-(const _Sentinel& __x, const _Iterator& __y)
540 requires sized_sentinel_for<_Bound, _Winc>
541 { return __x._M_distance_from(__y); }
546 _Winc _M_value = _Winc();
547 [[no_unique_address]] _Bound _M_bound = _Bound();
550 iota_view() requires default_initializable<_Winc> = default;
553 iota_view(_Winc __value)
558 iota_view(type_identity_t<_Winc> __value,
559 type_identity_t<_Bound> __bound)
560 : _M_value(__value), _M_bound(__bound)
562 if constexpr (totally_ordered_with<_Winc, _Bound>)
563 __glibcxx_assert( bool(__value <= __bound) );
567 iota_view(_Iterator __first, _Iterator __last)
568 requires same_as<_Winc, _Bound>
569 : iota_view(__first._M_value, __last._M_value)
573 iota_view(_Iterator __first, unreachable_sentinel_t __last)
574 requires same_as<_Bound, unreachable_sentinel_t>
575 : iota_view(__first._M_value, __last)
579 iota_view(_Iterator __first, _Sentinel __last)
580 requires (!same_as<_Winc, _Bound>) && (!same_as<_Bound, unreachable_sentinel_t>)
581 : iota_view(__first._M_value, __last._M_bound)
585 begin() const { return _Iterator{_M_value}; }
590 if constexpr (same_as<_Bound, unreachable_sentinel_t>)
591 return unreachable_sentinel;
593 return _Sentinel{_M_bound};
597 end() const requires same_as<_Winc, _Bound>
598 { return _Iterator{_M_bound}; }
602 requires (same_as<_Winc, _Bound> && __detail::__advanceable<_Winc>)
603 || (integral<_Winc> && integral<_Bound>)
604 || sized_sentinel_for<_Bound, _Winc>
606 using __detail::__is_integer_like;
607 using __detail::__to_unsigned_like;
608 if constexpr (integral<_Winc> && integral<_Bound>)
610 using _Up = make_unsigned_t<decltype(_M_bound - _M_value)>;
611 return _Up(_M_bound) - _Up(_M_value);
613 else if constexpr (__is_integer_like<_Winc>)
614 return __to_unsigned_like(_M_bound) - __to_unsigned_like(_M_value);
616 return __to_unsigned_like(_M_bound - _M_value);
620 template<typename _Winc, typename _Bound>
621 requires (!__detail::__is_integer_like<_Winc>
622 || !__detail::__is_integer_like<_Bound>
623 || (__detail::__is_signed_integer_like<_Winc>
624 == __detail::__is_signed_integer_like<_Bound>))
625 iota_view(_Winc, _Bound) -> iota_view<_Winc, _Bound>;
627 template<weakly_incrementable _Winc, semiregular _Bound>
628 inline constexpr bool
629 enable_borrowed_range<iota_view<_Winc, _Bound>> = true;
633 template<typename _Tp>
634 inline constexpr empty_view<_Tp> empty{};
638 template<typename _Tp>
640 operator()(_Tp&& __e) const
641 { return single_view<decay_t<_Tp>>(std::forward<_Tp>(__e)); }
644 inline constexpr _Single single{};
648 template<typename _Tp>
650 operator()(_Tp&& __e) const
651 { return iota_view(std::forward<_Tp>(__e)); }
653 template<typename _Tp, typename _Up>
655 operator()(_Tp&& __e, _Up&& __f) const
656 { return iota_view(std::forward<_Tp>(__e), std::forward<_Up>(__f)); }
659 inline constexpr _Iota iota{};
664 template<typename _Val, typename _CharT, typename _Traits>
665 concept __stream_extractable
666 = requires(basic_istream<_CharT, _Traits>& is, _Val& t) { is >> t; };
667 } // namespace __detail
669 template<movable _Val, typename _CharT,
670 typename _Traits = char_traits<_CharT>>
671 requires default_initializable<_Val>
672 && __detail::__stream_extractable<_Val, _CharT, _Traits>
673 class basic_istream_view
674 : public view_interface<basic_istream_view<_Val, _CharT, _Traits>>
677 basic_istream_view() = default;
680 basic_istream_view(basic_istream<_CharT, _Traits>& __stream)
681 : _M_stream(std::__addressof(__stream))
687 if (_M_stream != nullptr)
688 *_M_stream >> _M_object;
689 return _Iterator{this};
692 constexpr default_sentinel_t
694 { return default_sentinel; }
697 basic_istream<_CharT, _Traits>* _M_stream = nullptr;
698 _Val _M_object = _Val();
703 using iterator_concept = input_iterator_tag;
704 using difference_type = ptrdiff_t;
705 using value_type = _Val;
707 _Iterator() = default;
710 _Iterator(basic_istream_view* __parent) noexcept
711 : _M_parent(__parent)
714 _Iterator(const _Iterator&) = delete;
715 _Iterator(_Iterator&&) = default;
716 _Iterator& operator=(const _Iterator&) = delete;
717 _Iterator& operator=(_Iterator&&) = default;
722 __glibcxx_assert(_M_parent->_M_stream != nullptr);
723 *_M_parent->_M_stream >> _M_parent->_M_object;
734 __glibcxx_assert(_M_parent->_M_stream != nullptr);
735 return _M_parent->_M_object;
739 operator==(const _Iterator& __x, default_sentinel_t)
740 { return __x._M_at_end(); }
743 basic_istream_view* _M_parent = nullptr;
747 { return _M_parent == nullptr || !*_M_parent->_M_stream; }
753 template<typename _Val, typename _CharT, typename _Traits>
754 basic_istream_view<_Val, _CharT, _Traits>
755 istream_view(basic_istream<_CharT, _Traits>& __s)
756 { return basic_istream_view<_Val, _CharT, _Traits>{__s}; }
758 // C++20 24.7 [range.adaptors] Range adaptors
764 // Alias for a type that is conditionally present
765 // (and is an empty type otherwise).
766 // Data members using this alias should use [[no_unique_address]] so that
767 // they take no space when not needed.
768 template<bool _Present, typename _Tp>
769 using __maybe_present_t = conditional_t<_Present, _Tp, _Empty>;
771 // Alias for a type that is conditionally const.
772 template<bool _Const, typename _Tp>
773 using __maybe_const_t = conditional_t<_Const, const _Tp, _Tp>;
775} // namespace __detail
777namespace views::__adaptor
779 // True if the range adaptor _Adaptor can be applied with _Args.
780 template<typename _Adaptor, typename... _Args>
781 concept __adaptor_invocable
782 = requires { std::declval<_Adaptor>()(declval<_Args>()...); };
784 // True if the range adaptor non-closure _Adaptor can be partially applied
786 template<typename _Adaptor, typename... _Args>
787 concept __adaptor_partial_app_viable = (_Adaptor::_S_arity > 1)
788 && (sizeof...(_Args) == _Adaptor::_S_arity - 1)
789 && (constructible_from<decay_t<_Args>, _Args> && ...);
791 template<typename _Adaptor, typename... _Args>
794 template<typename _Lhs, typename _Rhs>
797 // The base class of every range adaptor closure.
799 // The derived class should define the optional static data member
800 // _S_has_simple_call_op to true if the behavior of this adaptor is
801 // independent of the constness/value category of the adaptor object.
802 struct _RangeAdaptorClosure
804 // range | adaptor is equivalent to adaptor(range).
805 template<typename _Self, typename _Range>
806 requires derived_from<remove_cvref_t<_Self>, _RangeAdaptorClosure>
807 && __adaptor_invocable<_Self, _Range>
808 friend constexpr auto
809 operator|(_Range&& __r, _Self&& __self)
810 { return std::forward<_Self>(__self)(std::forward<_Range>(__r)); }
812 // Compose the adaptors __lhs and __rhs into a pipeline, returning
813 // another range adaptor closure object.
814 template<typename _Lhs, typename _Rhs>
815 requires derived_from<_Lhs, _RangeAdaptorClosure>
816 && derived_from<_Rhs, _RangeAdaptorClosure>
817 friend constexpr auto
818 operator|(_Lhs __lhs, _Rhs __rhs)
819 { return _Pipe<_Lhs, _Rhs>{std::move(__lhs), std::move(__rhs)}; }
822 // The base class of every range adaptor non-closure.
824 // The static data member _Derived::_S_arity must contain the total number of
825 // arguments that the adaptor takes, and the class _Derived must introduce
826 // _RangeAdaptor::operator() into the class scope via a using-declaration.
828 // The optional static data member _Derived::_S_has_simple_extra_args should
829 // be defined to true if the behavior of this adaptor is independent of the
830 // constness/value category of the extra arguments. This data member could
831 // also be defined as a variable template parameterized by the types of the
833 template<typename _Derived>
836 // Partially apply the arguments __args to the range adaptor _Derived,
837 // returning a range adaptor closure object.
838 template<typename... _Args>
839 requires __adaptor_partial_app_viable<_Derived, _Args...>
841 operator()(_Args&&... __args) const
843 return _Partial<_Derived, decay_t<_Args>...>{std::forward<_Args>(__args)...};
847 // True if the range adaptor closure _Adaptor has a simple operator(), i.e.
848 // one that's not overloaded according to constness or value category of the
850 template<typename _Adaptor>
851 concept __closure_has_simple_call_op = _Adaptor::_S_has_simple_call_op;
853 // True if the behavior of the range adaptor non-closure _Adaptor is
854 // independent of the value category of its extra arguments _Args.
855 template<typename _Adaptor, typename... _Args>
856 concept __adaptor_has_simple_extra_args = _Adaptor::_S_has_simple_extra_args
857 || _Adaptor::template _S_has_simple_extra_args<_Args...>;
859 // A range adaptor closure that represents partial application of
860 // the range adaptor _Adaptor with arguments _Args.
861 template<typename _Adaptor, typename... _Args>
862 struct _Partial : _RangeAdaptorClosure
864 tuple<_Args...> _M_args;
867 _Partial(_Args... __args)
868 : _M_args(std::move(__args)...)
871 // Invoke _Adaptor with arguments __r, _M_args... according to the
872 // value category of this _Partial object.
873 template<typename _Range>
874 requires __adaptor_invocable<_Adaptor, _Range, const _Args&...>
876 operator()(_Range&& __r) const &
878 auto __forwarder = [&__r] (const auto&... __args) {
879 return _Adaptor{}(std::forward<_Range>(__r), __args...);
881 return std::apply(__forwarder, _M_args);
884 template<typename _Range>
885 requires __adaptor_invocable<_Adaptor, _Range, _Args...>
887 operator()(_Range&& __r) &&
889 auto __forwarder = [&__r] (auto&... __args) {
890 return _Adaptor{}(std::forward<_Range>(__r), std::move(__args)...);
892 return std::apply(__forwarder, _M_args);
895 template<typename _Range>
897 operator()(_Range&& __r) const && = delete;
900 // A lightweight specialization of the above primary template for
901 // the common case where _Adaptor accepts a single extra argument.
902 template<typename _Adaptor, typename _Arg>
903 struct _Partial<_Adaptor, _Arg> : _RangeAdaptorClosure
909 : _M_arg(std::move(__arg))
912 template<typename _Range>
913 requires __adaptor_invocable<_Adaptor, _Range, const _Arg&>
915 operator()(_Range&& __r) const &
916 { return _Adaptor{}(std::forward<_Range>(__r), _M_arg); }
918 template<typename _Range>
919 requires __adaptor_invocable<_Adaptor, _Range, _Arg>
921 operator()(_Range&& __r) &&
922 { return _Adaptor{}(std::forward<_Range>(__r), std::move(_M_arg)); }
924 template<typename _Range>
926 operator()(_Range&& __r) const && = delete;
929 // Partial specialization of the primary template for the case where the extra
930 // arguments of the adaptor can always be safely and efficiently forwarded by
931 // const reference. This lets us get away with a single operator() overload,
932 // which makes overload resolution failure diagnostics more concise.
933 template<typename _Adaptor, typename... _Args>
934 requires __adaptor_has_simple_extra_args<_Adaptor, _Args...>
935 && (is_trivially_copyable_v<_Args> && ...)
936 struct _Partial<_Adaptor, _Args...> : _RangeAdaptorClosure
938 tuple<_Args...> _M_args;
941 _Partial(_Args... __args)
942 : _M_args(std::move(__args)...)
945 // Invoke _Adaptor with arguments __r, const _M_args&... regardless
946 // of the value category of this _Partial object.
947 template<typename _Range>
948 requires __adaptor_invocable<_Adaptor, _Range, const _Args&...>
950 operator()(_Range&& __r) const
952 auto __forwarder = [&__r] (const auto&... __args) {
953 return _Adaptor{}(std::forward<_Range>(__r), __args...);
955 return std::apply(__forwarder, _M_args);
958 static constexpr bool _S_has_simple_call_op = true;
961 // A lightweight specialization of the above template for the common case
962 // where _Adaptor accepts a single extra argument.
963 template<typename _Adaptor, typename _Arg>
964 requires __adaptor_has_simple_extra_args<_Adaptor, _Arg>
965 && is_trivially_copyable_v<_Arg>
966 struct _Partial<_Adaptor, _Arg> : _RangeAdaptorClosure
972 : _M_arg(std::move(__arg))
975 template<typename _Range>
976 requires __adaptor_invocable<_Adaptor, _Range, const _Arg&>
978 operator()(_Range&& __r) const
979 { return _Adaptor{}(std::forward<_Range>(__r), _M_arg); }
981 static constexpr bool _S_has_simple_call_op = true;
984 template<typename _Lhs, typename _Rhs, typename _Range>
985 concept __pipe_invocable
986 = requires { std::declval<_Rhs>()(std::declval<_Lhs>()(std::declval<_Range>())); };
988 // A range adaptor closure that represents composition of the range
989 // adaptor closures _Lhs and _Rhs.
990 template<typename _Lhs, typename _Rhs>
991 struct _Pipe : _RangeAdaptorClosure
993 [[no_unique_address]] _Lhs _M_lhs;
994 [[no_unique_address]] _Rhs _M_rhs;
997 _Pipe(_Lhs __lhs, _Rhs __rhs)
998 : _M_lhs(std::move(__lhs)), _M_rhs(std::move(__rhs))
1001 // Invoke _M_rhs(_M_lhs(__r)) according to the value category of this
1002 // range adaptor closure object.
1003 template<typename _Range>
1004 requires __pipe_invocable<const _Lhs&, const _Rhs&, _Range>
1006 operator()(_Range&& __r) const &
1007 { return _M_rhs(_M_lhs(std::forward<_Range>(__r))); }
1009 template<typename _Range>
1010 requires __pipe_invocable<_Lhs, _Rhs, _Range>
1012 operator()(_Range&& __r) &&
1013 { return std::move(_M_rhs)(std::move(_M_lhs)(std::forward<_Range>(__r))); }
1015 template<typename _Range>
1017 operator()(_Range&& __r) const && = delete;
1020 // A partial specialization of the above primary template for the case where
1021 // both adaptor operands have a simple operator(). This in turn lets us
1022 // implement composition using a single simple operator(), which makes
1023 // overload resolution failure diagnostics more concise.
1024 template<typename _Lhs, typename _Rhs>
1025 requires __closure_has_simple_call_op<_Lhs>
1026 && __closure_has_simple_call_op<_Rhs>
1027 struct _Pipe<_Lhs, _Rhs> : _RangeAdaptorClosure
1029 [[no_unique_address]] _Lhs _M_lhs;
1030 [[no_unique_address]] _Rhs _M_rhs;
1033 _Pipe(_Lhs __lhs, _Rhs __rhs)
1034 : _M_lhs(std::move(__lhs)), _M_rhs(std::move(__rhs))
1037 template<typename _Range>
1038 requires __pipe_invocable<const _Lhs&, const _Rhs&, _Range>
1040 operator()(_Range&& __r) const
1041 { return _M_rhs(_M_lhs(std::forward<_Range>(__r))); }
1043 static constexpr bool _S_has_simple_call_op = true;
1045} // namespace views::__adaptor
1047 template<range _Range> requires is_object_v<_Range>
1048 class ref_view : public view_interface<ref_view<_Range>>
1051 _Range* _M_r = nullptr;
1053 static void _S_fun(_Range&); // not defined
1054 static void _S_fun(_Range&&) = delete;
1058 ref_view() noexcept = default;
1060 template<__detail::__not_same_as<ref_view> _Tp>
1061 requires convertible_to<_Tp, _Range&>
1062 && requires { _S_fun(declval<_Tp>()); }
1065 : _M_r(std::__addressof(static_cast<_Range&>(std::forward<_Tp>(__t))))
1072 constexpr iterator_t<_Range>
1074 { return ranges::begin(*_M_r); }
1076 constexpr sentinel_t<_Range>
1078 { return ranges::end(*_M_r); }
1081 empty() const requires requires { ranges::empty(*_M_r); }
1082 { return ranges::empty(*_M_r); }
1085 size() const requires sized_range<_Range>
1086 { return ranges::size(*_M_r); }
1089 data() const requires contiguous_range<_Range>
1090 { return ranges::data(*_M_r); }
1093 template<typename _Range>
1094 ref_view(_Range&) -> ref_view<_Range>;
1096 template<typename _Tp>
1097 inline constexpr bool enable_borrowed_range<ref_view<_Tp>> = true;
1103 template<typename _Range>
1104 concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; };
1106 template<typename _Range>
1107 concept __can_subrange = requires { subrange{std::declval<_Range>()}; };
1108 } // namespace __detail
1110 struct _All : __adaptor::_RangeAdaptorClosure
1112 template<viewable_range _Range>
1113 requires view<decay_t<_Range>>
1114 || __detail::__can_ref_view<_Range>
1115 || __detail::__can_subrange<_Range>
1117 operator()(_Range&& __r) const
1119 if constexpr (view<decay_t<_Range>>)
1120 return std::forward<_Range>(__r);
1121 else if constexpr (__detail::__can_ref_view<_Range>)
1122 return ref_view{std::forward<_Range>(__r)};
1124 return subrange{std::forward<_Range>(__r)};
1127 static constexpr bool _S_has_simple_call_op = true;
1130 inline constexpr _All all;
1132 template<viewable_range _Range>
1133 using all_t = decltype(all(std::declval<_Range>()));
1134 } // namespace views
1138 template<typename _Tp>
1139 struct __non_propagating_cache
1141 // When _Tp is not an object type (e.g. is a reference type), we make
1142 // __non_propagating_cache<_Tp> empty rather than ill-formed so that
1143 // users can easily conditionally declare data members with this type
1144 // (such as join_view::_M_inner).
1147 template<typename _Tp>
1148 requires is_object_v<_Tp>
1149 struct __non_propagating_cache<_Tp> : protected _Optional_base<_Tp>
1151 __non_propagating_cache() = default;
1154 __non_propagating_cache(const __non_propagating_cache&) noexcept
1158 __non_propagating_cache(__non_propagating_cache&& __other) noexcept
1159 { __other._M_reset(); }
1161 constexpr __non_propagating_cache&
1162 operator=(const __non_propagating_cache& __other) noexcept
1164 if (std::__addressof(__other) != this)
1169 constexpr __non_propagating_cache&
1170 operator=(__non_propagating_cache&& __other) noexcept
1178 operator*() noexcept
1179 { return this->_M_get(); }
1181 constexpr const _Tp&
1182 operator*() const noexcept
1183 { return this->_M_get(); }
1185 template<typename _Iter>
1187 _M_emplace_deref(const _Iter& __i)
1190 // Using _Optional_base::_M_construct to initialize from '*__i'
1191 // would incur an extra move due to the indirection, so we instead
1192 // use placement new directly.
1193 ::new ((void *) std::__addressof(this->_M_payload._M_payload)) _Tp(*__i);
1194 this->_M_payload._M_engaged = true;
1195 return this->_M_get();
1199 template<range _Range>
1200 struct _CachedPosition
1203 _M_has_value() const
1206 constexpr iterator_t<_Range>
1207 _M_get(const _Range&) const
1209 __glibcxx_assert(false);
1210 __builtin_unreachable();
1214 _M_set(const _Range&, const iterator_t<_Range>&) const
1218 template<forward_range _Range>
1219 struct _CachedPosition<_Range>
1220 : protected __non_propagating_cache<iterator_t<_Range>>
1223 _M_has_value() const
1224 { return this->_M_is_engaged(); }
1226 constexpr iterator_t<_Range>
1227 _M_get(const _Range&) const
1229 __glibcxx_assert(_M_has_value());
1234 _M_set(const _Range&, const iterator_t<_Range>& __it)
1236 __glibcxx_assert(!_M_has_value());
1237 std::construct_at(std::__addressof(this->_M_payload._M_payload),
1239 this->_M_payload._M_engaged = true;
1243 template<random_access_range _Range>
1244 requires (sizeof(range_difference_t<_Range>)
1245 <= sizeof(iterator_t<_Range>))
1246 struct _CachedPosition<_Range>
1249 range_difference_t<_Range> _M_offset = -1;
1252 _CachedPosition() = default;
1255 _CachedPosition(const _CachedPosition&) = default;
1258 _CachedPosition(_CachedPosition&& __other) noexcept
1259 { *this = std::move(__other); }
1261 constexpr _CachedPosition&
1262 operator=(const _CachedPosition&) = default;
1264 constexpr _CachedPosition&
1265 operator=(_CachedPosition&& __other) noexcept
1267 // Propagate the cached offset, but invalidate the source.
1268 _M_offset = __other._M_offset;
1269 __other._M_offset = -1;
1274 _M_has_value() const
1275 { return _M_offset >= 0; }
1277 constexpr iterator_t<_Range>
1278 _M_get(_Range& __r) const
1280 __glibcxx_assert(_M_has_value());
1281 return ranges::begin(__r) + _M_offset;
1285 _M_set(_Range& __r, const iterator_t<_Range>& __it)
1287 __glibcxx_assert(!_M_has_value());
1288 _M_offset = __it - ranges::begin(__r);
1291 } // namespace __detail
1295 template<typename _Base>
1296 struct __filter_view_iter_cat
1299 template<forward_range _Base>
1300 struct __filter_view_iter_cat<_Base>
1306 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
1307 if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
1308 return bidirectional_iterator_tag{};
1309 else if constexpr (derived_from<_Cat, forward_iterator_tag>)
1310 return forward_iterator_tag{};
1315 using iterator_category = decltype(_S_iter_cat());
1317 } // namespace __detail
1319 template<input_range _Vp,
1320 indirect_unary_predicate<iterator_t<_Vp>> _Pred>
1321 requires view<_Vp> && is_object_v<_Pred>
1322 class filter_view : public view_interface<filter_view<_Vp, _Pred>>
1327 struct _Iterator : __detail::__filter_view_iter_cat<_Vp>
1330 static constexpr auto
1333 if constexpr (bidirectional_range<_Vp>)
1334 return bidirectional_iterator_tag{};
1335 else if constexpr (forward_range<_Vp>)
1336 return forward_iterator_tag{};
1338 return input_iterator_tag{};
1343 using _Vp_iter = iterator_t<_Vp>;
1345 _Vp_iter _M_current = _Vp_iter();
1346 filter_view* _M_parent = nullptr;
1349 using iterator_concept = decltype(_S_iter_concept());
1350 // iterator_category defined in __filter_view_iter_cat
1351 using value_type = range_value_t<_Vp>;
1352 using difference_type = range_difference_t<_Vp>;
1354 _Iterator() requires default_initializable<_Vp_iter> = default;
1357 _Iterator(filter_view* __parent, _Vp_iter __current)
1358 : _M_current(std::move(__current)),
1362 constexpr const _Vp_iter&
1363 base() const & noexcept
1364 { return _M_current; }
1368 { return std::move(_M_current); }
1370 constexpr range_reference_t<_Vp>
1372 { return *_M_current; }
1376 requires __detail::__has_arrow<_Vp_iter>
1377 && copyable<_Vp_iter>
1378 { return _M_current; }
1380 constexpr _Iterator&
1383 _M_current = ranges::find_if(std::move(++_M_current),
1384 ranges::end(_M_parent->_M_base),
1385 std::ref(*_M_parent->_M_pred));
1394 operator++(int) requires forward_range<_Vp>
1401 constexpr _Iterator&
1402 operator--() requires bidirectional_range<_Vp>
1406 while (!std::__invoke(*_M_parent->_M_pred, *_M_current));
1411 operator--(int) requires bidirectional_range<_Vp>
1418 friend constexpr bool
1419 operator==(const _Iterator& __x, const _Iterator& __y)
1420 requires equality_comparable<_Vp_iter>
1421 { return __x._M_current == __y._M_current; }
1423 friend constexpr range_rvalue_reference_t<_Vp>
1424 iter_move(const _Iterator& __i)
1425 noexcept(noexcept(ranges::iter_move(__i._M_current)))
1426 { return ranges::iter_move(__i._M_current); }
1428 friend constexpr void
1429 iter_swap(const _Iterator& __x, const _Iterator& __y)
1430 noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
1431 requires indirectly_swappable<_Vp_iter>
1432 { ranges::iter_swap(__x._M_current, __y._M_current); }
1438 sentinel_t<_Vp> _M_end = sentinel_t<_Vp>();
1441 __equal(const _Iterator& __i) const
1442 { return __i._M_current == _M_end; }
1445 _Sentinel() = default;
1448 _Sentinel(filter_view* __parent)
1449 : _M_end(ranges::end(__parent->_M_base))
1452 constexpr sentinel_t<_Vp>
1456 friend constexpr bool
1457 operator==(const _Iterator& __x, const _Sentinel& __y)
1458 { return __y.__equal(__x); }
1461 [[no_unique_address]] __detail::__box<_Pred> _M_pred;
1462 [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
1463 _Vp _M_base = _Vp();
1466 filter_view() requires default_initializable<_Vp> = default;
1469 filter_view(_Vp __base, _Pred __pred)
1470 : _M_pred(std::move(__pred)), _M_base(std::move(__base))
1474 base() const& requires copy_constructible<_Vp>
1479 { return std::move(_M_base); }
1481 constexpr const _Pred&
1483 { return *_M_pred; }
1488 if (_M_cached_begin._M_has_value())
1489 return {this, _M_cached_begin._M_get(_M_base)};
1491 __glibcxx_assert(_M_pred.has_value());
1492 auto __it = ranges::find_if(ranges::begin(_M_base),
1493 ranges::end(_M_base),
1494 std::ref(*_M_pred));
1495 _M_cached_begin._M_set(_M_base, __it);
1496 return {this, std::move(__it)};
1502 if constexpr (common_range<_Vp>)
1503 return _Iterator{this, ranges::end(_M_base)};
1505 return _Sentinel{this};
1509 template<typename _Range, typename _Pred>
1510 filter_view(_Range&&, _Pred) -> filter_view<views::all_t<_Range>, _Pred>;
1516 template<typename _Range, typename _Pred>
1517 concept __can_filter_view
1518 = requires { filter_view(std::declval<_Range>(), std::declval<_Pred>()); };
1519 } // namespace __detail
1521 struct _Filter : __adaptor::_RangeAdaptor<_Filter>
1523 template<viewable_range _Range, typename _Pred>
1524 requires __detail::__can_filter_view<_Range, _Pred>
1526 operator()(_Range&& __r, _Pred&& __p) const
1528 return filter_view(std::forward<_Range>(__r), std::forward<_Pred>(__p));
1531 using _RangeAdaptor<_Filter>::operator();
1532 static constexpr int _S_arity = 2;
1533 static constexpr bool _S_has_simple_extra_args = true;
1536 inline constexpr _Filter filter;
1537 } // namespace views
1539 template<input_range _Vp, copy_constructible _Fp>
1540 requires view<_Vp> && is_object_v<_Fp>
1541 && regular_invocable<_Fp&, range_reference_t<_Vp>>
1542 && std::__detail::__can_reference<invoke_result_t<_Fp&,
1543 range_reference_t<_Vp>>>
1544 class transform_view : public view_interface<transform_view<_Vp, _Fp>>
1547 template<bool _Const>
1548 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
1550 template<bool _Const>
1554 template<bool _Const>
1555 requires forward_range<_Base<_Const>>
1556 struct __iter_cat<_Const>
1562 using _Base = transform_view::_Base<_Const>;
1563 using _Res = invoke_result_t<_Fp&, range_reference_t<_Base>>;
1564 if constexpr (is_lvalue_reference_v<_Res>)
1567 = typename iterator_traits<iterator_t<_Base>>::iterator_category;
1568 if constexpr (derived_from<_Cat, contiguous_iterator_tag>)
1569 return random_access_iterator_tag{};
1574 return input_iterator_tag{};
1577 using iterator_category = decltype(_S_iter_cat());
1580 template<bool _Const>
1583 template<bool _Const>
1584 struct _Iterator : __iter_cat<_Const>
1587 using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
1588 using _Base = transform_view::_Base<_Const>;
1593 if constexpr (random_access_range<_Base>)
1594 return random_access_iterator_tag{};
1595 else if constexpr (bidirectional_range<_Base>)
1596 return bidirectional_iterator_tag{};
1597 else if constexpr (forward_range<_Base>)
1598 return forward_iterator_tag{};
1600 return input_iterator_tag{};
1603 using _Base_iter = iterator_t<_Base>;
1605 _Base_iter _M_current = _Base_iter();
1606 _Parent* _M_parent = nullptr;
1609 using iterator_concept = decltype(_S_iter_concept());
1610 // iterator_category defined in __transform_view_iter_cat
1612 = remove_cvref_t<invoke_result_t<_Fp&, range_reference_t<_Base>>>;
1613 using difference_type = range_difference_t<_Base>;
1615 _Iterator() requires default_initializable<_Base_iter> = default;
1618 _Iterator(_Parent* __parent, _Base_iter __current)
1619 : _M_current(std::move(__current)),
1624 _Iterator(_Iterator<!_Const> __i)
1626 && convertible_to<iterator_t<_Vp>, _Base_iter>
1627 : _M_current(std::move(__i._M_current)), _M_parent(__i._M_parent)
1630 constexpr const _Base_iter&
1631 base() const & noexcept
1632 { return _M_current; }
1634 constexpr _Base_iter
1636 { return std::move(_M_current); }
1638 constexpr decltype(auto)
1640 noexcept(noexcept(std::__invoke(*_M_parent->_M_fun, *_M_current)))
1641 { return std::__invoke(*_M_parent->_M_fun, *_M_current); }
1643 constexpr _Iterator&
1655 operator++(int) requires forward_range<_Base>
1662 constexpr _Iterator&
1663 operator--() requires bidirectional_range<_Base>
1670 operator--(int) requires bidirectional_range<_Base>
1677 constexpr _Iterator&
1678 operator+=(difference_type __n) requires random_access_range<_Base>
1684 constexpr _Iterator&
1685 operator-=(difference_type __n) requires random_access_range<_Base>
1691 constexpr decltype(auto)
1692 operator[](difference_type __n) const
1693 requires random_access_range<_Base>
1694 { return std::__invoke(*_M_parent->_M_fun, _M_current[__n]); }
1696 friend constexpr bool
1697 operator==(const _Iterator& __x, const _Iterator& __y)
1698 requires equality_comparable<_Base_iter>
1699 { return __x._M_current == __y._M_current; }
1701 friend constexpr bool
1702 operator<(const _Iterator& __x, const _Iterator& __y)
1703 requires random_access_range<_Base>
1704 { return __x._M_current < __y._M_current; }
1706 friend constexpr bool
1707 operator>(const _Iterator& __x, const _Iterator& __y)
1708 requires random_access_range<_Base>
1709 { return __y < __x; }
1711 friend constexpr bool
1712 operator<=(const _Iterator& __x, const _Iterator& __y)
1713 requires random_access_range<_Base>
1714 { return !(__y < __x); }
1716 friend constexpr bool
1717 operator>=(const _Iterator& __x, const _Iterator& __y)
1718 requires random_access_range<_Base>
1719 { return !(__x < __y); }
1721#ifdef __cpp_lib_three_way_comparison
1722 friend constexpr auto
1723 operator<=>(const _Iterator& __x, const _Iterator& __y)
1724 requires random_access_range<_Base>
1725 && three_way_comparable<_Base_iter>
1726 { return __x._M_current <=> __y._M_current; }
1729 friend constexpr _Iterator
1730 operator+(_Iterator __i, difference_type __n)
1731 requires random_access_range<_Base>
1732 { return {__i._M_parent, __i._M_current + __n}; }
1734 friend constexpr _Iterator
1735 operator+(difference_type __n, _Iterator __i)
1736 requires random_access_range<_Base>
1737 { return {__i._M_parent, __i._M_current + __n}; }
1739 friend constexpr _Iterator
1740 operator-(_Iterator __i, difference_type __n)
1741 requires random_access_range<_Base>
1742 { return {__i._M_parent, __i._M_current - __n}; }
1744 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1745 // 3483. transform_view::iterator's difference is overconstrained
1746 friend constexpr difference_type
1747 operator-(const _Iterator& __x, const _Iterator& __y)
1748 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
1749 { return __x._M_current - __y._M_current; }
1751 friend constexpr decltype(auto)
1752 iter_move(const _Iterator& __i) noexcept(noexcept(*__i))
1754 if constexpr (is_lvalue_reference_v<decltype(*__i)>)
1755 return std::move(*__i);
1760 friend _Iterator<!_Const>;
1761 template<bool> friend struct _Sentinel;
1764 template<bool _Const>
1768 using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
1769 using _Base = transform_view::_Base<_Const>;
1771 template<bool _Const2>
1773 __distance_from(const _Iterator<_Const2>& __i) const
1774 { return _M_end - __i._M_current; }
1776 template<bool _Const2>
1778 __equal(const _Iterator<_Const2>& __i) const
1779 { return __i._M_current == _M_end; }
1781 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
1784 _Sentinel() = default;
1787 _Sentinel(sentinel_t<_Base> __end)
1792 _Sentinel(_Sentinel<!_Const> __i)
1794 && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
1795 : _M_end(std::move(__i._M_end))
1798 constexpr sentinel_t<_Base>
1802 template<bool _Const2>
1803 requires sentinel_for<sentinel_t<_Base>,
1804 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
1805 friend constexpr bool
1806 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
1807 { return __y.__equal(__x); }
1809 template<bool _Const2,
1810 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
1811 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
1812 friend constexpr range_difference_t<_Base2>
1813 operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
1814 { return -__y.__distance_from(__x); }
1816 template<bool _Const2,
1817 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
1818 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
1819 friend constexpr range_difference_t<_Base2>
1820 operator-(const _Sentinel& __y, const _Iterator<_Const2>& __x)
1821 { return __y.__distance_from(__x); }
1823 friend _Sentinel<!_Const>;
1826 [[no_unique_address]] __detail::__box<_Fp> _M_fun;
1827 _Vp _M_base = _Vp();
1830 transform_view() requires default_initializable<_Vp> = default;
1833 transform_view(_Vp __base, _Fp __fun)
1834 : _M_fun(std::move(__fun)), _M_base(std::move(__base))
1838 base() const& requires copy_constructible<_Vp>
1839 { return _M_base ; }
1843 { return std::move(_M_base); }
1845 constexpr _Iterator<false>
1847 { return _Iterator<false>{this, ranges::begin(_M_base)}; }
1849 constexpr _Iterator<true>
1851 requires range<const _Vp>
1852 && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
1853 { return _Iterator<true>{this, ranges::begin(_M_base)}; }
1855 constexpr _Sentinel<false>
1857 { return _Sentinel<false>{ranges::end(_M_base)}; }
1859 constexpr _Iterator<false>
1860 end() requires common_range<_Vp>
1861 { return _Iterator<false>{this, ranges::end(_M_base)}; }
1863 constexpr _Sentinel<true>
1865 requires range<const _Vp>
1866 && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
1867 { return _Sentinel<true>{ranges::end(_M_base)}; }
1869 constexpr _Iterator<true>
1871 requires common_range<const _Vp>
1872 && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
1873 { return _Iterator<true>{this, ranges::end(_M_base)}; }
1876 size() requires sized_range<_Vp>
1877 { return ranges::size(_M_base); }
1880 size() const requires sized_range<const _Vp>
1881 { return ranges::size(_M_base); }
1884 template<typename _Range, typename _Fp>
1885 transform_view(_Range&&, _Fp) -> transform_view<views::all_t<_Range>, _Fp>;
1891 template<typename _Range, typename _Fp>
1892 concept __can_transform_view
1893 = requires { transform_view(std::declval<_Range>(), std::declval<_Fp>()); };
1894 } // namespace __detail
1896 struct _Transform : __adaptor::_RangeAdaptor<_Transform>
1898 template<viewable_range _Range, typename _Fp>
1899 requires __detail::__can_transform_view<_Range, _Fp>
1901 operator()(_Range&& __r, _Fp&& __f) const
1903 return transform_view(std::forward<_Range>(__r), std::forward<_Fp>(__f));
1906 using _RangeAdaptor<_Transform>::operator();
1907 static constexpr int _S_arity = 2;
1908 static constexpr bool _S_has_simple_extra_args = true;
1911 inline constexpr _Transform transform;
1912 } // namespace views
1915 class take_view : public view_interface<take_view<_Vp>>
1918 template<bool _Const>
1919 using _CI = counted_iterator<
1920 iterator_t<__detail::__maybe_const_t<_Const, _Vp>>>;
1922 template<bool _Const>
1926 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
1927 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
1930 _Sentinel() = default;
1933 _Sentinel(sentinel_t<_Base> __end)
1938 _Sentinel(_Sentinel<!_Const> __s)
1939 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
1940 : _M_end(std::move(__s._M_end))
1943 constexpr sentinel_t<_Base>
1947 friend constexpr bool
1948 operator==(const _CI<_Const>& __y, const _Sentinel& __x)
1949 { return __y.count() == 0 || __y.base() == __x._M_end; }
1951 template<bool _OtherConst = !_Const,
1952 typename _Base2 = __detail::__maybe_const_t<_OtherConst, _Vp>>
1953 requires sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
1954 friend constexpr bool
1955 operator==(const _CI<_OtherConst>& __y, const _Sentinel& __x)
1956 { return __y.count() == 0 || __y.base() == __x._M_end; }
1958 friend _Sentinel<!_Const>;
1961 range_difference_t<_Vp> _M_count = 0;
1962 _Vp _M_base = _Vp();
1965 take_view() requires default_initializable<_Vp> = default;
1968 take_view(_Vp base, range_difference_t<_Vp> __count)
1969 : _M_count(std::move(__count)), _M_base(std::move(base))
1973 base() const& requires copy_constructible<_Vp>
1978 { return std::move(_M_base); }
1981 begin() requires (!__detail::__simple_view<_Vp>)
1983 if constexpr (sized_range<_Vp>)
1985 if constexpr (random_access_range<_Vp>)
1986 return ranges::begin(_M_base);
1990 return counted_iterator(ranges::begin(_M_base), __sz);
1994 return counted_iterator(ranges::begin(_M_base), _M_count);
1998 begin() const requires range<const _Vp>
2000 if constexpr (sized_range<const _Vp>)
2002 if constexpr (random_access_range<const _Vp>)
2003 return ranges::begin(_M_base);
2007 return counted_iterator(ranges::begin(_M_base), __sz);
2011 return counted_iterator(ranges::begin(_M_base), _M_count);
2015 end() requires (!__detail::__simple_view<_Vp>)
2017 if constexpr (sized_range<_Vp>)
2019 if constexpr (random_access_range<_Vp>)
2020 return ranges::begin(_M_base) + size();
2022 return default_sentinel;
2025 return _Sentinel<false>{ranges::end(_M_base)};
2029 end() const requires range<const _Vp>
2031 if constexpr (sized_range<const _Vp>)
2033 if constexpr (random_access_range<const _Vp>)
2034 return ranges::begin(_M_base) + size();
2036 return default_sentinel;
2039 return _Sentinel<true>{ranges::end(_M_base)};
2043 size() requires sized_range<_Vp>
2045 auto __n = ranges::size(_M_base);
2046 return std::min(__n, static_cast<decltype(__n)>(_M_count));
2050 size() const requires sized_range<const _Vp>
2052 auto __n = ranges::size(_M_base);
2053 return std::min(__n, static_cast<decltype(__n)>(_M_count));
2057 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2058 // 3447. Deduction guides for take_view and drop_view have different
2060 template<typename _Range>
2061 take_view(_Range&&, range_difference_t<_Range>)
2062 -> take_view<views::all_t<_Range>>;
2064 template<typename _Tp>
2065 inline constexpr bool enable_borrowed_range<take_view<_Tp>>
2066 = enable_borrowed_range<_Tp>;
2072 template<typename _Range, typename _Tp>
2073 concept __can_take_view
2074 = requires { take_view(std::declval<_Range>(), std::declval<_Tp>()); };
2075 } // namespace __detail
2077 struct _Take : __adaptor::_RangeAdaptor<_Take>
2079 template<viewable_range _Range, typename _Tp>
2080 requires __detail::__can_take_view<_Range, _Tp>
2082 operator()(_Range&& __r, _Tp&& __n) const
2084 return take_view(std::forward<_Range>(__r), std::forward<_Tp>(__n));
2087 using _RangeAdaptor<_Take>::operator();
2088 static constexpr int _S_arity = 2;
2089 // The count argument of views::take is not always simple -- it can be
2090 // e.g. a move-only class that's implicitly convertible to the difference
2091 // type. But an integer-like count argument is surely simple.
2092 template<typename _Tp>
2093 static constexpr bool _S_has_simple_extra_args
2094 = ranges::__detail::__is_integer_like<_Tp>;
2097 inline constexpr _Take take;
2098 } // namespace views
2100 template<view _Vp, typename _Pred>
2101 requires input_range<_Vp> && is_object_v<_Pred>
2102 && indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
2103 class take_while_view : public view_interface<take_while_view<_Vp, _Pred>>
2105 template<bool _Const>
2109 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2111 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
2112 const _Pred* _M_pred = nullptr;
2115 _Sentinel() = default;
2118 _Sentinel(sentinel_t<_Base> __end, const _Pred* __pred)
2119 : _M_end(__end), _M_pred(__pred)
2123 _Sentinel(_Sentinel<!_Const> __s)
2124 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
2125 : _M_end(__s._M_end), _M_pred(__s._M_pred)
2128 constexpr sentinel_t<_Base>
2129 base() const { return _M_end; }
2131 friend constexpr bool
2132 operator==(const iterator_t<_Base>& __x, const _Sentinel& __y)
2133 { return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); }
2135 template<bool _OtherConst = !_Const,
2136 typename _Base2 = __detail::__maybe_const_t<_OtherConst, _Vp>>
2137 requires sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
2138 friend constexpr bool
2139 operator==(const iterator_t<_Base2>& __x, const _Sentinel& __y)
2140 { return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); }
2142 friend _Sentinel<!_Const>;
2145 [[no_unique_address]] __detail::__box<_Pred> _M_pred;
2146 _Vp _M_base = _Vp();
2149 take_while_view() requires default_initializable<_Vp> = default;
2152 take_while_view(_Vp base, _Pred __pred)
2153 : _M_pred(std::move(__pred)), _M_base(std::move(base))
2157 base() const& requires copy_constructible<_Vp>
2162 { return std::move(_M_base); }
2164 constexpr const _Pred&
2166 { return *_M_pred; }
2169 begin() requires (!__detail::__simple_view<_Vp>)
2170 { return ranges::begin(_M_base); }
2173 begin() const requires range<const _Vp>
2174 && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>
2175 { return ranges::begin(_M_base); }
2178 end() requires (!__detail::__simple_view<_Vp>)
2179 { return _Sentinel<false>(ranges::end(_M_base),
2180 std::__addressof(*_M_pred)); }
2183 end() const requires range<const _Vp>
2184 && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>
2185 { return _Sentinel<true>(ranges::end(_M_base),
2186 std::__addressof(*_M_pred)); }
2189 template<typename _Range, typename _Pred>
2190 take_while_view(_Range&&, _Pred)
2191 -> take_while_view<views::all_t<_Range>, _Pred>;
2197 template<typename _Range, typename _Pred>
2198 concept __can_take_while_view
2199 = requires { take_while_view(std::declval<_Range>(), std::declval<_Pred>()); };
2200 } // namespace __detail
2202 struct _TakeWhile : __adaptor::_RangeAdaptor<_TakeWhile>
2204 template<viewable_range _Range, typename _Pred>
2205 requires __detail::__can_take_while_view<_Range, _Pred>
2207 operator()(_Range&& __r, _Pred&& __p) const
2209 return take_while_view(std::forward<_Range>(__r), std::forward<_Pred>(__p));
2212 using _RangeAdaptor<_TakeWhile>::operator();
2213 static constexpr int _S_arity = 2;
2214 static constexpr bool _S_has_simple_extra_args = true;
2217 inline constexpr _TakeWhile take_while;
2218 } // namespace views
2221 class drop_view : public view_interface<drop_view<_Vp>>
2224 range_difference_t<_Vp> _M_count = 0;
2225 _Vp _M_base = _Vp();
2227 // ranges::next(begin(base), count, end(base)) is O(1) if _Vp satisfies
2228 // both random_access_range and sized_range. Otherwise, cache its result.
2229 static constexpr bool _S_needs_cached_begin
2230 = !(random_access_range<const _Vp> && sized_range<const _Vp>);
2231 [[no_unique_address]]
2232 __detail::__maybe_present_t<_S_needs_cached_begin,
2233 __detail::_CachedPosition<_Vp>>
2237 drop_view() requires default_initializable<_Vp> = default;
2240 drop_view(_Vp __base, range_difference_t<_Vp> __count)
2241 : _M_count(__count), _M_base(std::move(__base))
2242 { __glibcxx_assert(__count >= 0); }
2245 base() const& requires copy_constructible<_Vp>
2250 { return std::move(_M_base); }
2252 // This overload is disabled for simple views with constant-time begin().
2255 requires (!(__detail::__simple_view<_Vp>
2256 && random_access_range<const _Vp>
2257 && sized_range<const _Vp>))
2259 if constexpr (_S_needs_cached_begin)
2260 if (_M_cached_begin._M_has_value())
2261 return _M_cached_begin._M_get(_M_base);
2263 auto __it = ranges::next(ranges::begin(_M_base),
2264 _M_count, ranges::end(_M_base));
2265 if constexpr (_S_needs_cached_begin)
2266 _M_cached_begin._M_set(_M_base, __it);
2270 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2271 // 3482. drop_view's const begin should additionally require sized_range
2274 requires random_access_range<const _Vp> && sized_range<const _Vp>
2276 return ranges::next(ranges::begin(_M_base), _M_count,
2277 ranges::end(_M_base));
2281 end() requires (!__detail::__simple_view<_Vp>)
2282 { return ranges::end(_M_base); }
2285 end() const requires range<const _Vp>
2286 { return ranges::end(_M_base); }
2289 size() requires sized_range<_Vp>
2291 const auto __s = ranges::size(_M_base);
2292 const auto __c = static_cast<decltype(__s)>(_M_count);
2293 return __s < __c ? 0 : __s - __c;
2297 size() const requires sized_range<const _Vp>
2299 const auto __s = ranges::size(_M_base);
2300 const auto __c = static_cast<decltype(__s)>(_M_count);
2301 return __s < __c ? 0 : __s - __c;
2305 template<typename _Range>
2306 drop_view(_Range&&, range_difference_t<_Range>)
2307 -> drop_view<views::all_t<_Range>>;
2309 template<typename _Tp>
2310 inline constexpr bool enable_borrowed_range<drop_view<_Tp>>
2311 = enable_borrowed_range<_Tp>;
2317 template<typename _Range, typename _Tp>
2318 concept __can_drop_view
2319 = requires { drop_view(std::declval<_Range>(), std::declval<_Tp>()); };
2320 } // namespace __detail
2322 struct _Drop : __adaptor::_RangeAdaptor<_Drop>
2324 template<viewable_range _Range, typename _Tp>
2325 requires __detail::__can_drop_view<_Range, _Tp>
2327 operator()(_Range&& __r, _Tp&& __n) const
2329 return drop_view(std::forward<_Range>(__r), std::forward<_Tp>(__n));
2332 using _RangeAdaptor<_Drop>::operator();
2333 static constexpr int _S_arity = 2;
2334 template<typename _Tp>
2335 static constexpr bool _S_has_simple_extra_args
2336 = _Take::_S_has_simple_extra_args<_Tp>;
2339 inline constexpr _Drop drop;
2340 } // namespace views
2342 template<view _Vp, typename _Pred>
2343 requires input_range<_Vp> && is_object_v<_Pred>
2344 && indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
2345 class drop_while_view : public view_interface<drop_while_view<_Vp, _Pred>>
2348 [[no_unique_address]] __detail::__box<_Pred> _M_pred;
2349 [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
2350 _Vp _M_base = _Vp();
2353 drop_while_view() requires default_initializable<_Vp> = default;
2356 drop_while_view(_Vp __base, _Pred __pred)
2357 : _M_pred(std::move(__pred)), _M_base(std::move(__base))
2361 base() const& requires copy_constructible<_Vp>
2366 { return std::move(_M_base); }
2368 constexpr const _Pred&
2370 { return *_M_pred; }
2375 if (_M_cached_begin._M_has_value())
2376 return _M_cached_begin._M_get(_M_base);
2378 __glibcxx_assert(_M_pred.has_value());
2379 auto __it = ranges::find_if_not(ranges::begin(_M_base),
2380 ranges::end(_M_base),
2381 std::cref(*_M_pred));
2382 _M_cached_begin._M_set(_M_base, __it);
2388 { return ranges::end(_M_base); }
2391 template<typename _Range, typename _Pred>
2392 drop_while_view(_Range&&, _Pred)
2393 -> drop_while_view<views::all_t<_Range>, _Pred>;
2395 template<typename _Tp, typename _Pred>
2396 inline constexpr bool enable_borrowed_range<drop_while_view<_Tp, _Pred>>
2397 = enable_borrowed_range<_Tp>;
2403 template<typename _Range, typename _Pred>
2404 concept __can_drop_while_view
2405 = requires { drop_while_view(std::declval<_Range>(), std::declval<_Pred>()); };
2406 } // namespace __detail
2408 struct _DropWhile : __adaptor::_RangeAdaptor<_DropWhile>
2410 template<viewable_range _Range, typename _Pred>
2411 requires __detail::__can_drop_while_view<_Range, _Pred>
2413 operator()(_Range&& __r, _Pred&& __p) const
2415 return drop_while_view(std::forward<_Range>(__r),
2416 std::forward<_Pred>(__p));
2419 using _RangeAdaptor<_DropWhile>::operator();
2420 static constexpr int _S_arity = 2;
2421 static constexpr bool _S_has_simple_extra_args = true;
2424 inline constexpr _DropWhile drop_while;
2425 } // namespace views
2427 template<input_range _Vp>
2428 requires view<_Vp> && input_range<range_reference_t<_Vp>>
2429 class join_view : public view_interface<join_view<_Vp>>
2432 using _InnerRange = range_reference_t<_Vp>;
2434 template<bool _Const>
2435 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2437 template<bool _Const>
2438 using _Outer_iter = iterator_t<_Base<_Const>>;
2440 template<bool _Const>
2441 using _Inner_iter = iterator_t<range_reference_t<_Base<_Const>>>;
2443 template<bool _Const>
2444 static constexpr bool _S_ref_is_glvalue
2445 = is_reference_v<range_reference_t<_Base<_Const>>>;
2447 template<bool _Const>
2451 template<bool _Const>
2452 requires _S_ref_is_glvalue<_Const>
2453 && forward_range<_Base<_Const>>
2454 && forward_range<range_reference_t<_Base<_Const>>>
2455 struct __iter_cat<_Const>
2458 static constexpr auto
2461 using _Outer_iter = join_view::_Outer_iter<_Const>;
2462 using _Inner_iter = join_view::_Inner_iter<_Const>;
2463 using _OuterCat = typename iterator_traits<_Outer_iter>::iterator_category;
2464 using _InnerCat = typename iterator_traits<_Inner_iter>::iterator_category;
2465 if constexpr (derived_from<_OuterCat, bidirectional_iterator_tag>
2466 && derived_from<_InnerCat, bidirectional_iterator_tag>
2467 && common_range<range_reference_t<_Base<_Const>>>)
2468 return bidirectional_iterator_tag{};
2469 else if constexpr (derived_from<_OuterCat, forward_iterator_tag>
2470 && derived_from<_InnerCat, forward_iterator_tag>)
2471 return forward_iterator_tag{};
2473 return input_iterator_tag{};
2476 using iterator_category = decltype(_S_iter_cat());
2479 template<bool _Const>
2482 template<bool _Const>
2483 struct _Iterator : __iter_cat<_Const>
2486 using _Parent = __detail::__maybe_const_t<_Const, join_view>;
2487 using _Base = join_view::_Base<_Const>;
2489 static constexpr bool _S_ref_is_glvalue
2490 = join_view::_S_ref_is_glvalue<_Const>;
2495 auto __update_inner = [this] (const iterator_t<_Base>& __x) -> auto&& {
2496 if constexpr (_S_ref_is_glvalue)
2499 return _M_parent->_M_inner._M_emplace_deref(__x);
2502 for (; _M_outer != ranges::end(_M_parent->_M_base); ++_M_outer)
2504 auto&& __inner = __update_inner(_M_outer);
2505 _M_inner = ranges::begin(__inner);
2506 if (_M_inner != ranges::end(__inner))
2510 if constexpr (_S_ref_is_glvalue)
2511 _M_inner = _Inner_iter();
2514 static constexpr auto
2517 if constexpr (_S_ref_is_glvalue
2518 && bidirectional_range<_Base>
2519 && bidirectional_range<range_reference_t<_Base>>
2520 && common_range<range_reference_t<_Base>>)
2521 return bidirectional_iterator_tag{};
2522 else if constexpr (_S_ref_is_glvalue
2523 && forward_range<_Base>
2524 && forward_range<range_reference_t<_Base>>)
2525 return forward_iterator_tag{};
2527 return input_iterator_tag{};
2530 using _Outer_iter = join_view::_Outer_iter<_Const>;
2531 using _Inner_iter = join_view::_Inner_iter<_Const>;
2533 _Outer_iter _M_outer = _Outer_iter();
2534 _Inner_iter _M_inner = _Inner_iter();
2535 _Parent* _M_parent = nullptr;
2538 using iterator_concept = decltype(_S_iter_concept());
2539 // iterator_category defined in __join_view_iter_cat
2540 using value_type = range_value_t<range_reference_t<_Base>>;
2541 using difference_type
2542 = common_type_t<range_difference_t<_Base>,
2543 range_difference_t<range_reference_t<_Base>>>;
2545 _Iterator() requires (default_initializable<_Outer_iter>
2546 && default_initializable<_Inner_iter>)
2550 _Iterator(_Parent* __parent, _Outer_iter __outer)
2551 : _M_outer(std::move(__outer)),
2556 _Iterator(_Iterator<!_Const> __i)
2558 && convertible_to<iterator_t<_Vp>, _Outer_iter>
2559 && convertible_to<iterator_t<_InnerRange>, _Inner_iter>
2560 : _M_outer(std::move(__i._M_outer)), _M_inner(std::move(__i._M_inner)),
2561 _M_parent(__i._M_parent)
2564 constexpr decltype(auto)
2566 { return *_M_inner; }
2568 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2569 // 3500. join_view::iterator::operator->() is bogus
2570 constexpr _Inner_iter
2572 requires __detail::__has_arrow<_Inner_iter>
2573 && copyable<_Inner_iter>
2574 { return _M_inner; }
2576 constexpr _Iterator&
2579 auto&& __inner_range = [this] () -> auto&& {
2580 if constexpr (_S_ref_is_glvalue)
2583 return *_M_parent->_M_inner;
2585 if (++_M_inner == ranges::end(__inner_range))
2599 requires _S_ref_is_glvalue && forward_range<_Base>
2600 && forward_range<range_reference_t<_Base>>
2607 constexpr _Iterator&
2609 requires _S_ref_is_glvalue && bidirectional_range<_Base>
2610 && bidirectional_range<range_reference_t<_Base>>
2611 && common_range<range_reference_t<_Base>>
2613 if (_M_outer == ranges::end(_M_parent->_M_base))
2614 _M_inner = ranges::end(*--_M_outer);
2615 while (_M_inner == ranges::begin(*_M_outer))
2616 _M_inner = ranges::end(*--_M_outer);
2623 requires _S_ref_is_glvalue && bidirectional_range<_Base>
2624 && bidirectional_range<range_reference_t<_Base>>
2625 && common_range<range_reference_t<_Base>>
2632 friend constexpr bool
2633 operator==(const _Iterator& __x, const _Iterator& __y)
2634 requires _S_ref_is_glvalue
2635 && equality_comparable<_Outer_iter>
2636 && equality_comparable<_Inner_iter>
2638 return (__x._M_outer == __y._M_outer
2639 && __x._M_inner == __y._M_inner);
2642 friend constexpr decltype(auto)
2643 iter_move(const _Iterator& __i)
2644 noexcept(noexcept(ranges::iter_move(__i._M_inner)))
2645 { return ranges::iter_move(__i._M_inner); }
2647 friend constexpr void
2648 iter_swap(const _Iterator& __x, const _Iterator& __y)
2649 noexcept(noexcept(ranges::iter_swap(__x._M_inner, __y._M_inner)))
2650 requires indirectly_swappable<_Inner_iter>
2651 { return ranges::iter_swap(__x._M_inner, __y._M_inner); }
2653 friend _Iterator<!_Const>;
2654 template<bool> friend struct _Sentinel;
2657 template<bool _Const>
2661 using _Parent = __detail::__maybe_const_t<_Const, join_view>;
2662 using _Base = join_view::_Base<_Const>;
2664 template<bool _Const2>
2666 __equal(const _Iterator<_Const2>& __i) const
2667 { return __i._M_outer == _M_end; }
2669 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
2672 _Sentinel() = default;
2675 _Sentinel(_Parent* __parent)
2676 : _M_end(ranges::end(__parent->_M_base))
2680 _Sentinel(_Sentinel<!_Const> __s)
2681 requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
2682 : _M_end(std::move(__s._M_end))
2685 template<bool _Const2>
2686 requires sentinel_for<sentinel_t<_Base>,
2687 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
2688 friend constexpr bool
2689 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
2690 { return __y.__equal(__x); }
2692 friend _Sentinel<!_Const>;
2695 [[no_unique_address]]
2696 __detail::__non_propagating_cache<remove_cv_t<_InnerRange>> _M_inner;
2697 _Vp _M_base = _Vp();
2700 join_view() requires default_initializable<_Vp> = default;
2703 join_view(_Vp __base)
2704 : _M_base(std::move(__base))
2708 base() const& requires copy_constructible<_Vp>
2713 { return std::move(_M_base); }
2718 constexpr bool __use_const
2719 = (__detail::__simple_view<_Vp>
2720 && is_reference_v<range_reference_t<_Vp>>);
2721 return _Iterator<__use_const>{this, ranges::begin(_M_base)};
2726 requires input_range<const _Vp>
2727 && is_reference_v<range_reference_t<const _Vp>>
2729 return _Iterator<true>{this, ranges::begin(_M_base)};
2735 if constexpr (forward_range<_Vp> && is_reference_v<_InnerRange>
2736 && forward_range<_InnerRange>
2737 && common_range<_Vp> && common_range<_InnerRange>)
2738 return _Iterator<__detail::__simple_view<_Vp>>{this,
2739 ranges::end(_M_base)};
2741 return _Sentinel<__detail::__simple_view<_Vp>>{this};
2746 requires input_range<const _Vp>
2747 && is_reference_v<range_reference_t<const _Vp>>
2749 if constexpr (forward_range<const _Vp>
2750 && is_reference_v<range_reference_t<const _Vp>>
2751 && forward_range<range_reference_t<const _Vp>>
2752 && common_range<const _Vp>
2753 && common_range<range_reference_t<const _Vp>>)
2754 return _Iterator<true>{this, ranges::end(_M_base)};
2756 return _Sentinel<true>{this};
2760 template<typename _Range>
2761 explicit join_view(_Range&&) -> join_view<views::all_t<_Range>>;
2767 template<typename _Range>
2768 concept __can_join_view
2769 = requires { join_view<all_t<_Range>>{std::declval<_Range>()}; };
2770 } // namespace __detail
2772 struct _Join : __adaptor::_RangeAdaptorClosure
2774 template<viewable_range _Range>
2775 requires __detail::__can_join_view<_Range>
2777 operator()(_Range&& __r) const
2779 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2780 // 3474. Nesting join_views is broken because of CTAD
2781 return join_view<all_t<_Range>>{std::forward<_Range>(__r)};
2784 static constexpr bool _S_has_simple_call_op = true;
2787 inline constexpr _Join join;
2788 } // namespace views
2793 struct __require_constant;
2795 template<typename _Range>
2796 concept __tiny_range = sized_range<_Range>
2798 { typename __require_constant<remove_reference_t<_Range>::size()>; }
2799 && (remove_reference_t<_Range>::size() <= 1);
2801 template<typename _Base>
2802 struct __split_view_outer_iter_cat
2805 template<forward_range _Base>
2806 struct __split_view_outer_iter_cat<_Base>
2807 { using iterator_category = input_iterator_tag; };
2809 template<typename _Base>
2810 struct __split_view_inner_iter_cat
2813 template<forward_range _Base>
2814 struct __split_view_inner_iter_cat<_Base>
2817 static constexpr auto
2820 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
2821 if constexpr (derived_from<_Cat, forward_iterator_tag>)
2822 return forward_iterator_tag{};
2827 using iterator_category = decltype(_S_iter_cat());
2831 template<input_range _Vp, forward_range _Pattern>
2832 requires view<_Vp> && view<_Pattern>
2833 && indirectly_comparable<iterator_t<_Vp>, iterator_t<_Pattern>,
2835 && (forward_range<_Vp> || __detail::__tiny_range<_Pattern>)
2836 class split_view : public view_interface<split_view<_Vp, _Pattern>>
2839 template<bool _Const>
2840 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
2842 template<bool _Const>
2845 template<bool _Const>
2847 : __detail::__split_view_outer_iter_cat<_Base<_Const>>
2850 using _Parent = __detail::__maybe_const_t<_Const, split_view>;
2851 using _Base = split_view::_Base<_Const>;
2855 { return __current() == ranges::end(_M_parent->_M_base); }
2857 // [range.split.outer] p1
2858 // Many of the following specifications refer to the notional member
2859 // current of outer-iterator. current is equivalent to current_ if
2860 // V models forward_range, and parent_->current_ otherwise.
2862 __current() noexcept
2864 if constexpr (forward_range<_Vp>)
2867 return _M_parent->_M_current;
2871 __current() const noexcept
2873 if constexpr (forward_range<_Vp>)
2876 return _M_parent->_M_current;
2879 _Parent* _M_parent = nullptr;
2881 // XXX: _M_current is present only if "V models forward_range"
2882 [[no_unique_address]]
2883 __detail::__maybe_present_t<forward_range<_Vp>,
2884 iterator_t<_Base>> _M_current;
2887 using iterator_concept = conditional_t<forward_range<_Base>,
2888 forward_iterator_tag,
2889 input_iterator_tag>;
2890 // iterator_category defined in __split_view_outer_iter_cat
2891 using difference_type = range_difference_t<_Base>;
2893 struct value_type : view_interface<value_type>
2896 _OuterIter _M_i = _OuterIter();
2899 value_type() = default;
2902 value_type(_OuterIter __i)
2903 : _M_i(std::move(__i))
2906 constexpr _InnerIter<_Const>
2908 { return _InnerIter<_Const>{_M_i}; }
2910 constexpr default_sentinel_t
2911 end() const noexcept
2912 { return default_sentinel; }
2915 _OuterIter() = default;
2918 _OuterIter(_Parent* __parent) requires (!forward_range<_Base>)
2919 : _M_parent(__parent)
2923 _OuterIter(_Parent* __parent, iterator_t<_Base> __current)
2924 requires forward_range<_Base>
2925 : _M_parent(__parent),
2926 _M_current(std::move(__current))
2930 _OuterIter(_OuterIter<!_Const> __i)
2932 && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
2933 : _M_parent(__i._M_parent), _M_current(std::move(__i._M_current))
2936 constexpr value_type
2938 { return value_type{*this}; }
2940 constexpr _OuterIter&
2943 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2944 // 3505. split_view::outer-iterator::operator++ misspecified
2945 const auto __end = ranges::end(_M_parent->_M_base);
2946 if (__current() == __end)
2948 const auto [__pbegin, __pend] = subrange{_M_parent->_M_pattern};
2949 if (__pbegin == __pend)
2951 else if constexpr (__detail::__tiny_range<_Pattern>)
2953 __current() = ranges::find(std::move(__current()), __end,
2955 if (__current() != __end)
2962 = ranges::mismatch(__current(), __end, __pbegin, __pend);
2968 } while (++__current() != __end);
2972 constexpr decltype(auto)
2975 if constexpr (forward_range<_Base>)
2985 friend constexpr bool
2986 operator==(const _OuterIter& __x, const _OuterIter& __y)
2987 requires forward_range<_Base>
2988 { return __x._M_current == __y._M_current; }
2990 friend constexpr bool
2991 operator==(const _OuterIter& __x, default_sentinel_t)
2992 { return __x.__at_end(); };
2994 friend _OuterIter<!_Const>;
2995 friend _InnerIter<_Const>;
2998 template<bool _Const>
3000 : __detail::__split_view_inner_iter_cat<_Base<_Const>>
3003 using _Base = split_view::_Base<_Const>;
3008 auto [__pcur, __pend] = subrange{_M_i._M_parent->_M_pattern};
3009 auto __end = ranges::end(_M_i._M_parent->_M_base);
3010 if constexpr (__detail::__tiny_range<_Pattern>)
3012 const auto& __cur = _M_i_current();
3015 if (__pcur == __pend)
3016 return _M_incremented;
3017 return *__cur == *__pcur;
3021 auto __cur = _M_i_current();
3024 if (__pcur == __pend)
3025 return _M_incremented;
3028 if (*__cur != *__pcur)
3030 if (++__pcur == __pend)
3032 } while (++__cur != __end);
3038 _M_i_current() noexcept
3039 { return _M_i.__current(); }
3042 _M_i_current() const noexcept
3043 { return _M_i.__current(); }
3045 _OuterIter<_Const> _M_i = _OuterIter<_Const>();
3046 bool _M_incremented = false;
3049 using iterator_concept
3050 = typename _OuterIter<_Const>::iterator_concept;
3051 // iterator_category defined in __split_view_inner_iter_cat
3052 using value_type = range_value_t<_Base>;
3053 using difference_type = range_difference_t<_Base>;
3055 _InnerIter() = default;
3058 _InnerIter(_OuterIter<_Const> __i)
3059 : _M_i(std::move(__i))
3062 constexpr const iterator_t<_Base>&
3063 base() const& noexcept
3064 { return _M_i_current(); }
3066 constexpr iterator_t<_Base>
3067 base() && requires forward_range<_Vp>
3068 { return std::move(_M_i_current()); }
3070 constexpr decltype(auto)
3072 { return *_M_i_current(); }
3074 constexpr _InnerIter&
3077 _M_incremented = true;
3078 if constexpr (!forward_range<_Base>)
3079 if constexpr (_Pattern::size() == 0)
3085 constexpr decltype(auto)
3088 if constexpr (forward_range<_Base>)
3098 friend constexpr bool
3099 operator==(const _InnerIter& __x, const _InnerIter& __y)
3100 requires forward_range<_Base>
3101 { return __x._M_i == __y._M_i; }
3103 friend constexpr bool
3104 operator==(const _InnerIter& __x, default_sentinel_t)
3105 { return __x.__at_end(); }
3107 friend constexpr decltype(auto)
3108 iter_move(const _InnerIter& __i)
3109 noexcept(noexcept(ranges::iter_move(__i._M_i_current())))
3110 { return ranges::iter_move(__i._M_i_current()); }
3112 friend constexpr void
3113 iter_swap(const _InnerIter& __x, const _InnerIter& __y)
3114 noexcept(noexcept(ranges::iter_swap(__x._M_i_current(),
3115 __y._M_i_current())))
3116 requires indirectly_swappable<iterator_t<_Base>>
3117 { ranges::iter_swap(__x._M_i_current(), __y._M_i_current()); }
3120 _Pattern _M_pattern = _Pattern();
3121 // XXX: _M_current is "present only if !forward_range<V>"
3122 [[no_unique_address]]
3123 __detail::__maybe_present_t<!forward_range<_Vp>,
3124 iterator_t<_Vp>> _M_current;
3125 _Vp _M_base = _Vp();
3129 split_view() requires (default_initializable<_Vp>
3130 && default_initializable<_Pattern>
3131 && (forward_range<_Vp>
3132 || default_initializable<iterator_t<_Vp>>))
3136 split_view(_Vp __base, _Pattern __pattern)
3137 : _M_pattern(std::move(__pattern)), _M_base(std::move(__base))
3140 template<input_range _Range>
3141 requires constructible_from<_Vp, views::all_t<_Range>>
3142 && constructible_from<_Pattern, single_view<range_value_t<_Range>>>
3144 split_view(_Range&& __r, range_value_t<_Range> __e)
3145 : _M_pattern(views::single(std::move(__e))),
3146 _M_base(views::all(std::forward<_Range>(__r)))
3150 base() const& requires copy_constructible<_Vp>
3155 { return std::move(_M_base); }
3160 if constexpr (forward_range<_Vp>)
3162 constexpr bool __simple
3163 = __detail::__simple_view<_Vp> && __detail::__simple_view<_Pattern>;
3164 return _OuterIter<__simple>{this, ranges::begin(_M_base)};
3168 _M_current = ranges::begin(_M_base);
3169 return _OuterIter<false>{this};
3174 begin() const requires forward_range<_Vp> && forward_range<const _Vp>
3176 return _OuterIter<true>{this, ranges::begin(_M_base)};
3180 end() requires forward_range<_Vp> && common_range<_Vp>
3182 constexpr bool __simple
3183 = __detail::__simple_view<_Vp> && __detail::__simple_view<_Pattern>;
3184 return _OuterIter<__simple>{this, ranges::end(_M_base)};
3190 if constexpr (forward_range<_Vp>
3191 && forward_range<const _Vp>
3192 && common_range<const _Vp>)
3193 return _OuterIter<true>{this, ranges::end(_M_base)};
3195 return default_sentinel;
3199 template<typename _Range, typename _Pattern>
3200 split_view(_Range&&, _Pattern&&)
3201 -> split_view<views::all_t<_Range>, views::all_t<_Pattern>>;
3203 template<input_range _Range>
3204 split_view(_Range&&, range_value_t<_Range>)
3205 -> split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
3211 template<typename _Range, typename _Pattern>
3212 concept __can_split_view
3213 = requires { split_view(std::declval<_Range>(), std::declval<_Pattern>()); };
3214 } // namespace __detail
3216 struct _Split : __adaptor::_RangeAdaptor<_Split>
3218 template<viewable_range _Range, typename _Pattern>
3219 requires __detail::__can_split_view<_Range, _Pattern>
3221 operator()(_Range&& __r, _Pattern&& __f) const
3223 return split_view(std::forward<_Range>(__r), std::forward<_Pattern>(__f));
3226 using _RangeAdaptor<_Split>::operator();
3227 static constexpr int _S_arity = 2;
3228 // The pattern argument of views::split is not always simple -- it can be
3229 // a non-view range, the value category of which affects whether the call
3230 // is well-formed. But a scalar or a view pattern argument is surely
3232 template<typename _Pattern>
3233 static constexpr bool _S_has_simple_extra_args
3234 = is_scalar_v<_Pattern> || (view<_Pattern>
3235 && copy_constructible<_Pattern>);
3238 inline constexpr _Split split;
3239 } // namespace views
3245 template<input_or_output_iterator _Iter>
3247 operator()(_Iter __i, iter_difference_t<_Iter> __n) const
3249 if constexpr (random_access_iterator<_Iter>)
3250 return subrange(__i, __i + __n);
3252 return subrange(counted_iterator(std::move(__i), __n),
3257 inline constexpr _Counted counted{};
3258 } // namespace views
3261 requires (!common_range<_Vp>) && copyable<iterator_t<_Vp>>
3262 class common_view : public view_interface<common_view<_Vp>>
3265 _Vp _M_base = _Vp();
3268 common_view() requires default_initializable<_Vp> = default;
3271 common_view(_Vp __r)
3272 : _M_base(std::move(__r))
3275 /* XXX: LWG 3280 didn't remove this constructor, but I think it should?
3276 template<viewable_range _Range>
3277 requires (!common_range<_Range>)
3278 && constructible_from<_Vp, views::all_t<_Range>>
3280 common_view(_Range&& __r)
3281 : _M_base(views::all(std::forward<_Range>(__r)))
3286 base() const& requires copy_constructible<_Vp>
3291 { return std::move(_M_base); }
3296 if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
3297 return ranges::begin(_M_base);
3299 return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
3300 (ranges::begin(_M_base));
3304 begin() const requires range<const _Vp>
3306 if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
3307 return ranges::begin(_M_base);
3309 return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
3310 (ranges::begin(_M_base));
3316 if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
3317 return ranges::begin(_M_base) + ranges::size(_M_base);
3319 return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
3320 (ranges::end(_M_base));
3324 end() const requires range<const _Vp>
3326 if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
3327 return ranges::begin(_M_base) + ranges::size(_M_base);
3329 return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
3330 (ranges::end(_M_base));
3334 size() requires sized_range<_Vp>
3335 { return ranges::size(_M_base); }
3338 size() const requires sized_range<const _Vp>
3339 { return ranges::size(_M_base); }
3342 template<typename _Range>
3343 common_view(_Range&&) -> common_view<views::all_t<_Range>>;
3345 template<typename _Tp>
3346 inline constexpr bool enable_borrowed_range<common_view<_Tp>>
3347 = enable_borrowed_range<_Tp>;
3353 template<typename _Range>
3354 concept __already_common = common_range<_Range>
3355 && requires { views::all(std::declval<_Range>()); };
3357 template<typename _Range>
3358 concept __can_common_view
3359 = requires { common_view{std::declval<_Range>()}; };
3360 } // namespace __detail
3362 struct _Common : __adaptor::_RangeAdaptorClosure
3364 template<viewable_range _Range>
3365 requires __detail::__already_common<_Range>
3366 || __detail::__can_common_view<_Range>
3368 operator()(_Range&& __r) const
3370 if constexpr (__detail::__already_common<_Range>)
3371 return views::all(std::forward<_Range>(__r));
3373 return common_view{std::forward<_Range>(__r)};
3376 static constexpr bool _S_has_simple_call_op = true;
3379 inline constexpr _Common common;
3380 } // namespace views
3383 requires bidirectional_range<_Vp>
3384 class reverse_view : public view_interface<reverse_view<_Vp>>
3387 static constexpr bool _S_needs_cached_begin
3388 = !common_range<_Vp> && !random_access_range<_Vp>;
3390 [[no_unique_address]]
3391 __detail::__maybe_present_t<_S_needs_cached_begin,
3392 __detail::_CachedPosition<_Vp>>
3394 _Vp _M_base = _Vp();
3397 reverse_view() requires default_initializable<_Vp> = default;
3400 reverse_view(_Vp __r)
3401 : _M_base(std::move(__r))
3405 base() const& requires copy_constructible<_Vp>
3410 { return std::move(_M_base); }
3412 constexpr reverse_iterator<iterator_t<_Vp>>
3415 if constexpr (_S_needs_cached_begin)
3416 if (_M_cached_begin._M_has_value())
3417 return std::make_reverse_iterator(_M_cached_begin._M_get(_M_base));
3419 auto __it = ranges::next(ranges::begin(_M_base), ranges::end(_M_base));
3420 if constexpr (_S_needs_cached_begin)
3421 _M_cached_begin._M_set(_M_base, __it);
3422 return std::make_reverse_iterator(std::move(__it));
3426 begin() requires common_range<_Vp>
3427 { return std::make_reverse_iterator(ranges::end(_M_base)); }
3430 begin() const requires common_range<const _Vp>
3431 { return std::make_reverse_iterator(ranges::end(_M_base)); }
3433 constexpr reverse_iterator<iterator_t<_Vp>>
3435 { return std::make_reverse_iterator(ranges::begin(_M_base)); }
3438 end() const requires common_range<const _Vp>
3439 { return std::make_reverse_iterator(ranges::begin(_M_base)); }
3442 size() requires sized_range<_Vp>
3443 { return ranges::size(_M_base); }
3446 size() const requires sized_range<const _Vp>
3447 { return ranges::size(_M_base); }
3450 template<typename _Range>
3451 reverse_view(_Range&&) -> reverse_view<views::all_t<_Range>>;
3453 template<typename _Tp>
3454 inline constexpr bool enable_borrowed_range<reverse_view<_Tp>>
3455 = enable_borrowed_range<_Tp>;
3462 inline constexpr bool __is_reversible_subrange = false;
3464 template<typename _Iter, subrange_kind _Kind>
3465 inline constexpr bool
3466 __is_reversible_subrange<subrange<reverse_iterator<_Iter>,
3467 reverse_iterator<_Iter>,
3471 inline constexpr bool __is_reverse_view = false;
3473 template<typename _Vp>
3474 inline constexpr bool __is_reverse_view<reverse_view<_Vp>> = true;
3476 template<typename _Range>
3477 concept __can_reverse_view
3478 = requires { reverse_view{std::declval<_Range>()}; };
3479 } // namespace __detail
3481 struct _Reverse : __adaptor::_RangeAdaptorClosure
3483 template<viewable_range _Range>
3484 requires __detail::__is_reverse_view<remove_cvref_t<_Range>>
3485 || __detail::__is_reversible_subrange<remove_cvref_t<_Range>>
3486 || __detail::__can_reverse_view<_Range>
3488 operator()(_Range&& __r) const
3490 using _Tp = remove_cvref_t<_Range>;
3491 if constexpr (__detail::__is_reverse_view<_Tp>)
3492 return std::forward<_Range>(__r).base();
3493 else if constexpr (__detail::__is_reversible_subrange<_Tp>)
3495 using _Iter = decltype(ranges::begin(__r).base());
3496 if constexpr (sized_range<_Tp>)
3497 return subrange<_Iter, _Iter, subrange_kind::sized>
3498 {__r.end().base(), __r.begin().base(), __r.size()};
3500 return subrange<_Iter, _Iter, subrange_kind::unsized>
3501 {__r.end().base(), __r.begin().base()};
3504 return reverse_view{std::forward<_Range>(__r)};
3507 static constexpr bool _S_has_simple_call_op = true;
3510 inline constexpr _Reverse reverse;
3511 } // namespace views
3515 template<typename _Tp, size_t _Nm>
3516 concept __has_tuple_element = requires(_Tp __t)
3518 typename tuple_size<_Tp>::type;
3519 requires _Nm < tuple_size_v<_Tp>;
3520 typename tuple_element_t<_Nm, _Tp>;
3521 { std::get<_Nm>(__t) }
3522 -> convertible_to<const tuple_element_t<_Nm, _Tp>&>;
3525 template<typename _Tp, size_t _Nm>
3526 concept __returnable_element
3527 = is_reference_v<_Tp> || move_constructible<tuple_element_t<_Nm, _Tp>>;
3530 template<input_range _Vp, size_t _Nm>
3532 && __detail::__has_tuple_element<range_value_t<_Vp>, _Nm>
3533 && __detail::__has_tuple_element<remove_reference_t<range_reference_t<_Vp>>,
3535 && __detail::__returnable_element<range_reference_t<_Vp>, _Nm>
3536 class elements_view : public view_interface<elements_view<_Vp, _Nm>>
3539 elements_view() requires default_initializable<_Vp> = default;
3542 elements_view(_Vp base)
3543 : _M_base(std::move(base))
3547 base() const& requires copy_constructible<_Vp>
3552 { return std::move(_M_base); }
3555 begin() requires (!__detail::__simple_view<_Vp>)
3556 { return _Iterator<false>(ranges::begin(_M_base)); }
3559 begin() const requires range<const _Vp>
3560 { return _Iterator<true>(ranges::begin(_M_base)); }
3563 end() requires (!__detail::__simple_view<_Vp> && !common_range<_Vp>)
3564 { return _Sentinel<false>{ranges::end(_M_base)}; }
3567 end() requires (!__detail::__simple_view<_Vp> && common_range<_Vp>)
3568 { return _Iterator<false>{ranges::end(_M_base)}; }
3571 end() const requires range<const _Vp>
3572 { return _Sentinel<true>{ranges::end(_M_base)}; }
3575 end() const requires common_range<const _Vp>
3576 { return _Iterator<true>{ranges::end(_M_base)}; }
3579 size() requires sized_range<_Vp>
3580 { return ranges::size(_M_base); }
3583 size() const requires sized_range<const _Vp>
3584 { return ranges::size(_M_base); }
3587 template<bool _Const>
3588 using _Base = __detail::__maybe_const_t<_Const, _Vp>;
3590 template<bool _Const>
3594 template<bool _Const>
3595 requires forward_range<_Base<_Const>>
3596 struct __iter_cat<_Const>
3599 static auto _S_iter_cat()
3601 using _Base = elements_view::_Base<_Const>;
3602 using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
3603 using _Res = decltype((std::get<_Nm>(*std::declval<iterator_t<_Base>>())));
3604 if constexpr (!is_lvalue_reference_v<_Res>)
3605 return input_iterator_tag{};
3606 else if constexpr (derived_from<_Cat, random_access_iterator_tag>)
3607 return random_access_iterator_tag{};
3612 using iterator_category = decltype(_S_iter_cat());
3615 template<bool _Const>
3618 template<bool _Const>
3619 struct _Iterator : __iter_cat<_Const>
3622 using _Base = elements_view::_Base<_Const>;
3624 iterator_t<_Base> _M_current = iterator_t<_Base>();
3626 static constexpr decltype(auto)
3627 _S_get_element(const iterator_t<_Base>& __i)
3629 if constexpr (is_reference_v<range_reference_t<_Base>>)
3630 return std::get<_Nm>(*__i);
3633 using _Et = remove_cv_t<tuple_element_t<_Nm, range_reference_t<_Base>>>;
3634 return static_cast<_Et>(std::get<_Nm>(*__i));
3641 if constexpr (random_access_range<_Base>)
3642 return random_access_iterator_tag{};
3643 else if constexpr (bidirectional_range<_Base>)
3644 return bidirectional_iterator_tag{};
3645 else if constexpr (forward_range<_Base>)
3646 return forward_iterator_tag{};
3648 return input_iterator_tag{};
3651 friend _Iterator<!_Const>;
3654 using iterator_concept = decltype(_S_iter_concept());
3655 // iterator_category defined in elements_view::__iter_cat
3657 = remove_cvref_t<tuple_element_t<_Nm, range_value_t<_Base>>>;
3658 using difference_type = range_difference_t<_Base>;
3660 _Iterator() requires default_initializable<iterator_t<_Base>> = default;
3663 _Iterator(iterator_t<_Base> current)
3664 : _M_current(std::move(current))
3668 _Iterator(_Iterator<!_Const> i)
3669 requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
3670 : _M_current(std::move(i._M_current))
3673 constexpr const iterator_t<_Base>&
3674 base() const& noexcept
3675 { return _M_current; }
3677 constexpr iterator_t<_Base>
3679 { return std::move(_M_current); }
3681 constexpr decltype(auto)
3683 { return _S_get_element(_M_current); }
3685 constexpr _Iterator&
3697 operator++(int) requires forward_range<_Base>
3704 constexpr _Iterator&
3705 operator--() requires bidirectional_range<_Base>
3712 operator--(int) requires bidirectional_range<_Base>
3719 constexpr _Iterator&
3720 operator+=(difference_type __n)
3721 requires random_access_range<_Base>
3727 constexpr _Iterator&
3728 operator-=(difference_type __n)
3729 requires random_access_range<_Base>
3735 constexpr decltype(auto)
3736 operator[](difference_type __n) const
3737 requires random_access_range<_Base>
3738 { return _S_get_element(_M_current + __n); }
3740 friend constexpr bool
3741 operator==(const _Iterator& __x, const _Iterator& __y)
3742 requires equality_comparable<iterator_t<_Base>>
3743 { return __x._M_current == __y._M_current; }
3745 friend constexpr bool
3746 operator<(const _Iterator& __x, const _Iterator& __y)
3747 requires random_access_range<_Base>
3748 { return __x._M_current < __y._M_current; }
3750 friend constexpr bool
3751 operator>(const _Iterator& __x, const _Iterator& __y)
3752 requires random_access_range<_Base>
3753 { return __y._M_current < __x._M_current; }
3755 friend constexpr bool
3756 operator<=(const _Iterator& __x, const _Iterator& __y)
3757 requires random_access_range<_Base>
3758 { return !(__y._M_current > __x._M_current); }
3760 friend constexpr bool
3761 operator>=(const _Iterator& __x, const _Iterator& __y)
3762 requires random_access_range<_Base>
3763 { return !(__x._M_current > __y._M_current); }
3765#ifdef __cpp_lib_three_way_comparison
3766 friend constexpr auto
3767 operator<=>(const _Iterator& __x, const _Iterator& __y)
3768 requires random_access_range<_Base>
3769 && three_way_comparable<iterator_t<_Base>>
3770 { return __x._M_current <=> __y._M_current; }
3773 friend constexpr _Iterator
3774 operator+(const _Iterator& __x, difference_type __y)
3775 requires random_access_range<_Base>
3776 { return _Iterator{__x} += __y; }
3778 friend constexpr _Iterator
3779 operator+(difference_type __x, const _Iterator& __y)
3780 requires random_access_range<_Base>
3781 { return __y + __x; }
3783 friend constexpr _Iterator
3784 operator-(const _Iterator& __x, difference_type __y)
3785 requires random_access_range<_Base>
3786 { return _Iterator{__x} -= __y; }
3788 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3789 // 3483. transform_view::iterator's difference is overconstrained
3790 friend constexpr difference_type
3791 operator-(const _Iterator& __x, const _Iterator& __y)
3792 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
3793 { return __x._M_current - __y._M_current; }
3795 template <bool> friend struct _Sentinel;
3798 template<bool _Const>
3802 template<bool _Const2>
3804 _M_equal(const _Iterator<_Const2>& __x) const
3805 { return __x._M_current == _M_end; }
3807 template<bool _Const2>
3809 _M_distance_from(const _Iterator<_Const2>& __i) const
3810 { return _M_end - __i._M_current; }
3812 using _Base = elements_view::_Base<_Const>;
3813 sentinel_t<_Base> _M_end = sentinel_t<_Base>();
3816 _Sentinel() = default;
3819 _Sentinel(sentinel_t<_Base> __end)
3820 : _M_end(std::move(__end))
3824 _Sentinel(_Sentinel<!_Const> __other)
3826 && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
3827 : _M_end(std::move(__other._M_end))
3830 constexpr sentinel_t<_Base>
3834 template<bool _Const2>
3835 requires sentinel_for<sentinel_t<_Base>,
3836 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
3837 friend constexpr bool
3838 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
3839 { return __y._M_equal(__x); }
3841 template<bool _Const2,
3842 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
3843 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
3844 friend constexpr range_difference_t<_Base2>
3845 operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
3846 { return -__y._M_distance_from(__x); }
3848 template<bool _Const2,
3849 typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
3850 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
3851 friend constexpr range_difference_t<_Base2>
3852 operator-(const _Sentinel& __x, const _Iterator<_Const2>& __y)
3853 { return __x._M_distance_from(__y); }
3855 friend _Sentinel<!_Const>;
3858 _Vp _M_base = _Vp();
3861 template<typename _Tp, size_t _Nm>
3862 inline constexpr bool enable_borrowed_range<elements_view<_Tp, _Nm>>
3863 = enable_borrowed_range<_Tp>;
3865 template<typename _Range>
3866 using keys_view = elements_view<views::all_t<_Range>, 0>;
3868 template<typename _Range>
3869 using values_view = elements_view<views::all_t<_Range>, 1>;
3875 template<size_t _Nm, typename _Range>
3876 concept __can_elements_view
3877 = requires { elements_view<all_t<_Range>, _Nm>{std::declval<_Range>()}; };
3878 } // namespace __detail
3880 template<size_t _Nm>
3881 struct _Elements : __adaptor::_RangeAdaptorClosure
3883 template<viewable_range _Range>
3884 requires __detail::__can_elements_view<_Nm, _Range>
3886 operator()(_Range&& __r) const
3888 return elements_view<all_t<_Range>, _Nm>{std::forward<_Range>(__r)};
3891 static constexpr bool _S_has_simple_call_op = true;
3894 template<size_t _Nm>
3895 inline constexpr _Elements<_Nm> elements;
3896 inline constexpr auto keys = elements<0>;
3897 inline constexpr auto values = elements<1>;
3898 } // namespace views
3900} // namespace ranges
3902 namespace views = ranges::views;
3904_GLIBCXX_END_NAMESPACE_VERSION
3906#endif // library concepts
3908#endif /* _GLIBCXX_RANGES */