Where Online Learning is simpler!
The C and C++ Include Header Files
/usr/include/c++/11/ranges
$ cat -n /usr/include/c++/11/ranges 1 //
-*- C++ -*- 2 3 // Copyright (C) 2019-2021 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 //
. 24 25 /** @file include/ranges 26 * This is a Standard C++ Library header. 27 * @ingroup concepts 28 */ 29 30 #ifndef _GLIBCXX_RANGES 31 #define _GLIBCXX_RANGES 1 32 33 #if __cplusplus > 201703L 34 35 #pragma GCC system_header 36 37 #include
38 39 #if __cpp_lib_concepts 40 41 #include
42 #include
43 #include
44 #include
45 #include
46 #include
47 #include
48 49 /** 50 * @defgroup ranges Ranges 51 * 52 * Components for dealing with ranges of elements. 53 */ 54 55 namespace std _GLIBCXX_VISIBILITY(default) 56 { 57 _GLIBCXX_BEGIN_NAMESPACE_VERSION 58 namespace ranges 59 { 60 // [range.access] customization point objects 61 // [range.req] range and view concepts 62 // [range.dangling] dangling iterator handling 63 // Defined in
64 65 // [view.interface] View interface 66 // [range.subrange] Sub-ranges 67 // Defined in
68 69 // C++20 24.6 [range.factories] Range factories 70 71 /// A view that contains no elements. 72 template
requires is_object_v<_Tp> 73 class empty_view 74 : public view_interface
> 75 { 76 public: 77 static constexpr _Tp* begin() noexcept { return nullptr; } 78 static constexpr _Tp* end() noexcept { return nullptr; } 79 static constexpr _Tp* data() noexcept { return nullptr; } 80 static constexpr size_t size() noexcept { return 0; } 81 static constexpr bool empty() noexcept { return true; } 82 }; 83 84 template
85 inline constexpr bool enable_borrowed_range
> = true; 86 87 namespace __detail 88 { 89 template
90 concept __boxable = copy_constructible<_Tp> && is_object_v<_Tp>; 91 92 template<__boxable _Tp> 93 struct __box : std::optional<_Tp> 94 { 95 using std::optional<_Tp>::optional; 96 97 constexpr 98 __box() 99 noexcept(is_nothrow_default_constructible_v<_Tp>) 100 requires default_initializable<_Tp> 101 : std::optional<_Tp>{std::in_place} 102 { } 103 104 __box(const __box&) = default; 105 __box(__box&&) = default; 106 107 using std::optional<_Tp>::operator=; 108 109 // _GLIBCXX_RESOLVE_LIB_DEFECTS 110 // 3477. Simplify constraints for semiregular-box 111 __box& 112 operator=(const __box& __that) 113 noexcept(is_nothrow_copy_constructible_v<_Tp>) 114 requires (!copyable<_Tp>) 115 { 116 if (this != std::__addressof(__that)) 117 { 118 if ((bool)__that) 119 this->emplace(*__that); 120 else 121 this->reset(); 122 } 123 return *this; 124 } 125 126 __box& 127 operator=(__box&& __that) 128 noexcept(is_nothrow_move_constructible_v<_Tp>) 129 requires (!movable<_Tp>) 130 { 131 if (this != std::__addressof(__that)) 132 { 133 if ((bool)__that) 134 this->emplace(std::move(*__that)); 135 else 136 this->reset(); 137 } 138 return *this; 139 } 140 }; 141 142 // For types which are already copyable, this specialization of the 143 // copyable wrapper stores the object directly without going through 144 // std::optional. It provides just the subset of the primary template's 145 // API that we currently use. 146 template<__boxable _Tp> 147 requires copyable<_Tp> 148 struct __box<_Tp> 149 { 150 private: 151 [[no_unique_address]] _Tp _M_value = _Tp(); 152 153 public: 154 __box() requires default_initializable<_Tp> = default; 155 156 constexpr explicit 157 __box(const _Tp& __t) 158 noexcept(is_nothrow_copy_constructible_v<_Tp>) 159 : _M_value(__t) 160 { } 161 162 constexpr explicit 163 __box(_Tp&& __t) 164 noexcept(is_nothrow_move_constructible_v<_Tp>) 165 : _M_value(std::move(__t)) 166 { } 167 168 template
169 requires constructible_from<_Tp, _Args...> 170 constexpr explicit 171 __box(in_place_t, _Args&&... __args) 172 noexcept(is_nothrow_constructible_v<_Tp, _Args...>) 173 : _M_value(std::forward<_Args>(__args)...) 174 { } 175 176 constexpr bool 177 has_value() const noexcept 178 { return true; }; 179 180 constexpr _Tp& 181 operator*() noexcept 182 { return _M_value; } 183 184 constexpr const _Tp& 185 operator*() const noexcept 186 { return _M_value; } 187 188 constexpr _Tp* 189 operator->() noexcept 190 { return std::__addressof(_M_value); } 191 192 constexpr const _Tp* 193 operator->() const noexcept 194 { return std::__addressof(_M_value); } 195 }; 196 } // namespace __detail 197 198 /// A view that contains exactly one element. 199 template
requires is_object_v<_Tp> 200 class single_view : public view_interface
> 201 { 202 public: 203 single_view() = default; 204 205 constexpr explicit 206 single_view(const _Tp& __t) 207 : _M_value(__t) 208 { } 209 210 constexpr explicit 211 single_view(_Tp&& __t) 212 : _M_value(std::move(__t)) 213 { } 214 215 // _GLIBCXX_RESOLVE_LIB_DEFECTS 216 // 3428. single_view's in place constructor should be explicit 217 template
218 requires constructible_from<_Tp, _Args...> 219 constexpr explicit 220 single_view(in_place_t, _Args&&... __args) 221 : _M_value{in_place, std::forward<_Args>(__args)...} 222 { } 223 224 constexpr _Tp* 225 begin() noexcept 226 { return data(); } 227 228 constexpr const _Tp* 229 begin() const noexcept 230 { return data(); } 231 232 constexpr _Tp* 233 end() noexcept 234 { return data() + 1; } 235 236 constexpr const _Tp* 237 end() const noexcept 238 { return data() + 1; } 239 240 static constexpr size_t 241 size() noexcept 242 { return 1; } 243 244 constexpr _Tp* 245 data() noexcept 246 { return _M_value.operator->(); } 247 248 constexpr const _Tp* 249 data() const noexcept 250 { return _M_value.operator->(); } 251 252 private: 253 [[no_unique_address]] __detail::__box<_Tp> _M_value; 254 }; 255 256 template
257 single_view(_Tp) -> single_view<_Tp>; 258 259 namespace __detail 260 { 261 template
262 constexpr auto __to_signed_like(_Wp __w) noexcept 263 { 264 if constexpr (!integral<_Wp>) 265 return iter_difference_t<_Wp>(); 266 else if constexpr (sizeof(iter_difference_t<_Wp>) > sizeof(_Wp)) 267 return iter_difference_t<_Wp>(__w); 268 else if constexpr (sizeof(ptrdiff_t) > sizeof(_Wp)) 269 return ptrdiff_t(__w); 270 else if constexpr (sizeof(long long) > sizeof(_Wp)) 271 return (long long)(__w); 272 #ifdef __SIZEOF_INT128__ 273 else if constexpr (__SIZEOF_INT128__ > sizeof(_Wp)) 274 return __int128(__w); 275 #endif 276 else 277 return __max_diff_type(__w); 278 } 279 280 template
281 using __iota_diff_t = decltype(__to_signed_like(std::declval<_Wp>())); 282 283 template
284 concept __decrementable = incrementable<_It> 285 && requires(_It __i) 286 { 287 { --__i } -> same_as<_It&>; 288 { __i-- } -> same_as<_It>; 289 }; 290 291 template
292 concept __advanceable = __decrementable<_It> && totally_ordered<_It> 293 && requires( _It __i, const _It __j, const __iota_diff_t<_It> __n) 294 { 295 { __i += __n } -> same_as<_It&>; 296 { __i -= __n } -> same_as<_It&>; 297 _It(__j + __n); 298 _It(__n + __j); 299 _It(__j - __n); 300 { __j - __j } -> convertible_to<__iota_diff_t<_It>>; 301 }; 302 303 template
304 struct __iota_view_iter_cat 305 { }; 306 307 template
308 struct __iota_view_iter_cat<_Winc> 309 { using iterator_category = input_iterator_tag; }; 310 } // namespace __detail 311 312 template
314 requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound> 315 && copyable<_Winc> 316 class iota_view : public view_interface
> 317 { 318 private: 319 struct _Sentinel; 320 321 struct _Iterator : __detail::__iota_view_iter_cat<_Winc> 322 { 323 private: 324 static auto 325 _S_iter_concept() 326 { 327 using namespace __detail; 328 if constexpr (__advanceable<_Winc>) 329 return random_access_iterator_tag{}; 330 else if constexpr (__decrementable<_Winc>) 331 return bidirectional_iterator_tag{}; 332 else if constexpr (incrementable<_Winc>) 333 return forward_iterator_tag{}; 334 else 335 return input_iterator_tag{}; 336 } 337 338 public: 339 using iterator_concept = decltype(_S_iter_concept()); 340 // iterator_category defined in __iota_view_iter_cat 341 using value_type = _Winc; 342 using difference_type = __detail::__iota_diff_t<_Winc>; 343 344 _Iterator() requires default_initializable<_Winc> = default; 345 346 constexpr explicit 347 _Iterator(_Winc __value) 348 : _M_value(__value) { } 349 350 constexpr _Winc 351 operator*() const noexcept(is_nothrow_copy_constructible_v<_Winc>) 352 { return _M_value; } 353 354 constexpr _Iterator& 355 operator++() 356 { 357 ++_M_value; 358 return *this; 359 } 360 361 constexpr void 362 operator++(int) 363 { ++*this; } 364 365 constexpr _Iterator 366 operator++(int) requires incrementable<_Winc> 367 { 368 auto __tmp = *this; 369 ++*this; 370 return __tmp; 371 } 372 373 constexpr _Iterator& 374 operator--() requires __detail::__decrementable<_Winc> 375 { 376 --_M_value; 377 return *this; 378 } 379 380 constexpr _Iterator 381 operator--(int) requires __detail::__decrementable<_Winc> 382 { 383 auto __tmp = *this; 384 --*this; 385 return __tmp; 386 } 387 388 constexpr _Iterator& 389 operator+=(difference_type __n) requires __detail::__advanceable<_Winc> 390 { 391 using __detail::__is_integer_like; 392 using __detail::__is_signed_integer_like; 393 if constexpr (__is_integer_like<_Winc> 394 && !__is_signed_integer_like<_Winc>) 395 { 396 if (__n >= difference_type(0)) 397 _M_value += static_cast<_Winc>(__n); 398 else 399 _M_value -= static_cast<_Winc>(-__n); 400 } 401 else 402 _M_value += __n; 403 return *this; 404 } 405 406 constexpr _Iterator& 407 operator-=(difference_type __n) requires __detail::__advanceable<_Winc> 408 { 409 using __detail::__is_integer_like; 410 using __detail::__is_signed_integer_like; 411 if constexpr (__is_integer_like<_Winc> 412 && !__is_signed_integer_like<_Winc>) 413 { 414 if (__n >= difference_type(0)) 415 _M_value -= static_cast<_Winc>(__n); 416 else 417 _M_value += static_cast<_Winc>(-__n); 418 } 419 else 420 _M_value -= __n; 421 return *this; 422 } 423 424 constexpr _Winc 425 operator[](difference_type __n) const 426 requires __detail::__advanceable<_Winc> 427 { return _Winc(_M_value + __n); } 428 429 friend constexpr bool 430 operator==(const _Iterator& __x, const _Iterator& __y) 431 requires equality_comparable<_Winc> 432 { return __x._M_value == __y._M_value; } 433 434 friend constexpr bool 435 operator<(const _Iterator& __x, const _Iterator& __y) 436 requires totally_ordered<_Winc> 437 { return __x._M_value < __y._M_value; } 438 439 friend constexpr bool 440 operator>(const _Iterator& __x, const _Iterator& __y) 441 requires totally_ordered<_Winc> 442 { return __y < __x; } 443 444 friend constexpr bool 445 operator<=(const _Iterator& __x, const _Iterator& __y) 446 requires totally_ordered<_Winc> 447 { return !(__y < __x); } 448 449 friend constexpr bool 450 operator>=(const _Iterator& __x, const _Iterator& __y) 451 requires totally_ordered<_Winc> 452 { return !(__x < __y); } 453 454 #ifdef __cpp_lib_three_way_comparison 455 friend constexpr auto 456 operator<=>(const _Iterator& __x, const _Iterator& __y) 457 requires totally_ordered<_Winc> && three_way_comparable<_Winc> 458 { return __x._M_value <=> __y._M_value; } 459 #endif 460 461 friend constexpr _Iterator 462 operator+(_Iterator __i, difference_type __n) 463 requires __detail::__advanceable<_Winc> 464 { 465 __i += __n; 466 return __i; 467 } 468 469 friend constexpr _Iterator 470 operator+(difference_type __n, _Iterator __i) 471 requires __detail::__advanceable<_Winc> 472 { return __i += __n; } 473 474 friend constexpr _Iterator 475 operator-(_Iterator __i, difference_type __n) 476 requires __detail::__advanceable<_Winc> 477 { 478 __i -= __n; 479 return __i; 480 } 481 482 friend constexpr difference_type 483 operator-(const _Iterator& __x, const _Iterator& __y) 484 requires __detail::__advanceable<_Winc> 485 { 486 using __detail::__is_integer_like; 487 using __detail::__is_signed_integer_like; 488 using _Dt = difference_type; 489 if constexpr (__is_integer_like<_Winc>) 490 { 491 if constexpr (__is_signed_integer_like<_Winc>) 492 return _Dt(_Dt(__x._M_value) - _Dt(__y._M_value)); 493 else 494 return (__y._M_value > __x._M_value) 495 ? _Dt(-_Dt(__y._M_value - __x._M_value)) 496 : _Dt(__x._M_value - __y._M_value); 497 } 498 else 499 return __x._M_value - __y._M_value; 500 } 501 502 private: 503 _Winc _M_value = _Winc(); 504 505 friend iota_view; 506 friend _Sentinel; 507 }; 508 509 struct _Sentinel 510 { 511 private: 512 constexpr bool 513 _M_equal(const _Iterator& __x) const 514 { return __x._M_value == _M_bound; } 515 516 constexpr auto 517 _M_distance_from(const _Iterator& __x) const 518 { return _M_bound - __x._M_value; } 519 520 _Bound _M_bound = _Bound(); 521 522 public: 523 _Sentinel() = default; 524 525 constexpr explicit 526 _Sentinel(_Bound __bound) 527 : _M_bound(__bound) { } 528 529 friend constexpr bool 530 operator==(const _Iterator& __x, const _Sentinel& __y) 531 { return __y._M_equal(__x); } 532 533 friend constexpr iter_difference_t<_Winc> 534 operator-(const _Iterator& __x, const _Sentinel& __y) 535 requires sized_sentinel_for<_Bound, _Winc> 536 { return -__y._M_distance_from(__x); } 537 538 friend constexpr iter_difference_t<_Winc> 539 operator-(const _Sentinel& __x, const _Iterator& __y) 540 requires sized_sentinel_for<_Bound, _Winc> 541 { return __x._M_distance_from(__y); } 542 543 friend iota_view; 544 }; 545 546 _Winc _M_value = _Winc(); 547 [[no_unique_address]] _Bound _M_bound = _Bound(); 548 549 public: 550 iota_view() requires default_initializable<_Winc> = default; 551 552 constexpr explicit 553 iota_view(_Winc __value) 554 : _M_value(__value) 555 { } 556 557 constexpr 558 iota_view(type_identity_t<_Winc> __value, 559 type_identity_t<_Bound> __bound) 560 : _M_value(__value), _M_bound(__bound) 561 { 562 if constexpr (totally_ordered_with<_Winc, _Bound>) 563 __glibcxx_assert( bool(__value <= __bound) ); 564 } 565 566 constexpr 567 iota_view(_Iterator __first, _Iterator __last) 568 requires same_as<_Winc, _Bound> 569 : iota_view(__first._M_value, __last._M_value) 570 { } 571 572 constexpr 573 iota_view(_Iterator __first, unreachable_sentinel_t __last) 574 requires same_as<_Bound, unreachable_sentinel_t> 575 : iota_view(__first._M_value, __last) 576 { } 577 578 constexpr 579 iota_view(_Iterator __first, _Sentinel __last) 580 requires (!same_as<_Winc, _Bound>) && (!same_as<_Bound, unreachable_sentinel_t>) 581 : iota_view(__first._M_value, __last._M_bound) 582 { } 583 584 constexpr _Iterator 585 begin() const { return _Iterator{_M_value}; } 586 587 constexpr auto 588 end() const 589 { 590 if constexpr (same_as<_Bound, unreachable_sentinel_t>) 591 return unreachable_sentinel; 592 else 593 return _Sentinel{_M_bound}; 594 } 595 596 constexpr _Iterator 597 end() const requires same_as<_Winc, _Bound> 598 { return _Iterator{_M_bound}; } 599 600 constexpr auto 601 size() const 602 requires (same_as<_Winc, _Bound> && __detail::__advanceable<_Winc>) 603 || (integral<_Winc> && integral<_Bound>) 604 || sized_sentinel_for<_Bound, _Winc> 605 { 606 using __detail::__is_integer_like; 607 using __detail::__to_unsigned_like; 608 if constexpr (integral<_Winc> && integral<_Bound>) 609 { 610 using _Up = make_unsigned_t
; 611 return _Up(_M_bound) - _Up(_M_value); 612 } 613 else if constexpr (__is_integer_like<_Winc>) 614 return __to_unsigned_like(_M_bound) - __to_unsigned_like(_M_value); 615 else 616 return __to_unsigned_like(_M_bound - _M_value); 617 } 618 }; 619 620 template
621 requires (!__detail::__is_integer_like<_Winc> 622 || !__detail::__is_integer_like<_Bound> 623 || (__detail::__is_signed_integer_like<_Winc> 624 == __detail::__is_signed_integer_like<_Bound>)) 625 iota_view(_Winc, _Bound) -> iota_view<_Winc, _Bound>; 626 627 template
628 inline constexpr bool 629 enable_borrowed_range
> = true; 630 631 namespace views 632 { 633 template
634 inline constexpr empty_view<_Tp> empty{}; 635 636 struct _Single 637 { 638 template
639 constexpr auto 640 operator()(_Tp&& __e) const 641 { return single_view
>(std::forward<_Tp>(__e)); } 642 }; 643 644 inline constexpr _Single single{}; 645 646 struct _Iota 647 { 648 template
649 constexpr auto 650 operator()(_Tp&& __e) const 651 { return iota_view(std::forward<_Tp>(__e)); } 652 653 template
654 constexpr auto 655 operator()(_Tp&& __e, _Up&& __f) const 656 { return iota_view(std::forward<_Tp>(__e), std::forward<_Up>(__f)); } 657 }; 658 659 inline constexpr _Iota iota{}; 660 } // namespace views 661 662 namespace __detail 663 { 664 template
665 concept __stream_extractable 666 = requires(basic_istream<_CharT, _Traits>& is, _Val& t) { is >> t; }; 667 } // namespace __detail 668 669 template
> 671 requires default_initializable<_Val> 672 && __detail::__stream_extractable<_Val, _CharT, _Traits> 673 class basic_istream_view 674 : public view_interface
> 675 { 676 public: 677 basic_istream_view() = default; 678 679 constexpr explicit 680 basic_istream_view(basic_istream<_CharT, _Traits>& __stream) 681 : _M_stream(std::__addressof(__stream)) 682 { } 683 684 constexpr auto 685 begin() 686 { 687 if (_M_stream != nullptr) 688 *_M_stream >> _M_object; 689 return _Iterator{this}; 690 } 691 692 constexpr default_sentinel_t 693 end() const noexcept 694 { return default_sentinel; } 695 696 private: 697 basic_istream<_CharT, _Traits>* _M_stream = nullptr; 698 _Val _M_object = _Val(); 699 700 struct _Iterator 701 { 702 public: 703 using iterator_concept = input_iterator_tag; 704 using difference_type = ptrdiff_t; 705 using value_type = _Val; 706 707 _Iterator() = default; 708 709 constexpr explicit 710 _Iterator(basic_istream_view* __parent) noexcept 711 : _M_parent(__parent) 712 { } 713 714 _Iterator(const _Iterator&) = delete; 715 _Iterator(_Iterator&&) = default; 716 _Iterator& operator=(const _Iterator&) = delete; 717 _Iterator& operator=(_Iterator&&) = default; 718 719 _Iterator& 720 operator++() 721 { 722 __glibcxx_assert(_M_parent->_M_stream != nullptr); 723 *_M_parent->_M_stream >> _M_parent->_M_object; 724 return *this; 725 } 726 727 void 728 operator++(int) 729 { ++*this; } 730 731 _Val& 732 operator*() const 733 { 734 __glibcxx_assert(_M_parent->_M_stream != nullptr); 735 return _M_parent->_M_object; 736 } 737 738 friend bool 739 operator==(const _Iterator& __x, default_sentinel_t) 740 { return __x._M_at_end(); } 741 742 private: 743 basic_istream_view* _M_parent = nullptr; 744 745 bool 746 _M_at_end() const 747 { return _M_parent == nullptr || !*_M_parent->_M_stream; } 748 }; 749 750 friend _Iterator; 751 }; 752 753 template
754 basic_istream_view<_Val, _CharT, _Traits> 755 istream_view(basic_istream<_CharT, _Traits>& __s) 756 { return basic_istream_view<_Val, _CharT, _Traits>{__s}; } 757 758 // C++20 24.7 [range.adaptors] Range adaptors 759 760 namespace __detail 761 { 762 struct _Empty { }; 763 764 // Alias for a type that is conditionally present 765 // (and is an empty type otherwise). 766 // Data members using this alias should use [[no_unique_address]] so that 767 // they take no space when not needed. 768 template
769 using __maybe_present_t = conditional_t<_Present, _Tp, _Empty>; 770 771 // Alias for a type that is conditionally const. 772 template
773 using __maybe_const_t = conditional_t<_Const, const _Tp, _Tp>; 774 775 } // namespace __detail 776 777 namespace views::__adaptor 778 { 779 // True if the range adaptor _Adaptor can be applied with _Args. 780 template
781 concept __adaptor_invocable 782 = requires { std::declval<_Adaptor>()(declval<_Args>()...); }; 783 784 // True if the range adaptor non-closure _Adaptor can be partially applied 785 // with _Args. 786 template
787 concept __adaptor_partial_app_viable = (_Adaptor::_S_arity > 1) 788 && (sizeof...(_Args) == _Adaptor::_S_arity - 1) 789 && (constructible_from
, _Args> && ...); 790 791 template
792 struct _Partial; 793 794 template
795 struct _Pipe; 796 797 // The base class of every range adaptor closure. 798 // 799 // The derived class should define the optional static data member 800 // _S_has_simple_call_op to true if the behavior of this adaptor is 801 // independent of the constness/value category of the adaptor object. 802 struct _RangeAdaptorClosure 803 { 804 // range | adaptor is equivalent to adaptor(range). 805 template
806 requires derived_from
, _RangeAdaptorClosure> 807 && __adaptor_invocable<_Self, _Range> 808 friend constexpr auto 809 operator|(_Range&& __r, _Self&& __self) 810 { return std::forward<_Self>(__self)(std::forward<_Range>(__r)); } 811 812 // Compose the adaptors __lhs and __rhs into a pipeline, returning 813 // another range adaptor closure object. 814 template
815 requires derived_from<_Lhs, _RangeAdaptorClosure> 816 && derived_from<_Rhs, _RangeAdaptorClosure> 817 friend constexpr auto 818 operator|(_Lhs __lhs, _Rhs __rhs) 819 { return _Pipe<_Lhs, _Rhs>{std::move(__lhs), std::move(__rhs)}; } 820 }; 821 822 // The base class of every range adaptor non-closure. 823 // 824 // The static data member _Derived::_S_arity must contain the total number of 825 // arguments that the adaptor takes, and the class _Derived must introduce 826 // _RangeAdaptor::operator() into the class scope via a using-declaration. 827 // 828 // The optional static data member _Derived::_S_has_simple_extra_args should 829 // be defined to true if the behavior of this adaptor is independent of the 830 // constness/value category of the extra arguments. This data member could 831 // also be defined as a variable template parameterized by the types of the 832 // extra arguments. 833 template
834 struct _RangeAdaptor 835 { 836 // Partially apply the arguments __args to the range adaptor _Derived, 837 // returning a range adaptor closure object. 838 template
839 requires __adaptor_partial_app_viable<_Derived, _Args...> 840 constexpr auto 841 operator()(_Args&&... __args) const 842 { 843 return _Partial<_Derived, decay_t<_Args>...>{std::forward<_Args>(__args)...}; 844 } 845 }; 846 847 // True if the range adaptor closure _Adaptor has a simple operator(), i.e. 848 // one that's not overloaded according to constness or value category of the 849 // _Adaptor object. 850 template
851 concept __closure_has_simple_call_op = _Adaptor::_S_has_simple_call_op; 852 853 // True if the behavior of the range adaptor non-closure _Adaptor is 854 // independent of the value category of its extra arguments _Args. 855 template
856 concept __adaptor_has_simple_extra_args = _Adaptor::_S_has_simple_extra_args 857 || _Adaptor::template _S_has_simple_extra_args<_Args...>; 858 859 // A range adaptor closure that represents partial application of 860 // the range adaptor _Adaptor with arguments _Args. 861 template
862 struct _Partial : _RangeAdaptorClosure 863 { 864 tuple<_Args...> _M_args; 865 866 constexpr 867 _Partial(_Args... __args) 868 : _M_args(std::move(__args)...) 869 { } 870 871 // Invoke _Adaptor with arguments __r, _M_args... according to the 872 // value category of this _Partial object. 873 template
874 requires __adaptor_invocable<_Adaptor, _Range, const _Args&...> 875 constexpr auto 876 operator()(_Range&& __r) const & 877 { 878 auto __forwarder = [&__r] (const auto&... __args) { 879 return _Adaptor{}(std::forward<_Range>(__r), __args...); 880 }; 881 return std::apply(__forwarder, _M_args); 882 } 883 884 template
885 requires __adaptor_invocable<_Adaptor, _Range, _Args...> 886 constexpr auto 887 operator()(_Range&& __r) && 888 { 889 auto __forwarder = [&__r] (auto&... __args) { 890 return _Adaptor{}(std::forward<_Range>(__r), std::move(__args)...); 891 }; 892 return std::apply(__forwarder, _M_args); 893 } 894 895 template
896 constexpr auto 897 operator()(_Range&& __r) const && = delete; 898 }; 899 900 // A lightweight specialization of the above primary template for 901 // the common case where _Adaptor accepts a single extra argument. 902 template
903 struct _Partial<_Adaptor, _Arg> : _RangeAdaptorClosure 904 { 905 _Arg _M_arg; 906 907 constexpr 908 _Partial(_Arg __arg) 909 : _M_arg(std::move(__arg)) 910 { } 911 912 template
913 requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> 914 constexpr auto 915 operator()(_Range&& __r) const & 916 { return _Adaptor{}(std::forward<_Range>(__r), _M_arg); } 917 918 template
919 requires __adaptor_invocable<_Adaptor, _Range, _Arg> 920 constexpr auto 921 operator()(_Range&& __r) && 922 { return _Adaptor{}(std::forward<_Range>(__r), std::move(_M_arg)); } 923 924 template
925 constexpr auto 926 operator()(_Range&& __r) const && = delete; 927 }; 928 929 // Partial specialization of the primary template for the case where the extra 930 // arguments of the adaptor can always be safely and efficiently forwarded by 931 // const reference. This lets us get away with a single operator() overload, 932 // which makes overload resolution failure diagnostics more concise. 933 template
934 requires __adaptor_has_simple_extra_args<_Adaptor, _Args...> 935 && (is_trivially_copyable_v<_Args> && ...) 936 struct _Partial<_Adaptor, _Args...> : _RangeAdaptorClosure 937 { 938 tuple<_Args...> _M_args; 939 940 constexpr 941 _Partial(_Args... __args) 942 : _M_args(std::move(__args)...) 943 { } 944 945 // Invoke _Adaptor with arguments __r, const _M_args&... regardless 946 // of the value category of this _Partial object. 947 template
948 requires __adaptor_invocable<_Adaptor, _Range, const _Args&...> 949 constexpr auto 950 operator()(_Range&& __r) const 951 { 952 auto __forwarder = [&__r] (const auto&... __args) { 953 return _Adaptor{}(std::forward<_Range>(__r), __args...); 954 }; 955 return std::apply(__forwarder, _M_args); 956 } 957 958 static constexpr bool _S_has_simple_call_op = true; 959 }; 960 961 // A lightweight specialization of the above template for the common case 962 // where _Adaptor accepts a single extra argument. 963 template
964 requires __adaptor_has_simple_extra_args<_Adaptor, _Arg> 965 && is_trivially_copyable_v<_Arg> 966 struct _Partial<_Adaptor, _Arg> : _RangeAdaptorClosure 967 { 968 _Arg _M_arg; 969 970 constexpr 971 _Partial(_Arg __arg) 972 : _M_arg(std::move(__arg)) 973 { } 974 975 template
976 requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> 977 constexpr auto 978 operator()(_Range&& __r) const 979 { return _Adaptor{}(std::forward<_Range>(__r), _M_arg); } 980 981 static constexpr bool _S_has_simple_call_op = true; 982 }; 983 984 template
985 concept __pipe_invocable 986 = requires { std::declval<_Rhs>()(std::declval<_Lhs>()(std::declval<_Range>())); }; 987 988 // A range adaptor closure that represents composition of the range 989 // adaptor closures _Lhs and _Rhs. 990 template
991 struct _Pipe : _RangeAdaptorClosure 992 { 993 [[no_unique_address]] _Lhs _M_lhs; 994 [[no_unique_address]] _Rhs _M_rhs; 995 996 constexpr 997 _Pipe(_Lhs __lhs, _Rhs __rhs) 998 : _M_lhs(std::move(__lhs)), _M_rhs(std::move(__rhs)) 999 { } 1000 1001 // Invoke _M_rhs(_M_lhs(__r)) according to the value category of this 1002 // range adaptor closure object. 1003 template
1004 requires __pipe_invocable
1005 constexpr auto 1006 operator()(_Range&& __r) const & 1007 { return _M_rhs(_M_lhs(std::forward<_Range>(__r))); } 1008 1009 template
1010 requires __pipe_invocable<_Lhs, _Rhs, _Range> 1011 constexpr auto 1012 operator()(_Range&& __r) && 1013 { return std::move(_M_rhs)(std::move(_M_lhs)(std::forward<_Range>(__r))); } 1014 1015 template
1016 constexpr auto 1017 operator()(_Range&& __r) const && = delete; 1018 }; 1019 1020 // A partial specialization of the above primary template for the case where 1021 // both adaptor operands have a simple operator(). This in turn lets us 1022 // implement composition using a single simple operator(), which makes 1023 // overload resolution failure diagnostics more concise. 1024 template
1025 requires __closure_has_simple_call_op<_Lhs> 1026 && __closure_has_simple_call_op<_Rhs> 1027 struct _Pipe<_Lhs, _Rhs> : _RangeAdaptorClosure 1028 { 1029 [[no_unique_address]] _Lhs _M_lhs; 1030 [[no_unique_address]] _Rhs _M_rhs; 1031 1032 constexpr 1033 _Pipe(_Lhs __lhs, _Rhs __rhs) 1034 : _M_lhs(std::move(__lhs)), _M_rhs(std::move(__rhs)) 1035 { } 1036 1037 template
1038 requires __pipe_invocable
1039 constexpr auto 1040 operator()(_Range&& __r) const 1041 { return _M_rhs(_M_lhs(std::forward<_Range>(__r))); } 1042 1043 static constexpr bool _S_has_simple_call_op = true; 1044 }; 1045 } // namespace views::__adaptor 1046 1047 template
requires is_object_v<_Range> 1048 class ref_view : public view_interface
> 1049 { 1050 private: 1051 _Range* _M_r = nullptr; 1052 1053 static void _S_fun(_Range&); // not defined 1054 static void _S_fun(_Range&&) = delete; 1055 1056 public: 1057 constexpr 1058 ref_view() noexcept = default; 1059 1060 template<__detail::__not_same_as
_Tp> 1061 requires convertible_to<_Tp, _Range&> 1062 && requires { _S_fun(declval<_Tp>()); } 1063 constexpr 1064 ref_view(_Tp&& __t) 1065 : _M_r(std::__addressof(static_cast<_Range&>(std::forward<_Tp>(__t)))) 1066 { } 1067 1068 constexpr _Range& 1069 base() const 1070 { return *_M_r; } 1071 1072 constexpr iterator_t<_Range> 1073 begin() const 1074 { return ranges::begin(*_M_r); } 1075 1076 constexpr sentinel_t<_Range> 1077 end() const 1078 { return ranges::end(*_M_r); } 1079 1080 constexpr bool 1081 empty() const requires requires { ranges::empty(*_M_r); } 1082 { return ranges::empty(*_M_r); } 1083 1084 constexpr auto 1085 size() const requires sized_range<_Range> 1086 { return ranges::size(*_M_r); } 1087 1088 constexpr auto 1089 data() const requires contiguous_range<_Range> 1090 { return ranges::data(*_M_r); } 1091 }; 1092 1093 template
1094 ref_view(_Range&) -> ref_view<_Range>; 1095 1096 template
1097 inline constexpr bool enable_borrowed_range
> = true; 1098 1099 namespace views 1100 { 1101 namespace __detail 1102 { 1103 template
1104 concept __can_ref_view = requires { ref_view{std::declval<_Range>()}; }; 1105 1106 template
1107 concept __can_subrange = requires { subrange{std::declval<_Range>()}; }; 1108 } // namespace __detail 1109 1110 struct _All : __adaptor::_RangeAdaptorClosure 1111 { 1112 template
1113 requires view
> 1114 || __detail::__can_ref_view<_Range> 1115 || __detail::__can_subrange<_Range> 1116 constexpr auto 1117 operator()(_Range&& __r) const 1118 { 1119 if constexpr (view
>) 1120 return std::forward<_Range>(__r); 1121 else if constexpr (__detail::__can_ref_view<_Range>) 1122 return ref_view{std::forward<_Range>(__r)}; 1123 else 1124 return subrange{std::forward<_Range>(__r)}; 1125 } 1126 1127 static constexpr bool _S_has_simple_call_op = true; 1128 }; 1129 1130 inline constexpr _All all; 1131 1132 template
1133 using all_t = decltype(all(std::declval<_Range>())); 1134 } // namespace views 1135 1136 namespace __detail 1137 { 1138 template
1139 struct __non_propagating_cache 1140 { 1141 // When _Tp is not an object type (e.g. is a reference type), we make 1142 // __non_propagating_cache<_Tp> empty rather than ill-formed so that 1143 // users can easily conditionally declare data members with this type 1144 // (such as join_view::_M_inner). 1145 }; 1146 1147 template
1148 requires is_object_v<_Tp> 1149 struct __non_propagating_cache<_Tp> : protected _Optional_base<_Tp> 1150 { 1151 __non_propagating_cache() = default; 1152 1153 constexpr 1154 __non_propagating_cache(const __non_propagating_cache&) noexcept 1155 { } 1156 1157 constexpr 1158 __non_propagating_cache(__non_propagating_cache&& __other) noexcept 1159 { __other._M_reset(); } 1160 1161 constexpr __non_propagating_cache& 1162 operator=(const __non_propagating_cache& __other) noexcept 1163 { 1164 if (std::__addressof(__other) != this) 1165 this->_M_reset(); 1166 return *this; 1167 } 1168 1169 constexpr __non_propagating_cache& 1170 operator=(__non_propagating_cache&& __other) noexcept 1171 { 1172 this->_M_reset(); 1173 __other._M_reset(); 1174 return *this; 1175 } 1176 1177 constexpr _Tp& 1178 operator*() noexcept 1179 { return this->_M_get(); } 1180 1181 constexpr const _Tp& 1182 operator*() const noexcept 1183 { return this->_M_get(); } 1184 1185 template
1186 _Tp& 1187 _M_emplace_deref(const _Iter& __i) 1188 { 1189 this->_M_reset(); 1190 // Using _Optional_base::_M_construct to initialize from '*__i' 1191 // would incur an extra move due to the indirection, so we instead 1192 // use placement new directly. 1193 ::new ((void *) std::__addressof(this->_M_payload._M_payload)) _Tp(*__i); 1194 this->_M_payload._M_engaged = true; 1195 return this->_M_get(); 1196 } 1197 }; 1198 1199 template
1200 struct _CachedPosition 1201 { 1202 constexpr bool 1203 _M_has_value() const 1204 { return false; } 1205 1206 constexpr iterator_t<_Range> 1207 _M_get(const _Range&) const 1208 { 1209 __glibcxx_assert(false); 1210 __builtin_unreachable(); 1211 } 1212 1213 constexpr void 1214 _M_set(const _Range&, const iterator_t<_Range>&) const 1215 { } 1216 }; 1217 1218 template
1219 struct _CachedPosition<_Range> 1220 : protected __non_propagating_cache
> 1221 { 1222 constexpr bool 1223 _M_has_value() const 1224 { return this->_M_is_engaged(); } 1225 1226 constexpr iterator_t<_Range> 1227 _M_get(const _Range&) const 1228 { 1229 __glibcxx_assert(_M_has_value()); 1230 return **this; 1231 } 1232 1233 constexpr void 1234 _M_set(const _Range&, const iterator_t<_Range>& __it) 1235 { 1236 __glibcxx_assert(!_M_has_value()); 1237 std::construct_at(std::__addressof(this->_M_payload._M_payload), 1238 in_place, __it); 1239 this->_M_payload._M_engaged = true; 1240 } 1241 }; 1242 1243 template
1244 requires (sizeof(range_difference_t<_Range>) 1245 <= sizeof(iterator_t<_Range>)) 1246 struct _CachedPosition<_Range> 1247 { 1248 private: 1249 range_difference_t<_Range> _M_offset = -1; 1250 1251 public: 1252 _CachedPosition() = default; 1253 1254 constexpr 1255 _CachedPosition(const _CachedPosition&) = default; 1256 1257 constexpr 1258 _CachedPosition(_CachedPosition&& __other) noexcept 1259 { *this = std::move(__other); } 1260 1261 constexpr _CachedPosition& 1262 operator=(const _CachedPosition&) = default; 1263 1264 constexpr _CachedPosition& 1265 operator=(_CachedPosition&& __other) noexcept 1266 { 1267 // Propagate the cached offset, but invalidate the source. 1268 _M_offset = __other._M_offset; 1269 __other._M_offset = -1; 1270 return *this; 1271 } 1272 1273 constexpr bool 1274 _M_has_value() const 1275 { return _M_offset >= 0; } 1276 1277 constexpr iterator_t<_Range> 1278 _M_get(_Range& __r) const 1279 { 1280 __glibcxx_assert(_M_has_value()); 1281 return ranges::begin(__r) + _M_offset; 1282 } 1283 1284 constexpr void 1285 _M_set(_Range& __r, const iterator_t<_Range>& __it) 1286 { 1287 __glibcxx_assert(!_M_has_value()); 1288 _M_offset = __it - ranges::begin(__r); 1289 } 1290 }; 1291 } // namespace __detail 1292 1293 namespace __detail 1294 { 1295 template
1296 struct __filter_view_iter_cat 1297 { }; 1298 1299 template
1300 struct __filter_view_iter_cat<_Base> 1301 { 1302 private: 1303 static auto 1304 _S_iter_cat() 1305 { 1306 using _Cat = typename iterator_traits
>::iterator_category; 1307 if constexpr (derived_from<_Cat, bidirectional_iterator_tag>) 1308 return bidirectional_iterator_tag{}; 1309 else if constexpr (derived_from<_Cat, forward_iterator_tag>) 1310 return forward_iterator_tag{}; 1311 else 1312 return _Cat{}; 1313 } 1314 public: 1315 using iterator_category = decltype(_S_iter_cat()); 1316 }; 1317 } // namespace __detail 1318 1319 template
> _Pred> 1321 requires view<_Vp> && is_object_v<_Pred> 1322 class filter_view : public view_interface
> 1323 { 1324 private: 1325 struct _Sentinel; 1326 1327 struct _Iterator : __detail::__filter_view_iter_cat<_Vp> 1328 { 1329 private: 1330 static constexpr auto 1331 _S_iter_concept() 1332 { 1333 if constexpr (bidirectional_range<_Vp>) 1334 return bidirectional_iterator_tag{}; 1335 else if constexpr (forward_range<_Vp>) 1336 return forward_iterator_tag{}; 1337 else 1338 return input_iterator_tag{}; 1339 } 1340 1341 friend filter_view; 1342 1343 using _Vp_iter = iterator_t<_Vp>; 1344 1345 _Vp_iter _M_current = _Vp_iter(); 1346 filter_view* _M_parent = nullptr; 1347 1348 public: 1349 using iterator_concept = decltype(_S_iter_concept()); 1350 // iterator_category defined in __filter_view_iter_cat 1351 using value_type = range_value_t<_Vp>; 1352 using difference_type = range_difference_t<_Vp>; 1353 1354 _Iterator() requires default_initializable<_Vp_iter> = default; 1355 1356 constexpr 1357 _Iterator(filter_view* __parent, _Vp_iter __current) 1358 : _M_current(std::move(__current)), 1359 _M_parent(__parent) 1360 { } 1361 1362 constexpr const _Vp_iter& 1363 base() const & noexcept 1364 { return _M_current; } 1365 1366 constexpr _Vp_iter 1367 base() && 1368 { return std::move(_M_current); } 1369 1370 constexpr range_reference_t<_Vp> 1371 operator*() const 1372 { return *_M_current; } 1373 1374 constexpr _Vp_iter 1375 operator->() const 1376 requires __detail::__has_arrow<_Vp_iter> 1377 && copyable<_Vp_iter> 1378 { return _M_current; } 1379 1380 constexpr _Iterator& 1381 operator++() 1382 { 1383 _M_current = ranges::find_if(std::move(++_M_current), 1384 ranges::end(_M_parent->_M_base), 1385 std::ref(*_M_parent->_M_pred)); 1386 return *this; 1387 } 1388 1389 constexpr void 1390 operator++(int) 1391 { ++*this; } 1392 1393 constexpr _Iterator 1394 operator++(int) requires forward_range<_Vp> 1395 { 1396 auto __tmp = *this; 1397 ++*this; 1398 return __tmp; 1399 } 1400 1401 constexpr _Iterator& 1402 operator--() requires bidirectional_range<_Vp> 1403 { 1404 do 1405 --_M_current; 1406 while (!std::__invoke(*_M_parent->_M_pred, *_M_current)); 1407 return *this; 1408 } 1409 1410 constexpr _Iterator 1411 operator--(int) requires bidirectional_range<_Vp> 1412 { 1413 auto __tmp = *this; 1414 --*this; 1415 return __tmp; 1416 } 1417 1418 friend constexpr bool 1419 operator==(const _Iterator& __x, const _Iterator& __y) 1420 requires equality_comparable<_Vp_iter> 1421 { return __x._M_current == __y._M_current; } 1422 1423 friend constexpr range_rvalue_reference_t<_Vp> 1424 iter_move(const _Iterator& __i) 1425 noexcept(noexcept(ranges::iter_move(__i._M_current))) 1426 { return ranges::iter_move(__i._M_current); } 1427 1428 friend constexpr void 1429 iter_swap(const _Iterator& __x, const _Iterator& __y) 1430 noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current))) 1431 requires indirectly_swappable<_Vp_iter> 1432 { ranges::iter_swap(__x._M_current, __y._M_current); } 1433 }; 1434 1435 struct _Sentinel 1436 { 1437 private: 1438 sentinel_t<_Vp> _M_end = sentinel_t<_Vp>(); 1439 1440 constexpr bool 1441 __equal(const _Iterator& __i) const 1442 { return __i._M_current == _M_end; } 1443 1444 public: 1445 _Sentinel() = default; 1446 1447 constexpr explicit 1448 _Sentinel(filter_view* __parent) 1449 : _M_end(ranges::end(__parent->_M_base)) 1450 { } 1451 1452 constexpr sentinel_t<_Vp> 1453 base() const 1454 { return _M_end; } 1455 1456 friend constexpr bool 1457 operator==(const _Iterator& __x, const _Sentinel& __y) 1458 { return __y.__equal(__x); } 1459 }; 1460 1461 [[no_unique_address]] __detail::__box<_Pred> _M_pred; 1462 [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin; 1463 _Vp _M_base = _Vp(); 1464 1465 public: 1466 filter_view() requires default_initializable<_Vp> = default; 1467 1468 constexpr 1469 filter_view(_Vp __base, _Pred __pred) 1470 : _M_pred(std::move(__pred)), _M_base(std::move(__base)) 1471 { } 1472 1473 constexpr _Vp 1474 base() const& requires copy_constructible<_Vp> 1475 { return _M_base; } 1476 1477 constexpr _Vp 1478 base() && 1479 { return std::move(_M_base); } 1480 1481 constexpr const _Pred& 1482 pred() const 1483 { return *_M_pred; } 1484 1485 constexpr _Iterator 1486 begin() 1487 { 1488 if (_M_cached_begin._M_has_value()) 1489 return {this, _M_cached_begin._M_get(_M_base)}; 1490 1491 __glibcxx_assert(_M_pred.has_value()); 1492 auto __it = ranges::find_if(ranges::begin(_M_base), 1493 ranges::end(_M_base), 1494 std::ref(*_M_pred)); 1495 _M_cached_begin._M_set(_M_base, __it); 1496 return {this, std::move(__it)}; 1497 } 1498 1499 constexpr auto 1500 end() 1501 { 1502 if constexpr (common_range<_Vp>) 1503 return _Iterator{this, ranges::end(_M_base)}; 1504 else 1505 return _Sentinel{this}; 1506 } 1507 }; 1508 1509 template
1510 filter_view(_Range&&, _Pred) -> filter_view
, _Pred>; 1511 1512 namespace views 1513 { 1514 namespace __detail 1515 { 1516 template
1517 concept __can_filter_view 1518 = requires { filter_view(std::declval<_Range>(), std::declval<_Pred>()); }; 1519 } // namespace __detail 1520 1521 struct _Filter : __adaptor::_RangeAdaptor<_Filter> 1522 { 1523 template
1524 requires __detail::__can_filter_view<_Range, _Pred> 1525 constexpr auto 1526 operator()(_Range&& __r, _Pred&& __p) const 1527 { 1528 return filter_view(std::forward<_Range>(__r), std::forward<_Pred>(__p)); 1529 } 1530 1531 using _RangeAdaptor<_Filter>::operator(); 1532 static constexpr int _S_arity = 2; 1533 static constexpr bool _S_has_simple_extra_args = true; 1534 }; 1535 1536 inline constexpr _Filter filter; 1537 } // namespace views 1538 1539 template
1540 requires view<_Vp> && is_object_v<_Fp> 1541 && regular_invocable<_Fp&, range_reference_t<_Vp>> 1542 && std::__detail::__can_reference
>> 1544 class transform_view : public view_interface
> 1545 { 1546 private: 1547 template
1548 using _Base = __detail::__maybe_const_t<_Const, _Vp>; 1549 1550 template
1551 struct __iter_cat 1552 { }; 1553 1554 template
1555 requires forward_range<_Base<_Const>> 1556 struct __iter_cat<_Const> 1557 { 1558 private: 1559 static auto 1560 _S_iter_cat() 1561 { 1562 using _Base = transform_view::_Base<_Const>; 1563 using _Res = invoke_result_t<_Fp&, range_reference_t<_Base>>; 1564 if constexpr (is_lvalue_reference_v<_Res>) 1565 { 1566 using _Cat 1567 = typename iterator_traits
>::iterator_category; 1568 if constexpr (derived_from<_Cat, contiguous_iterator_tag>) 1569 return random_access_iterator_tag{}; 1570 else 1571 return _Cat{}; 1572 } 1573 else 1574 return input_iterator_tag{}; 1575 } 1576 public: 1577 using iterator_category = decltype(_S_iter_cat()); 1578 }; 1579 1580 template
1581 struct _Sentinel; 1582 1583 template
1584 struct _Iterator : __iter_cat<_Const> 1585 { 1586 private: 1587 using _Parent = __detail::__maybe_const_t<_Const, transform_view>; 1588 using _Base = transform_view::_Base<_Const>; 1589 1590 static auto 1591 _S_iter_concept() 1592 { 1593 if constexpr (random_access_range<_Base>) 1594 return random_access_iterator_tag{}; 1595 else if constexpr (bidirectional_range<_Base>) 1596 return bidirectional_iterator_tag{}; 1597 else if constexpr (forward_range<_Base>) 1598 return forward_iterator_tag{}; 1599 else 1600 return input_iterator_tag{}; 1601 } 1602 1603 using _Base_iter = iterator_t<_Base>; 1604 1605 _Base_iter _M_current = _Base_iter(); 1606 _Parent* _M_parent = nullptr; 1607 1608 public: 1609 using iterator_concept = decltype(_S_iter_concept()); 1610 // iterator_category defined in __transform_view_iter_cat 1611 using value_type 1612 = remove_cvref_t
>>; 1613 using difference_type = range_difference_t<_Base>; 1614 1615 _Iterator() requires default_initializable<_Base_iter> = default; 1616 1617 constexpr 1618 _Iterator(_Parent* __parent, _Base_iter __current) 1619 : _M_current(std::move(__current)), 1620 _M_parent(__parent) 1621 { } 1622 1623 constexpr 1624 _Iterator(_Iterator __i) 1625 requires _Const 1626 && convertible_to
, _Base_iter> 1627 : _M_current(std::move(__i._M_current)), _M_parent(__i._M_parent) 1628 { } 1629 1630 constexpr const _Base_iter& 1631 base() const & noexcept 1632 { return _M_current; } 1633 1634 constexpr _Base_iter 1635 base() && 1636 { return std::move(_M_current); } 1637 1638 constexpr decltype(auto) 1639 operator*() const 1640 noexcept(noexcept(std::__invoke(*_M_parent->_M_fun, *_M_current))) 1641 { return std::__invoke(*_M_parent->_M_fun, *_M_current); } 1642 1643 constexpr _Iterator& 1644 operator++() 1645 { 1646 ++_M_current; 1647 return *this; 1648 } 1649 1650 constexpr void 1651 operator++(int) 1652 { ++_M_current; } 1653 1654 constexpr _Iterator 1655 operator++(int) requires forward_range<_Base> 1656 { 1657 auto __tmp = *this; 1658 ++*this; 1659 return __tmp; 1660 } 1661 1662 constexpr _Iterator& 1663 operator--() requires bidirectional_range<_Base> 1664 { 1665 --_M_current; 1666 return *this; 1667 } 1668 1669 constexpr _Iterator 1670 operator--(int) requires bidirectional_range<_Base> 1671 { 1672 auto __tmp = *this; 1673 --*this; 1674 return __tmp; 1675 } 1676 1677 constexpr _Iterator& 1678 operator+=(difference_type __n) requires random_access_range<_Base> 1679 { 1680 _M_current += __n; 1681 return *this; 1682 } 1683 1684 constexpr _Iterator& 1685 operator-=(difference_type __n) requires random_access_range<_Base> 1686 { 1687 _M_current -= __n; 1688 return *this; 1689 } 1690 1691 constexpr decltype(auto) 1692 operator[](difference_type __n) const 1693 requires random_access_range<_Base> 1694 { return std::__invoke(*_M_parent->_M_fun, _M_current[__n]); } 1695 1696 friend constexpr bool 1697 operator==(const _Iterator& __x, const _Iterator& __y) 1698 requires equality_comparable<_Base_iter> 1699 { return __x._M_current == __y._M_current; } 1700 1701 friend constexpr bool 1702 operator<(const _Iterator& __x, const _Iterator& __y) 1703 requires random_access_range<_Base> 1704 { return __x._M_current < __y._M_current; } 1705 1706 friend constexpr bool 1707 operator>(const _Iterator& __x, const _Iterator& __y) 1708 requires random_access_range<_Base> 1709 { return __y < __x; } 1710 1711 friend constexpr bool 1712 operator<=(const _Iterator& __x, const _Iterator& __y) 1713 requires random_access_range<_Base> 1714 { return !(__y < __x); } 1715 1716 friend constexpr bool 1717 operator>=(const _Iterator& __x, const _Iterator& __y) 1718 requires random_access_range<_Base> 1719 { return !(__x < __y); } 1720 1721 #ifdef __cpp_lib_three_way_comparison 1722 friend constexpr auto 1723 operator<=>(const _Iterator& __x, const _Iterator& __y) 1724 requires random_access_range<_Base> 1725 && three_way_comparable<_Base_iter> 1726 { return __x._M_current <=> __y._M_current; } 1727 #endif 1728 1729 friend constexpr _Iterator 1730 operator+(_Iterator __i, difference_type __n) 1731 requires random_access_range<_Base> 1732 { return {__i._M_parent, __i._M_current + __n}; } 1733 1734 friend constexpr _Iterator 1735 operator+(difference_type __n, _Iterator __i) 1736 requires random_access_range<_Base> 1737 { return {__i._M_parent, __i._M_current + __n}; } 1738 1739 friend constexpr _Iterator 1740 operator-(_Iterator __i, difference_type __n) 1741 requires random_access_range<_Base> 1742 { return {__i._M_parent, __i._M_current - __n}; } 1743 1744 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1745 // 3483. transform_view::iterator's difference is overconstrained 1746 friend constexpr difference_type 1747 operator-(const _Iterator& __x, const _Iterator& __y) 1748 requires sized_sentinel_for
, iterator_t<_Base>> 1749 { return __x._M_current - __y._M_current; } 1750 1751 friend constexpr decltype(auto) 1752 iter_move(const _Iterator& __i) noexcept(noexcept(*__i)) 1753 { 1754 if constexpr (is_lvalue_reference_v
) 1755 return std::move(*__i); 1756 else 1757 return *__i; 1758 } 1759 1760 friend _Iterator; 1761 template
friend struct _Sentinel; 1762 }; 1763 1764 template
1765 struct _Sentinel 1766 { 1767 private: 1768 using _Parent = __detail::__maybe_const_t<_Const, transform_view>; 1769 using _Base = transform_view::_Base<_Const>; 1770 1771 template
1772 constexpr auto 1773 __distance_from(const _Iterator<_Const2>& __i) const 1774 { return _M_end - __i._M_current; } 1775 1776 template
1777 constexpr bool 1778 __equal(const _Iterator<_Const2>& __i) const 1779 { return __i._M_current == _M_end; } 1780 1781 sentinel_t<_Base> _M_end = sentinel_t<_Base>(); 1782 1783 public: 1784 _Sentinel() = default; 1785 1786 constexpr explicit 1787 _Sentinel(sentinel_t<_Base> __end) 1788 : _M_end(__end) 1789 { } 1790 1791 constexpr 1792 _Sentinel(_Sentinel __i) 1793 requires _Const 1794 && convertible_to
, sentinel_t<_Base>> 1795 : _M_end(std::move(__i._M_end)) 1796 { } 1797 1798 constexpr sentinel_t<_Base> 1799 base() const 1800 { return _M_end; } 1801 1802 template
1803 requires sentinel_for
, 1804 iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>> 1805 friend constexpr bool 1806 operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y) 1807 { return __y.__equal(__x); } 1808 1809 template
> 1811 requires sized_sentinel_for
, iterator_t<_Base2>> 1812 friend constexpr range_difference_t<_Base2> 1813 operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y) 1814 { return -__y.__distance_from(__x); } 1815 1816 template
> 1818 requires sized_sentinel_for
, iterator_t<_Base2>> 1819 friend constexpr range_difference_t<_Base2> 1820 operator-(const _Sentinel& __y, const _Iterator<_Const2>& __x) 1821 { return __y.__distance_from(__x); } 1822 1823 friend _Sentinel; 1824 }; 1825 1826 [[no_unique_address]] __detail::__box<_Fp> _M_fun; 1827 _Vp _M_base = _Vp(); 1828 1829 public: 1830 transform_view() requires default_initializable<_Vp> = default; 1831 1832 constexpr 1833 transform_view(_Vp __base, _Fp __fun) 1834 : _M_fun(std::move(__fun)), _M_base(std::move(__base)) 1835 { } 1836 1837 constexpr _Vp 1838 base() const& requires copy_constructible<_Vp> 1839 { return _M_base ; } 1840 1841 constexpr _Vp 1842 base() && 1843 { return std::move(_M_base); } 1844 1845 constexpr _Iterator
1846 begin() 1847 { return _Iterator
{this, ranges::begin(_M_base)}; } 1848 1849 constexpr _Iterator
1850 begin() const 1851 requires range
1852 && regular_invocable
> 1853 { return _Iterator
{this, ranges::begin(_M_base)}; } 1854 1855 constexpr _Sentinel
1856 end() 1857 { return _Sentinel
{ranges::end(_M_base)}; } 1858 1859 constexpr _Iterator
1860 end() requires common_range<_Vp> 1861 { return _Iterator
{this, ranges::end(_M_base)}; } 1862 1863 constexpr _Sentinel
1864 end() const 1865 requires range
1866 && regular_invocable
> 1867 { return _Sentinel
{ranges::end(_M_base)}; } 1868 1869 constexpr _Iterator
1870 end() const 1871 requires common_range
1872 && regular_invocable
> 1873 { return _Iterator
{this, ranges::end(_M_base)}; } 1874 1875 constexpr auto 1876 size() requires sized_range<_Vp> 1877 { return ranges::size(_M_base); } 1878 1879 constexpr auto 1880 size() const requires sized_range
1881 { return ranges::size(_M_base); } 1882 }; 1883 1884 template
1885 transform_view(_Range&&, _Fp) -> transform_view
, _Fp>; 1886 1887 namespace views 1888 { 1889 namespace __detail 1890 { 1891 template
1892 concept __can_transform_view 1893 = requires { transform_view(std::declval<_Range>(), std::declval<_Fp>()); }; 1894 } // namespace __detail 1895 1896 struct _Transform : __adaptor::_RangeAdaptor<_Transform> 1897 { 1898 template
1899 requires __detail::__can_transform_view<_Range, _Fp> 1900 constexpr auto 1901 operator()(_Range&& __r, _Fp&& __f) const 1902 { 1903 return transform_view(std::forward<_Range>(__r), std::forward<_Fp>(__f)); 1904 } 1905 1906 using _RangeAdaptor<_Transform>::operator(); 1907 static constexpr int _S_arity = 2; 1908 static constexpr bool _S_has_simple_extra_args = true; 1909 }; 1910 1911 inline constexpr _Transform transform; 1912 } // namespace views 1913 1914 template
1915 class take_view : public view_interface
> 1916 { 1917 private: 1918 template
1919 using _CI = counted_iterator< 1920 iterator_t<__detail::__maybe_const_t<_Const, _Vp>>>; 1921 1922 template
1923 struct _Sentinel 1924 { 1925 private: 1926 using _Base = __detail::__maybe_const_t<_Const, _Vp>; 1927 sentinel_t<_Base> _M_end = sentinel_t<_Base>(); 1928 1929 public: 1930 _Sentinel() = default; 1931 1932 constexpr explicit 1933 _Sentinel(sentinel_t<_Base> __end) 1934 : _M_end(__end) 1935 { } 1936 1937 constexpr 1938 _Sentinel(_Sentinel __s) 1939 requires _Const && convertible_to
, sentinel_t<_Base>> 1940 : _M_end(std::move(__s._M_end)) 1941 { } 1942 1943 constexpr sentinel_t<_Base> 1944 base() const 1945 { return _M_end; } 1946 1947 friend constexpr bool 1948 operator==(const _CI<_Const>& __y, const _Sentinel& __x) 1949 { return __y.count() == 0 || __y.base() == __x._M_end; } 1950 1951 template
> 1953 requires sentinel_for
, iterator_t<_Base2>> 1954 friend constexpr bool 1955 operator==(const _CI<_OtherConst>& __y, const _Sentinel& __x) 1956 { return __y.count() == 0 || __y.base() == __x._M_end; } 1957 1958 friend _Sentinel; 1959 }; 1960 1961 range_difference_t<_Vp> _M_count = 0; 1962 _Vp _M_base = _Vp(); 1963 1964 public: 1965 take_view() requires default_initializable<_Vp> = default; 1966 1967 constexpr 1968 take_view(_Vp base, range_difference_t<_Vp> __count) 1969 : _M_count(std::move(__count)), _M_base(std::move(base)) 1970 { } 1971 1972 constexpr _Vp 1973 base() const& requires copy_constructible<_Vp> 1974 { return _M_base; } 1975 1976 constexpr _Vp 1977 base() && 1978 { return std::move(_M_base); } 1979 1980 constexpr auto 1981 begin() requires (!__detail::__simple_view<_Vp>) 1982 { 1983 if constexpr (sized_range<_Vp>) 1984 { 1985 if constexpr (random_access_range<_Vp>) 1986 return ranges::begin(_M_base); 1987 else 1988 { 1989 auto __sz = size(); 1990 return counted_iterator(ranges::begin(_M_base), __sz); 1991 } 1992 } 1993 else 1994 return counted_iterator(ranges::begin(_M_base), _M_count); 1995 } 1996 1997 constexpr auto 1998 begin() const requires range
1999 { 2000 if constexpr (sized_range
) 2001 { 2002 if constexpr (random_access_range
) 2003 return ranges::begin(_M_base); 2004 else 2005 { 2006 auto __sz = size(); 2007 return counted_iterator(ranges::begin(_M_base), __sz); 2008 } 2009 } 2010 else 2011 return counted_iterator(ranges::begin(_M_base), _M_count); 2012 } 2013 2014 constexpr auto 2015 end() requires (!__detail::__simple_view<_Vp>) 2016 { 2017 if constexpr (sized_range<_Vp>) 2018 { 2019 if constexpr (random_access_range<_Vp>) 2020 return ranges::begin(_M_base) + size(); 2021 else 2022 return default_sentinel; 2023 } 2024 else 2025 return _Sentinel
{ranges::end(_M_base)}; 2026 } 2027 2028 constexpr auto 2029 end() const requires range
2030 { 2031 if constexpr (sized_range
) 2032 { 2033 if constexpr (random_access_range
) 2034 return ranges::begin(_M_base) + size(); 2035 else 2036 return default_sentinel; 2037 } 2038 else 2039 return _Sentinel
{ranges::end(_M_base)}; 2040 } 2041 2042 constexpr auto 2043 size() requires sized_range<_Vp> 2044 { 2045 auto __n = ranges::size(_M_base); 2046 return std::min(__n, static_cast
(_M_count)); 2047 } 2048 2049 constexpr auto 2050 size() const requires sized_range
2051 { 2052 auto __n = ranges::size(_M_base); 2053 return std::min(__n, static_cast
(_M_count)); 2054 } 2055 }; 2056 2057 // _GLIBCXX_RESOLVE_LIB_DEFECTS 2058 // 3447. Deduction guides for take_view and drop_view have different 2059 // constraints 2060 template
2061 take_view(_Range&&, range_difference_t<_Range>) 2062 -> take_view
>; 2063 2064 template
2065 inline constexpr bool enable_borrowed_range
> 2066 = enable_borrowed_range<_Tp>; 2067 2068 namespace views 2069 { 2070 namespace __detail 2071 { 2072 template
2073 concept __can_take_view 2074 = requires { take_view(std::declval<_Range>(), std::declval<_Tp>()); }; 2075 } // namespace __detail 2076 2077 struct _Take : __adaptor::_RangeAdaptor<_Take> 2078 { 2079 template
2080 requires __detail::__can_take_view<_Range, _Tp> 2081 constexpr auto 2082 operator()(_Range&& __r, _Tp&& __n) const 2083 { 2084 return take_view(std::forward<_Range>(__r), std::forward<_Tp>(__n)); 2085 } 2086 2087 using _RangeAdaptor<_Take>::operator(); 2088 static constexpr int _S_arity = 2; 2089 // The count argument of views::take is not always simple -- it can be 2090 // e.g. a move-only class that's implicitly convertible to the difference 2091 // type. But an integer-like count argument is surely simple. 2092 template
2093 static constexpr bool _S_has_simple_extra_args 2094 = ranges::__detail::__is_integer_like<_Tp>; 2095 }; 2096 2097 inline constexpr _Take take; 2098 } // namespace views 2099 2100 template
2101 requires input_range<_Vp> && is_object_v<_Pred> 2102 && indirect_unary_predicate
> 2103 class take_while_view : public view_interface
> 2104 { 2105 template
2106 struct _Sentinel 2107 { 2108 private: 2109 using _Base = __detail::__maybe_const_t<_Const, _Vp>; 2110 2111 sentinel_t<_Base> _M_end = sentinel_t<_Base>(); 2112 const _Pred* _M_pred = nullptr; 2113 2114 public: 2115 _Sentinel() = default; 2116 2117 constexpr explicit 2118 _Sentinel(sentinel_t<_Base> __end, const _Pred* __pred) 2119 : _M_end(__end), _M_pred(__pred) 2120 { } 2121 2122 constexpr 2123 _Sentinel(_Sentinel __s) 2124 requires _Const && convertible_to
, sentinel_t<_Base>> 2125 : _M_end(__s._M_end), _M_pred(__s._M_pred) 2126 { } 2127 2128 constexpr sentinel_t<_Base> 2129 base() const { return _M_end; } 2130 2131 friend constexpr bool 2132 operator==(const iterator_t<_Base>& __x, const _Sentinel& __y) 2133 { return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); } 2134 2135 template
> 2137 requires sentinel_for
, iterator_t<_Base2>> 2138 friend constexpr bool 2139 operator==(const iterator_t<_Base2>& __x, const _Sentinel& __y) 2140 { return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); } 2141 2142 friend _Sentinel; 2143 }; 2144 2145 [[no_unique_address]] __detail::__box<_Pred> _M_pred; 2146 _Vp _M_base = _Vp(); 2147 2148 public: 2149 take_while_view() requires default_initializable<_Vp> = default; 2150 2151 constexpr 2152 take_while_view(_Vp base, _Pred __pred) 2153 : _M_pred(std::move(__pred)), _M_base(std::move(base)) 2154 { } 2155 2156 constexpr _Vp 2157 base() const& requires copy_constructible<_Vp> 2158 { return _M_base; } 2159 2160 constexpr _Vp 2161 base() && 2162 { return std::move(_M_base); } 2163 2164 constexpr const _Pred& 2165 pred() const 2166 { return *_M_pred; } 2167 2168 constexpr auto 2169 begin() requires (!__detail::__simple_view<_Vp>) 2170 { return ranges::begin(_M_base); } 2171 2172 constexpr auto 2173 begin() const requires range
2174 && indirect_unary_predicate
> 2175 { return ranges::begin(_M_base); } 2176 2177 constexpr auto 2178 end() requires (!__detail::__simple_view<_Vp>) 2179 { return _Sentinel
(ranges::end(_M_base), 2180 std::__addressof(*_M_pred)); } 2181 2182 constexpr auto 2183 end() const requires range
2184 && indirect_unary_predicate
> 2185 { return _Sentinel
(ranges::end(_M_base), 2186 std::__addressof(*_M_pred)); } 2187 }; 2188 2189 template
2190 take_while_view(_Range&&, _Pred) 2191 -> take_while_view
, _Pred>; 2192 2193 namespace views 2194 { 2195 namespace __detail 2196 { 2197 template
2198 concept __can_take_while_view 2199 = requires { take_while_view(std::declval<_Range>(), std::declval<_Pred>()); }; 2200 } // namespace __detail 2201 2202 struct _TakeWhile : __adaptor::_RangeAdaptor<_TakeWhile> 2203 { 2204 template
2205 requires __detail::__can_take_while_view<_Range, _Pred> 2206 constexpr auto 2207 operator()(_Range&& __r, _Pred&& __p) const 2208 { 2209 return take_while_view(std::forward<_Range>(__r), std::forward<_Pred>(__p)); 2210 } 2211 2212 using _RangeAdaptor<_TakeWhile>::operator(); 2213 static constexpr int _S_arity = 2; 2214 static constexpr bool _S_has_simple_extra_args = true; 2215 }; 2216 2217 inline constexpr _TakeWhile take_while; 2218 } // namespace views 2219 2220 template
2221 class drop_view : public view_interface
> 2222 { 2223 private: 2224 range_difference_t<_Vp> _M_count = 0; 2225 _Vp _M_base = _Vp(); 2226 2227 // ranges::next(begin(base), count, end(base)) is O(1) if _Vp satisfies 2228 // both random_access_range and sized_range. Otherwise, cache its result. 2229 static constexpr bool _S_needs_cached_begin 2230 = !(random_access_range
&& sized_range
); 2231 [[no_unique_address]] 2232 __detail::__maybe_present_t<_S_needs_cached_begin, 2233 __detail::_CachedPosition<_Vp>> 2234 _M_cached_begin; 2235 2236 public: 2237 drop_view() requires default_initializable<_Vp> = default; 2238 2239 constexpr 2240 drop_view(_Vp __base, range_difference_t<_Vp> __count) 2241 : _M_count(__count), _M_base(std::move(__base)) 2242 { __glibcxx_assert(__count >= 0); } 2243 2244 constexpr _Vp 2245 base() const& requires copy_constructible<_Vp> 2246 { return _M_base; } 2247 2248 constexpr _Vp 2249 base() && 2250 { return std::move(_M_base); } 2251 2252 // This overload is disabled for simple views with constant-time begin(). 2253 constexpr auto 2254 begin() 2255 requires (!(__detail::__simple_view<_Vp> 2256 && random_access_range
2257 && sized_range
)) 2258 { 2259 if constexpr (_S_needs_cached_begin) 2260 if (_M_cached_begin._M_has_value()) 2261 return _M_cached_begin._M_get(_M_base); 2262 2263 auto __it = ranges::next(ranges::begin(_M_base), 2264 _M_count, ranges::end(_M_base)); 2265 if constexpr (_S_needs_cached_begin) 2266 _M_cached_begin._M_set(_M_base, __it); 2267 return __it; 2268 } 2269 2270 // _GLIBCXX_RESOLVE_LIB_DEFECTS 2271 // 3482. drop_view's const begin should additionally require sized_range 2272 constexpr auto 2273 begin() const 2274 requires random_access_range
&& sized_range
2275 { 2276 return ranges::next(ranges::begin(_M_base), _M_count, 2277 ranges::end(_M_base)); 2278 } 2279 2280 constexpr auto 2281 end() requires (!__detail::__simple_view<_Vp>) 2282 { return ranges::end(_M_base); } 2283 2284 constexpr auto 2285 end() const requires range
2286 { return ranges::end(_M_base); } 2287 2288 constexpr auto 2289 size() requires sized_range<_Vp> 2290 { 2291 const auto __s = ranges::size(_M_base); 2292 const auto __c = static_cast
(_M_count); 2293 return __s < __c ? 0 : __s - __c; 2294 } 2295 2296 constexpr auto 2297 size() const requires sized_range
2298 { 2299 const auto __s = ranges::size(_M_base); 2300 const auto __c = static_cast
(_M_count); 2301 return __s < __c ? 0 : __s - __c; 2302 } 2303 }; 2304 2305 template
2306 drop_view(_Range&&, range_difference_t<_Range>) 2307 -> drop_view
>; 2308 2309 template
2310 inline constexpr bool enable_borrowed_range
> 2311 = enable_borrowed_range<_Tp>; 2312 2313 namespace views 2314 { 2315 namespace __detail 2316 { 2317 template
2318 concept __can_drop_view 2319 = requires { drop_view(std::declval<_Range>(), std::declval<_Tp>()); }; 2320 } // namespace __detail 2321 2322 struct _Drop : __adaptor::_RangeAdaptor<_Drop> 2323 { 2324 template
2325 requires __detail::__can_drop_view<_Range, _Tp> 2326 constexpr auto 2327 operator()(_Range&& __r, _Tp&& __n) const 2328 { 2329 return drop_view(std::forward<_Range>(__r), std::forward<_Tp>(__n)); 2330 } 2331 2332 using _RangeAdaptor<_Drop>::operator(); 2333 static constexpr int _S_arity = 2; 2334 template
2335 static constexpr bool _S_has_simple_extra_args 2336 = _Take::_S_has_simple_extra_args<_Tp>; 2337 }; 2338 2339 inline constexpr _Drop drop; 2340 } // namespace views 2341 2342 template
2343 requires input_range<_Vp> && is_object_v<_Pred> 2344 && indirect_unary_predicate
> 2345 class drop_while_view : public view_interface
> 2346 { 2347 private: 2348 [[no_unique_address]] __detail::__box<_Pred> _M_pred; 2349 [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin; 2350 _Vp _M_base = _Vp(); 2351 2352 public: 2353 drop_while_view() requires default_initializable<_Vp> = default; 2354 2355 constexpr 2356 drop_while_view(_Vp __base, _Pred __pred) 2357 : _M_pred(std::move(__pred)), _M_base(std::move(__base)) 2358 { } 2359 2360 constexpr _Vp 2361 base() const& requires copy_constructible<_Vp> 2362 { return _M_base; } 2363 2364 constexpr _Vp 2365 base() && 2366 { return std::move(_M_base); } 2367 2368 constexpr const _Pred& 2369 pred() const 2370 { return *_M_pred; } 2371 2372 constexpr auto 2373 begin() 2374 { 2375 if (_M_cached_begin._M_has_value()) 2376 return _M_cached_begin._M_get(_M_base); 2377 2378 __glibcxx_assert(_M_pred.has_value()); 2379 auto __it = ranges::find_if_not(ranges::begin(_M_base), 2380 ranges::end(_M_base), 2381 std::cref(*_M_pred)); 2382 _M_cached_begin._M_set(_M_base, __it); 2383 return __it; 2384 } 2385 2386 constexpr auto 2387 end() 2388 { return ranges::end(_M_base); } 2389 }; 2390 2391 template
2392 drop_while_view(_Range&&, _Pred) 2393 -> drop_while_view
, _Pred>; 2394 2395 template
2396 inline constexpr bool enable_borrowed_range
> 2397 = enable_borrowed_range<_Tp>; 2398 2399 namespace views 2400 { 2401 namespace __detail 2402 { 2403 template
2404 concept __can_drop_while_view 2405 = requires { drop_while_view(std::declval<_Range>(), std::declval<_Pred>()); }; 2406 } // namespace __detail 2407 2408 struct _DropWhile : __adaptor::_RangeAdaptor<_DropWhile> 2409 { 2410 template
2411 requires __detail::__can_drop_while_view<_Range, _Pred> 2412 constexpr auto 2413 operator()(_Range&& __r, _Pred&& __p) const 2414 { 2415 return drop_while_view(std::forward<_Range>(__r), 2416 std::forward<_Pred>(__p)); 2417 } 2418 2419 using _RangeAdaptor<_DropWhile>::operator(); 2420 static constexpr int _S_arity = 2; 2421 static constexpr bool _S_has_simple_extra_args = true; 2422 }; 2423 2424 inline constexpr _DropWhile drop_while; 2425 } // namespace views 2426 2427 template
2428 requires view<_Vp> && input_range
> 2429 class join_view : public view_interface
> 2430 { 2431 private: 2432 using _InnerRange = range_reference_t<_Vp>; 2433 2434 template
2435 using _Base = __detail::__maybe_const_t<_Const, _Vp>; 2436 2437 template
2438 using _Outer_iter = iterator_t<_Base<_Const>>; 2439 2440 template
2441 using _Inner_iter = iterator_t
>>; 2442 2443 template
2444 static constexpr bool _S_ref_is_glvalue 2445 = is_reference_v
>>; 2446 2447 template
2448 struct __iter_cat 2449 { }; 2450 2451 template
2452 requires _S_ref_is_glvalue<_Const> 2453 && forward_range<_Base<_Const>> 2454 && forward_range
>> 2455 struct __iter_cat<_Const> 2456 { 2457 private: 2458 static constexpr auto 2459 _S_iter_cat() 2460 { 2461 using _Outer_iter = join_view::_Outer_iter<_Const>; 2462 using _Inner_iter = join_view::_Inner_iter<_Const>; 2463 using _OuterCat = typename iterator_traits<_Outer_iter>::iterator_category; 2464 using _InnerCat = typename iterator_traits<_Inner_iter>::iterator_category; 2465 if constexpr (derived_from<_OuterCat, bidirectional_iterator_tag> 2466 && derived_from<_InnerCat, bidirectional_iterator_tag> 2467 && common_range
>>) 2468 return bidirectional_iterator_tag{}; 2469 else if constexpr (derived_from<_OuterCat, forward_iterator_tag> 2470 && derived_from<_InnerCat, forward_iterator_tag>) 2471 return forward_iterator_tag{}; 2472 else 2473 return input_iterator_tag{}; 2474 } 2475 public: 2476 using iterator_category = decltype(_S_iter_cat()); 2477 }; 2478 2479 template
2480 struct _Sentinel; 2481 2482 template
2483 struct _Iterator : __iter_cat<_Const> 2484 { 2485 private: 2486 using _Parent = __detail::__maybe_const_t<_Const, join_view>; 2487 using _Base = join_view::_Base<_Const>; 2488 2489 static constexpr bool _S_ref_is_glvalue 2490 = join_view::_S_ref_is_glvalue<_Const>; 2491 2492 constexpr void 2493 _M_satisfy() 2494 { 2495 auto __update_inner = [this] (const iterator_t<_Base>& __x) -> auto&& { 2496 if constexpr (_S_ref_is_glvalue) 2497 return *__x; 2498 else 2499 return _M_parent->_M_inner._M_emplace_deref(__x); 2500 }; 2501 2502 for (; _M_outer != ranges::end(_M_parent->_M_base); ++_M_outer) 2503 { 2504 auto&& __inner = __update_inner(_M_outer); 2505 _M_inner = ranges::begin(__inner); 2506 if (_M_inner != ranges::end(__inner)) 2507 return; 2508 } 2509 2510 if constexpr (_S_ref_is_glvalue) 2511 _M_inner = _Inner_iter(); 2512 } 2513 2514 static constexpr auto 2515 _S_iter_concept() 2516 { 2517 if constexpr (_S_ref_is_glvalue 2518 && bidirectional_range<_Base> 2519 && bidirectional_range
> 2520 && common_range
>) 2521 return bidirectional_iterator_tag{}; 2522 else if constexpr (_S_ref_is_glvalue 2523 && forward_range<_Base> 2524 && forward_range
>) 2525 return forward_iterator_tag{}; 2526 else 2527 return input_iterator_tag{}; 2528 } 2529 2530 using _Outer_iter = join_view::_Outer_iter<_Const>; 2531 using _Inner_iter = join_view::_Inner_iter<_Const>; 2532 2533 _Outer_iter _M_outer = _Outer_iter(); 2534 _Inner_iter _M_inner = _Inner_iter(); 2535 _Parent* _M_parent = nullptr; 2536 2537 public: 2538 using iterator_concept = decltype(_S_iter_concept()); 2539 // iterator_category defined in __join_view_iter_cat 2540 using value_type = range_value_t
>; 2541 using difference_type 2542 = common_type_t
, 2543 range_difference_t
>>; 2544 2545 _Iterator() requires (default_initializable<_Outer_iter> 2546 && default_initializable<_Inner_iter>) 2547 = default; 2548 2549 constexpr 2550 _Iterator(_Parent* __parent, _Outer_iter __outer) 2551 : _M_outer(std::move(__outer)), 2552 _M_parent(__parent) 2553 { _M_satisfy(); } 2554 2555 constexpr 2556 _Iterator(_Iterator __i) 2557 requires _Const 2558 && convertible_to
, _Outer_iter> 2559 && convertible_to
, _Inner_iter> 2560 : _M_outer(std::move(__i._M_outer)), _M_inner(std::move(__i._M_inner)), 2561 _M_parent(__i._M_parent) 2562 { } 2563 2564 constexpr decltype(auto) 2565 operator*() const 2566 { return *_M_inner; } 2567 2568 // _GLIBCXX_RESOLVE_LIB_DEFECTS 2569 // 3500. join_view::iterator::operator->() is bogus 2570 constexpr _Inner_iter 2571 operator->() const 2572 requires __detail::__has_arrow<_Inner_iter> 2573 && copyable<_Inner_iter> 2574 { return _M_inner; } 2575 2576 constexpr _Iterator& 2577 operator++() 2578 { 2579 auto&& __inner_range = [this] () -> auto&& { 2580 if constexpr (_S_ref_is_glvalue) 2581 return *_M_outer; 2582 else 2583 return *_M_parent->_M_inner; 2584 }(); 2585 if (++_M_inner == ranges::end(__inner_range)) 2586 { 2587 ++_M_outer; 2588 _M_satisfy(); 2589 } 2590 return *this; 2591 } 2592 2593 constexpr void 2594 operator++(int) 2595 { ++*this; } 2596 2597 constexpr _Iterator 2598 operator++(int) 2599 requires _S_ref_is_glvalue && forward_range<_Base> 2600 && forward_range
> 2601 { 2602 auto __tmp = *this; 2603 ++*this; 2604 return __tmp; 2605 } 2606 2607 constexpr _Iterator& 2608 operator--() 2609 requires _S_ref_is_glvalue && bidirectional_range<_Base> 2610 && bidirectional_range
> 2611 && common_range
> 2612 { 2613 if (_M_outer == ranges::end(_M_parent->_M_base)) 2614 _M_inner = ranges::end(*--_M_outer); 2615 while (_M_inner == ranges::begin(*_M_outer)) 2616 _M_inner = ranges::end(*--_M_outer); 2617 --_M_inner; 2618 return *this; 2619 } 2620 2621 constexpr _Iterator 2622 operator--(int) 2623 requires _S_ref_is_glvalue && bidirectional_range<_Base> 2624 && bidirectional_range
> 2625 && common_range
> 2626 { 2627 auto __tmp = *this; 2628 --*this; 2629 return __tmp; 2630 } 2631 2632 friend constexpr bool 2633 operator==(const _Iterator& __x, const _Iterator& __y) 2634 requires _S_ref_is_glvalue 2635 && equality_comparable<_Outer_iter> 2636 && equality_comparable<_Inner_iter> 2637 { 2638 return (__x._M_outer == __y._M_outer 2639 && __x._M_inner == __y._M_inner); 2640 } 2641 2642 friend constexpr decltype(auto) 2643 iter_move(const _Iterator& __i) 2644 noexcept(noexcept(ranges::iter_move(__i._M_inner))) 2645 { return ranges::iter_move(__i._M_inner); } 2646 2647 friend constexpr void 2648 iter_swap(const _Iterator& __x, const _Iterator& __y) 2649 noexcept(noexcept(ranges::iter_swap(__x._M_inner, __y._M_inner))) 2650 requires indirectly_swappable<_Inner_iter> 2651 { return ranges::iter_swap(__x._M_inner, __y._M_inner); } 2652 2653 friend _Iterator; 2654 template
friend struct _Sentinel; 2655 }; 2656 2657 template
2658 struct _Sentinel 2659 { 2660 private: 2661 using _Parent = __detail::__maybe_const_t<_Const, join_view>; 2662 using _Base = join_view::_Base<_Const>; 2663 2664 template
2665 constexpr bool 2666 __equal(const _Iterator<_Const2>& __i) const 2667 { return __i._M_outer == _M_end; } 2668 2669 sentinel_t<_Base> _M_end = sentinel_t<_Base>(); 2670 2671 public: 2672 _Sentinel() = default; 2673 2674 constexpr explicit 2675 _Sentinel(_Parent* __parent) 2676 : _M_end(ranges::end(__parent->_M_base)) 2677 { } 2678 2679 constexpr 2680 _Sentinel(_Sentinel __s) 2681 requires _Const && convertible_to
, sentinel_t<_Base>> 2682 : _M_end(std::move(__s._M_end)) 2683 { } 2684 2685 template