You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3657 lines
97KB

  1. // <ranges> -*- C++ -*-
  2. // Copyright (C) 2019-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/ranges
  21. * This is a Standard C++ Library header.
  22. * @ingroup concepts
  23. */
  24. #ifndef _GLIBCXX_RANGES
  25. #define _GLIBCXX_RANGES 1
  26. #if __cplusplus > 201703L
  27. #pragma GCC system_header
  28. #include <concepts>
  29. #if __cpp_lib_concepts
  30. #include <bits/refwrap.h>
  31. #include <compare>
  32. #include <initializer_list>
  33. #include <iterator>
  34. #include <optional>
  35. #include <tuple>
  36. /**
  37. * @defgroup ranges Ranges
  38. *
  39. * Components for dealing with ranges of elements.
  40. */
  41. namespace std _GLIBCXX_VISIBILITY(default)
  42. {
  43. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  44. namespace ranges
  45. {
  46. // [range.range] The range concept.
  47. // [range.sized] The sized_range concept.
  48. // Defined in <bits/range_access.h>
  49. // [range.refinements]
  50. // Defined in <bits/range_access.h>
  51. struct view_base { };
  52. template<typename _Tp>
  53. inline constexpr bool enable_view = derived_from<_Tp, view_base>;
  54. template<typename _Tp>
  55. concept view
  56. = range<_Tp> && movable<_Tp> && default_initializable<_Tp>
  57. && enable_view<_Tp>;
  58. /// A range which can be safely converted to a view.
  59. template<typename _Tp>
  60. concept viewable_range = range<_Tp>
  61. && (borrowed_range<_Tp> || view<remove_cvref_t<_Tp>>);
  62. namespace __detail
  63. {
  64. template<typename _Range>
  65. concept __simple_view = view<_Range> && range<const _Range>
  66. && same_as<iterator_t<_Range>, iterator_t<const _Range>>
  67. && same_as<sentinel_t<_Range>, sentinel_t<const _Range>>;
  68. template<typename _It>
  69. concept __has_arrow = input_iterator<_It>
  70. && (is_pointer_v<_It> || requires(_It __it) { __it.operator->(); });
  71. template<typename _Tp, typename _Up>
  72. concept __not_same_as
  73. = !same_as<remove_cvref_t<_Tp>, remove_cvref_t<_Up>>;
  74. } // namespace __detail
  75. template<typename _Derived>
  76. requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
  77. class view_interface : public view_base
  78. {
  79. private:
  80. constexpr _Derived& _M_derived() noexcept
  81. {
  82. static_assert(derived_from<_Derived, view_interface<_Derived>>);
  83. static_assert(view<_Derived>);
  84. return static_cast<_Derived&>(*this);
  85. }
  86. constexpr const _Derived& _M_derived() const noexcept
  87. {
  88. static_assert(derived_from<_Derived, view_interface<_Derived>>);
  89. static_assert(view<_Derived>);
  90. return static_cast<const _Derived&>(*this);
  91. }
  92. public:
  93. constexpr bool
  94. empty() requires forward_range<_Derived>
  95. { return ranges::begin(_M_derived()) == ranges::end(_M_derived()); }
  96. constexpr bool
  97. empty() const requires forward_range<const _Derived>
  98. { return ranges::begin(_M_derived()) == ranges::end(_M_derived()); }
  99. constexpr explicit
  100. operator bool() requires requires { ranges::empty(_M_derived()); }
  101. { return !ranges::empty(_M_derived()); }
  102. constexpr explicit
  103. operator bool() const requires requires { ranges::empty(_M_derived()); }
  104. { return !ranges::empty(_M_derived()); }
  105. constexpr auto
  106. data() requires contiguous_iterator<iterator_t<_Derived>>
  107. { return to_address(ranges::begin(_M_derived())); }
  108. constexpr auto
  109. data() const
  110. requires range<const _Derived>
  111. && contiguous_iterator<iterator_t<const _Derived>>
  112. { return to_address(ranges::begin(_M_derived())); }
  113. constexpr auto
  114. size()
  115. requires forward_range<_Derived>
  116. && sized_sentinel_for<sentinel_t<_Derived>, iterator_t<_Derived>>
  117. { return ranges::end(_M_derived()) - ranges::begin(_M_derived()); }
  118. constexpr auto
  119. size() const
  120. requires forward_range<const _Derived>
  121. && sized_sentinel_for<sentinel_t<const _Derived>,
  122. iterator_t<const _Derived>>
  123. { return ranges::end(_M_derived()) - ranges::begin(_M_derived()); }
  124. constexpr decltype(auto)
  125. front() requires forward_range<_Derived>
  126. {
  127. __glibcxx_assert(!empty());
  128. return *ranges::begin(_M_derived());
  129. }
  130. constexpr decltype(auto)
  131. front() const requires forward_range<const _Derived>
  132. {
  133. __glibcxx_assert(!empty());
  134. return *ranges::begin(_M_derived());
  135. }
  136. constexpr decltype(auto)
  137. back()
  138. requires bidirectional_range<_Derived> && common_range<_Derived>
  139. {
  140. __glibcxx_assert(!empty());
  141. return *ranges::prev(ranges::end(_M_derived()));
  142. }
  143. constexpr decltype(auto)
  144. back() const
  145. requires bidirectional_range<const _Derived>
  146. && common_range<const _Derived>
  147. {
  148. __glibcxx_assert(!empty());
  149. return *ranges::prev(ranges::end(_M_derived()));
  150. }
  151. template<random_access_range _Range = _Derived>
  152. constexpr decltype(auto)
  153. operator[](range_difference_t<_Range> __n)
  154. { return ranges::begin(_M_derived())[__n]; }
  155. template<random_access_range _Range = const _Derived>
  156. constexpr decltype(auto)
  157. operator[](range_difference_t<_Range> __n) const
  158. { return ranges::begin(_M_derived())[__n]; }
  159. };
  160. namespace __detail
  161. {
  162. template<class _From, class _To>
  163. concept __convertible_to_non_slicing = convertible_to<_From, _To>
  164. && !(is_pointer_v<decay_t<_From>> && is_pointer_v<decay_t<_To>>
  165. && __not_same_as<remove_pointer_t<decay_t<_From>>,
  166. remove_pointer_t<decay_t<_To>>>);
  167. template<typename _Tp>
  168. concept __pair_like
  169. = !is_reference_v<_Tp> && requires(_Tp __t)
  170. {
  171. typename tuple_size<_Tp>::type;
  172. requires derived_from<tuple_size<_Tp>, integral_constant<size_t, 2>>;
  173. typename tuple_element_t<0, remove_const_t<_Tp>>;
  174. typename tuple_element_t<1, remove_const_t<_Tp>>;
  175. { get<0>(__t) } -> convertible_to<const tuple_element_t<0, _Tp>&>;
  176. { get<1>(__t) } -> convertible_to<const tuple_element_t<1, _Tp>&>;
  177. };
  178. template<typename _Tp, typename _Up, typename _Vp>
  179. concept __pair_like_convertible_from
  180. = !range<_Tp> && __pair_like<_Tp>
  181. && constructible_from<_Tp, _Up, _Vp>
  182. && __convertible_to_non_slicing<_Up, tuple_element_t<0, _Tp>>
  183. && convertible_to<_Vp, tuple_element_t<1, _Tp>>;
  184. template<typename _Tp>
  185. concept __iterator_sentinel_pair
  186. = !range<_Tp> && __pair_like<_Tp>
  187. && sentinel_for<tuple_element_t<1, _Tp>, tuple_element_t<0, _Tp>>;
  188. } // namespace __detail
  189. enum class subrange_kind : bool { unsized, sized };
  190. template<input_or_output_iterator _It, sentinel_for<_It> _Sent = _It,
  191. subrange_kind _Kind = sized_sentinel_for<_Sent, _It>
  192. ? subrange_kind::sized : subrange_kind::unsized>
  193. requires (_Kind == subrange_kind::sized || !sized_sentinel_for<_Sent, _It>)
  194. class subrange : public view_interface<subrange<_It, _Sent, _Kind>>
  195. {
  196. private:
  197. // XXX: gcc complains when using constexpr here
  198. static const bool _S_store_size
  199. = _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _It>;
  200. _It _M_begin = _It();
  201. _Sent _M_end = _Sent();
  202. template<typename, bool = _S_store_size>
  203. struct _Size
  204. { };
  205. template<typename _Tp>
  206. struct _Size<_Tp, true>
  207. { __detail::__make_unsigned_like_t<_Tp> _M_size; };
  208. [[no_unique_address]] _Size<iter_difference_t<_It>> _M_size = {};
  209. public:
  210. subrange() = default;
  211. constexpr
  212. subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s)
  213. requires (!_S_store_size)
  214. : _M_begin(std::move(__i)), _M_end(__s)
  215. { }
  216. constexpr
  217. subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s,
  218. __detail::__make_unsigned_like_t<iter_difference_t<_It>> __n)
  219. requires (_Kind == subrange_kind::sized)
  220. : _M_begin(std::move(__i)), _M_end(__s)
  221. {
  222. using __detail::__to_unsigned_like;
  223. __glibcxx_assert(__n == __to_unsigned_like(ranges::distance(__i, __s)));
  224. if constexpr (_S_store_size)
  225. _M_size._M_size = __n;
  226. }
  227. template<__detail::__not_same_as<subrange> _Rng>
  228. requires borrowed_range<_Rng>
  229. && __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
  230. && convertible_to<sentinel_t<_Rng>, _Sent>
  231. constexpr
  232. subrange(_Rng&& __r) requires _S_store_size && sized_range<_Rng>
  233. : subrange{__r, ranges::size(__r)}
  234. { }
  235. template<__detail::__not_same_as<subrange> _Rng>
  236. requires borrowed_range<_Rng>
  237. && __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
  238. && convertible_to<sentinel_t<_Rng>, _Sent>
  239. constexpr
  240. subrange(_Rng&& __r) requires (!_S_store_size)
  241. : subrange{ranges::begin(__r), ranges::end(__r)}
  242. { }
  243. template<borrowed_range _Rng>
  244. requires __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
  245. && convertible_to<sentinel_t<_Rng>, _Sent>
  246. constexpr
  247. subrange(_Rng&& __r,
  248. __detail::__make_unsigned_like_t<iter_difference_t<_It>> __n)
  249. requires (_Kind == subrange_kind::sized)
  250. : subrange{ranges::begin(__r), ranges::end(__r), __n}
  251. { }
  252. template<__detail::__not_same_as<subrange> _PairLike>
  253. requires __detail::__pair_like_convertible_from<_PairLike, const _It&,
  254. const _Sent&>
  255. constexpr
  256. operator _PairLike() const
  257. { return _PairLike(_M_begin, _M_end); }
  258. constexpr _It
  259. begin() const requires copyable<_It>
  260. { return _M_begin; }
  261. [[nodiscard]] constexpr _It
  262. begin() requires (!copyable<_It>)
  263. { return std::move(_M_begin); }
  264. constexpr _Sent end() const { return _M_end; }
  265. constexpr bool empty() const { return _M_begin == _M_end; }
  266. constexpr __detail::__make_unsigned_like_t<iter_difference_t<_It>>
  267. size() const requires (_Kind == subrange_kind::sized)
  268. {
  269. if constexpr (_S_store_size)
  270. return _M_size._M_size;
  271. else
  272. return __detail::__to_unsigned_like(_M_end - _M_begin);
  273. }
  274. [[nodiscard]] constexpr subrange
  275. next(iter_difference_t<_It> __n = 1) const &
  276. requires forward_iterator<_It>
  277. {
  278. auto __tmp = *this;
  279. __tmp.advance(__n);
  280. return __tmp;
  281. }
  282. [[nodiscard]] constexpr subrange
  283. next(iter_difference_t<_It> __n = 1) &&
  284. {
  285. advance(__n);
  286. return std::move(*this);
  287. }
  288. [[nodiscard]] constexpr subrange
  289. prev(iter_difference_t<_It> __n = 1) const
  290. requires bidirectional_iterator<_It>
  291. {
  292. auto __tmp = *this;
  293. __tmp.advance(-__n);
  294. return __tmp;
  295. }
  296. constexpr subrange&
  297. advance(iter_difference_t<_It> __n)
  298. {
  299. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  300. // 3433. subrange::advance(n) has UB when n < 0
  301. if constexpr (bidirectional_iterator<_It>)
  302. if (__n < 0)
  303. {
  304. ranges::advance(_M_begin, __n);
  305. if constexpr (_S_store_size)
  306. _M_size._M_size += __detail::__to_unsigned_like(-__n);
  307. return *this;
  308. }
  309. __glibcxx_assert(__n >= 0);
  310. auto __d = __n - ranges::advance(_M_begin, __n, _M_end);
  311. if constexpr (_S_store_size)
  312. _M_size._M_size -= __detail::__to_unsigned_like(__d);
  313. return *this;
  314. }
  315. };
  316. template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
  317. subrange(_It, _Sent) -> subrange<_It, _Sent>;
  318. template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
  319. subrange(_It, _Sent,
  320. __detail::__make_unsigned_like_t<iter_difference_t<_It>>)
  321. -> subrange<_It, _Sent, subrange_kind::sized>;
  322. template<__detail::__iterator_sentinel_pair _Pr>
  323. subrange(_Pr)
  324. -> subrange<tuple_element_t<0, _Pr>, tuple_element_t<1, _Pr>>;
  325. template<__detail::__iterator_sentinel_pair _Pr>
  326. subrange(_Pr, __detail::__make_unsigned_like_t<iter_difference_t<
  327. tuple_element_t<0, _Pr>>>)
  328. -> subrange<tuple_element_t<0, _Pr>, tuple_element_t<1, _Pr>,
  329. subrange_kind::sized>;
  330. template<borrowed_range _Rng>
  331. subrange(_Rng&&)
  332. -> subrange<iterator_t<_Rng>, sentinel_t<_Rng>,
  333. (sized_range<_Rng>
  334. || sized_sentinel_for<sentinel_t<_Rng>, iterator_t<_Rng>>)
  335. ? subrange_kind::sized : subrange_kind::unsized>;
  336. template<borrowed_range _Rng>
  337. subrange(_Rng&&,
  338. __detail::__make_unsigned_like_t<range_difference_t<_Rng>>)
  339. -> subrange<iterator_t<_Rng>, sentinel_t<_Rng>, subrange_kind::sized>;
  340. template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
  341. requires (_Num < 2)
  342. constexpr auto
  343. get(const subrange<_It, _Sent, _Kind>& __r)
  344. {
  345. if constexpr (_Num == 0)
  346. return __r.begin();
  347. else
  348. return __r.end();
  349. }
  350. template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
  351. requires (_Num < 2)
  352. constexpr auto
  353. get(subrange<_It, _Sent, _Kind>&& __r)
  354. {
  355. if constexpr (_Num == 0)
  356. return __r.begin();
  357. else
  358. return __r.end();
  359. }
  360. template<input_or_output_iterator _It, sentinel_for<_It> _Sent,
  361. subrange_kind _Kind>
  362. inline constexpr bool
  363. enable_borrowed_range<subrange<_It, _Sent, _Kind>> = true;
  364. } // namespace ranges
  365. using ranges::get;
  366. namespace ranges
  367. {
  368. /// Type returned by algorithms instead of a dangling iterator or subrange.
  369. struct dangling
  370. {
  371. constexpr dangling() noexcept = default;
  372. template<typename... _Args>
  373. constexpr dangling(_Args&&...) noexcept { }
  374. };
  375. template<range _Range>
  376. using borrowed_iterator_t = conditional_t<borrowed_range<_Range>,
  377. iterator_t<_Range>,
  378. dangling>;
  379. template<range _Range>
  380. using borrowed_subrange_t = conditional_t<borrowed_range<_Range>,
  381. subrange<iterator_t<_Range>>,
  382. dangling>;
  383. template<typename _Tp> requires is_object_v<_Tp>
  384. class empty_view
  385. : public view_interface<empty_view<_Tp>>
  386. {
  387. public:
  388. static constexpr _Tp* begin() noexcept { return nullptr; }
  389. static constexpr _Tp* end() noexcept { return nullptr; }
  390. static constexpr _Tp* data() noexcept { return nullptr; }
  391. static constexpr size_t size() noexcept { return 0; }
  392. static constexpr bool empty() noexcept { return true; }
  393. };
  394. template<typename _Tp>
  395. inline constexpr bool enable_borrowed_range<empty_view<_Tp>> = true;
  396. namespace __detail
  397. {
  398. template<copy_constructible _Tp> requires is_object_v<_Tp>
  399. struct __box : std::optional<_Tp>
  400. {
  401. using std::optional<_Tp>::optional;
  402. constexpr
  403. __box()
  404. noexcept(is_nothrow_default_constructible_v<_Tp>)
  405. requires default_initializable<_Tp>
  406. : std::optional<_Tp>{std::in_place}
  407. { }
  408. __box(const __box&) = default;
  409. __box(__box&&) = default;
  410. using std::optional<_Tp>::operator=;
  411. __box&
  412. operator=(const __box& __that)
  413. noexcept(is_nothrow_copy_constructible_v<_Tp>)
  414. requires (!assignable_from<_Tp&, const _Tp&>)
  415. {
  416. if ((bool)__that)
  417. this->emplace(*__that);
  418. else
  419. this->reset();
  420. return *this;
  421. }
  422. __box&
  423. operator=(__box&& __that)
  424. noexcept(is_nothrow_move_constructible_v<_Tp>)
  425. requires (!assignable_from<_Tp&, _Tp>)
  426. {
  427. if ((bool)__that)
  428. this->emplace(std::move(*__that));
  429. else
  430. this->reset();
  431. return *this;
  432. }
  433. };
  434. } // namespace __detail
  435. /// A view that contains exactly one element.
  436. template<copy_constructible _Tp> requires is_object_v<_Tp>
  437. class single_view : public view_interface<single_view<_Tp>>
  438. {
  439. public:
  440. single_view() = default;
  441. constexpr explicit
  442. single_view(const _Tp& __t)
  443. : _M_value(__t)
  444. { }
  445. constexpr explicit
  446. single_view(_Tp&& __t)
  447. : _M_value(std::move(__t))
  448. { }
  449. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  450. // 3428. single_view's in place constructor should be explicit
  451. template<typename... _Args>
  452. requires constructible_from<_Tp, _Args...>
  453. constexpr explicit
  454. single_view(in_place_t, _Args&&... __args)
  455. : _M_value{in_place, std::forward<_Args>(__args)...}
  456. { }
  457. constexpr _Tp*
  458. begin() noexcept
  459. { return data(); }
  460. constexpr const _Tp*
  461. begin() const noexcept
  462. { return data(); }
  463. constexpr _Tp*
  464. end() noexcept
  465. { return data() + 1; }
  466. constexpr const _Tp*
  467. end() const noexcept
  468. { return data() + 1; }
  469. static constexpr size_t
  470. size() noexcept
  471. { return 1; }
  472. constexpr _Tp*
  473. data() noexcept
  474. { return _M_value.operator->(); }
  475. constexpr const _Tp*
  476. data() const noexcept
  477. { return _M_value.operator->(); }
  478. private:
  479. __detail::__box<_Tp> _M_value;
  480. };
  481. namespace __detail
  482. {
  483. template<typename _Wp>
  484. constexpr auto __to_signed_like(_Wp __w) noexcept
  485. {
  486. if constexpr (!integral<_Wp>)
  487. return iter_difference_t<_Wp>();
  488. else if constexpr (sizeof(iter_difference_t<_Wp>) > sizeof(_Wp))
  489. return iter_difference_t<_Wp>(__w);
  490. else if constexpr (sizeof(ptrdiff_t) > sizeof(_Wp))
  491. return ptrdiff_t(__w);
  492. else if constexpr (sizeof(long long) > sizeof(_Wp))
  493. return (long long)(__w);
  494. #ifdef __SIZEOF_INT128__
  495. else if constexpr (__SIZEOF_INT128__ > sizeof(_Wp))
  496. return __int128(__w);
  497. #endif
  498. else
  499. return __max_diff_type(__w);
  500. }
  501. template<typename _Wp>
  502. using __iota_diff_t = decltype(__to_signed_like(std::declval<_Wp>()));
  503. template<typename _It>
  504. concept __decrementable = incrementable<_It>
  505. && requires(_It __i)
  506. {
  507. { --__i } -> same_as<_It&>;
  508. { __i-- } -> same_as<_It>;
  509. };
  510. template<typename _It>
  511. concept __advanceable = __decrementable<_It> && totally_ordered<_It>
  512. && requires( _It __i, const _It __j, const __iota_diff_t<_It> __n)
  513. {
  514. { __i += __n } -> same_as<_It&>;
  515. { __i -= __n } -> same_as<_It&>;
  516. _It(__j + __n);
  517. _It(__n + __j);
  518. _It(__j - __n);
  519. { __j - __j } -> convertible_to<__iota_diff_t<_It>>;
  520. };
  521. } // namespace __detail
  522. template<weakly_incrementable _Winc,
  523. semiregular _Bound = unreachable_sentinel_t>
  524. requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound>
  525. && semiregular<_Winc>
  526. class iota_view : public view_interface<iota_view<_Winc, _Bound>>
  527. {
  528. private:
  529. struct _Sentinel;
  530. struct _Iterator
  531. {
  532. private:
  533. static auto
  534. _S_iter_cat()
  535. {
  536. using namespace __detail;
  537. if constexpr (__advanceable<_Winc>)
  538. return random_access_iterator_tag{};
  539. else if constexpr (__decrementable<_Winc>)
  540. return bidirectional_iterator_tag{};
  541. else if constexpr (incrementable<_Winc>)
  542. return forward_iterator_tag{};
  543. else
  544. return input_iterator_tag{};
  545. }
  546. public:
  547. using iterator_category = decltype(_S_iter_cat());
  548. using value_type = _Winc;
  549. using difference_type = __detail::__iota_diff_t<_Winc>;
  550. _Iterator() = default;
  551. constexpr explicit
  552. _Iterator(_Winc __value)
  553. : _M_value(__value) { }
  554. constexpr _Winc
  555. operator*() const noexcept(is_nothrow_copy_constructible_v<_Winc>)
  556. { return _M_value; }
  557. constexpr _Iterator&
  558. operator++()
  559. {
  560. ++_M_value;
  561. return *this;
  562. }
  563. constexpr void
  564. operator++(int)
  565. { ++*this; }
  566. constexpr _Iterator
  567. operator++(int) requires incrementable<_Winc>
  568. {
  569. auto __tmp = *this;
  570. ++*this;
  571. return __tmp;
  572. }
  573. constexpr _Iterator&
  574. operator--() requires __detail::__decrementable<_Winc>
  575. {
  576. --_M_value;
  577. return *this;
  578. }
  579. constexpr _Iterator
  580. operator--(int) requires __detail::__decrementable<_Winc>
  581. {
  582. auto __tmp = *this;
  583. --*this;
  584. return __tmp;
  585. }
  586. constexpr _Iterator&
  587. operator+=(difference_type __n) requires __detail::__advanceable<_Winc>
  588. {
  589. using __detail::__is_integer_like;
  590. using __detail::__is_signed_integer_like;
  591. if constexpr (__is_integer_like<_Winc>
  592. && !__is_signed_integer_like<_Winc>)
  593. {
  594. if (__n >= difference_type(0))
  595. _M_value += static_cast<_Winc>(__n);
  596. else
  597. _M_value -= static_cast<_Winc>(-__n);
  598. }
  599. else
  600. _M_value += __n;
  601. return *this;
  602. }
  603. constexpr _Iterator&
  604. operator-=(difference_type __n) requires __detail::__advanceable<_Winc>
  605. {
  606. using __detail::__is_integer_like;
  607. using __detail::__is_signed_integer_like;
  608. if constexpr (__is_integer_like<_Winc>
  609. && !__is_signed_integer_like<_Winc>)
  610. {
  611. if (__n >= difference_type(0))
  612. _M_value -= static_cast<_Winc>(__n);
  613. else
  614. _M_value += static_cast<_Winc>(-__n);
  615. }
  616. else
  617. _M_value -= __n;
  618. return *this;
  619. }
  620. constexpr _Winc
  621. operator[](difference_type __n) const
  622. requires __detail::__advanceable<_Winc>
  623. { return _Winc(_M_value + __n); }
  624. friend constexpr bool
  625. operator==(const _Iterator& __x, const _Iterator& __y)
  626. requires equality_comparable<_Winc>
  627. { return __x._M_value == __y._M_value; }
  628. friend constexpr bool
  629. operator<(const _Iterator& __x, const _Iterator& __y)
  630. requires totally_ordered<_Winc>
  631. { return __x._M_value < __y._M_value; }
  632. friend constexpr bool
  633. operator>(const _Iterator& __x, const _Iterator& __y)
  634. requires totally_ordered<_Winc>
  635. { return __y < __x; }
  636. friend constexpr bool
  637. operator<=(const _Iterator& __x, const _Iterator& __y)
  638. requires totally_ordered<_Winc>
  639. { return !(__y < __x); }
  640. friend constexpr bool
  641. operator>=(const _Iterator& __x, const _Iterator& __y)
  642. requires totally_ordered<_Winc>
  643. { return !(__x < __y); }
  644. #ifdef __cpp_lib_three_way_comparison
  645. friend constexpr auto
  646. operator<=>(const _Iterator& __x, const _Iterator& __y)
  647. requires totally_ordered<_Winc> && three_way_comparable<_Winc>
  648. { return __x._M_value <=> __y._M_value; }
  649. #endif
  650. friend constexpr _Iterator
  651. operator+(_Iterator __i, difference_type __n)
  652. requires __detail::__advanceable<_Winc>
  653. { return __i += __n; }
  654. friend constexpr _Iterator
  655. operator+(difference_type __n, _Iterator __i)
  656. requires __detail::__advanceable<_Winc>
  657. { return __i += __n; }
  658. friend constexpr _Iterator
  659. operator-(_Iterator __i, difference_type __n)
  660. requires __detail::__advanceable<_Winc>
  661. { return __i -= __n; }
  662. friend constexpr difference_type
  663. operator-(const _Iterator& __x, const _Iterator& __y)
  664. requires __detail::__advanceable<_Winc>
  665. {
  666. using __detail::__is_integer_like;
  667. using __detail::__is_signed_integer_like;
  668. using _Dt = difference_type;
  669. if constexpr (__is_integer_like<_Winc>)
  670. {
  671. if constexpr (__is_signed_integer_like<_Winc>)
  672. return _Dt(_Dt(__x._M_value) - _Dt(__y._M_value));
  673. else
  674. return (__y._M_value > __x._M_value)
  675. ? _Dt(-_Dt(__y._M_value - __x._M_value))
  676. : _Dt(__x._M_value - __y._M_value);
  677. }
  678. else
  679. return __x._M_value - __y._M_value;
  680. }
  681. private:
  682. _Winc _M_value = _Winc();
  683. friend _Sentinel;
  684. };
  685. struct _Sentinel
  686. {
  687. private:
  688. constexpr bool
  689. _M_equal(const _Iterator& __x) const
  690. { return __x._M_value == _M_bound; }
  691. _Bound _M_bound = _Bound();
  692. public:
  693. _Sentinel() = default;
  694. constexpr explicit
  695. _Sentinel(_Bound __bound)
  696. : _M_bound(__bound) { }
  697. friend constexpr bool
  698. operator==(const _Iterator& __x, const _Sentinel& __y)
  699. { return __y._M_equal(__x); }
  700. friend constexpr iter_difference_t<_Winc>
  701. operator-(const _Iterator& __x, const _Sentinel& __y)
  702. requires sized_sentinel_for<_Bound, _Winc>
  703. { return __x._M_value - __y._M_bound; }
  704. friend constexpr iter_difference_t<_Winc>
  705. operator-(const _Sentinel& __x, const _Iterator& __y)
  706. requires sized_sentinel_for<_Bound, _Winc>
  707. { return -(__y - __x); }
  708. };
  709. _Winc _M_value = _Winc();
  710. _Bound _M_bound = _Bound();
  711. public:
  712. iota_view() = default;
  713. constexpr explicit
  714. iota_view(_Winc __value)
  715. : _M_value(__value)
  716. { }
  717. constexpr
  718. iota_view(type_identity_t<_Winc> __value,
  719. type_identity_t<_Bound> __bound)
  720. : _M_value(__value), _M_bound(__bound)
  721. {
  722. if constexpr (totally_ordered_with<_Winc, _Bound>)
  723. {
  724. __glibcxx_assert( bool(__value <= __bound) );
  725. }
  726. }
  727. constexpr _Iterator
  728. begin() const { return _Iterator{_M_value}; }
  729. constexpr auto
  730. end() const
  731. {
  732. if constexpr (same_as<_Bound, unreachable_sentinel_t>)
  733. return unreachable_sentinel;
  734. else
  735. return _Sentinel{_M_bound};
  736. }
  737. constexpr _Iterator
  738. end() const requires same_as<_Winc, _Bound>
  739. { return _Iterator{_M_bound}; }
  740. constexpr auto
  741. size() const
  742. requires (same_as<_Winc, _Bound> && __detail::__advanceable<_Winc>)
  743. || (integral<_Winc> && integral<_Bound>)
  744. || sized_sentinel_for<_Bound, _Winc>
  745. {
  746. using __detail::__is_integer_like;
  747. using __detail::__to_unsigned_like;
  748. if constexpr (__is_integer_like<_Winc> && __is_integer_like<_Bound>)
  749. return (_M_value < 0)
  750. ? ((_M_bound < 0)
  751. ? __to_unsigned_like(-_M_value) - __to_unsigned_like(-_M_bound)
  752. : __to_unsigned_like(_M_bound) + __to_unsigned_like(-_M_value))
  753. : __to_unsigned_like(_M_bound) - __to_unsigned_like(_M_value);
  754. else
  755. return __to_unsigned_like(_M_bound - _M_value);
  756. }
  757. };
  758. template<typename _Winc, typename _Bound>
  759. requires (!__detail::__is_integer_like<_Winc>
  760. || !__detail::__is_integer_like<_Bound>
  761. || (__detail::__is_signed_integer_like<_Winc>
  762. == __detail::__is_signed_integer_like<_Bound>))
  763. iota_view(_Winc, _Bound) -> iota_view<_Winc, _Bound>;
  764. template<weakly_incrementable _Winc, semiregular _Bound>
  765. inline constexpr bool
  766. enable_borrowed_range<iota_view<_Winc, _Bound>> = true;
  767. namespace views
  768. {
  769. template<typename _Tp>
  770. inline constexpr empty_view<_Tp> empty{};
  771. struct _Single
  772. {
  773. template<typename _Tp>
  774. constexpr auto
  775. operator()(_Tp&& __e) const
  776. { return single_view{std::forward<_Tp>(__e)}; }
  777. };
  778. inline constexpr _Single single{};
  779. struct _Iota
  780. {
  781. template<typename _Tp>
  782. constexpr auto
  783. operator()(_Tp&& __e) const
  784. { return iota_view{std::forward<_Tp>(__e)}; }
  785. template<typename _Tp, typename _Up>
  786. constexpr auto
  787. operator()(_Tp&& __e, _Up&& __f) const
  788. { return iota_view{std::forward<_Tp>(__e), std::forward<_Up>(__f)}; }
  789. };
  790. inline constexpr _Iota iota{};
  791. } // namespace views
  792. namespace __detail
  793. {
  794. template<typename _Val, typename _CharT, typename _Traits>
  795. concept __stream_extractable
  796. = requires(basic_istream<_CharT, _Traits>& is, _Val& t) { is >> t; };
  797. } // namespace __detail
  798. template<movable _Val, typename _CharT, typename _Traits>
  799. requires default_initializable<_Val>
  800. && __detail::__stream_extractable<_Val, _CharT, _Traits>
  801. class basic_istream_view
  802. : public view_interface<basic_istream_view<_Val, _CharT, _Traits>>
  803. {
  804. public:
  805. basic_istream_view() = default;
  806. constexpr explicit
  807. basic_istream_view(basic_istream<_CharT, _Traits>& __stream)
  808. : _M_stream(std::__addressof(__stream))
  809. { }
  810. constexpr auto
  811. begin()
  812. {
  813. if (_M_stream != nullptr)
  814. *_M_stream >> _M_object;
  815. return _Iterator{*this};
  816. }
  817. constexpr default_sentinel_t
  818. end() const noexcept
  819. { return default_sentinel; }
  820. private:
  821. basic_istream<_CharT, _Traits>* _M_stream = nullptr;
  822. _Val _M_object = _Val();
  823. struct _Iterator
  824. {
  825. public:
  826. using iterator_concept = input_iterator_tag;
  827. using difference_type = ptrdiff_t;
  828. using value_type = _Val;
  829. _Iterator() = default;
  830. constexpr explicit
  831. _Iterator(basic_istream_view& __parent) noexcept
  832. : _M_parent(std::__addressof(__parent))
  833. { }
  834. _Iterator(const _Iterator&) = delete;
  835. _Iterator(_Iterator&&) = default;
  836. _Iterator& operator=(const _Iterator&) = delete;
  837. _Iterator& operator=(_Iterator&&) = default;
  838. _Iterator&
  839. operator++()
  840. {
  841. __glibcxx_assert(_M_parent->_M_stream != nullptr);
  842. *_M_parent->_M_stream >> _M_parent->_M_object;
  843. return *this;
  844. }
  845. void
  846. operator++(int)
  847. { ++*this; }
  848. _Val&
  849. operator*() const
  850. {
  851. __glibcxx_assert(_M_parent->_M_stream != nullptr);
  852. return _M_parent->_M_object;
  853. }
  854. friend bool
  855. operator==(const _Iterator& __x, default_sentinel_t)
  856. { return __x._M_at_end(); }
  857. private:
  858. basic_istream_view* _M_parent = nullptr;
  859. bool
  860. _M_at_end() const
  861. { return _M_parent == nullptr || !*_M_parent->_M_stream; }
  862. };
  863. friend _Iterator;
  864. };
  865. template<typename _Val, typename _CharT, typename _Traits>
  866. basic_istream_view<_Val, _CharT, _Traits>
  867. istream_view(basic_istream<_CharT, _Traits>& __s)
  868. { return basic_istream_view<_Val, _CharT, _Traits>{__s}; }
  869. namespace __detail
  870. {
  871. struct _Empty { };
  872. // Alias for a type that is conditionally present
  873. // (and is an empty type otherwise).
  874. // Data members using this alias should use [[no_unique_address]] so that
  875. // they take no space when not needed.
  876. template<bool _Present, typename _Tp>
  877. using __maybe_present_t = conditional_t<_Present, _Tp, _Empty>;
  878. // Alias for a type that is conditionally const.
  879. template<bool _Const, typename _Tp>
  880. using __maybe_const_t = conditional_t<_Const, const _Tp, _Tp>;
  881. } // namespace __detail
  882. namespace views
  883. {
  884. namespace __adaptor
  885. {
  886. template<typename _Tp>
  887. inline constexpr auto
  888. __maybe_refwrap(_Tp& __arg)
  889. { return reference_wrapper<_Tp>{__arg}; }
  890. template<typename _Tp>
  891. inline constexpr auto
  892. __maybe_refwrap(const _Tp& __arg)
  893. { return reference_wrapper<const _Tp>{__arg}; }
  894. template<typename _Tp>
  895. inline constexpr decltype(auto)
  896. __maybe_refwrap(_Tp&& __arg)
  897. { return std::forward<_Tp>(__arg); }
  898. template<typename _Callable>
  899. struct _RangeAdaptorClosure;
  900. template<typename _Callable>
  901. struct _RangeAdaptor
  902. {
  903. protected:
  904. [[no_unique_address]]
  905. __detail::__maybe_present_t<!is_default_constructible_v<_Callable>,
  906. _Callable> _M_callable;
  907. public:
  908. constexpr
  909. _RangeAdaptor(const _Callable& = {})
  910. requires is_default_constructible_v<_Callable>
  911. { }
  912. constexpr
  913. _RangeAdaptor(_Callable __callable)
  914. requires (!is_default_constructible_v<_Callable>)
  915. : _M_callable(std::move(__callable))
  916. { }
  917. template<typename... _Args>
  918. requires (sizeof...(_Args) >= 1)
  919. constexpr auto
  920. operator()(_Args&&... __args) const
  921. {
  922. // [range.adaptor.object]: If a range adaptor object accepts more
  923. // than one argument, then the following expressions are equivalent:
  924. //
  925. // (1) adaptor(range, args...)
  926. // (2) adaptor(args...)(range)
  927. // (3) range | adaptor(args...)
  928. //
  929. // In this case, adaptor(args...) is a range adaptor closure object.
  930. //
  931. // We handle (1) and (2) here, and (3) is just a special case of a
  932. // more general case already handled by _RangeAdaptorClosure.
  933. if constexpr (is_invocable_v<_Callable, _Args...>)
  934. {
  935. static_assert(sizeof...(_Args) != 1,
  936. "a _RangeAdaptor that accepts only one argument "
  937. "should be defined as a _RangeAdaptorClosure");
  938. // Here we handle adaptor(range, args...) -- just forward all
  939. // arguments to the underlying adaptor routine.
  940. return _Callable{}(std::forward<_Args>(__args)...);
  941. }
  942. else
  943. {
  944. // Here we handle adaptor(args...)(range).
  945. // Given args..., we return a _RangeAdaptorClosure that takes a
  946. // range argument, such that (2) is equivalent to (1).
  947. //
  948. // We need to be careful about how we capture args... in this
  949. // closure. By using __maybe_refwrap, we capture lvalue
  950. // references by reference (through a reference_wrapper) and
  951. // otherwise capture by value.
  952. auto __closure
  953. = [...__args(__maybe_refwrap(std::forward<_Args>(__args)))]
  954. <typename _Range> (_Range&& __r) {
  955. // This static_cast has two purposes: it forwards a
  956. // reference_wrapper<T> capture as a T&, and otherwise
  957. // forwards the captured argument as an rvalue.
  958. return _Callable{}(std::forward<_Range>(__r),
  959. (static_cast<unwrap_reference_t
  960. <remove_const_t<decltype(__args)>>>
  961. (__args))...);
  962. };
  963. using _ClosureType = decltype(__closure);
  964. return _RangeAdaptorClosure<_ClosureType>(std::move(__closure));
  965. }
  966. }
  967. };
  968. template<typename _Callable>
  969. _RangeAdaptor(_Callable) -> _RangeAdaptor<_Callable>;
  970. template<typename _Callable>
  971. struct _RangeAdaptorClosure : public _RangeAdaptor<_Callable>
  972. {
  973. using _RangeAdaptor<_Callable>::_RangeAdaptor;
  974. template<viewable_range _Range>
  975. requires requires { declval<_Callable>()(declval<_Range>()); }
  976. constexpr auto
  977. operator()(_Range&& __r) const
  978. {
  979. if constexpr (is_default_constructible_v<_Callable>)
  980. return _Callable{}(std::forward<_Range>(__r));
  981. else
  982. return this->_M_callable(std::forward<_Range>(__r));
  983. }
  984. template<viewable_range _Range>
  985. requires requires { declval<_Callable>()(declval<_Range>()); }
  986. friend constexpr auto
  987. operator|(_Range&& __r, const _RangeAdaptorClosure& __o)
  988. { return __o(std::forward<_Range>(__r)); }
  989. template<typename _Tp>
  990. friend constexpr auto
  991. operator|(const _RangeAdaptorClosure<_Tp>& __x,
  992. const _RangeAdaptorClosure& __y)
  993. {
  994. if constexpr (is_default_constructible_v<_Tp>
  995. && is_default_constructible_v<_Callable>)
  996. {
  997. auto __closure = [] <typename _Up> (_Up&& __e) {
  998. return std::forward<_Up>(__e) | decltype(__x){} | decltype(__y){};
  999. };
  1000. return _RangeAdaptorClosure<decltype(__closure)>(__closure);
  1001. }
  1002. else if constexpr (is_default_constructible_v<_Tp>
  1003. && !is_default_constructible_v<_Callable>)
  1004. {
  1005. auto __closure = [__y] <typename _Up> (_Up&& __e) {
  1006. return std::forward<_Up>(__e) | decltype(__x){} | __y;
  1007. };
  1008. return _RangeAdaptorClosure<decltype(__closure)>(__closure);
  1009. }
  1010. else if constexpr (!is_default_constructible_v<_Tp>
  1011. && is_default_constructible_v<_Callable>)
  1012. {
  1013. auto __closure = [__x] <typename _Up> (_Up&& __e) {
  1014. return std::forward<_Up>(__e) | __x | decltype(__y){};
  1015. };
  1016. return _RangeAdaptorClosure<decltype(__closure)>(__closure);
  1017. }
  1018. else
  1019. {
  1020. auto __closure = [__x, __y] <typename _Up> (_Up&& __e) {
  1021. return std::forward<_Up>(__e) | __x | __y;
  1022. };
  1023. return _RangeAdaptorClosure<decltype(__closure)>(__closure);
  1024. }
  1025. }
  1026. };
  1027. template<typename _Callable>
  1028. _RangeAdaptorClosure(_Callable) -> _RangeAdaptorClosure<_Callable>;
  1029. } // namespace __adaptor
  1030. } // namespace views
  1031. template<range _Range> requires is_object_v<_Range>
  1032. class ref_view : public view_interface<ref_view<_Range>>
  1033. {
  1034. private:
  1035. _Range* _M_r = nullptr;
  1036. static void _S_fun(_Range&); // not defined
  1037. static void _S_fun(_Range&&) = delete;
  1038. public:
  1039. constexpr
  1040. ref_view() noexcept = default;
  1041. template<__detail::__not_same_as<ref_view> _Tp>
  1042. requires convertible_to<_Tp, _Range&>
  1043. && requires { _S_fun(declval<_Tp>()); }
  1044. constexpr
  1045. ref_view(_Tp&& __t)
  1046. : _M_r(std::__addressof(static_cast<_Range&>(std::forward<_Tp>(__t))))
  1047. { }
  1048. constexpr _Range&
  1049. base() const
  1050. { return *_M_r; }
  1051. constexpr iterator_t<_Range>
  1052. begin() const
  1053. { return ranges::begin(*_M_r); }
  1054. constexpr sentinel_t<_Range>
  1055. end() const
  1056. { return ranges::end(*_M_r); }
  1057. constexpr bool
  1058. empty() const requires requires { ranges::empty(*_M_r); }
  1059. { return ranges::empty(*_M_r); }
  1060. constexpr auto
  1061. size() const requires sized_range<_Range>
  1062. { return ranges::size(*_M_r); }
  1063. constexpr auto
  1064. data() const requires contiguous_range<_Range>
  1065. { return ranges::data(*_M_r); }
  1066. };
  1067. template<typename _Range>
  1068. ref_view(_Range&) -> ref_view<_Range>;
  1069. template<typename _Tp>
  1070. inline constexpr bool enable_borrowed_range<ref_view<_Tp>> = true;
  1071. namespace views
  1072. {
  1073. inline constexpr __adaptor::_RangeAdaptorClosure all
  1074. = [] <viewable_range _Range> (_Range&& __r)
  1075. {
  1076. if constexpr (view<decay_t<_Range>>)
  1077. return std::forward<_Range>(__r);
  1078. else if constexpr (requires { ref_view{std::forward<_Range>(__r)}; })
  1079. return ref_view{std::forward<_Range>(__r)};
  1080. else
  1081. return subrange{std::forward<_Range>(__r)};
  1082. };
  1083. template<viewable_range _Range>
  1084. using all_t = decltype(all(std::declval<_Range>()));
  1085. } // namespace views
  1086. // XXX: the following algos are copied from ranges_algo.h to avoid a circular
  1087. // dependency with that header.
  1088. namespace __detail
  1089. {
  1090. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1091. typename _Proj = identity,
  1092. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  1093. constexpr _Iter
  1094. find_if(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {})
  1095. {
  1096. while (__first != __last
  1097. && !(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
  1098. ++__first;
  1099. return __first;
  1100. }
  1101. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1102. typename _Proj = identity,
  1103. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  1104. constexpr _Iter
  1105. find_if_not(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {})
  1106. {
  1107. while (__first != __last
  1108. && (bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
  1109. ++__first;
  1110. return __first;
  1111. }
  1112. template<typename _Tp, typename _Proj = identity,
  1113. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  1114. _Comp = ranges::less>
  1115. constexpr const _Tp&
  1116. min(const _Tp& __a, const _Tp& __b, _Comp __comp = {}, _Proj __proj = {})
  1117. {
  1118. if (std::__invoke(std::move(__comp),
  1119. std::__invoke(__proj, __b),
  1120. std::__invoke(__proj, __a)))
  1121. return __b;
  1122. else
  1123. return __a;
  1124. }
  1125. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  1126. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  1127. typename _Pred = ranges::equal_to,
  1128. typename _Proj1 = identity, typename _Proj2 = identity>
  1129. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  1130. constexpr pair<_Iter1, _Iter2>
  1131. mismatch(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
  1132. _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
  1133. {
  1134. while (__first1 != __last1 && __first2 != __last2
  1135. && (bool)std::__invoke(__pred,
  1136. std::__invoke(__proj1, *__first1),
  1137. std::__invoke(__proj2, *__first2)))
  1138. {
  1139. ++__first1;
  1140. ++__first2;
  1141. }
  1142. return { std::move(__first1), std::move(__first2) };
  1143. }
  1144. } // namespace __detail
  1145. namespace __detail
  1146. {
  1147. template<range _Range>
  1148. struct _CachedPosition
  1149. {
  1150. constexpr bool
  1151. _M_has_value() const
  1152. { return false; }
  1153. constexpr iterator_t<_Range>
  1154. _M_get(const _Range&) const
  1155. {
  1156. __glibcxx_assert(false);
  1157. return {};
  1158. }
  1159. constexpr void
  1160. _M_set(const _Range&, const iterator_t<_Range>&) const
  1161. { }
  1162. };
  1163. template<forward_range _Range>
  1164. struct _CachedPosition<_Range>
  1165. {
  1166. private:
  1167. iterator_t<_Range> _M_iter{};
  1168. public:
  1169. constexpr bool
  1170. _M_has_value() const
  1171. { return _M_iter != iterator_t<_Range>{}; }
  1172. constexpr iterator_t<_Range>
  1173. _M_get(const _Range&) const
  1174. {
  1175. __glibcxx_assert(_M_has_value());
  1176. return _M_iter;
  1177. }
  1178. constexpr void
  1179. _M_set(const _Range&, const iterator_t<_Range>& __it)
  1180. {
  1181. __glibcxx_assert(!_M_has_value());
  1182. _M_iter = __it;
  1183. }
  1184. };
  1185. template<random_access_range _Range>
  1186. requires (sizeof(range_difference_t<_Range>)
  1187. <= sizeof(iterator_t<_Range>))
  1188. struct _CachedPosition<_Range>
  1189. {
  1190. private:
  1191. range_difference_t<_Range> _M_offset = -1;
  1192. public:
  1193. constexpr bool
  1194. _M_has_value() const
  1195. { return _M_offset >= 0; }
  1196. constexpr iterator_t<_Range>
  1197. _M_get(_Range& __r) const
  1198. {
  1199. __glibcxx_assert(_M_has_value());
  1200. return ranges::begin(__r) + _M_offset;
  1201. }
  1202. constexpr void
  1203. _M_set(_Range& __r, const iterator_t<_Range>& __it)
  1204. {
  1205. __glibcxx_assert(!_M_has_value());
  1206. _M_offset = __it - ranges::begin(__r);
  1207. }
  1208. };
  1209. } // namespace __detail
  1210. template<input_range _Vp,
  1211. indirect_unary_predicate<iterator_t<_Vp>> _Pred>
  1212. requires view<_Vp> && is_object_v<_Pred>
  1213. class filter_view : public view_interface<filter_view<_Vp, _Pred>>
  1214. {
  1215. private:
  1216. struct _Sentinel;
  1217. struct _Iterator
  1218. {
  1219. private:
  1220. static constexpr auto
  1221. _S_iter_concept()
  1222. {
  1223. if constexpr (bidirectional_range<_Vp>)
  1224. return bidirectional_iterator_tag{};
  1225. else if constexpr (forward_range<_Vp>)
  1226. return forward_iterator_tag{};
  1227. else
  1228. return input_iterator_tag{};
  1229. }
  1230. static constexpr auto
  1231. _S_iter_cat()
  1232. {
  1233. using _Cat = typename iterator_traits<_Vp_iter>::iterator_category;
  1234. if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
  1235. return bidirectional_iterator_tag{};
  1236. else if constexpr (derived_from<_Cat, forward_iterator_tag>)
  1237. return forward_iterator_tag{};
  1238. else
  1239. return _Cat{};
  1240. }
  1241. friend filter_view;
  1242. using _Vp_iter = iterator_t<_Vp>;
  1243. _Vp_iter _M_current = _Vp_iter();
  1244. filter_view* _M_parent = nullptr;
  1245. public:
  1246. using iterator_concept = decltype(_S_iter_concept());
  1247. using iterator_category = decltype(_S_iter_cat());
  1248. using value_type = range_value_t<_Vp>;
  1249. using difference_type = range_difference_t<_Vp>;
  1250. _Iterator() = default;
  1251. constexpr
  1252. _Iterator(filter_view& __parent, _Vp_iter __current)
  1253. : _M_current(std::move(__current)),
  1254. _M_parent(std::__addressof(__parent))
  1255. { }
  1256. constexpr _Vp_iter
  1257. base() const &
  1258. requires copyable<_Vp_iter>
  1259. { return _M_current; }
  1260. constexpr _Vp_iter
  1261. base() &&
  1262. { return std::move(_M_current); }
  1263. constexpr range_reference_t<_Vp>
  1264. operator*() const
  1265. { return *_M_current; }
  1266. constexpr _Vp_iter
  1267. operator->() const
  1268. requires __detail::__has_arrow<_Vp_iter>
  1269. && copyable<_Vp_iter>
  1270. { return _M_current; }
  1271. constexpr _Iterator&
  1272. operator++()
  1273. {
  1274. _M_current = __detail::find_if(std::move(++_M_current),
  1275. ranges::end(_M_parent->_M_base),
  1276. std::ref(*_M_parent->_M_pred));
  1277. return *this;
  1278. }
  1279. constexpr void
  1280. operator++(int)
  1281. { ++*this; }
  1282. constexpr _Iterator
  1283. operator++(int) requires forward_range<_Vp>
  1284. {
  1285. auto __tmp = *this;
  1286. ++*this;
  1287. return __tmp;
  1288. }
  1289. constexpr _Iterator&
  1290. operator--() requires bidirectional_range<_Vp>
  1291. {
  1292. do
  1293. --_M_current;
  1294. while (!std::__invoke(*_M_parent->_M_pred, *_M_current));
  1295. return *this;
  1296. }
  1297. constexpr _Iterator
  1298. operator--(int) requires bidirectional_range<_Vp>
  1299. {
  1300. auto __tmp = *this;
  1301. --*this;
  1302. return __tmp;
  1303. }
  1304. friend constexpr bool
  1305. operator==(const _Iterator& __x, const _Iterator& __y)
  1306. requires equality_comparable<_Vp_iter>
  1307. { return __x._M_current == __y._M_current; }
  1308. friend constexpr range_rvalue_reference_t<_Vp>
  1309. iter_move(const _Iterator& __i)
  1310. noexcept(noexcept(ranges::iter_move(__i._M_current)))
  1311. { return ranges::iter_move(__i._M_current); }
  1312. friend constexpr void
  1313. iter_swap(const _Iterator& __x, const _Iterator& __y)
  1314. noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
  1315. requires indirectly_swappable<_Vp_iter>
  1316. { ranges::iter_swap(__x._M_current, __y._M_current); }
  1317. };
  1318. struct _Sentinel
  1319. {
  1320. private:
  1321. sentinel_t<_Vp> _M_end = sentinel_t<_Vp>();
  1322. constexpr bool
  1323. __equal(const _Iterator& __i) const
  1324. { return __i._M_current == _M_end; }
  1325. public:
  1326. _Sentinel() = default;
  1327. constexpr explicit
  1328. _Sentinel(filter_view& __parent)
  1329. : _M_end(ranges::end(__parent._M_base))
  1330. { }
  1331. constexpr sentinel_t<_Vp>
  1332. base() const
  1333. { return _M_end; }
  1334. friend constexpr bool
  1335. operator==(const _Iterator& __x, const _Sentinel& __y)
  1336. { return __y.__equal(__x); }
  1337. };
  1338. _Vp _M_base = _Vp();
  1339. __detail::__box<_Pred> _M_pred;
  1340. [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
  1341. public:
  1342. filter_view() = default;
  1343. constexpr
  1344. filter_view(_Vp __base, _Pred __pred)
  1345. : _M_base(std::move(__base)), _M_pred(std::move(__pred))
  1346. { }
  1347. constexpr _Vp
  1348. base() const& requires copy_constructible<_Vp>
  1349. { return _M_base; }
  1350. constexpr _Vp
  1351. base() &&
  1352. { return std::move(_M_base); }
  1353. constexpr const _Pred&
  1354. pred() const
  1355. { return *_M_pred; }
  1356. constexpr _Iterator
  1357. begin()
  1358. {
  1359. if (_M_cached_begin._M_has_value())
  1360. return {*this, _M_cached_begin._M_get(_M_base)};
  1361. __glibcxx_assert(_M_pred.has_value());
  1362. auto __it = __detail::find_if(ranges::begin(_M_base),
  1363. ranges::end(_M_base),
  1364. std::ref(*_M_pred));
  1365. _M_cached_begin._M_set(_M_base, __it);
  1366. return {*this, std::move(__it)};
  1367. }
  1368. constexpr auto
  1369. end()
  1370. {
  1371. if constexpr (common_range<_Vp>)
  1372. return _Iterator{*this, ranges::end(_M_base)};
  1373. else
  1374. return _Sentinel{*this};
  1375. }
  1376. };
  1377. template<typename _Range, typename _Pred>
  1378. filter_view(_Range&&, _Pred) -> filter_view<views::all_t<_Range>, _Pred>;
  1379. namespace views
  1380. {
  1381. inline constexpr __adaptor::_RangeAdaptor filter
  1382. = [] <viewable_range _Range, typename _Pred> (_Range&& __r, _Pred&& __p)
  1383. {
  1384. return filter_view{std::forward<_Range>(__r), std::forward<_Pred>(__p)};
  1385. };
  1386. } // namespace views
  1387. template<input_range _Vp, copy_constructible _Fp>
  1388. requires view<_Vp> && is_object_v<_Fp>
  1389. && regular_invocable<_Fp&, range_reference_t<_Vp>>
  1390. && std::__detail::__can_reference<invoke_result_t<_Fp&,
  1391. range_reference_t<_Vp>>>
  1392. class transform_view : public view_interface<transform_view<_Vp, _Fp>>
  1393. {
  1394. private:
  1395. template<bool _Const>
  1396. struct _Sentinel;
  1397. template<bool _Const>
  1398. struct _Iterator
  1399. {
  1400. private:
  1401. using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
  1402. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  1403. static constexpr auto
  1404. _S_iter_concept()
  1405. {
  1406. if constexpr (random_access_range<_Vp>)
  1407. return random_access_iterator_tag{};
  1408. else if constexpr (bidirectional_range<_Vp>)
  1409. return bidirectional_iterator_tag{};
  1410. else if constexpr (forward_range<_Vp>)
  1411. return forward_iterator_tag{};
  1412. else
  1413. return input_iterator_tag{};
  1414. }
  1415. static constexpr auto
  1416. _S_iter_cat()
  1417. {
  1418. using _Res = invoke_result_t<_Fp&, range_reference_t<_Base>>;
  1419. if constexpr (is_lvalue_reference_v<_Res>)
  1420. {
  1421. using _Cat
  1422. = typename iterator_traits<_Base_iter>::iterator_category;
  1423. if constexpr (derived_from<_Cat, contiguous_iterator_tag>)
  1424. return random_access_iterator_tag{};
  1425. else
  1426. return _Cat{};
  1427. }
  1428. else
  1429. return input_iterator_tag{};
  1430. }
  1431. using _Base_iter = iterator_t<_Base>;
  1432. _Base_iter _M_current = _Base_iter();
  1433. _Parent* _M_parent = nullptr;
  1434. public:
  1435. using iterator_concept = decltype(_S_iter_concept());
  1436. using iterator_category = decltype(_S_iter_cat());
  1437. using value_type
  1438. = remove_cvref_t<invoke_result_t<_Fp&, range_reference_t<_Base>>>;
  1439. using difference_type = range_difference_t<_Base>;
  1440. _Iterator() = default;
  1441. constexpr
  1442. _Iterator(_Parent& __parent, _Base_iter __current)
  1443. : _M_current(std::move(__current)),
  1444. _M_parent(std::__addressof(__parent))
  1445. { }
  1446. constexpr
  1447. _Iterator(_Iterator<!_Const> __i)
  1448. requires _Const
  1449. && convertible_to<iterator_t<_Vp>, _Base_iter>
  1450. : _M_current(std::move(__i._M_current)), _M_parent(__i._M_parent)
  1451. { }
  1452. constexpr _Base_iter
  1453. base() const &
  1454. requires copyable<_Base_iter>
  1455. { return _M_current; }
  1456. constexpr _Base_iter
  1457. base() &&
  1458. { return std::move(_M_current); }
  1459. constexpr decltype(auto)
  1460. operator*() const
  1461. noexcept(noexcept(std::__invoke(*_M_parent->_M_fun, *_M_current)))
  1462. { return std::__invoke(*_M_parent->_M_fun, *_M_current); }
  1463. constexpr _Iterator&
  1464. operator++()
  1465. {
  1466. ++_M_current;
  1467. return *this;
  1468. }
  1469. constexpr void
  1470. operator++(int)
  1471. { ++_M_current; }
  1472. constexpr _Iterator
  1473. operator++(int) requires forward_range<_Base>
  1474. {
  1475. auto __tmp = *this;
  1476. ++*this;
  1477. return __tmp;
  1478. }
  1479. constexpr _Iterator&
  1480. operator--() requires bidirectional_range<_Base>
  1481. {
  1482. --_M_current;
  1483. return *this;
  1484. }
  1485. constexpr _Iterator
  1486. operator--(int) requires bidirectional_range<_Base>
  1487. {
  1488. auto __tmp = *this;
  1489. --*this;
  1490. return __tmp;
  1491. }
  1492. constexpr _Iterator&
  1493. operator+=(difference_type __n) requires random_access_range<_Base>
  1494. {
  1495. _M_current += __n;
  1496. return *this;
  1497. }
  1498. constexpr _Iterator&
  1499. operator-=(difference_type __n) requires random_access_range<_Base>
  1500. {
  1501. _M_current -= __n;
  1502. return *this;
  1503. }
  1504. constexpr decltype(auto)
  1505. operator[](difference_type __n) const
  1506. requires random_access_range<_Base>
  1507. { return std::__invoke(*_M_parent->_M_fun, _M_current[__n]); }
  1508. friend constexpr bool
  1509. operator==(const _Iterator& __x, const _Iterator& __y)
  1510. requires equality_comparable<_Base_iter>
  1511. { return __x._M_current == __y._M_current; }
  1512. friend constexpr bool
  1513. operator<(const _Iterator& __x, const _Iterator& __y)
  1514. requires random_access_range<_Base>
  1515. { return __x._M_current < __y._M_current; }
  1516. friend constexpr bool
  1517. operator>(const _Iterator& __x, const _Iterator& __y)
  1518. requires random_access_range<_Base>
  1519. { return __y < __x; }
  1520. friend constexpr bool
  1521. operator<=(const _Iterator& __x, const _Iterator& __y)
  1522. requires random_access_range<_Base>
  1523. { return !(__y < __x); }
  1524. friend constexpr bool
  1525. operator>=(const _Iterator& __x, const _Iterator& __y)
  1526. requires random_access_range<_Base>
  1527. { return !(__x < __y); }
  1528. #ifdef __cpp_lib_three_way_comparison
  1529. friend constexpr auto
  1530. operator<=>(const _Iterator& __x, const _Iterator& __y)
  1531. requires random_access_range<_Base>
  1532. && three_way_comparable<_Base_iter>
  1533. { return __x._M_current <=> __y._M_current; }
  1534. #endif
  1535. friend constexpr _Iterator
  1536. operator+(_Iterator __i, difference_type __n)
  1537. requires random_access_range<_Base>
  1538. { return {*__i._M_parent, __i._M_current + __n}; }
  1539. friend constexpr _Iterator
  1540. operator+(difference_type __n, _Iterator __i)
  1541. requires random_access_range<_Base>
  1542. { return {*__i._M_parent, __i._M_current + __n}; }
  1543. friend constexpr _Iterator
  1544. operator-(_Iterator __i, difference_type __n)
  1545. requires random_access_range<_Base>
  1546. { return {*__i._M_parent, __i._M_current - __n}; }
  1547. friend constexpr difference_type
  1548. operator-(const _Iterator& __x, const _Iterator& __y)
  1549. requires random_access_range<_Base>
  1550. { return __x._M_current - __y._M_current; }
  1551. friend constexpr decltype(auto)
  1552. iter_move(const _Iterator& __i) noexcept(noexcept(*__i))
  1553. {
  1554. if constexpr (is_lvalue_reference_v<decltype(*__i)>)
  1555. return std::move(*__i);
  1556. else
  1557. return *__i;
  1558. }
  1559. friend constexpr void
  1560. iter_swap(const _Iterator& __x, const _Iterator& __y)
  1561. noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
  1562. requires indirectly_swappable<_Base_iter>
  1563. { return ranges::iter_swap(__x._M_current, __y._M_current); }
  1564. friend _Iterator<!_Const>;
  1565. template<bool> friend struct _Sentinel;
  1566. };
  1567. template<bool _Const>
  1568. struct _Sentinel
  1569. {
  1570. private:
  1571. using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
  1572. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  1573. template<bool _Const2>
  1574. constexpr auto
  1575. __distance_from(const _Iterator<_Const2>& __i) const
  1576. { return _M_end - __i._M_current; }
  1577. template<bool _Const2>
  1578. constexpr bool
  1579. __equal(const _Iterator<_Const2>& __i) const
  1580. { return __i._M_current == _M_end; }
  1581. sentinel_t<_Base> _M_end = sentinel_t<_Base>();
  1582. public:
  1583. _Sentinel() = default;
  1584. constexpr explicit
  1585. _Sentinel(sentinel_t<_Base> __end)
  1586. : _M_end(__end)
  1587. { }
  1588. constexpr
  1589. _Sentinel(_Sentinel<!_Const> __i)
  1590. requires _Const
  1591. && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
  1592. : _M_end(std::move(__i._M_end))
  1593. { }
  1594. constexpr sentinel_t<_Base>
  1595. base() const
  1596. { return _M_end; }
  1597. template<bool _Const2>
  1598. requires sentinel_for<sentinel_t<_Base>,
  1599. iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
  1600. friend constexpr bool
  1601. operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
  1602. { return __y.__equal(__x); }
  1603. template<bool _Const2,
  1604. typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
  1605. requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
  1606. friend constexpr range_difference_t<_Base2>
  1607. operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
  1608. { return -__y.__distance_from(__x); }
  1609. template<bool _Const2,
  1610. typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
  1611. requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
  1612. friend constexpr range_difference_t<_Base2>
  1613. operator-(const _Sentinel& __y, const _Iterator<_Const2>& __x)
  1614. { return __y.__distance_from(__x); }
  1615. friend _Sentinel<!_Const>;
  1616. };
  1617. _Vp _M_base = _Vp();
  1618. __detail::__box<_Fp> _M_fun;
  1619. public:
  1620. transform_view() = default;
  1621. constexpr
  1622. transform_view(_Vp __base, _Fp __fun)
  1623. : _M_base(std::move(__base)), _M_fun(std::move(__fun))
  1624. { }
  1625. constexpr _Vp
  1626. base() const& requires copy_constructible<_Vp>
  1627. { return _M_base ; }
  1628. constexpr _Vp
  1629. base() &&
  1630. { return std::move(_M_base); }
  1631. constexpr _Iterator<false>
  1632. begin()
  1633. { return _Iterator<false>{*this, ranges::begin(_M_base)}; }
  1634. constexpr _Iterator<true>
  1635. begin() const
  1636. requires range<const _Vp>
  1637. && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
  1638. { return _Iterator<true>{*this, ranges::begin(_M_base)}; }
  1639. constexpr _Sentinel<false>
  1640. end()
  1641. { return _Sentinel<false>{ranges::end(_M_base)}; }
  1642. constexpr _Iterator<false>
  1643. end() requires common_range<_Vp>
  1644. { return _Iterator<false>{*this, ranges::end(_M_base)}; }
  1645. constexpr _Sentinel<true>
  1646. end() const
  1647. requires range<const _Vp>
  1648. && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
  1649. { return _Sentinel<true>{ranges::end(_M_base)}; }
  1650. constexpr _Iterator<true>
  1651. end() const
  1652. requires common_range<const _Vp>
  1653. && regular_invocable<const _Fp&, range_reference_t<const _Vp>>
  1654. { return _Iterator<true>{*this, ranges::end(_M_base)}; }
  1655. constexpr auto
  1656. size() requires sized_range<_Vp>
  1657. { return ranges::size(_M_base); }
  1658. constexpr auto
  1659. size() const requires sized_range<const _Vp>
  1660. { return ranges::size(_M_base); }
  1661. };
  1662. template<typename _Range, typename _Fp>
  1663. transform_view(_Range&&, _Fp) -> transform_view<views::all_t<_Range>, _Fp>;
  1664. namespace views
  1665. {
  1666. inline constexpr __adaptor::_RangeAdaptor transform
  1667. = [] <viewable_range _Range, typename _Fp> (_Range&& __r, _Fp&& __f)
  1668. {
  1669. return transform_view{std::forward<_Range>(__r), std::forward<_Fp>(__f)};
  1670. };
  1671. } // namespace views
  1672. template<view _Vp>
  1673. class take_view : public view_interface<take_view<_Vp>>
  1674. {
  1675. private:
  1676. template<bool _Const>
  1677. struct _Sentinel
  1678. {
  1679. private:
  1680. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  1681. using _CI = counted_iterator<iterator_t<_Base>>;
  1682. sentinel_t<_Base> _M_end = sentinel_t<_Base>();
  1683. public:
  1684. _Sentinel() = default;
  1685. constexpr explicit
  1686. _Sentinel(sentinel_t<_Base> __end)
  1687. : _M_end(__end)
  1688. { }
  1689. constexpr
  1690. _Sentinel(_Sentinel<!_Const> __s)
  1691. requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
  1692. : _M_end(std::move(__s._M_end))
  1693. { }
  1694. constexpr sentinel_t<_Base>
  1695. base() const
  1696. { return _M_end; }
  1697. friend constexpr bool operator==(const _CI& __y, const _Sentinel& __x)
  1698. { return __y.count() == 0 || __y.base() == __x._M_end; }
  1699. friend _Sentinel<!_Const>;
  1700. };
  1701. _Vp _M_base = _Vp();
  1702. range_difference_t<_Vp> _M_count = 0;
  1703. public:
  1704. take_view() = default;
  1705. constexpr
  1706. take_view(_Vp base, range_difference_t<_Vp> __count)
  1707. : _M_base(std::move(base)), _M_count(std::move(__count))
  1708. { }
  1709. constexpr _Vp
  1710. base() const& requires copy_constructible<_Vp>
  1711. { return _M_base; }
  1712. constexpr _Vp
  1713. base() &&
  1714. { return std::move(_M_base); }
  1715. constexpr auto
  1716. begin() requires (!__detail::__simple_view<_Vp>)
  1717. {
  1718. if constexpr (sized_range<_Vp>)
  1719. {
  1720. if constexpr (random_access_range<_Vp>)
  1721. return ranges::begin(_M_base);
  1722. else
  1723. {
  1724. auto __sz = size();
  1725. return counted_iterator{ranges::begin(_M_base), __sz};
  1726. }
  1727. }
  1728. else
  1729. return counted_iterator{ranges::begin(_M_base), _M_count};
  1730. }
  1731. constexpr auto
  1732. begin() const requires range<const _Vp>
  1733. {
  1734. if constexpr (sized_range<const _Vp>)
  1735. {
  1736. if constexpr (random_access_range<const _Vp>)
  1737. return ranges::begin(_M_base);
  1738. else
  1739. {
  1740. auto __sz = size();
  1741. return counted_iterator{ranges::begin(_M_base), __sz};
  1742. }
  1743. }
  1744. else
  1745. return counted_iterator{ranges::begin(_M_base), _M_count};
  1746. }
  1747. constexpr auto
  1748. end() requires (!__detail::__simple_view<_Vp>)
  1749. {
  1750. if constexpr (sized_range<_Vp>)
  1751. {
  1752. if constexpr (random_access_range<_Vp>)
  1753. return ranges::begin(_M_base) + size();
  1754. else
  1755. return default_sentinel;
  1756. }
  1757. else
  1758. return _Sentinel<false>{ranges::end(_M_base)};
  1759. }
  1760. constexpr auto
  1761. end() const requires range<const _Vp>
  1762. {
  1763. if constexpr (sized_range<const _Vp>)
  1764. {
  1765. if constexpr (random_access_range<const _Vp>)
  1766. return ranges::begin(_M_base) + size();
  1767. else
  1768. return default_sentinel;
  1769. }
  1770. else
  1771. return _Sentinel<true>{ranges::end(_M_base)};
  1772. }
  1773. constexpr auto
  1774. size() requires sized_range<_Vp>
  1775. {
  1776. auto __n = ranges::size(_M_base);
  1777. return __detail::min(__n, static_cast<decltype(__n)>(_M_count));
  1778. }
  1779. constexpr auto
  1780. size() const requires sized_range<const _Vp>
  1781. {
  1782. auto __n = ranges::size(_M_base);
  1783. return __detail::min(__n, static_cast<decltype(__n)>(_M_count));
  1784. }
  1785. };
  1786. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1787. // 3447. Deduction guides for take_view and drop_view have different
  1788. // constraints
  1789. template<typename _Range>
  1790. take_view(_Range&&, range_difference_t<_Range>)
  1791. -> take_view<views::all_t<_Range>>;
  1792. namespace views
  1793. {
  1794. inline constexpr __adaptor::_RangeAdaptor take
  1795. = [] <viewable_range _Range, typename _Tp> (_Range&& __r, _Tp&& __n)
  1796. {
  1797. return take_view{std::forward<_Range>(__r), std::forward<_Tp>(__n)};
  1798. };
  1799. } // namespace views
  1800. template<view _Vp, typename _Pred>
  1801. requires input_range<_Vp> && is_object_v<_Pred>
  1802. && indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
  1803. class take_while_view : public view_interface<take_while_view<_Vp, _Pred>>
  1804. {
  1805. template<bool _Const>
  1806. struct _Sentinel
  1807. {
  1808. private:
  1809. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  1810. sentinel_t<_Base> _M_end = sentinel_t<_Base>();
  1811. const _Pred* _M_pred = nullptr;
  1812. public:
  1813. _Sentinel() = default;
  1814. constexpr explicit
  1815. _Sentinel(sentinel_t<_Base> __end, const _Pred* __pred)
  1816. : _M_end(__end), _M_pred(__pred)
  1817. { }
  1818. constexpr
  1819. _Sentinel(_Sentinel<!_Const> __s)
  1820. requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
  1821. : _M_end(__s._M_end), _M_pred(__s._M_pred)
  1822. { }
  1823. constexpr sentinel_t<_Base>
  1824. base() const { return _M_end; }
  1825. friend constexpr bool
  1826. operator==(const iterator_t<_Base>& __x, const _Sentinel& __y)
  1827. { return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); }
  1828. friend _Sentinel<!_Const>;
  1829. };
  1830. _Vp _M_base = _Vp();
  1831. __detail::__box<_Pred> _M_pred;
  1832. public:
  1833. take_while_view() = default;
  1834. constexpr
  1835. take_while_view(_Vp base, _Pred __pred)
  1836. : _M_base(std::move(base)), _M_pred(std::move(__pred))
  1837. {
  1838. }
  1839. constexpr _Vp
  1840. base() const& requires copy_constructible<_Vp>
  1841. { return _M_base; }
  1842. constexpr _Vp
  1843. base() &&
  1844. { return std::move(_M_base); }
  1845. constexpr const _Pred&
  1846. pred() const
  1847. { return *_M_pred; }
  1848. constexpr auto
  1849. begin() requires (!__detail::__simple_view<_Vp>)
  1850. { return ranges::begin(_M_base); }
  1851. constexpr auto
  1852. begin() const requires range<const _Vp>
  1853. && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>
  1854. { return ranges::begin(_M_base); }
  1855. constexpr auto
  1856. end() requires (!__detail::__simple_view<_Vp>)
  1857. { return _Sentinel<false>(ranges::end(_M_base),
  1858. std::__addressof(*_M_pred)); }
  1859. constexpr auto
  1860. end() const requires range<const _Vp>
  1861. && indirect_unary_predicate<const _Pred, iterator_t<const _Vp>>
  1862. { return _Sentinel<true>(ranges::end(_M_base),
  1863. std::__addressof(*_M_pred)); }
  1864. };
  1865. template<typename _Range, typename _Pred>
  1866. take_while_view(_Range&&, _Pred)
  1867. -> take_while_view<views::all_t<_Range>, _Pred>;
  1868. namespace views
  1869. {
  1870. inline constexpr __adaptor::_RangeAdaptor take_while
  1871. = [] <viewable_range _Range, typename _Pred> (_Range&& __r, _Pred&& __p)
  1872. {
  1873. return take_while_view{std::forward<_Range>(__r), std::forward<_Pred>(__p)};
  1874. };
  1875. } // namespace views
  1876. template<view _Vp>
  1877. class drop_view : public view_interface<drop_view<_Vp>>
  1878. {
  1879. private:
  1880. _Vp _M_base = _Vp();
  1881. range_difference_t<_Vp> _M_count = 0;
  1882. static constexpr bool _S_needs_cached_begin = !random_access_range<_Vp>;
  1883. [[no_unique_address]]
  1884. __detail::__maybe_present_t<_S_needs_cached_begin,
  1885. __detail::_CachedPosition<_Vp>>
  1886. _M_cached_begin;
  1887. public:
  1888. drop_view() = default;
  1889. constexpr
  1890. drop_view(_Vp __base, range_difference_t<_Vp> __count)
  1891. : _M_base(std::move(__base)), _M_count(__count)
  1892. { __glibcxx_assert(__count >= 0); }
  1893. constexpr _Vp
  1894. base() const& requires copy_constructible<_Vp>
  1895. { return _M_base; }
  1896. constexpr _Vp
  1897. base() &&
  1898. { return std::move(_M_base); }
  1899. constexpr auto
  1900. begin() requires (!(__detail::__simple_view<_Vp>
  1901. && random_access_range<_Vp>))
  1902. {
  1903. if constexpr (_S_needs_cached_begin)
  1904. if (_M_cached_begin._M_has_value())
  1905. return _M_cached_begin._M_get(_M_base);
  1906. auto __it = ranges::next(ranges::begin(_M_base),
  1907. _M_count, ranges::end(_M_base));
  1908. if constexpr (_S_needs_cached_begin)
  1909. _M_cached_begin._M_set(_M_base, __it);
  1910. return __it;
  1911. }
  1912. constexpr auto
  1913. begin() const requires random_access_range<const _Vp>
  1914. {
  1915. return ranges::next(ranges::begin(_M_base), _M_count,
  1916. ranges::end(_M_base));
  1917. }
  1918. constexpr auto
  1919. end() requires (!__detail::__simple_view<_Vp>)
  1920. { return ranges::end(_M_base); }
  1921. constexpr auto
  1922. end() const requires range<const _Vp>
  1923. { return ranges::end(_M_base); }
  1924. constexpr auto
  1925. size() requires sized_range<_Vp>
  1926. {
  1927. const auto __s = ranges::size(_M_base);
  1928. const auto __c = static_cast<decltype(__s)>(_M_count);
  1929. return __s < __c ? 0 : __s - __c;
  1930. }
  1931. constexpr auto
  1932. size() const requires sized_range<const _Vp>
  1933. {
  1934. const auto __s = ranges::size(_M_base);
  1935. const auto __c = static_cast<decltype(__s)>(_M_count);
  1936. return __s < __c ? 0 : __s - __c;
  1937. }
  1938. };
  1939. template<typename _Range>
  1940. drop_view(_Range&&, range_difference_t<_Range>)
  1941. -> drop_view<views::all_t<_Range>>;
  1942. namespace views
  1943. {
  1944. inline constexpr __adaptor::_RangeAdaptor drop
  1945. = [] <viewable_range _Range, typename _Tp> (_Range&& __r, _Tp&& __n)
  1946. {
  1947. return drop_view{std::forward<_Range>(__r), std::forward<_Tp>(__n)};
  1948. };
  1949. } // namespace views
  1950. template<view _Vp, typename _Pred>
  1951. requires input_range<_Vp> && is_object_v<_Pred>
  1952. && indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
  1953. class drop_while_view : public view_interface<drop_while_view<_Vp, _Pred>>
  1954. {
  1955. private:
  1956. _Vp _M_base = _Vp();
  1957. __detail::__box<_Pred> _M_pred;
  1958. [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
  1959. public:
  1960. drop_while_view() = default;
  1961. constexpr
  1962. drop_while_view(_Vp __base, _Pred __pred)
  1963. : _M_base(std::move(__base)), _M_pred(std::move(__pred))
  1964. { }
  1965. constexpr _Vp
  1966. base() const& requires copy_constructible<_Vp>
  1967. { return _M_base; }
  1968. constexpr _Vp
  1969. base() &&
  1970. { return std::move(_M_base); }
  1971. constexpr const _Pred&
  1972. pred() const
  1973. { return *_M_pred; }
  1974. constexpr auto
  1975. begin()
  1976. {
  1977. if (_M_cached_begin._M_has_value())
  1978. return _M_cached_begin._M_get(_M_base);
  1979. auto __it = __detail::find_if_not(ranges::begin(_M_base),
  1980. ranges::end(_M_base),
  1981. std::cref(*_M_pred));
  1982. _M_cached_begin._M_set(_M_base, __it);
  1983. return __it;
  1984. }
  1985. constexpr auto
  1986. end()
  1987. { return ranges::end(_M_base); }
  1988. };
  1989. template<typename _Range, typename _Pred>
  1990. drop_while_view(_Range&&, _Pred)
  1991. -> drop_while_view<views::all_t<_Range>, _Pred>;
  1992. namespace views
  1993. {
  1994. inline constexpr __adaptor::_RangeAdaptor drop_while
  1995. = [] <viewable_range _Range, typename _Pred> (_Range&& __r, _Pred&& __p)
  1996. {
  1997. return drop_while_view{std::forward<_Range>(__r),
  1998. std::forward<_Pred>(__p)};
  1999. };
  2000. } // namespace views
  2001. template<input_range _Vp>
  2002. requires view<_Vp> && input_range<range_reference_t<_Vp>>
  2003. && (is_reference_v<range_reference_t<_Vp>>
  2004. || view<range_value_t<_Vp>>)
  2005. class join_view : public view_interface<join_view<_Vp>>
  2006. {
  2007. private:
  2008. using _InnerRange = range_reference_t<_Vp>;
  2009. template<bool _Const>
  2010. struct _Sentinel;
  2011. template<bool _Const>
  2012. struct _Iterator
  2013. {
  2014. private:
  2015. using _Parent = __detail::__maybe_const_t<_Const, join_view>;
  2016. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  2017. static constexpr bool _S_ref_is_glvalue
  2018. = is_reference_v<range_reference_t<_Base>>;
  2019. constexpr void
  2020. _M_satisfy()
  2021. {
  2022. auto __update_inner = [this] (range_reference_t<_Base> __x) -> auto&
  2023. {
  2024. if constexpr (_S_ref_is_glvalue)
  2025. return __x;
  2026. else
  2027. return (_M_parent->_M_inner = views::all(std::move(__x)));
  2028. };
  2029. for (; _M_outer != ranges::end(_M_parent->_M_base); ++_M_outer)
  2030. {
  2031. auto& inner = __update_inner(*_M_outer);
  2032. _M_inner = ranges::begin(inner);
  2033. if (_M_inner != ranges::end(inner))
  2034. return;
  2035. }
  2036. if constexpr (_S_ref_is_glvalue)
  2037. _M_inner = _Inner_iter();
  2038. }
  2039. static constexpr auto
  2040. _S_iter_concept()
  2041. {
  2042. if constexpr (_S_ref_is_glvalue
  2043. && bidirectional_range<_Base>
  2044. && bidirectional_range<range_reference_t<_Base>>)
  2045. return bidirectional_iterator_tag{};
  2046. else if constexpr (_S_ref_is_glvalue
  2047. && forward_range<_Base>
  2048. && forward_range<range_reference_t<_Base>>)
  2049. return forward_iterator_tag{};
  2050. else
  2051. return input_iterator_tag{};
  2052. }
  2053. static constexpr auto
  2054. _S_iter_cat()
  2055. {
  2056. using _OuterCat
  2057. = typename iterator_traits<_Outer_iter>::iterator_category;
  2058. using _InnerCat
  2059. = typename iterator_traits<_Inner_iter>::iterator_category;
  2060. if constexpr (_S_ref_is_glvalue
  2061. && derived_from<_OuterCat, bidirectional_iterator_tag>
  2062. && derived_from<_InnerCat, bidirectional_iterator_tag>)
  2063. return bidirectional_iterator_tag{};
  2064. else if constexpr (_S_ref_is_glvalue
  2065. && derived_from<_OuterCat, forward_iterator_tag>
  2066. && derived_from<_InnerCat, forward_iterator_tag>)
  2067. return forward_iterator_tag{};
  2068. else if constexpr (derived_from<_OuterCat, input_iterator_tag>
  2069. && derived_from<_InnerCat, input_iterator_tag>)
  2070. return input_iterator_tag{};
  2071. else
  2072. return output_iterator_tag{};
  2073. }
  2074. using _Outer_iter = iterator_t<_Base>;
  2075. using _Inner_iter = iterator_t<range_reference_t<_Base>>;
  2076. _Outer_iter _M_outer = _Outer_iter();
  2077. _Inner_iter _M_inner = _Inner_iter();
  2078. _Parent* _M_parent = nullptr;
  2079. public:
  2080. using iterator_concept = decltype(_S_iter_concept());
  2081. using iterator_category = decltype(_S_iter_cat());
  2082. using value_type = range_value_t<range_reference_t<_Base>>;
  2083. using difference_type
  2084. = common_type_t<range_difference_t<_Base>,
  2085. range_difference_t<range_reference_t<_Base>>>;
  2086. _Iterator() = default;
  2087. constexpr
  2088. _Iterator(_Parent& __parent, _Outer_iter __outer)
  2089. : _M_outer(std::move(__outer)),
  2090. _M_parent(std::__addressof(__parent))
  2091. { _M_satisfy(); }
  2092. constexpr
  2093. _Iterator(_Iterator<!_Const> __i)
  2094. requires _Const
  2095. && convertible_to<iterator_t<_Vp>, _Outer_iter>
  2096. && convertible_to<iterator_t<_InnerRange>, _Inner_iter>
  2097. : _M_outer(std::move(__i._M_outer)), _M_inner(__i._M_inner),
  2098. _M_parent(__i._M_parent)
  2099. { }
  2100. constexpr decltype(auto)
  2101. operator*() const
  2102. { return *_M_inner; }
  2103. constexpr _Outer_iter
  2104. operator->() const
  2105. requires __detail::__has_arrow<_Outer_iter>
  2106. && copyable<_Outer_iter>
  2107. { return _M_inner; }
  2108. constexpr _Iterator&
  2109. operator++()
  2110. {
  2111. auto&& __inner_range = [this] () -> decltype(auto) {
  2112. if constexpr (_S_ref_is_glvalue)
  2113. return *_M_outer;
  2114. else
  2115. return _M_parent->_M_inner;
  2116. }();
  2117. if (++_M_inner == ranges::end(__inner_range))
  2118. {
  2119. ++_M_outer;
  2120. _M_satisfy();
  2121. }
  2122. return *this;
  2123. }
  2124. constexpr void
  2125. operator++(int)
  2126. { ++*this; }
  2127. constexpr _Iterator
  2128. operator++(int)
  2129. requires _S_ref_is_glvalue && forward_range<_Base>
  2130. && forward_range<range_reference_t<_Base>>
  2131. {
  2132. auto __tmp = *this;
  2133. ++*this;
  2134. return __tmp;
  2135. }
  2136. constexpr _Iterator&
  2137. operator--()
  2138. requires _S_ref_is_glvalue && bidirectional_range<_Base>
  2139. && bidirectional_range<range_reference_t<_Base>>
  2140. && common_range<range_reference_t<_Base>>
  2141. {
  2142. if (_M_outer == ranges::end(_M_parent->_M_base))
  2143. _M_inner = ranges::end(*--_M_outer);
  2144. while (_M_inner == ranges::begin(*_M_outer))
  2145. _M_inner = ranges::end(*--_M_outer);
  2146. --_M_inner;
  2147. return *this;
  2148. }
  2149. constexpr _Iterator
  2150. operator--(int)
  2151. requires _S_ref_is_glvalue && bidirectional_range<_Base>
  2152. && bidirectional_range<range_reference_t<_Base>>
  2153. && common_range<range_reference_t<_Base>>
  2154. {
  2155. auto __tmp = *this;
  2156. --*this;
  2157. return __tmp;
  2158. }
  2159. friend constexpr bool
  2160. operator==(const _Iterator& __x, const _Iterator& __y)
  2161. requires _S_ref_is_glvalue
  2162. && equality_comparable<_Outer_iter>
  2163. && equality_comparable<_Inner_iter>
  2164. {
  2165. return (__x._M_outer == __y._M_outer
  2166. && __x._M_inner == __y._M_inner);
  2167. }
  2168. friend constexpr decltype(auto)
  2169. iter_move(const _Iterator& __i)
  2170. noexcept(noexcept(ranges::iter_move(__i._M_inner)))
  2171. { return ranges::iter_move(__i._M_inner); }
  2172. friend constexpr void
  2173. iter_swap(const _Iterator& __x, const _Iterator& __y)
  2174. noexcept(noexcept(ranges::iter_swap(__x._M_inner, __y._M_inner)))
  2175. { return ranges::iter_swap(__x._M_inner, __y._M_inner); }
  2176. friend _Iterator<!_Const>;
  2177. template<bool> friend struct _Sentinel;
  2178. };
  2179. template<bool _Const>
  2180. struct _Sentinel
  2181. {
  2182. private:
  2183. using _Parent = __detail::__maybe_const_t<_Const, join_view>;
  2184. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  2185. template<bool _Const2>
  2186. constexpr bool
  2187. __equal(const _Iterator<_Const2>& __i) const
  2188. { return __i._M_outer == _M_end; }
  2189. sentinel_t<_Base> _M_end = sentinel_t<_Base>();
  2190. public:
  2191. _Sentinel() = default;
  2192. constexpr explicit
  2193. _Sentinel(_Parent& __parent)
  2194. : _M_end(ranges::end(__parent._M_base))
  2195. { }
  2196. constexpr
  2197. _Sentinel(_Sentinel<!_Const> __s)
  2198. requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
  2199. : _M_end(std::move(__s._M_end))
  2200. { }
  2201. template<bool _Const2>
  2202. requires sentinel_for<sentinel_t<_Base>,
  2203. iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
  2204. friend constexpr bool
  2205. operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
  2206. { return __y.__equal(__x); }
  2207. friend _Sentinel<!_Const>;
  2208. };
  2209. _Vp _M_base = _Vp();
  2210. // XXX: _M_inner is "present only when !is_reference_v<_InnerRange>"
  2211. [[no_unique_address]]
  2212. __detail::__maybe_present_t<!is_reference_v<_InnerRange>,
  2213. views::all_t<_InnerRange>> _M_inner;
  2214. public:
  2215. join_view() = default;
  2216. constexpr explicit
  2217. join_view(_Vp __base)
  2218. : _M_base(std::move(__base))
  2219. { }
  2220. constexpr _Vp
  2221. base() const& requires copy_constructible<_Vp>
  2222. { return _M_base; }
  2223. constexpr _Vp
  2224. base() &&
  2225. { return std::move(_M_base); }
  2226. constexpr auto
  2227. begin()
  2228. {
  2229. constexpr bool __use_const
  2230. = (__detail::__simple_view<_Vp>
  2231. && is_reference_v<range_reference_t<_Vp>>);
  2232. return _Iterator<__use_const>{*this, ranges::begin(_M_base)};
  2233. }
  2234. constexpr auto
  2235. begin() const
  2236. requires input_range<const _Vp>
  2237. && is_reference_v<range_reference_t<const _Vp>>
  2238. {
  2239. return _Iterator<true>{*this, ranges::begin(_M_base)};
  2240. }
  2241. constexpr auto
  2242. end()
  2243. {
  2244. if constexpr (forward_range<_Vp> && is_reference_v<_InnerRange>
  2245. && forward_range<_InnerRange>
  2246. && common_range<_Vp> && common_range<_InnerRange>)
  2247. return _Iterator<__detail::__simple_view<_Vp>>{*this,
  2248. ranges::end(_M_base)};
  2249. else
  2250. return _Sentinel<__detail::__simple_view<_Vp>>{*this};
  2251. }
  2252. constexpr auto
  2253. end() const
  2254. requires input_range<const _Vp>
  2255. && is_reference_v<range_reference_t<const _Vp>>
  2256. {
  2257. if constexpr (forward_range<const _Vp>
  2258. && is_reference_v<range_reference_t<const _Vp>>
  2259. && forward_range<range_reference_t<const _Vp>>
  2260. && common_range<const _Vp>
  2261. && common_range<range_reference_t<const _Vp>>)
  2262. return _Iterator<true>{*this, ranges::end(_M_base)};
  2263. else
  2264. return _Sentinel<true>{*this};
  2265. }
  2266. };
  2267. template<typename _Range>
  2268. explicit join_view(_Range&&) -> join_view<views::all_t<_Range>>;
  2269. namespace views
  2270. {
  2271. inline constexpr __adaptor::_RangeAdaptorClosure join
  2272. = [] <viewable_range _Range> (_Range&& __r)
  2273. {
  2274. return join_view{std::forward<_Range>(__r)};
  2275. };
  2276. } // namespace views
  2277. namespace __detail
  2278. {
  2279. template<auto>
  2280. struct __require_constant;
  2281. template<typename _Range>
  2282. concept __tiny_range = sized_range<_Range>
  2283. && requires
  2284. { typename __require_constant<remove_reference_t<_Range>::size()>; }
  2285. && (remove_reference_t<_Range>::size() <= 1);
  2286. }
  2287. template<input_range _Vp, forward_range _Pattern>
  2288. requires view<_Vp> && view<_Pattern>
  2289. && indirectly_comparable<iterator_t<_Vp>, iterator_t<_Pattern>,
  2290. ranges::equal_to>
  2291. && (forward_range<_Vp> || __detail::__tiny_range<_Pattern>)
  2292. class split_view : public view_interface<split_view<_Vp, _Pattern>>
  2293. {
  2294. private:
  2295. template<bool _Const>
  2296. struct _InnerIter;
  2297. template<bool _Const>
  2298. struct _OuterIter
  2299. {
  2300. private:
  2301. using _Parent = __detail::__maybe_const_t<_Const, split_view>;
  2302. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  2303. constexpr bool
  2304. __at_end() const
  2305. { return __current() == ranges::end(_M_parent->_M_base); }
  2306. // [range.split.outer] p1
  2307. // Many of the following specifications refer to the notional member
  2308. // current of outer-iterator. current is equivalent to current_ if
  2309. // V models forward_range, and parent_->current_ otherwise.
  2310. constexpr auto&
  2311. __current() noexcept
  2312. {
  2313. if constexpr (forward_range<_Vp>)
  2314. return _M_current;
  2315. else
  2316. return _M_parent->_M_current;
  2317. }
  2318. constexpr auto&
  2319. __current() const noexcept
  2320. {
  2321. if constexpr (forward_range<_Vp>)
  2322. return _M_current;
  2323. else
  2324. return _M_parent->_M_current;
  2325. }
  2326. _Parent* _M_parent = nullptr;
  2327. // XXX: _M_current is present only if "V models forward_range"
  2328. [[no_unique_address]]
  2329. __detail::__maybe_present_t<forward_range<_Vp>,
  2330. iterator_t<_Base>> _M_current;
  2331. public:
  2332. using iterator_concept = conditional_t<forward_range<_Base>,
  2333. forward_iterator_tag,
  2334. input_iterator_tag>;
  2335. using iterator_category = input_iterator_tag;
  2336. using difference_type = range_difference_t<_Base>;
  2337. struct value_type : view_interface<value_type>
  2338. {
  2339. private:
  2340. _OuterIter _M_i = _OuterIter();
  2341. public:
  2342. value_type() = default;
  2343. constexpr explicit
  2344. value_type(_OuterIter __i)
  2345. : _M_i(std::move(__i))
  2346. { }
  2347. constexpr _InnerIter<_Const>
  2348. begin() const
  2349. requires copyable<_OuterIter>
  2350. { return _InnerIter<_Const>{_M_i}; }
  2351. constexpr _InnerIter<_Const>
  2352. begin()
  2353. requires (!copyable<_OuterIter>)
  2354. { return _InnerIter<_Const>{std::move(_M_i)}; }
  2355. constexpr default_sentinel_t
  2356. end() const
  2357. { return default_sentinel; }
  2358. };
  2359. _OuterIter() = default;
  2360. constexpr explicit
  2361. _OuterIter(_Parent& __parent) requires (!forward_range<_Base>)
  2362. : _M_parent(std::__addressof(__parent))
  2363. { }
  2364. constexpr
  2365. _OuterIter(_Parent& __parent, iterator_t<_Base> __current)
  2366. requires forward_range<_Base>
  2367. : _M_parent(std::__addressof(__parent)),
  2368. _M_current(std::move(__current))
  2369. { }
  2370. constexpr
  2371. _OuterIter(_OuterIter<!_Const> __i)
  2372. requires _Const
  2373. && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
  2374. : _M_parent(__i._M_parent), _M_current(std::move(__i._M_current))
  2375. { }
  2376. constexpr value_type
  2377. operator*() const
  2378. { return value_type{*this}; }
  2379. constexpr _OuterIter&
  2380. operator++()
  2381. {
  2382. const auto __end = ranges::end(_M_parent->_M_base);
  2383. if (__current() == __end)
  2384. return *this;
  2385. const auto [__pbegin, __pend] = subrange{_M_parent->_M_pattern};
  2386. if (__pbegin == __pend)
  2387. ++__current();
  2388. else
  2389. do
  2390. {
  2391. auto [__b, __p]
  2392. = __detail::mismatch(std::move(__current()), __end,
  2393. __pbegin, __pend);
  2394. __current() = std::move(__b);
  2395. if (__p == __pend)
  2396. break;
  2397. } while (++__current() != __end);
  2398. return *this;
  2399. }
  2400. constexpr decltype(auto)
  2401. operator++(int)
  2402. {
  2403. if constexpr (forward_range<_Base>)
  2404. {
  2405. auto __tmp = *this;
  2406. ++*this;
  2407. return __tmp;
  2408. }
  2409. else
  2410. ++*this;
  2411. }
  2412. friend constexpr bool
  2413. operator==(const _OuterIter& __x, const _OuterIter& __y)
  2414. requires forward_range<_Base>
  2415. { return __x._M_current == __y._M_current; }
  2416. friend constexpr bool
  2417. operator==(const _OuterIter& __x, default_sentinel_t)
  2418. { return __x.__at_end(); };
  2419. friend _OuterIter<!_Const>;
  2420. friend _InnerIter<_Const>;
  2421. };
  2422. template<bool _Const>
  2423. struct _InnerIter
  2424. {
  2425. private:
  2426. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  2427. constexpr bool
  2428. __at_end() const
  2429. {
  2430. auto [__pcur, __pend] = subrange{_M_i._M_parent->_M_pattern};
  2431. auto __end = ranges::end(_M_i._M_parent->_M_base);
  2432. if constexpr (__detail::__tiny_range<_Pattern>)
  2433. {
  2434. const auto& __cur = _M_i_current();
  2435. if (__cur == __end)
  2436. return true;
  2437. if (__pcur == __pend)
  2438. return _M_incremented;
  2439. return *__cur == *__pcur;
  2440. }
  2441. else
  2442. {
  2443. auto __cur = _M_i_current();
  2444. if (__cur == __end)
  2445. return true;
  2446. if (__pcur == __pend)
  2447. return _M_incremented;
  2448. do
  2449. {
  2450. if (*__cur != *__pcur)
  2451. return false;
  2452. if (++__pcur == __pend)
  2453. return true;
  2454. } while (++__cur != __end);
  2455. return false;
  2456. }
  2457. }
  2458. static constexpr auto
  2459. _S_iter_cat()
  2460. {
  2461. using _Cat
  2462. = typename iterator_traits<iterator_t<_Base>>::iterator_category;
  2463. if constexpr (derived_from<_Cat, forward_iterator_tag>)
  2464. return forward_iterator_tag{};
  2465. else
  2466. return _Cat{};
  2467. }
  2468. constexpr auto&
  2469. _M_i_current() noexcept
  2470. { return _M_i.__current(); }
  2471. constexpr auto&
  2472. _M_i_current() const noexcept
  2473. { return _M_i.__current(); }
  2474. _OuterIter<_Const> _M_i = _OuterIter<_Const>();
  2475. bool _M_incremented = false;
  2476. public:
  2477. using iterator_concept
  2478. = typename _OuterIter<_Const>::iterator_concept;
  2479. using iterator_category = decltype(_S_iter_cat());
  2480. using value_type = range_value_t<_Base>;
  2481. using difference_type = range_difference_t<_Base>;
  2482. _InnerIter() = default;
  2483. constexpr explicit
  2484. _InnerIter(_OuterIter<_Const> __i)
  2485. : _M_i(std::move(__i))
  2486. { }
  2487. constexpr decltype(auto)
  2488. operator*() const
  2489. { return *_M_i_current(); }
  2490. constexpr _InnerIter&
  2491. operator++()
  2492. {
  2493. _M_incremented = true;
  2494. if constexpr (!forward_range<_Base>)
  2495. if constexpr (_Pattern::size() == 0)
  2496. return *this;
  2497. ++_M_i_current();
  2498. return *this;
  2499. }
  2500. constexpr decltype(auto)
  2501. operator++(int)
  2502. {
  2503. if constexpr (forward_range<_Vp>)
  2504. {
  2505. auto __tmp = *this;
  2506. ++*this;
  2507. return __tmp;
  2508. }
  2509. else
  2510. ++*this;
  2511. }
  2512. friend constexpr bool
  2513. operator==(const _InnerIter& __x, const _InnerIter& __y)
  2514. requires forward_range<_Base>
  2515. { return __x._M_i == __y._M_i; }
  2516. friend constexpr bool
  2517. operator==(const _InnerIter& __x, default_sentinel_t)
  2518. { return __x.__at_end(); }
  2519. friend constexpr decltype(auto)
  2520. iter_move(const _InnerIter& __i)
  2521. noexcept(noexcept(ranges::iter_move(__i._M_i_current())))
  2522. { return ranges::iter_move(__i._M_i_current()); }
  2523. friend constexpr void
  2524. iter_swap(const _InnerIter& __x, const _InnerIter& __y)
  2525. noexcept(noexcept(ranges::iter_swap(__x._M_i_current(),
  2526. __y._M_i_current())))
  2527. requires indirectly_swappable<iterator_t<_Base>>
  2528. { ranges::iter_swap(__x._M_i_current(), __y._M_i_current()); }
  2529. };
  2530. _Vp _M_base = _Vp();
  2531. _Pattern _M_pattern = _Pattern();
  2532. // XXX: _M_current is "present only if !forward_range<V>"
  2533. [[no_unique_address]]
  2534. __detail::__maybe_present_t<!forward_range<_Vp>, iterator_t<_Vp>>
  2535. _M_current;
  2536. public:
  2537. split_view() = default;
  2538. constexpr
  2539. split_view(_Vp __base, _Pattern __pattern)
  2540. : _M_base(std::move(__base)), _M_pattern(std::move(__pattern))
  2541. { }
  2542. template<input_range _Range>
  2543. requires constructible_from<_Vp, views::all_t<_Range>>
  2544. && constructible_from<_Pattern, single_view<range_value_t<_Range>>>
  2545. constexpr
  2546. split_view(_Range&& __r, range_value_t<_Range> __e)
  2547. : _M_base(views::all(std::forward<_Range>(__r))),
  2548. _M_pattern(std::move(__e))
  2549. { }
  2550. constexpr _Vp
  2551. base() const& requires copy_constructible<_Vp>
  2552. { return _M_base; }
  2553. constexpr _Vp
  2554. base() &&
  2555. { return std::move(_M_base); }
  2556. constexpr auto
  2557. begin()
  2558. {
  2559. if constexpr (forward_range<_Vp>)
  2560. return _OuterIter<__detail::__simple_view<_Vp>>{
  2561. *this, ranges::begin(_M_base)};
  2562. else
  2563. {
  2564. _M_current = ranges::begin(_M_base);
  2565. return _OuterIter<false>{*this};
  2566. }
  2567. }
  2568. constexpr auto
  2569. begin() const requires forward_range<_Vp> && forward_range<const _Vp>
  2570. {
  2571. return _OuterIter<true>{*this, ranges::begin(_M_base)};
  2572. }
  2573. constexpr auto
  2574. end() requires forward_range<_Vp> && common_range<_Vp>
  2575. {
  2576. return _OuterIter<__detail::__simple_view<_Vp>>{
  2577. *this, ranges::end(_M_base)};
  2578. }
  2579. constexpr auto
  2580. end() const
  2581. {
  2582. if constexpr (forward_range<_Vp>
  2583. && forward_range<const _Vp>
  2584. && common_range<const _Vp>)
  2585. return _OuterIter<true>{*this, ranges::end(_M_base)};
  2586. else
  2587. return default_sentinel;
  2588. }
  2589. };
  2590. template<typename _Range, typename _Pred>
  2591. split_view(_Range&&, _Pred&&)
  2592. -> split_view<views::all_t<_Range>, views::all_t<_Pred>>;
  2593. template<input_range _Range>
  2594. split_view(_Range&&, range_value_t<_Range>)
  2595. -> split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
  2596. namespace views
  2597. {
  2598. inline constexpr __adaptor::_RangeAdaptor split
  2599. = [] <viewable_range _Range, typename _Fp> (_Range&& __r, _Fp&& __f)
  2600. {
  2601. return split_view{std::forward<_Range>(__r), std::forward<_Fp>(__f)};
  2602. };
  2603. } // namespace views
  2604. namespace views
  2605. {
  2606. struct _Counted
  2607. {
  2608. template<input_or_output_iterator _Iter>
  2609. constexpr auto
  2610. operator()(_Iter __i, iter_difference_t<_Iter> __n) const
  2611. {
  2612. if constexpr (random_access_iterator<_Iter>)
  2613. return subrange{__i, __i + __n};
  2614. else
  2615. return subrange{counted_iterator{std::move(__i), __n},
  2616. default_sentinel};
  2617. }
  2618. };
  2619. inline constexpr _Counted counted{};
  2620. } // namespace views
  2621. template<view _Vp>
  2622. requires (!common_range<_Vp>) && copyable<iterator_t<_Vp>>
  2623. class common_view : public view_interface<common_view<_Vp>>
  2624. {
  2625. private:
  2626. _Vp _M_base = _Vp();
  2627. public:
  2628. common_view() = default;
  2629. constexpr explicit
  2630. common_view(_Vp __r)
  2631. : _M_base(std::move(__r))
  2632. { }
  2633. /* XXX: LWG 3280 didn't remove this constructor, but I think it should?
  2634. template<viewable_range _Range>
  2635. requires (!common_range<_Range>)
  2636. && constructible_from<_Vp, views::all_t<_Range>>
  2637. constexpr explicit
  2638. common_view(_Range&& __r)
  2639. : _M_base(views::all(std::forward<_Range>(__r)))
  2640. { }
  2641. */
  2642. constexpr _Vp
  2643. base() const& requires copy_constructible<_Vp>
  2644. { return _M_base; }
  2645. constexpr _Vp
  2646. base() &&
  2647. { return std::move(_M_base); }
  2648. constexpr auto
  2649. begin()
  2650. {
  2651. if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
  2652. return ranges::begin(_M_base);
  2653. else
  2654. return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
  2655. (ranges::begin(_M_base));
  2656. }
  2657. constexpr auto
  2658. begin() const requires range<const _Vp>
  2659. {
  2660. if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
  2661. return ranges::begin(_M_base);
  2662. else
  2663. return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
  2664. (ranges::begin(_M_base));
  2665. }
  2666. constexpr auto
  2667. end()
  2668. {
  2669. if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
  2670. return ranges::begin(_M_base) + ranges::size(_M_base);
  2671. else
  2672. return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
  2673. (ranges::end(_M_base));
  2674. }
  2675. constexpr auto
  2676. end() const requires range<const _Vp>
  2677. {
  2678. if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
  2679. return ranges::begin(_M_base) + ranges::size(_M_base);
  2680. else
  2681. return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
  2682. (ranges::end(_M_base));
  2683. }
  2684. constexpr auto
  2685. size() requires sized_range<_Vp>
  2686. { return ranges::size(_M_base); }
  2687. constexpr auto
  2688. size() const requires sized_range<const _Vp>
  2689. { return ranges::size(_M_base); }
  2690. };
  2691. template<typename _Range>
  2692. common_view(_Range&&) -> common_view<views::all_t<_Range>>;
  2693. namespace views
  2694. {
  2695. inline constexpr __adaptor::_RangeAdaptorClosure common
  2696. = [] <viewable_range _Range> (_Range&& __r)
  2697. {
  2698. if constexpr (common_range<_Range>
  2699. && requires { views::all(std::forward<_Range>(__r)); })
  2700. return views::all(std::forward<_Range>(__r));
  2701. else
  2702. return common_view{std::forward<_Range>(__r)};
  2703. };
  2704. } // namespace views
  2705. template<view _Vp>
  2706. requires bidirectional_range<_Vp>
  2707. class reverse_view : public view_interface<reverse_view<_Vp>>
  2708. {
  2709. private:
  2710. _Vp _M_base = _Vp();
  2711. static constexpr bool _S_needs_cached_begin
  2712. = !common_range<_Vp> && !random_access_range<_Vp>;
  2713. [[no_unique_address]]
  2714. __detail::__maybe_present_t<_S_needs_cached_begin,
  2715. __detail::_CachedPosition<_Vp>>
  2716. _M_cached_begin;
  2717. public:
  2718. reverse_view() = default;
  2719. constexpr explicit
  2720. reverse_view(_Vp __r)
  2721. : _M_base(std::move(__r))
  2722. { }
  2723. constexpr _Vp
  2724. base() const& requires copy_constructible<_Vp>
  2725. { return _M_base; }
  2726. constexpr _Vp
  2727. base() &&
  2728. { return std::move(_M_base); }
  2729. constexpr reverse_iterator<iterator_t<_Vp>>
  2730. begin()
  2731. {
  2732. if constexpr (_S_needs_cached_begin)
  2733. if (_M_cached_begin._M_has_value())
  2734. return make_reverse_iterator(_M_cached_begin._M_get(_M_base));
  2735. auto __it = ranges::next(ranges::begin(_M_base), ranges::end(_M_base));
  2736. if constexpr (_S_needs_cached_begin)
  2737. _M_cached_begin._M_set(_M_base, __it);
  2738. return make_reverse_iterator(std::move(__it));
  2739. }
  2740. constexpr auto
  2741. begin() requires common_range<_Vp>
  2742. { return make_reverse_iterator(ranges::end(_M_base)); }
  2743. constexpr auto
  2744. begin() const requires common_range<const _Vp>
  2745. { return make_reverse_iterator(ranges::end(_M_base)); }
  2746. constexpr reverse_iterator<iterator_t<_Vp>>
  2747. end()
  2748. { return make_reverse_iterator(ranges::begin(_M_base)); }
  2749. constexpr auto
  2750. end() const requires common_range<const _Vp>
  2751. { return make_reverse_iterator(ranges::begin(_M_base)); }
  2752. constexpr auto
  2753. size() requires sized_range<_Vp>
  2754. { return ranges::size(_M_base); }
  2755. constexpr auto
  2756. size() const requires sized_range<const _Vp>
  2757. { return ranges::size(_M_base); }
  2758. };
  2759. template<typename _Range>
  2760. reverse_view(_Range&&) -> reverse_view<views::all_t<_Range>>;
  2761. namespace views
  2762. {
  2763. namespace __detail
  2764. {
  2765. template<typename>
  2766. inline constexpr bool __is_reversible_subrange = false;
  2767. template<typename _Iter, subrange_kind _Kind>
  2768. inline constexpr bool
  2769. __is_reversible_subrange<subrange<reverse_iterator<_Iter>,
  2770. reverse_iterator<_Iter>,
  2771. _Kind>> = true;
  2772. template<typename>
  2773. inline constexpr bool __is_reverse_view = false;
  2774. template<typename _Vp>
  2775. inline constexpr bool __is_reverse_view<reverse_view<_Vp>> = true;
  2776. }
  2777. inline constexpr __adaptor::_RangeAdaptorClosure reverse
  2778. = [] <viewable_range _Range> (_Range&& __r)
  2779. {
  2780. using _Tp = remove_cvref_t<_Range>;
  2781. if constexpr (__detail::__is_reverse_view<_Tp>)
  2782. return std::forward<_Range>(__r).base();
  2783. else if constexpr (__detail::__is_reversible_subrange<_Tp>)
  2784. {
  2785. using _Iter = decltype(ranges::begin(__r).base());
  2786. if constexpr (sized_range<_Tp>)
  2787. return subrange<_Iter, _Iter, subrange_kind::sized>
  2788. (__r.end().base(), __r.begin().base(), __r.size());
  2789. else
  2790. return subrange<_Iter, _Iter, subrange_kind::unsized>
  2791. (__r.end().base(), __r.begin().base());
  2792. }
  2793. else
  2794. return reverse_view{std::forward<_Range>(__r)};
  2795. };
  2796. } // namespace views
  2797. namespace __detail
  2798. {
  2799. template<typename _Tp, size_t _Nm>
  2800. concept __has_tuple_element = requires(_Tp __t)
  2801. {
  2802. typename tuple_size<_Tp>::type;
  2803. requires _Nm < tuple_size_v<_Tp>;
  2804. typename tuple_element_t<_Nm, _Tp>;
  2805. { std::get<_Nm>(__t) }
  2806. -> convertible_to<const tuple_element_t<_Nm, _Tp>&>;
  2807. };
  2808. }
  2809. template<input_range _Vp, size_t _Nm>
  2810. requires view<_Vp>
  2811. && __detail::__has_tuple_element<range_value_t<_Vp>, _Nm>
  2812. && __detail::__has_tuple_element<remove_reference_t<range_reference_t<_Vp>>,
  2813. _Nm>
  2814. class elements_view : public view_interface<elements_view<_Vp, _Nm>>
  2815. {
  2816. public:
  2817. elements_view() = default;
  2818. constexpr explicit
  2819. elements_view(_Vp base)
  2820. : _M_base(std::move(base))
  2821. { }
  2822. constexpr _Vp
  2823. base() const& requires copy_constructible<_Vp>
  2824. { return _M_base; }
  2825. constexpr _Vp
  2826. base() &&
  2827. { return std::move(_M_base); }
  2828. constexpr auto
  2829. begin() requires (!__detail::__simple_view<_Vp>)
  2830. { return _Iterator<false>(ranges::begin(_M_base)); }
  2831. constexpr auto
  2832. begin() const requires range<const _Vp>
  2833. { return _Iterator<true>(ranges::begin(_M_base)); }
  2834. constexpr auto
  2835. end() requires (!__detail::__simple_view<_Vp> && !common_range<_Vp>)
  2836. { return _Sentinel<false>{ranges::end(_M_base)}; }
  2837. constexpr auto
  2838. end() requires (!__detail::__simple_view<_Vp> && common_range<_Vp>)
  2839. { return _Iterator<false>{ranges::end(_M_base)}; }
  2840. constexpr auto
  2841. end() const requires range<const _Vp>
  2842. { return _Sentinel<true>{ranges::end(_M_base)}; }
  2843. constexpr auto
  2844. end() const requires common_range<const _Vp>
  2845. { return _Iterator<true>{ranges::end(_M_base)}; }
  2846. constexpr auto
  2847. size() requires sized_range<_Vp>
  2848. { return ranges::size(_M_base); }
  2849. constexpr auto
  2850. size() const requires sized_range<const _Vp>
  2851. { return ranges::size(_M_base); }
  2852. private:
  2853. template<bool _Const>
  2854. struct _Sentinel;
  2855. template<bool _Const>
  2856. struct _Iterator
  2857. {
  2858. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  2859. iterator_t<_Base> _M_current = iterator_t<_Base>();
  2860. friend _Iterator<!_Const>;
  2861. public:
  2862. using iterator_category
  2863. = typename iterator_traits<iterator_t<_Base>>::iterator_category;
  2864. using value_type
  2865. = remove_cvref_t<tuple_element_t<_Nm, range_value_t<_Base>>>;
  2866. using difference_type = range_difference_t<_Base>;
  2867. _Iterator() = default;
  2868. constexpr explicit
  2869. _Iterator(iterator_t<_Base> current)
  2870. : _M_current(std::move(current))
  2871. { }
  2872. constexpr
  2873. _Iterator(_Iterator<!_Const> i)
  2874. requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
  2875. : _M_current(std::move(i._M_current))
  2876. { }
  2877. constexpr iterator_t<_Base>
  2878. base() const&
  2879. requires copyable<iterator_t<_Base>>
  2880. { return _M_current; }
  2881. constexpr iterator_t<_Base>
  2882. base() &&
  2883. { return std::move(_M_current); }
  2884. constexpr decltype(auto)
  2885. operator*() const
  2886. { return std::get<_Nm>(*_M_current); }
  2887. constexpr _Iterator&
  2888. operator++()
  2889. {
  2890. ++_M_current;
  2891. return *this;
  2892. }
  2893. constexpr void
  2894. operator++(int) requires (!forward_range<_Base>)
  2895. { ++_M_current; }
  2896. constexpr _Iterator
  2897. operator++(int) requires forward_range<_Base>
  2898. {
  2899. auto __tmp = *this;
  2900. ++_M_current;
  2901. return __tmp;
  2902. }
  2903. constexpr _Iterator&
  2904. operator--() requires bidirectional_range<_Base>
  2905. {
  2906. --_M_current;
  2907. return *this;
  2908. }
  2909. constexpr _Iterator
  2910. operator--(int) requires bidirectional_range<_Base>
  2911. {
  2912. auto __tmp = *this;
  2913. --_M_current;
  2914. return __tmp;
  2915. }
  2916. constexpr _Iterator&
  2917. operator+=(difference_type __n)
  2918. requires random_access_range<_Base>
  2919. {
  2920. _M_current += __n;
  2921. return *this;
  2922. }
  2923. constexpr _Iterator&
  2924. operator-=(difference_type __n)
  2925. requires random_access_range<_Base>
  2926. {
  2927. _M_current -= __n;
  2928. return *this;
  2929. }
  2930. constexpr decltype(auto)
  2931. operator[](difference_type __n) const
  2932. requires random_access_range<_Base>
  2933. { return std::get<_Nm>(*(_M_current + __n)); }
  2934. friend constexpr bool
  2935. operator==(const _Iterator& __x, const _Iterator& __y)
  2936. requires equality_comparable<iterator_t<_Base>>
  2937. { return __x._M_current == __y._M_current; }
  2938. friend constexpr bool
  2939. operator<(const _Iterator& __x, const _Iterator& __y)
  2940. requires random_access_range<_Base>
  2941. { return __x._M_current < __y._M_current; }
  2942. friend constexpr bool
  2943. operator>(const _Iterator& __x, const _Iterator& __y)
  2944. requires random_access_range<_Base>
  2945. { return __y._M_current < __x._M_current; }
  2946. friend constexpr bool
  2947. operator<=(const _Iterator& __x, const _Iterator& __y)
  2948. requires random_access_range<_Base>
  2949. { return !(__y._M_current > __x._M_current); }
  2950. friend constexpr bool
  2951. operator>=(const _Iterator& __x, const _Iterator& __y)
  2952. requires random_access_range<_Base>
  2953. { return !(__x._M_current > __y._M_current); }
  2954. #ifdef __cpp_lib_three_way_comparison
  2955. friend constexpr auto
  2956. operator<=>(const _Iterator& __x, const _Iterator& __y)
  2957. requires random_access_range<_Base>
  2958. && three_way_comparable<iterator_t<_Base>>
  2959. { return __x._M_current <=> __y._M_current; }
  2960. #endif
  2961. friend constexpr _Iterator
  2962. operator+(const _Iterator& __x, difference_type __y)
  2963. requires random_access_range<_Base>
  2964. { return _Iterator{__x} += __y; }
  2965. friend constexpr _Iterator
  2966. operator+(difference_type __x, const _Iterator& __y)
  2967. requires random_access_range<_Base>
  2968. { return __y + __x; }
  2969. friend constexpr _Iterator
  2970. operator-(const _Iterator& __x, difference_type __y)
  2971. requires random_access_range<_Base>
  2972. { return _Iterator{__x} -= __y; }
  2973. friend constexpr difference_type
  2974. operator-(const _Iterator& __x, const _Iterator& __y)
  2975. requires random_access_range<_Base>
  2976. { return __x._M_current - __y._M_current; }
  2977. friend _Sentinel<_Const>;
  2978. };
  2979. template<bool _Const>
  2980. struct _Sentinel
  2981. {
  2982. private:
  2983. constexpr bool
  2984. _M_equal(const _Iterator<_Const>& __x) const
  2985. { return __x._M_current == _M_end; }
  2986. using _Base = __detail::__maybe_const_t<_Const, _Vp>;
  2987. sentinel_t<_Base> _M_end = sentinel_t<_Base>();
  2988. public:
  2989. _Sentinel() = default;
  2990. constexpr explicit
  2991. _Sentinel(sentinel_t<_Base> __end)
  2992. : _M_end(std::move(__end))
  2993. { }
  2994. constexpr
  2995. _Sentinel(_Sentinel<!_Const> __other)
  2996. requires _Const
  2997. && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
  2998. : _M_end(std::move(__other._M_end))
  2999. { }
  3000. constexpr sentinel_t<_Base>
  3001. base() const
  3002. { return _M_end; }
  3003. template<bool _Const2>
  3004. requires sentinel_for<sentinel_t<_Base>,
  3005. iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
  3006. friend constexpr bool
  3007. operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
  3008. { return __y._M_equal(__x); }
  3009. template<bool _Const2,
  3010. typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
  3011. requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
  3012. friend constexpr range_difference_t<_Base2>
  3013. operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
  3014. { return __x._M_current - __y._M_end; }
  3015. template<bool _Const2,
  3016. typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
  3017. requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
  3018. friend constexpr range_difference_t<_Base>
  3019. operator-(const _Sentinel& __x, const _Iterator<_Const2>& __y)
  3020. { return __x._M_end - __y._M_current; }
  3021. friend _Sentinel<!_Const>;
  3022. };
  3023. _Vp _M_base = _Vp();
  3024. };
  3025. template<typename _Range>
  3026. using keys_view = elements_view<views::all_t<_Range>, 0>;
  3027. template<typename _Range>
  3028. using values_view = elements_view<views::all_t<_Range>, 1>;
  3029. namespace views
  3030. {
  3031. template<size_t _Nm>
  3032. inline constexpr __adaptor::_RangeAdaptorClosure elements
  3033. = [] <viewable_range _Range> (_Range&& __r)
  3034. {
  3035. using _El = elements_view<views::all_t<_Range>, _Nm>;
  3036. return _El{std::forward<_Range>(__r)};
  3037. };
  3038. inline constexpr __adaptor::_RangeAdaptorClosure keys = elements<0>;
  3039. inline constexpr __adaptor::_RangeAdaptorClosure values = elements<1>;
  3040. } // namespace views
  3041. } // namespace ranges
  3042. namespace views = ranges::views;
  3043. template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
  3044. struct tuple_size<ranges::subrange<_Iter, _Sent, _Kind>>
  3045. : integral_constant<size_t, 2>
  3046. { };
  3047. template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
  3048. struct tuple_element<0, ranges::subrange<_Iter, _Sent, _Kind>>
  3049. { using type = _Iter; };
  3050. template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
  3051. struct tuple_element<1, ranges::subrange<_Iter, _Sent, _Kind>>
  3052. { using type = _Sent; };
  3053. template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
  3054. struct tuple_element<0, const ranges::subrange<_Iter, _Sent, _Kind>>
  3055. { using type = _Iter; };
  3056. template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
  3057. struct tuple_element<1, const ranges::subrange<_Iter, _Sent, _Kind>>
  3058. { using type = _Sent; };
  3059. _GLIBCXX_END_NAMESPACE_VERSION
  3060. } // namespace
  3061. #endif // library concepts
  3062. #endif // C++2a
  3063. #endif /* _GLIBCXX_RANGES */