1*bdd1243dSDimitry Andric // -*- C++ -*- 2*bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 3*bdd1243dSDimitry Andric // 4*bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5*bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6*bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7*bdd1243dSDimitry Andric // 8*bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 9*bdd1243dSDimitry Andric 10*bdd1243dSDimitry Andric #ifndef _LIBCPP___RANGES_ELEMENTS_VIEW_H 11*bdd1243dSDimitry Andric #define _LIBCPP___RANGES_ELEMENTS_VIEW_H 12*bdd1243dSDimitry Andric 13*bdd1243dSDimitry Andric #include <__compare/three_way_comparable.h> 14*bdd1243dSDimitry Andric #include <__concepts/constructible.h> 15*bdd1243dSDimitry Andric #include <__concepts/convertible_to.h> 16*bdd1243dSDimitry Andric #include <__concepts/derived_from.h> 17*bdd1243dSDimitry Andric #include <__concepts/equality_comparable.h> 18*bdd1243dSDimitry Andric #include <__config> 19*bdd1243dSDimitry Andric #include <__fwd/get.h> 20*bdd1243dSDimitry Andric #include <__iterator/concepts.h> 21*bdd1243dSDimitry Andric #include <__iterator/iterator_traits.h> 22*bdd1243dSDimitry Andric #include <__ranges/access.h> 23*bdd1243dSDimitry Andric #include <__ranges/all.h> 24*bdd1243dSDimitry Andric #include <__ranges/concepts.h> 25*bdd1243dSDimitry Andric #include <__ranges/enable_borrowed_range.h> 26*bdd1243dSDimitry Andric #include <__ranges/range_adaptor.h> 27*bdd1243dSDimitry Andric #include <__ranges/size.h> 28*bdd1243dSDimitry Andric #include <__ranges/view_interface.h> 29*bdd1243dSDimitry Andric #include <__tuple_dir/tuple_element.h> 30*bdd1243dSDimitry Andric #include <__tuple_dir/tuple_like.h> 31*bdd1243dSDimitry Andric #include <__tuple_dir/tuple_size.h> 32*bdd1243dSDimitry Andric #include <__type_traits/is_reference.h> 33*bdd1243dSDimitry Andric #include <__type_traits/maybe_const.h> 34*bdd1243dSDimitry Andric #include <__type_traits/remove_cv.h> 35*bdd1243dSDimitry Andric #include <__type_traits/remove_cvref.h> 36*bdd1243dSDimitry Andric #include <__type_traits/remove_reference.h> 37*bdd1243dSDimitry Andric #include <__utility/declval.h> 38*bdd1243dSDimitry Andric #include <__utility/forward.h> 39*bdd1243dSDimitry Andric #include <__utility/move.h> 40*bdd1243dSDimitry Andric #include <cstddef> 41*bdd1243dSDimitry Andric 42*bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 43*bdd1243dSDimitry Andric # pragma GCC system_header 44*bdd1243dSDimitry Andric #endif 45*bdd1243dSDimitry Andric 46*bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 47*bdd1243dSDimitry Andric 48*bdd1243dSDimitry Andric #if _LIBCPP_STD_VER >= 20 49*bdd1243dSDimitry Andric 50*bdd1243dSDimitry Andric namespace ranges { 51*bdd1243dSDimitry Andric 52*bdd1243dSDimitry Andric template <class _View, size_t _Np, bool _Const> 53*bdd1243dSDimitry Andric class __elements_view_iterator; 54*bdd1243dSDimitry Andric 55*bdd1243dSDimitry Andric template <class _View, size_t _Np, bool _Const> 56*bdd1243dSDimitry Andric class __elements_view_sentinel; 57*bdd1243dSDimitry Andric 58*bdd1243dSDimitry Andric template <class _Tp, size_t _Np> 59*bdd1243dSDimitry Andric concept __has_tuple_element = __tuple_like<_Tp> && _Np < tuple_size<_Tp>::value; 60*bdd1243dSDimitry Andric 61*bdd1243dSDimitry Andric template <class _Tp, size_t _Np> 62*bdd1243dSDimitry Andric concept __returnable_element = is_reference_v<_Tp> || move_constructible<tuple_element_t<_Np, _Tp>>; 63*bdd1243dSDimitry Andric 64*bdd1243dSDimitry Andric template <input_range _View, size_t _Np> 65*bdd1243dSDimitry Andric requires view<_View> && __has_tuple_element<range_value_t<_View>, _Np> && 66*bdd1243dSDimitry Andric __has_tuple_element<remove_reference_t<range_reference_t<_View>>, _Np> && 67*bdd1243dSDimitry Andric __returnable_element<range_reference_t<_View>, _Np> 68*bdd1243dSDimitry Andric class elements_view : public view_interface<elements_view<_View, _Np>> { 69*bdd1243dSDimitry Andric public: 70*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI elements_view() 71*bdd1243dSDimitry Andric requires default_initializable<_View> 72*bdd1243dSDimitry Andric = default; 73*bdd1243dSDimitry Andric 74*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit elements_view(_View __base) : __base_(std::move(__base)) {} 75*bdd1243dSDimitry Andric 76*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& 77*bdd1243dSDimitry Andric requires copy_constructible<_View> 78*bdd1243dSDimitry Andric { 79*bdd1243dSDimitry Andric return __base_; 80*bdd1243dSDimitry Andric } 81*bdd1243dSDimitry Andric 82*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); } 83*bdd1243dSDimitry Andric 84*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto begin() 85*bdd1243dSDimitry Andric requires(!__simple_view<_View>) 86*bdd1243dSDimitry Andric { 87*bdd1243dSDimitry Andric return __iterator</*_Const=*/false>(ranges::begin(__base_)); 88*bdd1243dSDimitry Andric } 89*bdd1243dSDimitry Andric 90*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const 91*bdd1243dSDimitry Andric requires range<const _View> 92*bdd1243dSDimitry Andric { 93*bdd1243dSDimitry Andric return __iterator</*_Const=*/true>(ranges::begin(__base_)); 94*bdd1243dSDimitry Andric } 95*bdd1243dSDimitry Andric 96*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto end() 97*bdd1243dSDimitry Andric requires(!__simple_view<_View> && !common_range<_View>) 98*bdd1243dSDimitry Andric { 99*bdd1243dSDimitry Andric return __sentinel</*_Const=*/false>{ranges::end(__base_)}; 100*bdd1243dSDimitry Andric } 101*bdd1243dSDimitry Andric 102*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto end() 103*bdd1243dSDimitry Andric requires(!__simple_view<_View> && common_range<_View>) 104*bdd1243dSDimitry Andric { 105*bdd1243dSDimitry Andric return __iterator</*_Const=*/false>{ranges::end(__base_)}; 106*bdd1243dSDimitry Andric } 107*bdd1243dSDimitry Andric 108*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto end() const 109*bdd1243dSDimitry Andric requires range<const _View> 110*bdd1243dSDimitry Andric { 111*bdd1243dSDimitry Andric return __sentinel</*_Const=*/true>{ranges::end(__base_)}; 112*bdd1243dSDimitry Andric } 113*bdd1243dSDimitry Andric 114*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto end() const 115*bdd1243dSDimitry Andric requires common_range<const _View> 116*bdd1243dSDimitry Andric { 117*bdd1243dSDimitry Andric return __iterator</*_Const=*/true>{ranges::end(__base_)}; 118*bdd1243dSDimitry Andric } 119*bdd1243dSDimitry Andric 120*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto size() 121*bdd1243dSDimitry Andric requires sized_range<_View> 122*bdd1243dSDimitry Andric { 123*bdd1243dSDimitry Andric return ranges::size(__base_); 124*bdd1243dSDimitry Andric } 125*bdd1243dSDimitry Andric 126*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto size() const 127*bdd1243dSDimitry Andric requires sized_range<const _View> 128*bdd1243dSDimitry Andric { 129*bdd1243dSDimitry Andric return ranges::size(__base_); 130*bdd1243dSDimitry Andric } 131*bdd1243dSDimitry Andric 132*bdd1243dSDimitry Andric private: 133*bdd1243dSDimitry Andric template <bool _Const> 134*bdd1243dSDimitry Andric using __iterator = __elements_view_iterator<_View, _Np, _Const>; 135*bdd1243dSDimitry Andric 136*bdd1243dSDimitry Andric template <bool _Const> 137*bdd1243dSDimitry Andric using __sentinel = __elements_view_sentinel<_View, _Np, _Const>; 138*bdd1243dSDimitry Andric 139*bdd1243dSDimitry Andric _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View(); 140*bdd1243dSDimitry Andric }; 141*bdd1243dSDimitry Andric 142*bdd1243dSDimitry Andric template <class, size_t> 143*bdd1243dSDimitry Andric struct __elements_view_iterator_category_base {}; 144*bdd1243dSDimitry Andric 145*bdd1243dSDimitry Andric template <forward_range _Base, size_t _Np> 146*bdd1243dSDimitry Andric struct __elements_view_iterator_category_base<_Base, _Np> { 147*bdd1243dSDimitry Andric static consteval auto __get_iterator_category() { 148*bdd1243dSDimitry Andric using _Result = decltype(std::get<_Np>(*std::declval<iterator_t<_Base>>())); 149*bdd1243dSDimitry Andric using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category; 150*bdd1243dSDimitry Andric 151*bdd1243dSDimitry Andric if constexpr (!is_lvalue_reference_v<_Result>) { 152*bdd1243dSDimitry Andric return input_iterator_tag{}; 153*bdd1243dSDimitry Andric } else if constexpr (derived_from<_Cat, random_access_iterator_tag>) { 154*bdd1243dSDimitry Andric return random_access_iterator_tag{}; 155*bdd1243dSDimitry Andric } else { 156*bdd1243dSDimitry Andric return _Cat{}; 157*bdd1243dSDimitry Andric } 158*bdd1243dSDimitry Andric } 159*bdd1243dSDimitry Andric 160*bdd1243dSDimitry Andric using iterator_category = decltype(__get_iterator_category()); 161*bdd1243dSDimitry Andric }; 162*bdd1243dSDimitry Andric 163*bdd1243dSDimitry Andric template <class _View, size_t _Np, bool _Const> 164*bdd1243dSDimitry Andric class __elements_view_iterator : public __elements_view_iterator_category_base<__maybe_const<_Const, _View>, _Np> { 165*bdd1243dSDimitry Andric template <class, size_t, bool > 166*bdd1243dSDimitry Andric friend class __elements_view_iterator; 167*bdd1243dSDimitry Andric 168*bdd1243dSDimitry Andric template <class, size_t, bool > 169*bdd1243dSDimitry Andric friend class __elements_view_sentinel; 170*bdd1243dSDimitry Andric 171*bdd1243dSDimitry Andric using _Base = __maybe_const<_Const, _View>; 172*bdd1243dSDimitry Andric 173*bdd1243dSDimitry Andric iterator_t<_Base> __current_ = iterator_t<_Base>(); 174*bdd1243dSDimitry Andric 175*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __get_element(const iterator_t<_Base>& __i) { 176*bdd1243dSDimitry Andric if constexpr (is_reference_v<range_reference_t<_Base>>) { 177*bdd1243dSDimitry Andric return std::get<_Np>(*__i); 178*bdd1243dSDimitry Andric } else { 179*bdd1243dSDimitry Andric using _Element = remove_cv_t<tuple_element_t<_Np, range_reference_t<_Base>>>; 180*bdd1243dSDimitry Andric return static_cast<_Element>(std::get<_Np>(*__i)); 181*bdd1243dSDimitry Andric } 182*bdd1243dSDimitry Andric } 183*bdd1243dSDimitry Andric 184*bdd1243dSDimitry Andric static consteval auto __get_iterator_concept() { 185*bdd1243dSDimitry Andric if constexpr (random_access_range<_Base>) { 186*bdd1243dSDimitry Andric return random_access_iterator_tag{}; 187*bdd1243dSDimitry Andric } else if constexpr (bidirectional_range<_Base>) { 188*bdd1243dSDimitry Andric return bidirectional_iterator_tag{}; 189*bdd1243dSDimitry Andric } else if constexpr (forward_range<_Base>) { 190*bdd1243dSDimitry Andric return forward_iterator_tag{}; 191*bdd1243dSDimitry Andric } else { 192*bdd1243dSDimitry Andric return input_iterator_tag{}; 193*bdd1243dSDimitry Andric } 194*bdd1243dSDimitry Andric } 195*bdd1243dSDimitry Andric 196*bdd1243dSDimitry Andric public: 197*bdd1243dSDimitry Andric using iterator_concept = decltype(__get_iterator_concept()); 198*bdd1243dSDimitry Andric using value_type = remove_cvref_t<tuple_element_t<_Np, range_value_t<_Base>>>; 199*bdd1243dSDimitry Andric using difference_type = range_difference_t<_Base>; 200*bdd1243dSDimitry Andric 201*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI __elements_view_iterator() 202*bdd1243dSDimitry Andric requires default_initializable<iterator_t<_Base>> 203*bdd1243dSDimitry Andric = default; 204*bdd1243dSDimitry Andric 205*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit __elements_view_iterator(iterator_t<_Base> __current) 206*bdd1243dSDimitry Andric : __current_(std::move(__current)) {} 207*bdd1243dSDimitry Andric 208*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr __elements_view_iterator(__elements_view_iterator<_View, _Np, !_Const> __i) 209*bdd1243dSDimitry Andric requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>> 210*bdd1243dSDimitry Andric : __current_(std::move(__i.__current_)) {} 211*bdd1243dSDimitry Andric 212*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __current_; } 213*bdd1243dSDimitry Andric 214*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() && { return std::move(__current_); } 215*bdd1243dSDimitry Andric 216*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const { return __get_element(__current_); } 217*bdd1243dSDimitry Andric 218*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr __elements_view_iterator& operator++() { 219*bdd1243dSDimitry Andric ++__current_; 220*bdd1243dSDimitry Andric return *this; 221*bdd1243dSDimitry Andric } 222*bdd1243dSDimitry Andric 223*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++__current_; } 224*bdd1243dSDimitry Andric 225*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr __elements_view_iterator operator++(int) 226*bdd1243dSDimitry Andric requires forward_range<_Base> 227*bdd1243dSDimitry Andric { 228*bdd1243dSDimitry Andric auto temp = *this; 229*bdd1243dSDimitry Andric ++__current_; 230*bdd1243dSDimitry Andric return temp; 231*bdd1243dSDimitry Andric } 232*bdd1243dSDimitry Andric 233*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr __elements_view_iterator& operator--() 234*bdd1243dSDimitry Andric requires bidirectional_range<_Base> 235*bdd1243dSDimitry Andric { 236*bdd1243dSDimitry Andric --__current_; 237*bdd1243dSDimitry Andric return *this; 238*bdd1243dSDimitry Andric } 239*bdd1243dSDimitry Andric 240*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr __elements_view_iterator operator--(int) 241*bdd1243dSDimitry Andric requires bidirectional_range<_Base> 242*bdd1243dSDimitry Andric { 243*bdd1243dSDimitry Andric auto temp = *this; 244*bdd1243dSDimitry Andric --__current_; 245*bdd1243dSDimitry Andric return temp; 246*bdd1243dSDimitry Andric } 247*bdd1243dSDimitry Andric 248*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr __elements_view_iterator& operator+=(difference_type __n) 249*bdd1243dSDimitry Andric requires random_access_range<_Base> 250*bdd1243dSDimitry Andric { 251*bdd1243dSDimitry Andric __current_ += __n; 252*bdd1243dSDimitry Andric return *this; 253*bdd1243dSDimitry Andric } 254*bdd1243dSDimitry Andric 255*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr __elements_view_iterator& operator-=(difference_type __n) 256*bdd1243dSDimitry Andric requires random_access_range<_Base> 257*bdd1243dSDimitry Andric { 258*bdd1243dSDimitry Andric __current_ -= __n; 259*bdd1243dSDimitry Andric return *this; 260*bdd1243dSDimitry Andric } 261*bdd1243dSDimitry Andric 262*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const 263*bdd1243dSDimitry Andric requires random_access_range<_Base> 264*bdd1243dSDimitry Andric { 265*bdd1243dSDimitry Andric return __get_element(__current_ + __n); 266*bdd1243dSDimitry Andric } 267*bdd1243dSDimitry Andric 268*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool 269*bdd1243dSDimitry Andric operator==(const __elements_view_iterator& __x, const __elements_view_iterator& __y) 270*bdd1243dSDimitry Andric requires equality_comparable<iterator_t<_Base>> 271*bdd1243dSDimitry Andric { 272*bdd1243dSDimitry Andric return __x.__current_ == __y.__current_; 273*bdd1243dSDimitry Andric } 274*bdd1243dSDimitry Andric 275*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool 276*bdd1243dSDimitry Andric operator<(const __elements_view_iterator& __x, const __elements_view_iterator& __y) 277*bdd1243dSDimitry Andric requires random_access_range<_Base> 278*bdd1243dSDimitry Andric { 279*bdd1243dSDimitry Andric return __x.__current_ < __y.__current_; 280*bdd1243dSDimitry Andric } 281*bdd1243dSDimitry Andric 282*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool 283*bdd1243dSDimitry Andric operator>(const __elements_view_iterator& __x, const __elements_view_iterator& __y) 284*bdd1243dSDimitry Andric requires random_access_range<_Base> 285*bdd1243dSDimitry Andric { 286*bdd1243dSDimitry Andric return __y < __x; 287*bdd1243dSDimitry Andric } 288*bdd1243dSDimitry Andric 289*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool 290*bdd1243dSDimitry Andric operator<=(const __elements_view_iterator& __x, const __elements_view_iterator& __y) 291*bdd1243dSDimitry Andric requires random_access_range<_Base> 292*bdd1243dSDimitry Andric { 293*bdd1243dSDimitry Andric return !(__y < __x); 294*bdd1243dSDimitry Andric } 295*bdd1243dSDimitry Andric 296*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool 297*bdd1243dSDimitry Andric operator>=(const __elements_view_iterator& __x, const __elements_view_iterator& __y) 298*bdd1243dSDimitry Andric requires random_access_range<_Base> 299*bdd1243dSDimitry Andric { 300*bdd1243dSDimitry Andric return !(__x < __y); 301*bdd1243dSDimitry Andric } 302*bdd1243dSDimitry Andric 303*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr auto 304*bdd1243dSDimitry Andric operator<=>(const __elements_view_iterator& __x, const __elements_view_iterator& __y) 305*bdd1243dSDimitry Andric requires random_access_range<_Base> && three_way_comparable<iterator_t<_Base>> 306*bdd1243dSDimitry Andric { 307*bdd1243dSDimitry Andric return __x.__current_ <=> __y.__current_; 308*bdd1243dSDimitry Andric } 309*bdd1243dSDimitry Andric 310*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr __elements_view_iterator 311*bdd1243dSDimitry Andric operator+(const __elements_view_iterator& __x, difference_type __y) 312*bdd1243dSDimitry Andric requires random_access_range<_Base> 313*bdd1243dSDimitry Andric { 314*bdd1243dSDimitry Andric return __elements_view_iterator{__x} += __y; 315*bdd1243dSDimitry Andric } 316*bdd1243dSDimitry Andric 317*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr __elements_view_iterator 318*bdd1243dSDimitry Andric operator+(difference_type __x, const __elements_view_iterator& __y) 319*bdd1243dSDimitry Andric requires random_access_range<_Base> 320*bdd1243dSDimitry Andric { 321*bdd1243dSDimitry Andric return __y + __x; 322*bdd1243dSDimitry Andric } 323*bdd1243dSDimitry Andric 324*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr __elements_view_iterator 325*bdd1243dSDimitry Andric operator-(const __elements_view_iterator& __x, difference_type __y) 326*bdd1243dSDimitry Andric requires random_access_range<_Base> 327*bdd1243dSDimitry Andric { 328*bdd1243dSDimitry Andric return __elements_view_iterator{__x} -= __y; 329*bdd1243dSDimitry Andric } 330*bdd1243dSDimitry Andric 331*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type 332*bdd1243dSDimitry Andric operator-(const __elements_view_iterator& __x, const __elements_view_iterator& __y) 333*bdd1243dSDimitry Andric requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>> 334*bdd1243dSDimitry Andric { 335*bdd1243dSDimitry Andric return __x.__current_ - __y.__current_; 336*bdd1243dSDimitry Andric } 337*bdd1243dSDimitry Andric }; 338*bdd1243dSDimitry Andric 339*bdd1243dSDimitry Andric template <class _View, size_t _Np, bool _Const> 340*bdd1243dSDimitry Andric class __elements_view_sentinel { 341*bdd1243dSDimitry Andric private: 342*bdd1243dSDimitry Andric using _Base = __maybe_const<_Const, _View>; 343*bdd1243dSDimitry Andric _LIBCPP_NO_UNIQUE_ADDRESS sentinel_t<_Base> __end_ = sentinel_t<_Base>(); 344*bdd1243dSDimitry Andric 345*bdd1243dSDimitry Andric template <class, size_t, bool > 346*bdd1243dSDimitry Andric friend class __elements_view_sentinel; 347*bdd1243dSDimitry Andric 348*bdd1243dSDimitry Andric template <bool _AnyConst> 349*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) 350*bdd1243dSDimitry Andric __get_current(const __elements_view_iterator<_View, _Np, _AnyConst>& __iter) { 351*bdd1243dSDimitry Andric return (__iter.__current_); 352*bdd1243dSDimitry Andric } 353*bdd1243dSDimitry Andric 354*bdd1243dSDimitry Andric public: 355*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI __elements_view_sentinel() = default; 356*bdd1243dSDimitry Andric 357*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit __elements_view_sentinel(sentinel_t<_Base> __end) 358*bdd1243dSDimitry Andric : __end_(std::move(__end)) {} 359*bdd1243dSDimitry Andric 360*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr __elements_view_sentinel(__elements_view_sentinel<_View, _Np, !_Const> __other) 361*bdd1243dSDimitry Andric requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>> 362*bdd1243dSDimitry Andric : __end_(std::move(__other.__end_)) {} 363*bdd1243dSDimitry Andric 364*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; } 365*bdd1243dSDimitry Andric 366*bdd1243dSDimitry Andric template <bool _OtherConst> 367*bdd1243dSDimitry Andric requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>> 368*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool 369*bdd1243dSDimitry Andric operator==(const __elements_view_iterator<_View, _Np, _OtherConst>& __x, const __elements_view_sentinel& __y) { 370*bdd1243dSDimitry Andric return __get_current(__x) == __y.__end_; 371*bdd1243dSDimitry Andric } 372*bdd1243dSDimitry Andric 373*bdd1243dSDimitry Andric template <bool _OtherConst> 374*bdd1243dSDimitry Andric requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>> 375*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>> 376*bdd1243dSDimitry Andric operator-(const __elements_view_iterator<_View, _Np, _OtherConst>& __x, const __elements_view_sentinel& __y) { 377*bdd1243dSDimitry Andric return __get_current(__x) - __y.__end_; 378*bdd1243dSDimitry Andric } 379*bdd1243dSDimitry Andric 380*bdd1243dSDimitry Andric template <bool _OtherConst> 381*bdd1243dSDimitry Andric requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>> 382*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>> 383*bdd1243dSDimitry Andric operator-(const __elements_view_sentinel& __x, const __elements_view_iterator<_View, _Np, _OtherConst>& __y) { 384*bdd1243dSDimitry Andric return __x.__end_ - __get_current(__y); 385*bdd1243dSDimitry Andric } 386*bdd1243dSDimitry Andric }; 387*bdd1243dSDimitry Andric 388*bdd1243dSDimitry Andric template <class _Tp, size_t _Np> 389*bdd1243dSDimitry Andric inline constexpr bool enable_borrowed_range<elements_view<_Tp, _Np>> = enable_borrowed_range<_Tp>; 390*bdd1243dSDimitry Andric 391*bdd1243dSDimitry Andric template <class _Tp> 392*bdd1243dSDimitry Andric using keys_view = elements_view<_Tp, 0>; 393*bdd1243dSDimitry Andric template <class _Tp> 394*bdd1243dSDimitry Andric using values_view = elements_view<_Tp, 1>; 395*bdd1243dSDimitry Andric 396*bdd1243dSDimitry Andric namespace views { 397*bdd1243dSDimitry Andric namespace __elements { 398*bdd1243dSDimitry Andric 399*bdd1243dSDimitry Andric template <size_t _Np> 400*bdd1243dSDimitry Andric struct __fn : __range_adaptor_closure<__fn<_Np>> { 401*bdd1243dSDimitry Andric template <class _Range> 402*bdd1243dSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const 403*bdd1243dSDimitry Andric /**/ noexcept(noexcept(elements_view<all_t<_Range&&>, _Np>(std::forward<_Range>(__range)))) 404*bdd1243dSDimitry Andric /*------*/ -> decltype(elements_view<all_t<_Range&&>, _Np>(std::forward<_Range>(__range))) { 405*bdd1243dSDimitry Andric /*-------------*/ return elements_view<all_t<_Range&&>, _Np>(std::forward<_Range>(__range)); 406*bdd1243dSDimitry Andric } 407*bdd1243dSDimitry Andric }; 408*bdd1243dSDimitry Andric } // namespace __elements 409*bdd1243dSDimitry Andric 410*bdd1243dSDimitry Andric inline namespace __cpo { 411*bdd1243dSDimitry Andric template <size_t _Np> 412*bdd1243dSDimitry Andric inline constexpr auto elements = __elements::__fn<_Np>{}; 413*bdd1243dSDimitry Andric inline constexpr auto keys = elements<0>; 414*bdd1243dSDimitry Andric inline constexpr auto values = elements<1>; 415*bdd1243dSDimitry Andric } // namespace __cpo 416*bdd1243dSDimitry Andric } // namespace views 417*bdd1243dSDimitry Andric } // namespace ranges 418*bdd1243dSDimitry Andric 419*bdd1243dSDimitry Andric #endif // _LIBCPP_STD_VER >= 20 420*bdd1243dSDimitry Andric 421*bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD 422*bdd1243dSDimitry Andric 423*bdd1243dSDimitry Andric #endif // _LIBCPP___RANGES_ELEMENTS_VIEW_H 424