-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathranges
3561 lines (2986 loc) · 94.9 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-2020 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 <bits/refwrap.h>
#include <compare>
#include <initializer_list>
#include <iterator>
#include <optional>
#include <tuple>
/**
* @defgroup ranges Ranges
*
* Components for dealing with ranges of elements.
*/
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
namespace ranges
{
// [range.range] The range concept.
// [range.sized] The sized_range concept.
// Defined in <bits/range_access.h>
// [range.refinements]
// Defined in <bits/range_access.h>
struct view_base { };
template<typename _Tp>
inline constexpr bool enable_view = derived_from<_Tp, view_base>;
template<typename _Tp>
concept view
= range<_Tp> && movable<_Tp> && default_initializable<_Tp>
&& enable_view<_Tp>;
/// A range which can be safely converted to a view.
template<typename _Tp>
concept viewable_range = range<_Tp>
&& (borrowed_range<_Tp> || view<remove_cvref_t<_Tp>>);
namespace __detail
{
template<typename _Range>
concept __simple_view = view<_Range> && range<const _Range>
&& same_as<iterator_t<_Range>, iterator_t<const _Range>>
&& same_as<sentinel_t<_Range>, sentinel_t<const _Range>>;
template<typename _It>
concept __has_arrow = input_iterator<_It>
&& (is_pointer_v<_It> || requires(_It __it) { __it.operator->(); });
template<typename _Tp, typename _Up>
concept __not_same_as
= !same_as<remove_cvref_t<_Tp>, remove_cvref_t<_Up>>;
} // namespace __detail
template<typename _Derived>
requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
class view_interface : public view_base
{
private:
constexpr _Derived& _M_derived() noexcept
{
static_assert(derived_from<_Derived, view_interface<_Derived>>);
static_assert(view<_Derived>);
return static_cast<_Derived&>(*this);
}
constexpr const _Derived& _M_derived() const noexcept
{
static_assert(derived_from<_Derived, view_interface<_Derived>>);
static_assert(view<_Derived>);
return static_cast<const _Derived&>(*this);
}
public:
constexpr bool
empty() requires forward_range<_Derived>
{ return ranges::begin(_M_derived()) == ranges::end(_M_derived()); }
constexpr bool
empty() const requires forward_range<const _Derived>
{ return ranges::begin(_M_derived()) == ranges::end(_M_derived()); }
constexpr explicit
operator bool() requires requires { ranges::empty(_M_derived()); }
{ return !ranges::empty(_M_derived()); }
constexpr explicit
operator bool() const requires requires { ranges::empty(_M_derived()); }
{ return !ranges::empty(_M_derived()); }
constexpr auto
data() requires contiguous_iterator<iterator_t<_Derived>>
{ return to_address(ranges::begin(_M_derived())); }
constexpr auto
data() const
requires range<const _Derived>
&& contiguous_iterator<iterator_t<const _Derived>>
{ return to_address(ranges::begin(_M_derived())); }
constexpr auto
size()
requires forward_range<_Derived>
&& sized_sentinel_for<sentinel_t<_Derived>, iterator_t<_Derived>>
{ return ranges::end(_M_derived()) - ranges::begin(_M_derived()); }
constexpr auto
size() const
requires forward_range<const _Derived>
&& sized_sentinel_for<sentinel_t<const _Derived>,
iterator_t<const _Derived>>
{ return ranges::end(_M_derived()) - ranges::begin(_M_derived()); }
constexpr decltype(auto)
front() requires forward_range<_Derived>
{
__glibcxx_assert(!empty());
return *ranges::begin(_M_derived());
}
constexpr decltype(auto)
front() const requires forward_range<const _Derived>
{
__glibcxx_assert(!empty());
return *ranges::begin(_M_derived());
}
constexpr decltype(auto)
back()
requires bidirectional_range<_Derived> && common_range<_Derived>
{
__glibcxx_assert(!empty());
return *ranges::prev(ranges::end(_M_derived()));
}
constexpr decltype(auto)
back() const
requires bidirectional_range<const _Derived>
&& common_range<const _Derived>
{
__glibcxx_assert(!empty());
return *ranges::prev(ranges::end(_M_derived()));
}
template<random_access_range _Range = _Derived>
constexpr decltype(auto)
operator[](range_difference_t<_Range> __n)
{ return ranges::begin(_M_derived())[__n]; }
template<random_access_range _Range = const _Derived>
constexpr decltype(auto)
operator[](range_difference_t<_Range> __n) const
{ return ranges::begin(_M_derived())[__n]; }
};
namespace __detail
{
template<class _From, class _To>
concept __convertible_to_non_slicing = convertible_to<_From, _To>
&& !(is_pointer_v<decay_t<_From>> && is_pointer_v<decay_t<_To>>
&& __not_same_as<remove_pointer_t<decay_t<_From>>,
remove_pointer_t<decay_t<_To>>>);
template<typename _Tp>
concept __pair_like
= !is_reference_v<_Tp> && requires(_Tp __t)
{
typename tuple_size<_Tp>::type;
requires derived_from<tuple_size<_Tp>, integral_constant<size_t, 2>>;
typename tuple_element_t<0, remove_const_t<_Tp>>;
typename tuple_element_t<1, remove_const_t<_Tp>>;
{ get<0>(__t) } -> convertible_to<const tuple_element_t<0, _Tp>&>;
{ get<1>(__t) } -> convertible_to<const tuple_element_t<1, _Tp>&>;
};
template<typename _Tp, typename _Up, typename _Vp>
concept __pair_like_convertible_from
= !range<_Tp> && __pair_like<_Tp>
&& constructible_from<_Tp, _Up, _Vp>
&& __convertible_to_non_slicing<_Up, tuple_element_t<0, _Tp>>
&& convertible_to<_Vp, tuple_element_t<1, _Tp>>;
template<typename _Tp>
concept __iterator_sentinel_pair
= !range<_Tp> && __pair_like<_Tp>
&& sentinel_for<tuple_element_t<1, _Tp>, tuple_element_t<0, _Tp>>;
} // namespace __detail
enum class subrange_kind : bool { unsized, sized };
template<input_or_output_iterator _It, sentinel_for<_It> _Sent = _It,
subrange_kind _Kind = sized_sentinel_for<_Sent, _It>
? subrange_kind::sized : subrange_kind::unsized>
requires (_Kind == subrange_kind::sized || !sized_sentinel_for<_Sent, _It>)
class subrange : public view_interface<subrange<_It, _Sent, _Kind>>
{
private:
// XXX: gcc complains when using constexpr here
static const bool _S_store_size
= _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _It>;
_It _M_begin = _It();
_Sent _M_end = _Sent();
template<typename, bool = _S_store_size>
struct _Size
{ };
template<typename _Tp>
struct _Size<_Tp, true>
{ __detail::__make_unsigned_like_t<_Tp> _M_size; };
[[no_unique_address]] _Size<iter_difference_t<_It>> _M_size = {};
public:
subrange() = default;
constexpr
subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s)
requires (!_S_store_size)
: _M_begin(std::move(__i)), _M_end(__s)
{ }
constexpr
subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s,
__detail::__make_unsigned_like_t<iter_difference_t<_It>> __n)
requires (_Kind == subrange_kind::sized)
: _M_begin(std::move(__i)), _M_end(__s)
{
using __detail::__to_unsigned_like;
__glibcxx_assert(__n == __to_unsigned_like(ranges::distance(__i, __s)));
if constexpr (_S_store_size)
_M_size._M_size = __n;
}
template<__detail::__not_same_as<subrange> _Rng>
requires borrowed_range<_Rng>
&& __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
&& convertible_to<sentinel_t<_Rng>, _Sent>
constexpr
subrange(_Rng&& __r) requires _S_store_size && sized_range<_Rng>
: subrange{__r, ranges::size(__r)}
{ }
template<__detail::__not_same_as<subrange> _Rng>
requires borrowed_range<_Rng>
&& __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
&& convertible_to<sentinel_t<_Rng>, _Sent>
constexpr
subrange(_Rng&& __r) requires (!_S_store_size)
: subrange{ranges::begin(__r), ranges::end(__r)}
{ }
template<borrowed_range _Rng>
requires __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
&& convertible_to<sentinel_t<_Rng>, _Sent>
constexpr
subrange(_Rng&& __r,
__detail::__make_unsigned_like_t<iter_difference_t<_It>> __n)
requires (_Kind == subrange_kind::sized)
: subrange{ranges::begin(__r), ranges::end(__r), __n}
{ }
template<__detail::__not_same_as<subrange> _PairLike>
requires __detail::__pair_like_convertible_from<_PairLike, const _It&,
const _Sent&>
constexpr
operator _PairLike() const
{ return _PairLike(_M_begin, _M_end); }
constexpr _It
begin() const requires copyable<_It>
{ return _M_begin; }
[[nodiscard]] constexpr _It
begin() requires (!copyable<_It>)
{ return std::move(_M_begin); }
constexpr _Sent end() const { return _M_end; }
constexpr bool empty() const { return _M_begin == _M_end; }
constexpr __detail::__make_unsigned_like_t<iter_difference_t<_It>>
size() const requires (_Kind == subrange_kind::sized)
{
if constexpr (_S_store_size)
return _M_size._M_size;
else
return __detail::__to_unsigned_like(_M_end - _M_begin);
}
[[nodiscard]] constexpr subrange
next(iter_difference_t<_It> __n = 1) const &
requires forward_iterator<_It>
{
auto __tmp = *this;
__tmp.advance(__n);
return __tmp;
}
[[nodiscard]] constexpr subrange
next(iter_difference_t<_It> __n = 1) &&
{
advance(__n);
return std::move(*this);
}
[[nodiscard]] constexpr subrange
prev(iter_difference_t<_It> __n = 1) const
requires bidirectional_iterator<_It>
{
auto __tmp = *this;
__tmp.advance(-__n);
return __tmp;
}
constexpr subrange&
advance(iter_difference_t<_It> __n)
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3433. subrange::advance(n) has UB when n < 0
if constexpr (bidirectional_iterator<_It>)
if (__n < 0)
{
ranges::advance(_M_begin, __n);
if constexpr (_S_store_size)
_M_size._M_size += __detail::__to_unsigned_like(-__n);
return *this;
}
__glibcxx_assert(__n >= 0);
auto __d = __n - ranges::advance(_M_begin, __n, _M_end);
if constexpr (_S_store_size)
_M_size._M_size -= __detail::__to_unsigned_like(__d);
return *this;
}
};
template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
subrange(_It, _Sent) -> subrange<_It, _Sent>;
template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
subrange(_It, _Sent,
__detail::__make_unsigned_like_t<iter_difference_t<_It>>)
-> subrange<_It, _Sent, subrange_kind::sized>;
template<__detail::__iterator_sentinel_pair _Pr>
subrange(_Pr)
-> subrange<tuple_element_t<0, _Pr>, tuple_element_t<1, _Pr>>;
template<__detail::__iterator_sentinel_pair _Pr>
subrange(_Pr, __detail::__make_unsigned_like_t<iter_difference_t<
tuple_element_t<0, _Pr>>>)
-> subrange<tuple_element_t<0, _Pr>, tuple_element_t<1, _Pr>,
subrange_kind::sized>;
template<borrowed_range _Rng>
subrange(_Rng&&)
-> subrange<iterator_t<_Rng>, sentinel_t<_Rng>,
(sized_range<_Rng>
|| sized_sentinel_for<sentinel_t<_Rng>, iterator_t<_Rng>>)
? subrange_kind::sized : subrange_kind::unsized>;
template<borrowed_range _Rng>
subrange(_Rng&&,
__detail::__make_unsigned_like_t<range_difference_t<_Rng>>)
-> subrange<iterator_t<_Rng>, sentinel_t<_Rng>, subrange_kind::sized>;
template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
requires (_Num < 2)
constexpr auto
get(const subrange<_It, _Sent, _Kind>& __r)
{
if constexpr (_Num == 0)
return __r.begin();
else
return __r.end();
}
template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
requires (_Num < 2)
constexpr auto
get(subrange<_It, _Sent, _Kind>&& __r)
{
if constexpr (_Num == 0)
return __r.begin();
else
return __r.end();
}
template<input_or_output_iterator _It, sentinel_for<_It> _Sent,
subrange_kind _Kind>
inline constexpr bool
enable_borrowed_range<subrange<_It, _Sent, _Kind>> = true;
} // namespace ranges
using ranges::get;
namespace ranges
{
/// Type returned by algorithms instead of a dangling iterator or subrange.
struct dangling
{
constexpr dangling() noexcept = default;
template<typename... _Args>
constexpr dangling(_Args&&...) noexcept { }
};
template<range _Range>
using borrowed_iterator_t = conditional_t<borrowed_range<_Range>,
iterator_t<_Range>,
dangling>;
template<range _Range>
using borrowed_subrange_t = conditional_t<borrowed_range<_Range>,
subrange<iterator_t<_Range>>,
dangling>;
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
{
template<copy_constructible _Tp> requires is_object_v<_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=;
__box&
operator=(const __box& __that)
noexcept(is_nothrow_copy_constructible_v<_Tp>)
requires (!assignable_from<_Tp&, const _Tp&>)
{
if ((bool)__that)
this->emplace(*__that);
else
this->reset();
return *this;
}
__box&
operator=(__box&& __that)
noexcept(is_nothrow_move_constructible_v<_Tp>)
requires (!assignable_from<_Tp&, _Tp>)
{
if ((bool)__that)
this->emplace(std::move(*__that));
else
this->reset();
return *this;
}
};
} // namespace __detail
/// A view that contains exactly one element.
template<copy_constructible _Tp> requires is_object_v<_Tp>
class single_view : public view_interface<single_view<_Tp>>
{
public:
single_view() = default;
constexpr explicit
single_view(const _Tp& __t)
: _M_value(__t)
{ }
constexpr explicit
single_view(_Tp&& __t)
: _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)
: _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:
__detail::__box<_Tp> _M_value;
};
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>>;
};
} // namespace __detail
template<weakly_incrementable _Winc,
semiregular _Bound = unreachable_sentinel_t>
requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound>
&& semiregular<_Winc>
class iota_view : public view_interface<iota_view<_Winc, _Bound>>
{
private:
struct _Sentinel;
struct _Iterator
{
private:
static auto
_S_iter_cat()
{
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_category = decltype(_S_iter_cat());
using value_type = _Winc;
using difference_type = __detail::__iota_diff_t<_Winc>;
_Iterator() = 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>
{ return __i += __n; }
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>
{ return __i -= __n; }
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 _Sentinel;
};
struct _Sentinel
{
private:
constexpr bool
_M_equal(const _Iterator& __x) const
{ return __x._M_value == _M_bound; }
_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 __x._M_value - __y._M_bound; }
friend constexpr iter_difference_t<_Winc>
operator-(const _Sentinel& __x, const _Iterator& __y)
requires sized_sentinel_for<_Bound, _Winc>
{ return -(__y - __x); }
};
_Winc _M_value = _Winc();
_Bound _M_bound = _Bound();
public:
iota_view() = 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 _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 (__is_integer_like<_Winc> && __is_integer_like<_Bound>)
return (_M_value < 0)
? ((_M_bound < 0)
? __to_unsigned_like(-_M_value) - __to_unsigned_like(-_M_bound)
: __to_unsigned_like(_M_bound) + __to_unsigned_like(-_M_value))
: __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<weakly_incrementable _Winc, semiregular _Bound>
inline constexpr bool
enable_borrowed_range<iota_view<_Winc, _Bound>> = true;
namespace views
{
template<typename _Tp>
inline constexpr empty_view<_Tp> empty{};
struct _Single
{
template<typename _Tp>
constexpr auto
operator()(_Tp&& __e) const
{ return single_view{std::forward<_Tp>(__e)}; }
};
inline constexpr _Single single{};
struct _Iota
{
template<typename _Tp>
constexpr auto
operator()(_Tp&& __e) const
{ return iota_view{std::forward<_Tp>(__e)}; }
template<typename _Tp, typename _Up>
constexpr auto
operator()(_Tp&& __e, _Up&& __f) const
{ return iota_view{std::forward<_Tp>(__e), std::forward<_Up>(__f)}; }
};
inline constexpr _Iota iota{};
} // namespace views
namespace __detail
{
struct _Empty { };
// 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.
template<bool _Present, typename _Tp>
using __maybe_present_t = conditional_t<_Present, _Tp, _Empty>;
// 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
namespace views
{
namespace __adaptor
{
template<typename _Tp>
inline constexpr auto
__maybe_refwrap(_Tp& __arg)
{ return reference_wrapper<_Tp>{__arg}; }
template<typename _Tp>
inline constexpr auto
__maybe_refwrap(const _Tp& __arg)
{ return reference_wrapper<const _Tp>{__arg}; }
template<typename _Tp>
inline constexpr decltype(auto)
__maybe_refwrap(_Tp&& __arg)
{ return std::forward<_Tp>(__arg); }
template<typename _Callable>
struct _RangeAdaptorClosure;
template<typename _Callable>
struct _RangeAdaptor
{
protected:
[[no_unique_address]]
__detail::__maybe_present_t<!is_default_constructible_v<_Callable>,
_Callable> _M_callable;
public:
constexpr
_RangeAdaptor(const _Callable& = {})
requires is_default_constructible_v<_Callable>
{ }
constexpr