mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
ranges
10101 lines (8613 loc) · 287 KB
/
ranges
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// <ranges> -*- C++ -*-
// Copyright (C) 2019-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/ranges
* This is a Standard C++ Library header.
* @ingroup concepts
*/
#ifndef _GLIBCXX_RANGES
#define _GLIBCXX_RANGES 1
#if __cplusplus > 201703L
#pragma GCC system_header
#include <concepts>
#if __cpp_lib_concepts
#include <compare>
#include <initializer_list>
#include <iterator>
#include <optional>
#include <span>
#include <string_view>
#include <tuple>
#if __cplusplus > 202002L
#include <variant>
#endif
#include <bits/ranges_util.h>
#include <bits/refwrap.h>
#define __glibcxx_want_ranges
#define __glibcxx_want_ranges_as_const
#define __glibcxx_want_ranges_as_rvalue
#define __glibcxx_want_ranges_cartesian_product
#define __glibcxx_want_ranges_concat
#define __glibcxx_want_ranges_chunk
#define __glibcxx_want_ranges_chunk_by
#define __glibcxx_want_ranges_enumerate
#define __glibcxx_want_ranges_iota
#define __glibcxx_want_ranges_join_with
#define __glibcxx_want_ranges_repeat
#define __glibcxx_want_ranges_slide
#define __glibcxx_want_ranges_stride
#define __glibcxx_want_ranges_to_container
#define __glibcxx_want_ranges_zip
#include <bits/version.h>
#ifdef __glibcxx_generator // C++ >= 23 && __glibcxx_coroutine
# include <bits/elements_of.h>
#endif
/**
* @defgroup ranges Ranges
*
* Components for dealing with ranges of elements.
*/
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
namespace ranges
{
// [range.access] customization point objects
// [range.req] range and view concepts
// [range.dangling] dangling iterator handling
// Defined in <bits/ranges_base.h>
// [view.interface] View interface
// [range.subrange] Sub-ranges
// Defined in <bits/ranges_util.h>
// C++20 24.6 [range.factories] Range factories
/// A view that contains no elements.
template<typename _Tp> requires is_object_v<_Tp>
class empty_view
: public view_interface<empty_view<_Tp>>
{
public:
static constexpr _Tp* begin() noexcept { return nullptr; }
static constexpr _Tp* end() noexcept { return nullptr; }
static constexpr _Tp* data() noexcept { return nullptr; }
static constexpr size_t size() noexcept { return 0; }
static constexpr bool empty() noexcept { return true; }
};
template<typename _Tp>
inline constexpr bool enable_borrowed_range<empty_view<_Tp>> = true;
namespace __detail
{
#if __cpp_lib_ranges >= 202207L // C++ >= 23
// P2494R2 Relaxing range adaptors to allow for move only types
template<typename _Tp>
concept __boxable = move_constructible<_Tp> && is_object_v<_Tp>;
#else
template<typename _Tp>
concept __boxable = copy_constructible<_Tp> && is_object_v<_Tp>;
#endif
template<__boxable _Tp>
struct __box : std::optional<_Tp>
{
using std::optional<_Tp>::optional;
constexpr
__box()
noexcept(is_nothrow_default_constructible_v<_Tp>)
requires default_initializable<_Tp>
: std::optional<_Tp>{std::in_place}
{ }
__box(const __box&) = default;
__box(__box&&) = default;
using std::optional<_Tp>::operator=;
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3477. Simplify constraints for semiregular-box
// 3572. copyable-box should be fully constexpr
constexpr __box&
operator=(const __box& __that)
noexcept(is_nothrow_copy_constructible_v<_Tp>)
requires (!copyable<_Tp>) && copy_constructible<_Tp>
{
if (this != std::__addressof(__that))
{
if ((bool)__that)
this->emplace(*__that);
else
this->reset();
}
return *this;
}
constexpr __box&
operator=(__box&& __that)
noexcept(is_nothrow_move_constructible_v<_Tp>)
requires (!movable<_Tp>)
{
if (this != std::__addressof(__that))
{
if ((bool)__that)
this->emplace(std::move(*__that));
else
this->reset();
}
return *this;
}
};
template<typename _Tp>
concept __boxable_copyable
= copy_constructible<_Tp>
&& (copyable<_Tp> || (is_nothrow_move_constructible_v<_Tp>
&& is_nothrow_copy_constructible_v<_Tp>));
template<typename _Tp>
concept __boxable_movable
= (!copy_constructible<_Tp>)
&& (movable<_Tp> || is_nothrow_move_constructible_v<_Tp>);
// For types which are already copyable (or since C++23, movable)
// this specialization of the box wrapper stores the object directly
// without going through std::optional. It provides just the subset of
// the primary template's API that we currently use.
template<__boxable _Tp>
requires __boxable_copyable<_Tp> || __boxable_movable<_Tp>
struct __box<_Tp>
{
private:
[[no_unique_address]] _Tp _M_value = _Tp();
public:
__box() requires default_initializable<_Tp> = default;
constexpr explicit
__box(const _Tp& __t)
noexcept(is_nothrow_copy_constructible_v<_Tp>)
requires copy_constructible<_Tp>
: _M_value(__t)
{ }
constexpr explicit
__box(_Tp&& __t)
noexcept(is_nothrow_move_constructible_v<_Tp>)
: _M_value(std::move(__t))
{ }
template<typename... _Args>
requires constructible_from<_Tp, _Args...>
constexpr explicit
__box(in_place_t, _Args&&... __args)
noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
: _M_value(std::forward<_Args>(__args)...)
{ }
__box(const __box&) = default;
__box(__box&&) = default;
__box& operator=(const __box&) requires copyable<_Tp> = default;
__box& operator=(__box&&) requires movable<_Tp> = default;
// When _Tp is nothrow_copy_constructible but not copy_assignable,
// copy assignment is implemented via destroy-then-copy-construct.
constexpr __box&
operator=(const __box& __that) noexcept
requires (!copyable<_Tp>) && copy_constructible<_Tp>
{
static_assert(is_nothrow_copy_constructible_v<_Tp>);
if (this != std::__addressof(__that))
{
_M_value.~_Tp();
std::construct_at(std::__addressof(_M_value), *__that);
}
return *this;
}
// Likewise for move assignment.
constexpr __box&
operator=(__box&& __that) noexcept
requires (!movable<_Tp>)
{
static_assert(is_nothrow_move_constructible_v<_Tp>);
if (this != std::__addressof(__that))
{
_M_value.~_Tp();
std::construct_at(std::__addressof(_M_value), std::move(*__that));
}
return *this;
}
constexpr bool
has_value() const noexcept
{ return true; };
constexpr _Tp&
operator*() & noexcept
{ return _M_value; }
constexpr const _Tp&
operator*() const & noexcept
{ return _M_value; }
constexpr _Tp&&
operator*() && noexcept
{ return std::move(_M_value); }
constexpr const _Tp&&
operator*() const && noexcept
{ return std::move(_M_value); }
constexpr _Tp*
operator->() noexcept
{ return std::__addressof(_M_value); }
constexpr const _Tp*
operator->() const noexcept
{ return std::__addressof(_M_value); }
};
} // namespace __detail
/// A view that contains exactly one element.
#if __cpp_lib_ranges >= 202207L // C++ >= 23
template<move_constructible _Tp>
#else
template<copy_constructible _Tp>
#endif
requires is_object_v<_Tp>
class single_view : public view_interface<single_view<_Tp>>
{
public:
single_view() requires default_initializable<_Tp> = default;
constexpr explicit
single_view(const _Tp& __t)
noexcept(is_nothrow_copy_constructible_v<_Tp>)
requires copy_constructible<_Tp>
: _M_value(__t)
{ }
constexpr explicit
single_view(_Tp&& __t)
noexcept(is_nothrow_move_constructible_v<_Tp>)
: _M_value(std::move(__t))
{ }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3428. single_view's in place constructor should be explicit
template<typename... _Args>
requires constructible_from<_Tp, _Args...>
constexpr explicit
single_view(in_place_t, _Args&&... __args)
noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
: _M_value{in_place, std::forward<_Args>(__args)...}
{ }
constexpr _Tp*
begin() noexcept
{ return data(); }
constexpr const _Tp*
begin() const noexcept
{ return data(); }
constexpr _Tp*
end() noexcept
{ return data() + 1; }
constexpr const _Tp*
end() const noexcept
{ return data() + 1; }
static constexpr size_t
size() noexcept
{ return 1; }
constexpr _Tp*
data() noexcept
{ return _M_value.operator->(); }
constexpr const _Tp*
data() const noexcept
{ return _M_value.operator->(); }
private:
[[no_unique_address]] __detail::__box<_Tp> _M_value;
};
template<typename _Tp>
single_view(_Tp) -> single_view<_Tp>;
namespace __detail
{
template<typename _Wp>
constexpr auto __to_signed_like(_Wp __w) noexcept
{
if constexpr (!integral<_Wp>)
return iter_difference_t<_Wp>();
else if constexpr (sizeof(iter_difference_t<_Wp>) > sizeof(_Wp))
return iter_difference_t<_Wp>(__w);
else if constexpr (sizeof(ptrdiff_t) > sizeof(_Wp))
return ptrdiff_t(__w);
else if constexpr (sizeof(long long) > sizeof(_Wp))
return (long long)(__w);
#ifdef __SIZEOF_INT128__
else if constexpr (__SIZEOF_INT128__ > sizeof(_Wp))
return __int128(__w);
#endif
else
return __max_diff_type(__w);
}
template<typename _Wp>
using __iota_diff_t = decltype(__to_signed_like(std::declval<_Wp>()));
template<typename _It>
concept __decrementable = incrementable<_It>
&& requires(_It __i)
{
{ --__i } -> same_as<_It&>;
{ __i-- } -> same_as<_It>;
};
template<typename _It>
concept __advanceable = __decrementable<_It> && totally_ordered<_It>
&& requires( _It __i, const _It __j, const __iota_diff_t<_It> __n)
{
{ __i += __n } -> same_as<_It&>;
{ __i -= __n } -> same_as<_It&>;
_It(__j + __n);
_It(__n + __j);
_It(__j - __n);
{ __j - __j } -> convertible_to<__iota_diff_t<_It>>;
};
template<typename _Winc>
struct __iota_view_iter_cat
{ };
template<incrementable _Winc>
struct __iota_view_iter_cat<_Winc>
{ using iterator_category = input_iterator_tag; };
} // namespace __detail
template<weakly_incrementable _Winc,
semiregular _Bound = unreachable_sentinel_t>
requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound>
&& copyable<_Winc>
class iota_view : public view_interface<iota_view<_Winc, _Bound>>
{
private:
struct _Sentinel;
struct _Iterator : __detail::__iota_view_iter_cat<_Winc>
{
private:
static auto
_S_iter_concept()
{
using namespace __detail;
if constexpr (__advanceable<_Winc>)
return random_access_iterator_tag{};
else if constexpr (__decrementable<_Winc>)
return bidirectional_iterator_tag{};
else if constexpr (incrementable<_Winc>)
return forward_iterator_tag{};
else
return input_iterator_tag{};
}
public:
using iterator_concept = decltype(_S_iter_concept());
// iterator_category defined in __iota_view_iter_cat
using value_type = _Winc;
using difference_type = __detail::__iota_diff_t<_Winc>;
_Iterator() requires default_initializable<_Winc> = default;
constexpr explicit
_Iterator(_Winc __value)
: _M_value(__value) { }
constexpr _Winc
operator*() const noexcept(is_nothrow_copy_constructible_v<_Winc>)
{ return _M_value; }
constexpr _Iterator&
operator++()
{
++_M_value;
return *this;
}
constexpr void
operator++(int)
{ ++*this; }
constexpr _Iterator
operator++(int) requires incrementable<_Winc>
{
auto __tmp = *this;
++*this;
return __tmp;
}
constexpr _Iterator&
operator--() requires __detail::__decrementable<_Winc>
{
--_M_value;
return *this;
}
constexpr _Iterator
operator--(int) requires __detail::__decrementable<_Winc>
{
auto __tmp = *this;
--*this;
return __tmp;
}
constexpr _Iterator&
operator+=(difference_type __n) requires __detail::__advanceable<_Winc>
{
using __detail::__is_integer_like;
using __detail::__is_signed_integer_like;
if constexpr (__is_integer_like<_Winc>
&& !__is_signed_integer_like<_Winc>)
{
if (__n >= difference_type(0))
_M_value += static_cast<_Winc>(__n);
else
_M_value -= static_cast<_Winc>(-__n);
}
else
_M_value += __n;
return *this;
}
constexpr _Iterator&
operator-=(difference_type __n) requires __detail::__advanceable<_Winc>
{
using __detail::__is_integer_like;
using __detail::__is_signed_integer_like;
if constexpr (__is_integer_like<_Winc>
&& !__is_signed_integer_like<_Winc>)
{
if (__n >= difference_type(0))
_M_value -= static_cast<_Winc>(__n);
else
_M_value += static_cast<_Winc>(-__n);
}
else
_M_value -= __n;
return *this;
}
constexpr _Winc
operator[](difference_type __n) const
requires __detail::__advanceable<_Winc>
{ return _Winc(_M_value + __n); }
friend constexpr bool
operator==(const _Iterator& __x, const _Iterator& __y)
requires equality_comparable<_Winc>
{ return __x._M_value == __y._M_value; }
friend constexpr bool
operator<(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc>
{ return __x._M_value < __y._M_value; }
friend constexpr bool
operator>(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc>
{ return __y < __x; }
friend constexpr bool
operator<=(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc>
{ return !(__y < __x); }
friend constexpr bool
operator>=(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc>
{ return !(__x < __y); }
#ifdef __cpp_lib_three_way_comparison
friend constexpr auto
operator<=>(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc> && three_way_comparable<_Winc>
{ return __x._M_value <=> __y._M_value; }
#endif
friend constexpr _Iterator
operator+(_Iterator __i, difference_type __n)
requires __detail::__advanceable<_Winc>
{
__i += __n;
return __i;
}
friend constexpr _Iterator
operator+(difference_type __n, _Iterator __i)
requires __detail::__advanceable<_Winc>
{ return __i += __n; }
friend constexpr _Iterator
operator-(_Iterator __i, difference_type __n)
requires __detail::__advanceable<_Winc>
{
__i -= __n;
return __i;
}
friend constexpr difference_type
operator-(const _Iterator& __x, const _Iterator& __y)
requires __detail::__advanceable<_Winc>
{
using __detail::__is_integer_like;
using __detail::__is_signed_integer_like;
using _Dt = difference_type;
if constexpr (__is_integer_like<_Winc>)
{
if constexpr (__is_signed_integer_like<_Winc>)
return _Dt(_Dt(__x._M_value) - _Dt(__y._M_value));
else
return (__y._M_value > __x._M_value)
? _Dt(-_Dt(__y._M_value - __x._M_value))
: _Dt(__x._M_value - __y._M_value);
}
else
return __x._M_value - __y._M_value;
}
private:
_Winc _M_value = _Winc();
friend iota_view;
friend _Sentinel;
};
struct _Sentinel
{
private:
constexpr bool
_M_equal(const _Iterator& __x) const
{ return __x._M_value == _M_bound; }
constexpr auto
_M_distance_from(const _Iterator& __x) const
{ return _M_bound - __x._M_value; }
_Bound _M_bound = _Bound();
public:
_Sentinel() = default;
constexpr explicit
_Sentinel(_Bound __bound)
: _M_bound(__bound) { }
friend constexpr bool
operator==(const _Iterator& __x, const _Sentinel& __y)
{ return __y._M_equal(__x); }
friend constexpr iter_difference_t<_Winc>
operator-(const _Iterator& __x, const _Sentinel& __y)
requires sized_sentinel_for<_Bound, _Winc>
{ return -__y._M_distance_from(__x); }
friend constexpr iter_difference_t<_Winc>
operator-(const _Sentinel& __x, const _Iterator& __y)
requires sized_sentinel_for<_Bound, _Winc>
{ return __x._M_distance_from(__y); }
friend iota_view;
};
_Winc _M_value = _Winc();
[[no_unique_address]] _Bound _M_bound = _Bound();
public:
iota_view() requires default_initializable<_Winc> = default;
constexpr explicit
iota_view(_Winc __value)
: _M_value(__value)
{ }
constexpr
iota_view(type_identity_t<_Winc> __value,
type_identity_t<_Bound> __bound)
: _M_value(__value), _M_bound(__bound)
{
if constexpr (totally_ordered_with<_Winc, _Bound>)
__glibcxx_assert( bool(__value <= __bound) );
}
constexpr
iota_view(_Iterator __first, _Iterator __last)
requires same_as<_Winc, _Bound>
: iota_view(__first._M_value, __last._M_value)
{ }
constexpr
iota_view(_Iterator __first, unreachable_sentinel_t __last)
requires same_as<_Bound, unreachable_sentinel_t>
: iota_view(__first._M_value, __last)
{ }
constexpr
iota_view(_Iterator __first, _Sentinel __last)
requires (!same_as<_Winc, _Bound>) && (!same_as<_Bound, unreachable_sentinel_t>)
: iota_view(__first._M_value, __last._M_bound)
{ }
constexpr _Iterator
begin() const { return _Iterator{_M_value}; }
constexpr auto
end() const
{
if constexpr (same_as<_Bound, unreachable_sentinel_t>)
return unreachable_sentinel;
else
return _Sentinel{_M_bound};
}
constexpr _Iterator
end() const requires same_as<_Winc, _Bound>
{ return _Iterator{_M_bound}; }
constexpr auto
size() const
requires (same_as<_Winc, _Bound> && __detail::__advanceable<_Winc>)
|| (integral<_Winc> && integral<_Bound>)
|| sized_sentinel_for<_Bound, _Winc>
{
using __detail::__is_integer_like;
using __detail::__to_unsigned_like;
if constexpr (integral<_Winc> && integral<_Bound>)
{
using _Up = make_unsigned_t<decltype(_M_bound - _M_value)>;
return _Up(_M_bound) - _Up(_M_value);
}
else if constexpr (__is_integer_like<_Winc>)
return __to_unsigned_like(_M_bound) - __to_unsigned_like(_M_value);
else
return __to_unsigned_like(_M_bound - _M_value);
}
};
template<typename _Winc, typename _Bound>
requires (!__detail::__is_integer_like<_Winc>
|| !__detail::__is_integer_like<_Bound>
|| (__detail::__is_signed_integer_like<_Winc>
== __detail::__is_signed_integer_like<_Bound>))
iota_view(_Winc, _Bound) -> iota_view<_Winc, _Bound>;
template<typename _Winc, typename _Bound>
inline constexpr bool
enable_borrowed_range<iota_view<_Winc, _Bound>> = true;
namespace views
{
template<typename _Tp>
inline constexpr empty_view<_Tp> empty{};
namespace __detail
{
template<typename _Tp>
concept __can_single_view
= requires { single_view<decay_t<_Tp>>(std::declval<_Tp>()); };
} // namespace __detail
struct _Single
{
template<__detail::__can_single_view _Tp>
constexpr auto
operator() [[nodiscard]] (_Tp&& __e) const
noexcept(noexcept(single_view<decay_t<_Tp>>(std::forward<_Tp>(__e))))
{ return single_view<decay_t<_Tp>>(std::forward<_Tp>(__e)); }
};
inline constexpr _Single single{};
namespace __detail
{
template<typename... _Args>
concept __can_iota_view = requires { iota_view(std::declval<_Args>()...); };
} // namespace __detail
struct _Iota
{
template<__detail::__can_iota_view _Tp>
constexpr auto
operator() [[nodiscard]] (_Tp&& __e) const
{ return iota_view(std::forward<_Tp>(__e)); }
template<typename _Tp, typename _Up>
requires __detail::__can_iota_view<_Tp, _Up>
constexpr auto
operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
{ return iota_view(std::forward<_Tp>(__e), std::forward<_Up>(__f)); }
};
inline constexpr _Iota iota{};
} // namespace views
#if _GLIBCXX_HOSTED
namespace __detail
{
template<typename _Val, typename _CharT, typename _Traits>
concept __stream_extractable
= requires(basic_istream<_CharT, _Traits>& is, _Val& t) { is >> t; };
} // namespace __detail
template<movable _Val, typename _CharT,
typename _Traits = char_traits<_CharT>>
requires default_initializable<_Val>
&& __detail::__stream_extractable<_Val, _CharT, _Traits>
class basic_istream_view
: public view_interface<basic_istream_view<_Val, _CharT, _Traits>>
{
public:
constexpr explicit
basic_istream_view(basic_istream<_CharT, _Traits>& __stream)
: _M_stream(std::__addressof(__stream))
{ }
constexpr auto
begin()
{
*_M_stream >> _M_object;
return _Iterator{this};
}
constexpr default_sentinel_t
end() const noexcept
{ return default_sentinel; }
private:
basic_istream<_CharT, _Traits>* _M_stream;
_Val _M_object = _Val();
struct _Iterator
{
public:
using iterator_concept = input_iterator_tag;
using difference_type = ptrdiff_t;
using value_type = _Val;
constexpr explicit
_Iterator(basic_istream_view* __parent) noexcept
: _M_parent(__parent)
{ }
_Iterator(const _Iterator&) = delete;
_Iterator(_Iterator&&) = default;
_Iterator& operator=(const _Iterator&) = delete;
_Iterator& operator=(_Iterator&&) = default;
_Iterator&
operator++()
{
*_M_parent->_M_stream >> _M_parent->_M_object;
return *this;
}
void
operator++(int)
{ ++*this; }
_Val&
operator*() const
{ return _M_parent->_M_object; }
friend bool
operator==(const _Iterator& __x, default_sentinel_t)
{ return __x._M_at_end(); }
private:
basic_istream_view* _M_parent;
bool
_M_at_end() const
{ return !*_M_parent->_M_stream; }
};
friend _Iterator;
};
template<typename _Val>
using istream_view = basic_istream_view<_Val, char>;
template<typename _Val>
using wistream_view = basic_istream_view<_Val, wchar_t>;
namespace views
{
namespace __detail
{
template<typename _Tp, typename _Up>
concept __can_istream_view = requires (_Up __e) {
basic_istream_view<_Tp, typename _Up::char_type, typename _Up::traits_type>(__e);
};
} // namespace __detail
template<typename _Tp>
struct _Istream
{
template<typename _CharT, typename _Traits>
constexpr auto
operator() [[nodiscard]] (basic_istream<_CharT, _Traits>& __e) const
requires __detail::__can_istream_view<_Tp, remove_reference_t<decltype(__e)>>
{ return basic_istream_view<_Tp, _CharT, _Traits>(__e); }
};
template<typename _Tp>
inline constexpr _Istream<_Tp> istream;
}
#endif // HOSTED
// C++20 24.7 [range.adaptors] Range adaptors
namespace __detail
{
template<typename _Tp, int _Disc>
struct _Absent { };
// Alias for a type that is conditionally present
// (and is an empty type otherwise).
// Data members using this alias should use [[no_unique_address]] so that
// they take no space when not needed.
// The optional template parameter _Disc is for discriminating two otherwise
// equivalent absent types so that even they can overlap.
template<bool _Present, typename _Tp, int _Disc = 0>
using __maybe_present_t = __conditional_t<_Present, _Tp, _Absent<_Tp, _Disc>>;
// Alias for a type that is conditionally const.
template<bool _Const, typename _Tp>
using __maybe_const_t = __conditional_t<_Const, const _Tp, _Tp>;
} // namespace __detail
// Shorthand for __detail::__maybe_const_t.
using __detail::__maybe_const_t;
namespace views::__adaptor
{
// True if the range adaptor _Adaptor can be applied with _Args.
template<typename _Adaptor, typename... _Args>
concept __adaptor_invocable
= requires { std::declval<_Adaptor>()(declval<_Args>()...); };
// True if the range adaptor non-closure _Adaptor can be partially applied
// with _Args.
template<typename _Adaptor, typename... _Args>
concept __adaptor_partial_app_viable = (_Adaptor::_S_arity > 1)
&& (sizeof...(_Args) == _Adaptor::_S_arity - 1)
&& (constructible_from<decay_t<_Args>, _Args> && ...);
template<typename _Adaptor, typename... _Args>
struct _Partial;
template<typename _Lhs, typename _Rhs>
struct _Pipe;
// The base class of every range adaptor closure.
//
// The derived class should define the optional static data member
// _S_has_simple_call_op to true if the behavior of this adaptor is
// independent of the constness/value category of the adaptor object.
template<typename _Derived>
struct _RangeAdaptorClosure
{ };
template<typename _Tp, typename _Up>
requires (!same_as<_Tp, _RangeAdaptorClosure<_Up>>)
void __is_range_adaptor_closure_fn
(const _Tp&, const _RangeAdaptorClosure<_Up>&); // not defined
template<typename _Tp>
concept __is_range_adaptor_closure
= requires (_Tp __t) { __adaptor::__is_range_adaptor_closure_fn(__t, __t); };
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdangling-reference"
// range | adaptor is equivalent to adaptor(range).
template<typename _Self, typename _Range>
requires __is_range_adaptor_closure<_Self>
&& __adaptor_invocable<_Self, _Range>
constexpr auto
operator|(_Range&& __r, _Self&& __self)
{ return std::forward<_Self>(__self)(std::forward<_Range>(__r)); }
// Compose the adaptors __lhs and __rhs into a pipeline, returning
// another range adaptor closure object.
template<typename _Lhs, typename _Rhs>
requires __is_range_adaptor_closure<_Lhs>
&& __is_range_adaptor_closure<_Rhs>
constexpr auto
operator|(_Lhs&& __lhs, _Rhs&& __rhs)
{
return _Pipe<decay_t<_Lhs>, decay_t<_Rhs>>{std::forward<_Lhs>(__lhs),
std::forward<_Rhs>(__rhs)};
}
#pragma GCC diagnostic pop
// The base class of every range adaptor non-closure.
//
// The static data member _Derived::_S_arity must contain the total number of
// arguments that the adaptor takes, and the class _Derived must introduce
// _RangeAdaptor::operator() into the class scope via a using-declaration.
//
// The optional static data member _Derived::_S_has_simple_extra_args should
// be defined to true if the behavior of this adaptor is independent of the
// constness/value category of the extra arguments. This data member could
// also be defined as a variable template parameterized by the types of the
// extra arguments.
template<typename _Derived>
struct _RangeAdaptor
{
// Partially apply the arguments __args to the range adaptor _Derived,
// returning a range adaptor closure object.
template<typename... _Args>
requires __adaptor_partial_app_viable<_Derived, _Args...>
constexpr auto
operator()(_Args&&... __args) const
{
return _Partial<_Derived, decay_t<_Args>...>{0, std::forward<_Args>(__args)...};
}
};
// True if the range adaptor closure _Adaptor has a simple operator(), i.e.
// one that's not overloaded according to constness or value category of the
// _Adaptor object.
template<typename _Adaptor>