-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ranges
10500 lines (8935 loc) · 458 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 standard header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef _RANGES_
#define _RANGES_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#ifndef __cpp_lib_ranges
_EMIT_STL_WARNING(STL4038, "The contents of <ranges> are available only with C++20 or later.");
#else // ^^^ !defined(__cpp_lib_ranges) / defined(__cpp_lib_ranges) vvv
#include <__msvc_int128.hpp>
#include <iosfwd>
#include <iterator>
#include <limits>
#include <span>
#include <string_view>
#include <tuple>
#if _HAS_CXX23
#include <array>
#include <bit>
#endif // _HAS_CXX23
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
namespace ranges {
// MUCH machinery defined in <xutility>
template <class _Ty>
inline constexpr bool _Is_initializer_list = _Is_specialization_v<remove_cvref_t<_Ty>, initializer_list>;
template <auto> // _Require_constant<E> is a valid template-id iff E is a constant expression of structural type
struct _Require_constant;
#if _HAS_CXX23
_EXPORT_STD template <range _Rng>
using const_iterator_t = const_iterator<iterator_t<_Rng>>;
_EXPORT_STD template <range _Rng>
using const_sentinel_t = const_sentinel<sentinel_t<_Rng>>;
_EXPORT_STD template <range _Rng>
using range_const_reference_t = iter_const_reference_t<iterator_t<_Rng>>;
template <range _Ty>
inline constexpr auto _Compile_time_max_size =
(numeric_limits<_Make_unsigned_like_t<range_difference_t<_Ty>>>::max)();
template <sized_range _Ty>
inline constexpr auto _Compile_time_max_size<_Ty> = (numeric_limits<range_size_t<_Ty>>::max)();
template <sized_range _Ty>
requires requires { typename _Require_constant<_Ty::size()>; }
inline constexpr auto _Compile_time_max_size<_Ty> = _Ty::size();
template <class _Ty, size_t _Size>
inline constexpr auto _Compile_time_max_size<_Ty[_Size]> = _Size;
template <class _Ty, size_t _Size>
inline constexpr auto _Compile_time_max_size<array<_Ty, _Size>> = _Size;
template <class _Ty, size_t _Size>
inline constexpr auto _Compile_time_max_size<const array<_Ty, _Size>> = _Size;
template <class _Ty, size_t _Extent>
requires (_Extent != dynamic_extent)
inline constexpr auto _Compile_time_max_size<span<_Ty, _Extent>> = _Extent;
template <class _Ty, size_t _Extent>
requires (_Extent != dynamic_extent)
inline constexpr auto _Compile_time_max_size<const span<_Ty, _Extent>> = _Extent;
#endif // _HAS_CXX23
// clang-format off
_EXPORT_STD template <class _Rng>
concept viewable_range = range<_Rng>
&& ((view<remove_cvref_t<_Rng>> && constructible_from<remove_cvref_t<_Rng>, _Rng>)
|| (!view<remove_cvref_t<_Rng>>
&& (is_lvalue_reference_v<_Rng>
|| (movable<remove_reference_t<_Rng>> && !_Is_initializer_list<_Rng>))));
template <class _Rng>
concept _Simple_view = view<_Rng> && range<const _Rng>
&& same_as<iterator_t<_Rng>, iterator_t<const _Rng>>
&& same_as<sentinel_t<_Rng>, sentinel_t<const _Rng>>;
// clang-format on
template <class _Ty>
concept _Valid_movable_box_object =
#if _HAS_CXX23
move_constructible<_Ty>
#else // ^^^ C++23 / C++20 vvv
copy_constructible<_Ty>
#endif // C++20
&& _Destructible_object<_Ty>;
template <class _It>
concept _Has_arrow = input_iterator<_It> && (is_pointer_v<_It> || _Has_member_arrow<_It&>);
template <bool _IsConst, class _Ty>
using _Maybe_const = conditional_t<_IsConst, const _Ty, _Ty>;
template <bool _IsWrapped, class _Ty>
using _Maybe_wrapped = conditional_t<_IsWrapped, _Ty, _Unwrapped_t<_Ty>>;
namespace _Pipe {
template <class _Derived>
struct _Base {};
template <class _Ty>
_Ty* _Derived_from_range_adaptor_closure(_Base<_Ty>&); // not defined
// clang-format off
template <class _Ty>
concept _Range_adaptor_closure_object = !range<remove_cvref_t<_Ty>> && requires(remove_cvref_t<_Ty>& __t) {
{ _Pipe::_Derived_from_range_adaptor_closure(__t) } -> same_as<remove_cvref_t<_Ty>*>;
};
// clang-format on
template <class _ClosureLeft, class _ClosureRight>
struct _Pipeline : _Base<_Pipeline<_ClosureLeft, _ClosureRight>> {
_STL_INTERNAL_STATIC_ASSERT(_Range_adaptor_closure_object<_ClosureLeft>);
_STL_INTERNAL_STATIC_ASSERT(_Range_adaptor_closure_object<_ClosureRight>);
/* [[no_unique_address]] */ _ClosureLeft _Left;
/* [[no_unique_address]] */ _ClosureRight _Right;
template <class _Ty1, class _Ty2>
constexpr explicit _Pipeline(_Ty1&& _Val1, _Ty2&& _Val2) noexcept(
is_nothrow_constructible_v<_Ty1, _ClosureLeft>&& is_nothrow_constructible_v<_Ty2, _ClosureRight>)
: _Left(_STD forward<_Ty1>(_Val1)), _Right(_STD forward<_Ty2>(_Val2)) {}
template <class _Ty>
_NODISCARD constexpr auto operator()(_Ty&& _Val) & noexcept(
noexcept(_Right(_Left(_STD forward<_Ty>(_Val)))))
requires requires { _Right(_Left(_STD forward<_Ty>(_Val))); }
{
return _Right(_Left(_STD forward<_Ty>(_Val)));
}
template <class _Ty>
_NODISCARD constexpr auto operator()(_Ty&& _Val) const& noexcept(
noexcept(_Right(_Left(_STD forward<_Ty>(_Val)))))
requires requires { _Right(_Left(_STD forward<_Ty>(_Val))); }
{
return _Right(_Left(_STD forward<_Ty>(_Val)));
}
template <class _Ty>
_NODISCARD constexpr auto operator()(_Ty&& _Val) && noexcept(
noexcept(_STD move(_Right)(_STD move(_Left)(_STD forward<_Ty>(_Val)))))
requires requires { _STD move(_Right)(_STD move(_Left)(_STD forward<_Ty>(_Val))); }
{
return _STD move(_Right)(_STD move(_Left)(_STD forward<_Ty>(_Val)));
}
template <class _Ty>
_NODISCARD constexpr auto operator()(_Ty&& _Val) const&& noexcept(
noexcept(_STD move(_Right)(_STD move(_Left)(_STD forward<_Ty>(_Val)))))
requires requires { _STD move(_Right)(_STD move(_Left)(_STD forward<_Ty>(_Val))); }
{
return _STD move(_Right)(_STD move(_Left)(_STD forward<_Ty>(_Val)));
}
};
template <class _Ty1, class _Ty2>
_Pipeline(_Ty1, _Ty2) -> _Pipeline<_Ty1, _Ty2>;
_EXPORT_STD template <class _Left, class _Right>
requires _Range_adaptor_closure_object<_Left> && _Range_adaptor_closure_object<_Right>
&& constructible_from<remove_cvref_t<_Left>, _Left>
&& constructible_from<remove_cvref_t<_Right>, _Right>
_NODISCARD constexpr auto operator|(_Left&& __l, _Right&& __r) noexcept(
noexcept(_Pipeline{static_cast<_Left&&>(__l), static_cast<_Right&&>(__r)})) {
return _Pipeline{static_cast<_Left&&>(__l), static_cast<_Right&&>(__r)};
}
_EXPORT_STD template <class _Left, class _Right>
requires (_Range_adaptor_closure_object<_Right> && range<_Left>)
_NODISCARD constexpr auto operator|(_Left&& __l, _Right&& __r) noexcept(
noexcept(_STD forward<_Right>(__r)(_STD forward<_Left>(__l))))
requires requires { static_cast<_Right&&>(__r)(static_cast<_Left&&>(__l)); }
{
return _STD forward<_Right>(__r)(_STD forward<_Left>(__l));
}
} // namespace _Pipe
template <range _Rng, class _Derived>
class _Cached_position : public view_interface<_Derived> {
static_assert(_Always_false<_Rng>, "A range must be at least forward for position caching to be worthwhile.");
};
template <forward_range _Rng, class _Derived>
class _Cached_position<_Rng, _Derived> : public view_interface<_Derived> {
private:
using _It = iterator_t<_Rng>;
/* [[no_unique_address]] */ _It _Pos{};
bool _Cached = false;
protected:
_Cached_position() = default;
~_Cached_position() = default;
// a copied iterator doesn't point into a copied range, so cache values must not propagate via copy
constexpr _Cached_position(const _Cached_position&) noexcept(is_nothrow_default_constructible_v<_It>) {}
constexpr _Cached_position& operator=(const _Cached_position&) noexcept(noexcept(_Pos = _It{})) {
_Pos = _It{};
_Cached = false;
return *this;
}
// a moved iterator doesn't point into a moved range, so cache values must not propagate via move;
// similarly, a cache value might not be valid for a moved-from view so clear move sources
constexpr _Cached_position(_Cached_position&& _Other) noexcept(noexcept(_Pos = _It{})) {
_Other._Pos = _It{};
_Other._Cached = false;
}
constexpr _Cached_position& operator=(_Cached_position&& _Other) noexcept(noexcept(_Pos = _It{})) {
_Pos = _It{};
_Cached = false;
_Other._Pos = _It{};
_Other._Cached = false;
return *this;
}
_NODISCARD constexpr bool _Has_cache() const noexcept { // Is there a cached position?
return _Cached;
}
_NODISCARD constexpr _It _Get_cache(_Rng&) const noexcept(is_nothrow_copy_constructible_v<_It>) {
_STL_INTERNAL_CHECK(_Cached);
return _Pos;
}
constexpr void _Set_cache(_Rng&, _It _Iter) noexcept(is_nothrow_move_assignable_v<_It>) {
_Pos = _STD move(_Iter);
_Cached = true;
}
};
template <random_access_range _Rng, class _Derived>
class _Cached_position<_Rng, _Derived> : public view_interface<_Derived> {
private:
using _It = iterator_t<_Rng>;
range_difference_t<_Rng> _Off = -1;
protected:
_Cached_position() = default;
~_Cached_position() = default;
// Offsets are oblivious to copying, so cache values _do_ propagate via copying.
_Cached_position(const _Cached_position&) = default;
_Cached_position& operator=(const _Cached_position&) = default;
// Offsets are potentially invalidated by move, so source caches are invalidated after move
constexpr _Cached_position(_Cached_position&& _Other) noexcept
: _Off(_STD exchange(_Other._Off, range_difference_t<_Rng>{-1})) {}
constexpr _Cached_position& operator=(_Cached_position&& _Other) noexcept {
_Off = _STD exchange(_Other._Off, range_difference_t<_Rng>{-1});
return *this;
}
_NODISCARD constexpr bool _Has_cache() const noexcept { // Is there a cached position?
return _Off >= range_difference_t<_Rng>{0};
}
_NODISCARD constexpr _It _Get_cache(_Rng& _Range) const noexcept(noexcept(_RANGES begin(_Range) + _Off)) {
_STL_INTERNAL_CHECK(_Has_cache());
return _RANGES begin(_Range) + _Off;
}
constexpr void _Set_cache(_Rng& _Range, const _It& _Iter) noexcept(
noexcept(_Off = _Iter - _RANGES begin(_Range))) {
_Off = _Iter - _RANGES begin(_Range);
}
};
template <bool _Enable, class _Rng, class _Derived>
using _Cached_position_t = conditional_t<_Enable, _Cached_position<_Rng, _Derived>, view_interface<_Derived>>;
// A simplified optional that augments copy_constructible types with full copyability,
// and move_constructible types with full movability.
// In C++20, this corresponds to copyable-box.
template <_Valid_movable_box_object _Ty>
class _Movable_box {
public:
constexpr _Movable_box() noexcept(is_nothrow_default_constructible_v<_Ty>)
requires default_initializable<_Ty>
: _Val(), _Engaged{true} {}
template <class... _Types>
constexpr _Movable_box(in_place_t, _Types&&... _Args) noexcept(
is_nothrow_constructible_v<_Ty, _Types...>) // strengthened
: _Val(_STD forward<_Types>(_Args)...), _Engaged{true} {}
constexpr ~_Movable_box() {
if (_Engaged) {
_Val.~_Ty();
}
}
// clang-format off
~_Movable_box() requires is_trivially_destructible_v<_Ty> = default;
_Movable_box(const _Movable_box&)
requires copy_constructible<_Ty> && is_trivially_copy_constructible_v<_Ty> = default;
// clang-format on
constexpr _Movable_box(const _Movable_box& _That)
requires copy_constructible<_Ty>
: _Engaged{_That._Engaged} {
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<const _Ty&>(_That._Val));
}
}
// clang-format off
_Movable_box(_Movable_box&&) requires is_trivially_move_constructible_v<_Ty> = default;
// clang-format on
constexpr _Movable_box(_Movable_box&& _That) : _Engaged{_That._Engaged} {
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<_Ty&&>(_That._Val));
}
}
// clang-format off
_Movable_box& operator=(const _Movable_box&) noexcept
requires copyable<_Ty> && is_trivially_copy_assignable_v<_Ty> = default;
// clang-format on
constexpr _Movable_box& operator=(const _Movable_box& _That) noexcept(
is_nothrow_copy_constructible_v<_Ty>&& is_nothrow_copy_assignable_v<_Ty>) // strengthened
requires copyable<_Ty>
{
if (_Engaged) {
if (_That._Engaged) {
static_cast<_Ty&>(_Val) = static_cast<const _Ty&>(_That._Val);
} else {
_Val.~_Ty();
_Engaged = false;
}
} else {
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<const _Ty&>(_That._Val));
_Engaged = true;
} else {
// nothing to do
}
}
return *this;
}
constexpr _Movable_box& operator=(const _Movable_box& _That) noexcept(is_nothrow_copy_constructible_v<_Ty>)
requires copy_constructible<_Ty>
{
if (_STD addressof(_That) == this) {
return *this;
}
if (_Engaged) {
_Val.~_Ty();
_Engaged = false;
}
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<const _Ty&>(_That._Val));
_Engaged = true;
}
return *this;
}
// clang-format off
_Movable_box& operator=(_Movable_box&&) noexcept
requires movable<_Ty> && is_trivially_move_assignable_v<_Ty> = default;
// clang-format on
constexpr _Movable_box& operator=(_Movable_box&& _That) noexcept(
is_nothrow_move_constructible_v<_Ty>&& is_nothrow_move_assignable_v<_Ty>) // strengthened
requires movable<_Ty>
{
if (_Engaged) {
if (_That._Engaged) {
static_cast<_Ty&>(_Val) = static_cast<_Ty&&>(_That._Val);
} else {
_Val.~_Ty();
_Engaged = false;
}
} else {
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<_Ty&&>(_That._Val));
_Engaged = true;
} else {
// nothing to do
}
}
return *this;
}
constexpr _Movable_box& operator=(_Movable_box&& _That) noexcept(is_nothrow_move_constructible_v<_Ty>) {
if (_STD addressof(_That) == this) {
return *this;
}
if (_Engaged) {
_Val.~_Ty();
_Engaged = false;
}
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<_Ty&&>(_That._Val));
_Engaged = true;
}
return *this;
}
constexpr explicit operator bool() const noexcept {
return _Engaged;
}
_NODISCARD constexpr _Ty& operator*() noexcept {
_STL_INTERNAL_CHECK(_Engaged);
return _Val;
}
_NODISCARD constexpr const _Ty& operator*() const noexcept {
_STL_INTERNAL_CHECK(_Engaged);
return _Val;
}
private:
union {
remove_cv_t<_Ty> _Val;
};
bool _Engaged;
};
// [range.move.wrap]
template <class _Ty>
concept _Use_simple_movable_box_wrapper =
(copy_constructible<_Ty>
// 1. If copy_constructible<T> is true, movable-box<T> should store only a T if either T models
// copyable, or is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T> is true.
? copyable<_Ty> || (is_nothrow_move_constructible_v<_Ty> && is_nothrow_copy_constructible_v<_Ty>)
// 2. Otherwise, movable-box<T> should store only a T if either T models movable or
// is_nothrow_move_constructible_v<T> is true.
: movable<_Ty> || is_nothrow_move_constructible_v<_Ty>);
template <class _Ty>
concept _Copy_constructible_for_box = is_copy_constructible_v<_Ty>;
template <_Valid_movable_box_object _Ty>
requires _Use_simple_movable_box_wrapper<_Ty>
class _Movable_box<_Ty> { // provide the same API more efficiently when we can avoid the disengaged state
public:
// clang-format off
_Movable_box() requires default_initializable<_Ty> = default;
// clang-format on
template <class... _Types>
constexpr _Movable_box(in_place_t, _Types&&... _Args) noexcept(
is_nothrow_constructible_v<_Ty, _Types...>) // strengthened
: _Val(_STD forward<_Types>(_Args)...) {}
// clang-format off
_Movable_box(const _Movable_box&)
requires _Copy_constructible_for_box<_Ty> && is_trivially_copy_constructible_v<_Ty> = default;
_Movable_box(_Movable_box&&) requires is_trivially_move_constructible_v<_Ty> = default;
// clang-format on
constexpr _Movable_box(const _Movable_box& _That) noexcept(is_nothrow_copy_constructible_v<_Ty>)
requires _Copy_constructible_for_box<_Ty>
: _Val(static_cast<const _Ty&>(_That._Val)) {}
constexpr _Movable_box(_Movable_box&& _That) noexcept(is_nothrow_move_constructible_v<_Ty>)
: _Val(static_cast<_Ty&&>(_That._Val)) {}
// clang-format off
_Movable_box& operator=(const _Movable_box&)
requires copyable<_Ty> && is_trivially_copy_assignable_v<_Ty> = default;
_Movable_box& operator=(_Movable_box&&) requires movable<_Ty> && is_trivially_move_assignable_v<_Ty> = default;
// clang-format on
constexpr _Movable_box& operator=(const _Movable_box& _That) noexcept(
is_nothrow_copy_assignable_v<_Ty> || !copyable<_Ty>) // strengthened
requires copy_constructible<_Ty>
{
if constexpr (copyable<_Ty>) {
static_cast<_Ty&>(_Val) = static_cast<const _Ty&>(_That._Val);
} else {
if (_STD addressof(_That) != this) {
_Val.~_Ty();
_STD _Construct_in_place(_Val, static_cast<const _Ty&>(_That._Val));
}
}
return *this;
}
constexpr _Movable_box& operator=(_Movable_box&& _That) noexcept(
is_nothrow_move_assignable_v<_Ty> || !movable<_Ty>) /* strengthened */ {
if constexpr (movable<_Ty>) {
static_cast<_Ty&>(_Val) = static_cast<_Ty&&>(_That._Val);
} else {
if (_STD addressof(_That) != this) {
_Val.~_Ty();
_STD _Construct_in_place(_Val, static_cast<_Ty&&>(_That._Val));
}
}
return *this;
}
constexpr explicit operator bool() const noexcept {
return true;
}
_NODISCARD constexpr _Ty& operator*() noexcept {
return _Val;
}
_NODISCARD constexpr const _Ty& operator*() const noexcept {
return _Val;
}
private:
/* [[no_unique_address]] */ remove_cv_t<_Ty> _Val{};
};
template <movable _Ty>
class _Defaultabox { // a simplified optional that augments movable types with default-constructibility
public:
constexpr _Defaultabox() noexcept {}
constexpr ~_Defaultabox() {
if (_Engaged) {
_Val.~_Ty();
}
}
// clang-format off
~_Defaultabox() requires is_trivially_destructible_v<_Ty> = default;
_Defaultabox(const _Defaultabox&)
requires copy_constructible<_Ty> && is_trivially_copy_constructible_v<_Ty> = default;
// clang-format on
constexpr _Defaultabox(const _Defaultabox& _That) noexcept(is_nothrow_copy_constructible_v<_Ty>)
requires copy_constructible<_Ty>
: _Engaged{_That._Engaged} {
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<const _Ty&>(_That._Val));
}
}
// clang-format off
_Defaultabox(_Defaultabox&&) requires is_trivially_move_constructible_v<_Ty> = default;
// clang-format on
constexpr _Defaultabox(_Defaultabox&& _That) noexcept(is_nothrow_move_constructible_v<_Ty>)
: _Engaged{_That._Engaged} {
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<_Ty&&>(_That._Val));
}
}
template <_Different_from<_Ty> _Uty>
requires convertible_to<const _Uty&, _Ty>
constexpr _Defaultabox(const _Defaultabox<_Uty>& _That) noexcept(is_nothrow_constructible_v<_Ty, const _Uty&>)
: _Engaged{_That} {
if (_That) {
_STD _Construct_in_place(_Val, *_That);
}
}
template <_Different_from<_Ty> _Uty>
requires convertible_to<_Uty, _Ty>
constexpr _Defaultabox(_Defaultabox<_Uty>&& _That) noexcept(is_nothrow_constructible_v<_Ty, _Uty>)
: _Engaged{_That} {
if (_That) {
_STD _Construct_in_place(_Val, _STD move(*_That));
}
}
// clang-format off
_Defaultabox& operator=(const _Defaultabox&) noexcept
requires copyable<_Ty> && is_trivially_copy_assignable_v<_Ty> = default;
// clang-format on
constexpr _Defaultabox& operator=(const _Defaultabox& _That) noexcept(
is_nothrow_copy_constructible_v<_Ty>&& is_nothrow_copy_assignable_v<_Ty>)
requires copyable<_Ty>
{
if (_Engaged) {
if (_That._Engaged) {
static_cast<_Ty&>(_Val) = static_cast<const _Ty&>(_That._Val);
} else {
_Val.~_Ty();
_Engaged = false;
}
} else {
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<const _Ty&>(_That._Val));
_Engaged = true;
} else {
// nothing to do
}
}
return *this;
}
// clang-format off
_Defaultabox& operator=(_Defaultabox&&) noexcept requires is_trivially_move_assignable_v<_Ty> = default;
// clang-format on
constexpr _Defaultabox& operator=(_Defaultabox&& _That) noexcept(
is_nothrow_move_constructible_v<_Ty>&& is_nothrow_move_assignable_v<_Ty>) {
if (_Engaged) {
if (_That._Engaged) {
static_cast<_Ty&>(_Val) = static_cast<_Ty&&>(_That._Val);
} else {
_Val.~_Ty();
_Engaged = false;
}
} else {
if (_That._Engaged) {
_STD _Construct_in_place(_Val, static_cast<_Ty&&>(_That._Val));
_Engaged = true;
} else {
// nothing to do
}
}
return *this;
}
constexpr _Defaultabox& operator=(_Ty&& _That) noexcept(
is_nothrow_move_constructible_v<_Ty>&& is_nothrow_move_assignable_v<_Ty>) {
if (_Engaged) {
static_cast<_Ty&>(_Val) = _STD move(_That);
} else {
_STD _Construct_in_place(_Val, _STD move(_That));
_Engaged = true;
}
return *this;
}
constexpr _Defaultabox& operator=(const _Ty& _That) noexcept(
is_nothrow_copy_constructible_v<_Ty>&& is_nothrow_copy_assignable_v<_Ty>)
requires copyable<_Ty>
{
if (_Engaged) {
static_cast<_Ty&>(_Val) = _That;
} else {
_STD _Construct_in_place(_Val, _That);
_Engaged = true;
}
return *this;
}
constexpr explicit operator bool() const noexcept {
return _Engaged;
}
_NODISCARD constexpr _Ty& operator*() noexcept {
_STL_INTERNAL_CHECK(_Engaged);
return _Val;
}
_NODISCARD constexpr const _Ty& operator*() const noexcept {
_STL_INTERNAL_CHECK(_Engaged);
return _Val;
}
constexpr void _Reset() noexcept {
if (_Engaged) {
_Val.~_Ty();
_Engaged = false;
}
}
_NODISCARD constexpr bool operator==(const _Defaultabox& _That) const
noexcept(noexcept(static_cast<const _Ty&>(_Val) == static_cast<const _Ty&>(_That._Val))) {
_STL_INTERNAL_STATIC_ASSERT(equality_comparable<_Ty>);
return _Engaged == _That._Engaged
&& (!_Engaged || static_cast<const _Ty&>(_Val) == static_cast<const _Ty&>(_That._Val));
}
private:
union {
remove_cv_t<_Ty> _Val;
};
bool _Engaged = false;
};
template <movable _Ty>
requires default_initializable<_Ty>
class _Defaultabox<_Ty> { // provide the same API more efficiently for default-constructible types
public:
_Defaultabox() = default;
template <_Different_from<_Ty> _Uty>
requires convertible_to<const _Uty&, _Ty>
constexpr _Defaultabox(const _Defaultabox<_Uty>& _That) noexcept(
is_nothrow_default_constructible_v<_Ty>&& noexcept(_Value = static_cast<_Ty>(*_That))) {
if (_That) {
_Value = static_cast<_Ty>(*_That);
}
}
template <_Different_from<_Ty> _Uty>
requires convertible_to<_Uty, _Ty>
constexpr _Defaultabox(_Defaultabox<_Uty>&& _That) noexcept(
is_nothrow_default_constructible_v<_Ty>&& noexcept(_Value = static_cast<_Ty>(_STD move(*_That)))) {
if (_That) {
_Value = static_cast<_Ty>(_STD move(*_That));
}
}
constexpr _Defaultabox& operator=(const _Ty& _Right) noexcept(is_nothrow_copy_assignable_v<_Ty>)
requires copyable<_Ty>
{
_Value = _Right;
return *this;
}
constexpr _Defaultabox& operator=(_Ty&& _Right) noexcept(is_nothrow_move_assignable_v<_Ty>) {
_Value = _STD move(_Right);
return *this;
}
constexpr explicit operator bool() const noexcept {
return true;
}
_NODISCARD constexpr _Ty& operator*() noexcept {
return _Value;
}
_NODISCARD constexpr const _Ty& operator*() const noexcept {
return _Value;
}
constexpr void _Reset() noexcept(noexcept(_Value = _Ty{})) {
_Value = _Ty{};
}
_NODISCARD bool operator==(const _Defaultabox&) const = default;
private:
/* [[no_unique_address]] */ _Ty _Value{};
};
template <_Destructible_object _Ty, bool _Needs_operator_bool = true>
class _Non_propagating_cache { // a simplified optional that resets on copy / move
public:
constexpr _Non_propagating_cache() noexcept {}
constexpr ~_Non_propagating_cache() {
if (_Engaged) {
_Val.~_Ty();
}
}
// clang-format off
~_Non_propagating_cache() requires is_trivially_destructible_v<_Ty> = default;
// clang-format on
constexpr _Non_propagating_cache(const _Non_propagating_cache&) noexcept {}
constexpr _Non_propagating_cache(_Non_propagating_cache&& _Other) noexcept {
if (_Other._Engaged) {
_Other._Val.~_Ty();
_Other._Engaged = false;
}
}
constexpr _Non_propagating_cache& operator=(const _Non_propagating_cache& _Other) noexcept {
if (_STD addressof(_Other) == this) {
return *this;
}
if (_Engaged) {
_Val.~_Ty();
_Engaged = false;
}
return *this;
}
constexpr _Non_propagating_cache& operator=(_Non_propagating_cache&& _Other) noexcept {
if (_Engaged) {
_Val.~_Ty();
_Engaged = false;
}
if (_Other._Engaged) {
_Other._Val.~_Ty();
_Other._Engaged = false;
}
return *this;
}
_NODISCARD constexpr explicit operator bool() const noexcept
requires _Needs_operator_bool
{
return _Engaged;
}
_NODISCARD constexpr _Ty& operator*() noexcept {
_STL_INTERNAL_CHECK(_Engaged);
return _Val;
}
_NODISCARD constexpr const _Ty& operator*() const noexcept {
_STL_INTERNAL_CHECK(_Engaged);
return _Val;
}
template <class... _Types>
constexpr _Ty& _Emplace(_Types&&... _Args) noexcept(is_nothrow_constructible_v<_Ty, _Types...>) {
if (_Engaged) {
_Val.~_Ty();
_Engaged = false;
}
_STD _Construct_in_place(_Val, _STD forward<_Types>(_Args)...);
_Engaged = true;
return _Val;
}
private:
union {
remove_cv_t<_Ty> _Val;
};
bool _Engaged = false;
};
template <_Destructible_object _Ty>
requires is_trivially_destructible_v<_Ty>
class _Non_propagating_cache<_Ty, false> { // a specialization for trivially destructible types where checking if
// the cache contains a value is not needed
public:
constexpr _Non_propagating_cache() noexcept {}
~_Non_propagating_cache() = default;
constexpr _Non_propagating_cache(const _Non_propagating_cache&) noexcept {}
constexpr _Non_propagating_cache(_Non_propagating_cache&&) noexcept {}
constexpr _Non_propagating_cache& operator=(const _Non_propagating_cache&) noexcept {
return *this;
}
constexpr _Non_propagating_cache& operator=(_Non_propagating_cache&&) noexcept {
return *this;
}
_NODISCARD constexpr _Ty& operator*() noexcept {
return _Val;
}
_NODISCARD constexpr const _Ty& operator*() const noexcept {
return _Val;
}
template <class... _Types>
constexpr _Ty& _Emplace(_Types&&... _Args) noexcept(is_nothrow_constructible_v<_Ty, _Types...>) {
_STD _Construct_in_place(_Val, _STD forward<_Types>(_Args)...);
return _Val;
}
private:
union {
remove_cv_t<_Ty> _Val;
};
};
#if _HAS_CXX23
_EXPORT_STD template <class _Derived>
requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
class range_adaptor_closure : public _Pipe::_Base<_Derived> {};
#endif // _HAS_CXX23
template <class _Fn, class... _Types>
class _Range_closure : public _Pipe::_Base<_Range_closure<_Fn, _Types...>> {
public:
// We assume that _Fn is the type of a customization point object. That means
// 1. The behavior of operator() is independent of cvref qualifiers, so we can use `invocable<_Fn, ` without
// loss of generality, and
// 2. _Fn must be default-constructible and stateless, so we can create instances "on-the-fly" and avoid
// storing a copy.
_STL_INTERNAL_STATIC_ASSERT((same_as<decay_t<_Types>, _Types> && ...));
_STL_INTERNAL_STATIC_ASSERT(is_empty_v<_Fn>&& is_default_constructible_v<_Fn>);
template <class... _UTypes>
requires (same_as<decay_t<_UTypes>, _Types> && ...)
constexpr explicit _Range_closure(_UTypes&&... _Args) noexcept(
conjunction_v<is_nothrow_constructible<_Types, _UTypes>...>)
: _Captures(_STD forward<_UTypes>(_Args)...) {}
void operator()(auto&&) & = delete;
void operator()(auto&&) const& = delete;
void operator()(auto&&) && = delete;
void operator()(auto&&) const&& = delete;
using _Indices = index_sequence_for<_Types...>;
template <class _Ty>
requires invocable<_Fn, _Ty, _Types&...>
constexpr decltype(auto) operator()(_Ty&& _Arg) & noexcept(
noexcept(_Call(*this, _STD forward<_Ty>(_Arg), _Indices{}))) {
return _Call(*this, _STD forward<_Ty>(_Arg), _Indices{});
}
template <class _Ty>
requires invocable<_Fn, _Ty, const _Types&...>
constexpr decltype(auto) operator()(_Ty&& _Arg) const& noexcept(
noexcept(_Call(*this, _STD forward<_Ty>(_Arg), _Indices{}))) {
return _Call(*this, _STD forward<_Ty>(_Arg), _Indices{});
}
template <class _Ty>
requires invocable<_Fn, _Ty, _Types...>
constexpr decltype(auto) operator()(_Ty&& _Arg) && noexcept(
noexcept(_Call(_STD move(*this), _STD forward<_Ty>(_Arg), _Indices{}))) {
return _Call(_STD move(*this), _STD forward<_Ty>(_Arg), _Indices{});
}
template <class _Ty>
requires invocable<_Fn, _Ty, const _Types...>
constexpr decltype(auto) operator()(_Ty&& _Arg) const&& noexcept(
noexcept(_Call(_STD move(*this), _STD forward<_Ty>(_Arg), _Indices{}))) {
return _Call(_STD move(*this), _STD forward<_Ty>(_Arg), _Indices{});
}
private:
template <class _SelfTy, class _Ty, size_t... _Idx>
static constexpr decltype(auto) _Call(_SelfTy&& _Self, _Ty&& _Arg, index_sequence<_Idx...>) noexcept(
noexcept(_Fn{}(_STD forward<_Ty>(_Arg), _STD get<_Idx>(_STD forward<_SelfTy>(_Self)._Captures)...))) {
_STL_INTERNAL_STATIC_ASSERT(same_as<index_sequence<_Idx...>, _Indices>);
return _Fn{}(_STD forward<_Ty>(_Arg), _STD get<_Idx>(_STD forward<_SelfTy>(_Self)._Captures)...);
}
tuple<_Types...> _Captures;
};
_EXPORT_STD template <class _Ty>
requires is_object_v<_Ty>
class empty_view : public view_interface<empty_view<_Ty>> {
public:
_NODISCARD static constexpr _Ty* begin() noexcept {
return nullptr;
}
_NODISCARD static constexpr _Ty* end() noexcept {
return nullptr;
}
_NODISCARD static constexpr _Ty* data() noexcept {
return nullptr;
}
_NODISCARD static constexpr size_t size() noexcept {
return 0;
}
_NODISCARD static constexpr bool empty() noexcept {
return true;
}
};
template <class _Ty>
inline constexpr bool enable_borrowed_range<empty_view<_Ty>> = true;
namespace views {
_EXPORT_STD template <class _Ty>
inline constexpr empty_view<_Ty> empty;
} // namespace views
_EXPORT_STD template <_Valid_movable_box_object _Ty>
class single_view : public view_interface<single_view<_Ty>> {
public:
// clang-format off
single_view() requires default_initializable<_Ty> = default;
// clang-format on
constexpr explicit single_view(const _Ty& _Val_) noexcept(is_nothrow_copy_constructible_v<_Ty>) // strengthened