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