-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
xutility
7289 lines (6232 loc) · 305 KB
/
xutility
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
// xutility internal header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#ifndef _XUTILITY_
#define _XUTILITY_
#include <yvals.h>
#if _STL_COMPILER_PREPROCESSOR
#include <__msvc_iter_core.hpp>
#include <climits>
#include <cstdlib>
#include <cstring>
#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
#if defined(_CRTBLD) && defined(CRTDLL2)
// TRANSITION, ABI: The vector algorithms are compiled into the import lib, so we disable their usage when building
// the DLL. (We could additionally link them into the DLL - not as exports, just for internal usage - but we
// haven't chosen to do that yet.) When we can break ABI and export the vector algorithms from the DLL,
// this preprocessor case should be removed.
#ifndef _USE_STD_VECTOR_ALGORITHMS
#define _USE_STD_VECTOR_ALGORITHMS 0
#elif _USE_STD_VECTOR_ALGORITHMS
#error Vector algorithms are not supported when building msvcp140.dll, but _USE_STD_VECTOR_ALGORITHMS is set.
#endif // _USE_STD_VECTOR_ALGORITHMS
#elif (defined(_M_IX86) || defined(_M_X64)) && !defined(_M_CEE_PURE) && !defined(_M_HYBRID) && !defined(_M_ARM64EC)
#ifndef _USE_STD_VECTOR_ALGORITHMS
#define _USE_STD_VECTOR_ALGORITHMS 1
#endif // _USE_STD_VECTOR_ALGORITHMS
#else // ^^^ arch supports vector algorithms / no support for vector algorithms vvv
#ifndef _USE_STD_VECTOR_ALGORITHMS
#define _USE_STD_VECTOR_ALGORITHMS 0
#elif _USE_STD_VECTOR_ALGORITHMS
#error Vector algorithms are not supported on this architecture, but _USE_STD_VECTOR_ALGORITHMS is set.
#endif // _USE_STD_VECTOR_ALGORITHMS
#endif // ^^^ no support for vector algorithms ^^^
#if _USE_STD_VECTOR_ALGORITHMS
_EXTERN_C
// The "noalias" attribute tells the compiler optimizer that pointers going into these hand-vectorized algorithms
// won't be stored beyond the lifetime of the function, and that the function will only reference arrays denoted by
// those pointers. The optimizer also assumes in that case that a pointer parameter is not returned to the caller via
// the return value, so functions using "noalias" must usually return void. This attribute is valuable because these
// functions are in native code objects that the compiler cannot analyze. In the absence of the noalias attribute, the
// compiler has to assume that the denoted arrays are "globally address taken", and that any later calls to
// unanalyzable routines may modify those arrays.
__declspec(noalias) void __cdecl __std_reverse_trivially_swappable_1(void* _First, void* _Last) noexcept;
__declspec(noalias) void __cdecl __std_reverse_trivially_swappable_2(void* _First, void* _Last) noexcept;
__declspec(noalias) void __cdecl __std_reverse_trivially_swappable_4(void* _First, void* _Last) noexcept;
__declspec(noalias) void __cdecl __std_reverse_trivially_swappable_8(void* _First, void* _Last) noexcept;
__declspec(noalias) void __cdecl __std_swap_ranges_trivially_swappable_noalias(
void* _First1, void* _Last1, void* _First2) noexcept;
__declspec(noalias) size_t
__stdcall __std_count_trivial_1(const void* _First, const void* _Last, uint8_t _Val) noexcept;
__declspec(noalias) size_t
__stdcall __std_count_trivial_2(const void* _First, const void* _Last, uint16_t _Val) noexcept;
__declspec(noalias) size_t
__stdcall __std_count_trivial_4(const void* _First, const void* _Last, uint32_t _Val) noexcept;
__declspec(noalias) size_t
__stdcall __std_count_trivial_8(const void* _First, const void* _Last, uint64_t _Val) noexcept;
const void* __stdcall __std_find_trivial_1(const void* _First, const void* _Last, uint8_t _Val) noexcept;
const void* __stdcall __std_find_trivial_2(const void* _First, const void* _Last, uint16_t _Val) noexcept;
const void* __stdcall __std_find_trivial_4(const void* _First, const void* _Last, uint32_t _Val) noexcept;
const void* __stdcall __std_find_trivial_8(const void* _First, const void* _Last, uint64_t _Val) noexcept;
const void* __stdcall __std_find_trivial_unsized_1(const void* _First, uint8_t _Val) noexcept;
const void* __stdcall __std_find_trivial_unsized_2(const void* _First, uint16_t _Val) noexcept;
const void* __stdcall __std_find_trivial_unsized_4(const void* _First, uint32_t _Val) noexcept;
const void* __stdcall __std_find_trivial_unsized_8(const void* _First, uint64_t _Val) noexcept;
const void* __stdcall __std_min_element_1(const void* _First, const void* _Last, bool _Signed) noexcept;
const void* __stdcall __std_min_element_2(const void* _First, const void* _Last, bool _Signed) noexcept;
const void* __stdcall __std_min_element_4(const void* _First, const void* _Last, bool _Signed) noexcept;
const void* __stdcall __std_min_element_8(const void* _First, const void* _Last, bool _Signed) noexcept;
const void* __stdcall __std_max_element_1(const void* _First, const void* _Last, bool _Signed) noexcept;
const void* __stdcall __std_max_element_2(const void* _First, const void* _Last, bool _Signed) noexcept;
const void* __stdcall __std_max_element_4(const void* _First, const void* _Last, bool _Signed) noexcept;
const void* __stdcall __std_max_element_8(const void* _First, const void* _Last, bool _Signed) noexcept;
_END_EXTERN_C
template <class _Ty, class _TVal>
__declspec(noalias) size_t __std_count_trivial(_Ty* _First, _Ty* _Last, const _TVal _Val) noexcept {
if constexpr (_STD is_pointer_v<_TVal> || _STD is_null_pointer_v<_TVal>) {
return __std_count_trivial(_First, _Last, reinterpret_cast<uintptr_t>(_Val));
} else if constexpr (sizeof(_Ty) == 1) {
return __std_count_trivial_1(_First, _Last, static_cast<uint8_t>(_Val));
} else if constexpr (sizeof(_Ty) == 2) {
return __std_count_trivial_2(_First, _Last, static_cast<uint16_t>(_Val));
} else if constexpr (sizeof(_Ty) == 4) {
return __std_count_trivial_4(_First, _Last, static_cast<uint32_t>(_Val));
} else if constexpr (sizeof(_Ty) == 8) {
return __std_count_trivial_8(_First, _Last, static_cast<uint64_t>(_Val));
} else {
static_assert(_STD _Always_false<_Ty>, "Unexpected size");
}
}
template <class _Ty, class _TVal>
_Ty* __std_find_trivial(_Ty* _First, _Ty* _Last, const _TVal _Val) noexcept {
if constexpr (_STD is_pointer_v<_TVal> || _STD is_null_pointer_v<_TVal>) {
return __std_find_trivial(_First, _Last, reinterpret_cast<uintptr_t>(_Val));
} else if constexpr (sizeof(_Ty) == 1) {
return const_cast<_Ty*>(
static_cast<const _Ty*>(__std_find_trivial_1(_First, _Last, static_cast<uint8_t>(_Val))));
} else if constexpr (sizeof(_Ty) == 2) {
return const_cast<_Ty*>(
static_cast<const _Ty*>(__std_find_trivial_2(_First, _Last, static_cast<uint16_t>(_Val))));
} else if constexpr (sizeof(_Ty) == 4) {
return const_cast<_Ty*>(
static_cast<const _Ty*>(__std_find_trivial_4(_First, _Last, static_cast<uint32_t>(_Val))));
} else if constexpr (sizeof(_Ty) == 8) {
return const_cast<_Ty*>(
static_cast<const _Ty*>(__std_find_trivial_8(_First, _Last, static_cast<uint64_t>(_Val))));
} else {
static_assert(_STD _Always_false<_Ty>, "Unexpected size");
}
}
template <class _Ty, class _TVal>
_Ty* __std_find_trivial_unsized(_Ty* _First, const _TVal _Val) noexcept {
if constexpr (_STD is_pointer_v<_TVal> || _STD is_null_pointer_v<_TVal>) {
return __std_find_trivial_unsized(_First, reinterpret_cast<uintptr_t>(_Val));
} else if constexpr (sizeof(_Ty) == 1) {
return const_cast<_Ty*>(
static_cast<const _Ty*>(__std_find_trivial_unsized_1(_First, static_cast<uint8_t>(_Val))));
} else if constexpr (sizeof(_Ty) == 2) {
return const_cast<_Ty*>(
static_cast<const _Ty*>(__std_find_trivial_unsized_2(_First, static_cast<uint16_t>(_Val))));
} else if constexpr (sizeof(_Ty) == 4) {
return const_cast<_Ty*>(
static_cast<const _Ty*>(__std_find_trivial_unsized_4(_First, static_cast<uint32_t>(_Val))));
} else if constexpr (sizeof(_Ty) == 8) {
return const_cast<_Ty*>(
static_cast<const _Ty*>(__std_find_trivial_unsized_8(_First, static_cast<uint64_t>(_Val))));
} else {
static_assert(_STD _Always_false<_Ty>, "Unexpected size");
}
}
template <class _Ty>
_Ty* __std_min_element(_Ty* _First, _Ty* _Last) noexcept {
constexpr bool _Signed = _STD is_signed_v<_Ty>;
if constexpr (sizeof(_Ty) == 1) {
return const_cast<_Ty*>(static_cast<const _Ty*>(__std_min_element_1(_First, _Last, _Signed)));
} else if constexpr (sizeof(_Ty) == 2) {
return const_cast<_Ty*>(static_cast<const _Ty*>(__std_min_element_2(_First, _Last, _Signed)));
} else if constexpr (sizeof(_Ty) == 4) {
return const_cast<_Ty*>(static_cast<const _Ty*>(__std_min_element_4(_First, _Last, _Signed)));
} else if constexpr (sizeof(_Ty) == 8) {
return const_cast<_Ty*>(static_cast<const _Ty*>(__std_min_element_8(_First, _Last, _Signed)));
} else {
static_assert(_STD _Always_false<_Ty>, "Unexpected size");
}
}
template <class _Ty>
_Ty* __std_max_element(_Ty* _First, _Ty* _Last) noexcept {
constexpr bool _Signed = _STD is_signed_v<_Ty>;
if constexpr (sizeof(_Ty) == 1) {
return const_cast<_Ty*>(static_cast<const _Ty*>(__std_max_element_1(_First, _Last, _Signed)));
} else if constexpr (sizeof(_Ty) == 2) {
return const_cast<_Ty*>(static_cast<const _Ty*>(__std_max_element_2(_First, _Last, _Signed)));
} else if constexpr (sizeof(_Ty) == 4) {
return const_cast<_Ty*>(static_cast<const _Ty*>(__std_max_element_4(_First, _Last, _Signed)));
} else if constexpr (sizeof(_Ty) == 8) {
return const_cast<_Ty*>(static_cast<const _Ty*>(__std_max_element_8(_First, _Last, _Signed)));
} else {
static_assert(_STD _Always_false<_Ty>, "Unexpected size");
}
}
#endif // _USE_STD_VECTOR_ALGORITHMS
_STD_BEGIN
template <class _Ty>
struct _Get_first_parameter;
template <template <class, class...> class _Ty, class _First, class... _Rest>
struct _Get_first_parameter<_Ty<_First, _Rest...>> { // given _Ty<_First, _Rest...>, extract _First
using type = _First;
};
template <class _Newfirst, class _Ty>
struct _Replace_first_parameter;
template <class _Newfirst, template <class, class...> class _Ty, class _First, class... _Rest>
struct _Replace_first_parameter<_Newfirst, _Ty<_First, _Rest...>> { // given _Ty<_First, _Rest...>, replace _First
using type = _Ty<_Newfirst, _Rest...>;
};
template <class _Ty, class = void>
struct _Get_ptr_difference_type {
using type = ptrdiff_t;
};
template <class _Ty>
struct _Get_ptr_difference_type<_Ty, void_t<typename _Ty::difference_type>> {
using type = typename _Ty::difference_type;
};
template <class _Ty, class _Other, class = void>
struct _Get_rebind_alias {
using type = typename _Replace_first_parameter<_Other, _Ty>::type;
};
template <class _Ty, class _Other>
struct _Get_rebind_alias<_Ty, _Other, void_t<typename _Ty::template rebind<_Other>>> {
using type = typename _Ty::template rebind<_Other>;
};
#if _HAS_CXX20
#ifdef __EDG__ // TRANSITION, DevCom-1691516
// per LWG-3888
_EXPORT_STD template <class _Ty, class... _Types,
class = void_t<decltype(::new(static_cast<void*>(_STD declval<_Ty*>())) _Ty(_STD declval<_Types>()...))>>
#else // ^^^ no workaround / workaround vvv
// per LWG-3888
_EXPORT_STD template <class _Ty, class... _Types,
void_t<decltype(::new(static_cast<void*>(_STD declval<_Ty*>())) _Ty(_STD declval<_Types>()...))>* = nullptr>
#endif // TRANSITION, DevCom-1691516
constexpr _Ty* construct_at(_Ty* const _Location, _Types&&... _Args) noexcept(
noexcept(::new(static_cast<void*>(_Location)) _Ty(_STD forward<_Types>(_Args)...))) /* strengthened */ {
_MSVC_CONSTEXPR return ::new (static_cast<void*>(_Location)) _Ty(_STD forward<_Types>(_Args)...);
}
#endif // _HAS_CXX20
template <class _Ty, class... _Types>
_CONSTEXPR20 void _Construct_in_place(_Ty& _Obj, _Types&&... _Args) noexcept(
is_nothrow_constructible_v<_Ty, _Types...>) {
#if _HAS_CXX20
if (_STD is_constant_evaluated()) {
_STD construct_at(_STD addressof(_Obj), _STD forward<_Types>(_Args)...);
} else
#endif // _HAS_CXX20
{
::new (static_cast<void*>(_STD addressof(_Obj))) _Ty(_STD forward<_Types>(_Args)...);
}
}
template <class _Ty>
void _Default_construct_in_place(_Ty& _Obj) noexcept(is_nothrow_default_constructible_v<_Ty>) {
::new (static_cast<void*>(_STD addressof(_Obj))) _Ty;
}
template <class _Ty, class _Elem>
struct _Ptr_traits_base {
using pointer = _Ty;
using element_type = _Elem;
using difference_type = typename _Get_ptr_difference_type<_Ty>::type;
template <class _Other>
using rebind = typename _Get_rebind_alias<_Ty, _Other>::type;
using _Reftype = conditional_t<is_void_v<_Elem>, char, _Elem>&;
_NODISCARD static _CONSTEXPR20 pointer pointer_to(_Reftype _Val) noexcept(
noexcept(_Ty::pointer_to(_Val))) /* strengthened */ { // Per LWG-3454
return _Ty::pointer_to(_Val);
}
};
template <class, class = void, class = void>
struct _Ptr_traits_sfinae_layer {};
template <class _Ty, class _Uty>
struct _Ptr_traits_sfinae_layer<_Ty, _Uty, void_t<typename _Get_first_parameter<_Ty>::type>>
: _Ptr_traits_base<_Ty, typename _Get_first_parameter<_Ty>::type> {};
template <class _Ty>
struct _Ptr_traits_sfinae_layer<_Ty, void_t<typename _Ty::element_type>, void>
: _Ptr_traits_base<_Ty, typename _Ty::element_type> {};
_EXPORT_STD template <class _Ty>
struct pointer_traits : _Ptr_traits_sfinae_layer<_Ty> {};
template <class _Ty>
struct pointer_traits<_Ty*> {
using pointer = _Ty*;
using element_type = _Ty;
using difference_type = ptrdiff_t;
template <class _Other>
using rebind = _Other*;
using _Reftype = conditional_t<is_void_v<_Ty>, char, _Ty>&;
_NODISCARD static _CONSTEXPR20 pointer pointer_to(_Reftype _Val) noexcept {
return _STD addressof(_Val);
}
};
#if _HAS_CXX20
#ifdef __cpp_lib_concepts
template <class _Ty>
concept _Has_to_address = requires(const _Ty& _Val) {
typename pointer_traits<_Ty>;
pointer_traits<_Ty>::to_address(_Val);
};
#else // ^^^ Use concept / use variable template vvv
template <class _Ty, class = void>
inline constexpr bool _Has_to_address = false; // determines whether pointer_traits<_Ty> has to_address
template <class _Ty>
inline constexpr bool
_Has_to_address<_Ty, void_t<decltype(pointer_traits<_Ty>::to_address(_STD declval<const _Ty&>()))>> = true;
#endif // __cpp_lib_concepts
_EXPORT_STD template <class _Ty>
_NODISCARD constexpr _Ty* to_address(_Ty* const _Val) noexcept {
static_assert(!is_function_v<_Ty>,
"N4810 20.10.4 [pointer.conversion]/2: The program is ill-formed if T is a function type.");
return _Val;
}
_EXPORT_STD template <class _Ptr>
_NODISCARD constexpr auto to_address(const _Ptr& _Val) noexcept {
if constexpr (_Has_to_address<_Ptr>) {
return pointer_traits<_Ptr>::to_address(_Val);
} else {
return _STD to_address(_Val.operator->()); // plain pointer overload must come first
}
}
_EXPORT_STD struct identity {
template <class _Ty>
_NODISCARD constexpr _Ty&& operator()(_Ty&& _Left) const noexcept {
return _STD forward<_Ty>(_Left);
}
using is_transparent = int;
};
#endif // _HAS_CXX20
_EXPORT_STD template <class _Ty = void>
struct plus {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left + _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct minus {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left - _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct multiplies {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left * _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct equal_to {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = bool;
_NODISCARD constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const
noexcept(noexcept(_Fake_copy_init<bool>(_Left == _Right))) /* strengthened */ {
return _Left == _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct not_equal_to {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = bool;
_NODISCARD constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const
noexcept(noexcept(_Fake_copy_init<bool>(_Left != _Right))) /* strengthened */ {
return _Left != _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct greater {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = bool;
_NODISCARD constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const
noexcept(noexcept(_Fake_copy_init<bool>(_Left > _Right))) /* strengthened */ {
return _Left > _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct greater_equal {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = bool;
_NODISCARD constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const
noexcept(noexcept(_Fake_copy_init<bool>(_Left >= _Right))) /* strengthened */ {
return _Left >= _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct less_equal {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = bool;
_NODISCARD constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const
noexcept(noexcept(_Fake_copy_init<bool>(_Left <= _Right))) /* strengthened */ {
return _Left <= _Right;
}
};
template <>
struct plus<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
noexcept(noexcept(static_cast<_Ty1&&>(_Left) + static_cast<_Ty2&&>(_Right))) // strengthened
-> decltype(static_cast<_Ty1&&>(_Left) + static_cast<_Ty2&&>(_Right)) {
return static_cast<_Ty1&&>(_Left) + static_cast<_Ty2&&>(_Right);
}
using is_transparent = int;
};
template <>
struct minus<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
noexcept(noexcept(static_cast<_Ty1&&>(_Left) - static_cast<_Ty2&&>(_Right))) // strengthened
-> decltype(static_cast<_Ty1&&>(_Left) - static_cast<_Ty2&&>(_Right)) {
return static_cast<_Ty1&&>(_Left) - static_cast<_Ty2&&>(_Right);
}
using is_transparent = int;
};
template <>
struct multiplies<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
noexcept(noexcept(static_cast<_Ty1&&>(_Left) * static_cast<_Ty2&&>(_Right))) // strengthened
-> decltype(static_cast<_Ty1&&>(_Left) * static_cast<_Ty2&&>(_Right)) {
return static_cast<_Ty1&&>(_Left) * static_cast<_Ty2&&>(_Right);
}
using is_transparent = int;
};
template <>
struct equal_to<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
noexcept(noexcept(static_cast<_Ty1&&>(_Left) == static_cast<_Ty2&&>(_Right))) // strengthened
-> decltype(static_cast<_Ty1&&>(_Left) == static_cast<_Ty2&&>(_Right)) {
return static_cast<_Ty1&&>(_Left) == static_cast<_Ty2&&>(_Right);
}
using is_transparent = int;
};
template <>
struct not_equal_to<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
noexcept(noexcept(static_cast<_Ty1&&>(_Left) != static_cast<_Ty2&&>(_Right))) // strengthened
-> decltype(static_cast<_Ty1&&>(_Left) != static_cast<_Ty2&&>(_Right)) {
return static_cast<_Ty1&&>(_Left) != static_cast<_Ty2&&>(_Right);
}
using is_transparent = int;
};
template <>
struct greater<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
noexcept(noexcept(static_cast<_Ty1&&>(_Left) > static_cast<_Ty2&&>(_Right))) // strengthened
-> decltype(static_cast<_Ty1&&>(_Left) > static_cast<_Ty2&&>(_Right)) {
return static_cast<_Ty1&&>(_Left) > static_cast<_Ty2&&>(_Right);
}
using is_transparent = int;
};
template <>
struct greater_equal<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
noexcept(noexcept(static_cast<_Ty1&&>(_Left) >= static_cast<_Ty2&&>(_Right))) // strengthened
-> decltype(static_cast<_Ty1&&>(_Left) >= static_cast<_Ty2&&>(_Right)) {
return static_cast<_Ty1&&>(_Left) >= static_cast<_Ty2&&>(_Right);
}
using is_transparent = int;
};
template <>
struct less_equal<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
noexcept(noexcept(static_cast<_Ty1&&>(_Left) <= static_cast<_Ty2&&>(_Right))) // strengthened
-> decltype(static_cast<_Ty1&&>(_Left) <= static_cast<_Ty2&&>(_Right)) {
return static_cast<_Ty1&&>(_Left) <= static_cast<_Ty2&&>(_Right);
}
using is_transparent = int;
};
template <class _Fx>
struct _Ref_fn { // pass function object by value as a reference
template <class... _Args>
constexpr decltype(auto) operator()(_Args&&... _Vals) { // forward function call operator
if constexpr (is_member_pointer_v<_Fx>) {
return _STD invoke(_Fn, _STD forward<_Args>(_Vals)...);
} else {
return _Fn(_STD forward<_Args>(_Vals)...);
}
}
_Fx& _Fn;
};
template <class _Fn>
_NODISCARD constexpr auto _Pass_fn(_Fn& _Func) noexcept {
constexpr bool _Pass_by_value = conjunction_v<bool_constant<sizeof(_Fn) <= sizeof(void*)>,
is_trivially_copy_constructible<_Fn>, is_trivially_destructible<_Fn>>;
if constexpr (_Pass_by_value) {
return _Func;
} else {
return _Ref_fn<_Fn>{_Func}; // pass functor by "reference"
}
}
#if _HAS_CXX23
_EXPORT_STD template <class _Result_type, class _Callable, class... _Types,
enable_if_t<is_invocable_r_v<_Result_type, _Callable, _Types...>, int> = 0>
_NODISCARD constexpr _Result_type invoke_r(_Callable&& _Obj, _Types&&... _Args) noexcept(
is_nothrow_invocable_r_v<_Result_type, _Callable, _Types...>) {
if constexpr (is_void_v<_Result_type>) {
(void) _STD invoke(static_cast<_Callable&&>(_Obj), static_cast<_Types&&>(_Args)...);
} else {
return _STD invoke(static_cast<_Callable&&>(_Obj), static_cast<_Types&&>(_Args)...);
}
}
#endif // _HAS_CXX23
struct _Unused_parameter { // generic unused parameter struct
constexpr _Unused_parameter() noexcept = default;
template <class _Ty>
constexpr _Unused_parameter(_Ty&&) noexcept {}
};
template <class _Ty>
using _Algorithm_int_t = conditional_t<is_integral_v<_Ty>, _Ty, ptrdiff_t>;
#ifdef __cpp_lib_concepts
template <class _Ty>
concept _Destructible_object = is_object_v<_Ty> && destructible<_Ty>;
template <template <class...> class _Template, class... _Args>
void _Derived_from_specialization_impl(const _Template<_Args...>&);
template <class _Ty, template <class...> class _Template>
concept _Derived_from_specialization_of =
requires(const _Ty& _Obj) {
_STD _Derived_from_specialization_impl<_Template>(_Obj); // qualified: avoid ADL, handle incomplete types
};
namespace ranges {
namespace _Iter_move {
#if defined(__clang__) || defined(__EDG__) // TRANSITION, VSO-1681199
void iter_move() = delete; // Block unqualified name lookup
#else // ^^^ no workaround / workaround vvv
void iter_move();
#endif // ^^^ workaround ^^^
// clang-format off
template <class _Ty>
concept _Has_ADL = _Has_class_or_enum_type<_Ty> && requires(_Ty&& __t) {
iter_move(static_cast<_Ty&&>(__t)); // intentional ADL
};
template <class _Ty>
concept _Can_deref = requires(_Ty&& __t) {
*static_cast<_Ty&&>(__t);
};
// clang-format on
class _Cpo {
private:
enum class _St { _None, _Custom, _Fallback };
template <class _Ty>
_NODISCARD static consteval _Choice_t<_St> _Choose() noexcept {
if constexpr (_Has_ADL<_Ty>) {
return {_St::_Custom, noexcept(iter_move(_STD declval<_Ty>()))}; // intentional ADL
} else if constexpr (_Can_deref<_Ty>) {
return {_St::_Fallback, noexcept(*_STD declval<_Ty>())};
} else {
return {_St::_None};
}
}
template <class _Ty>
static constexpr _Choice_t<_St> _Choice = _Choose<_Ty>();
public:
template <class _Ty>
requires (_Choice<_Ty>._Strategy != _St::_None)
_NODISCARD constexpr decltype(auto) operator()(_Ty&& _Val) const noexcept(_Choice<_Ty>._No_throw) {
constexpr _St _Strat = _Choice<_Ty>._Strategy;
if constexpr (_Strat == _St::_Custom) {
return iter_move(static_cast<_Ty&&>(_Val)); // intentional ADL
} else if constexpr (_Strat == _St::_Fallback) {
using _Ref = decltype(*static_cast<_Ty&&>(_Val));
if constexpr (is_lvalue_reference_v<_Ref>) {
return _STD move(*static_cast<_Ty&&>(_Val));
} else {
return *static_cast<_Ty&&>(_Val);
}
} else {
static_assert(_Always_false<_Ty>, "should be unreachable");
}
}
};
} // namespace _Iter_move
inline namespace _Cpos {
_EXPORT_STD inline constexpr _Iter_move::_Cpo iter_move;
}
} // namespace ranges
// iter_swap defined below since it depends on indirectly_movable_storable
// clang-format off
_EXPORT_STD template <class _Ty>
requires _Dereferenceable<_Ty> && requires(_Ty& __t) {
{ _RANGES iter_move(__t) } -> _Can_reference;
}
using iter_rvalue_reference_t = decltype(_RANGES iter_move(_STD declval<_Ty&>()));
// clang-format on
template <class _It>
concept _Indirectly_readable_impl =
requires(const _It __i) {
typename iter_value_t<_It>;
typename iter_reference_t<_It>;
typename iter_rvalue_reference_t<_It>;
{ *__i } -> same_as<iter_reference_t<_It>>;
{ _RANGES iter_move(__i) } -> same_as<iter_rvalue_reference_t<_It>>;
} && common_reference_with<iter_reference_t<_It>&&, iter_value_t<_It>&>
&& common_reference_with<iter_reference_t<_It>&&, iter_rvalue_reference_t<_It>&&>
&& common_reference_with<iter_rvalue_reference_t<_It>&&, const iter_value_t<_It>&>;
_EXPORT_STD template <class _It>
concept indirectly_readable = _Indirectly_readable_impl<remove_cvref_t<_It>>;
template <class _Ty>
struct _Indirect_value_impl {
using type = iter_value_t<_Ty>&;
};
template <indirectly_readable _It>
using _Indirect_value_t = typename _Indirect_value_impl<_It>::type;
_EXPORT_STD template <indirectly_readable _Ty>
using iter_common_reference_t = common_reference_t<iter_reference_t<_Ty>, _Indirect_value_t<_Ty>>;
_EXPORT_STD template <class _It, class _Ty>
concept indirectly_writable =
requires(_It && __i, _Ty&& __t) {
*__i = static_cast<_Ty&&>(__t);
*static_cast<_It&&>(__i) = static_cast<_Ty&&>(__t);
const_cast<const iter_reference_t<_It>&&>(*__i) = static_cast<_Ty&&>(__t);
const_cast<const iter_reference_t<_It>&&>(*static_cast<_It&&>(__i)) = static_cast<_Ty&&>(__t);
};
template <bool _Is_integer_class>
struct _Make_unsigned_like_impl {
template <class _Ty>
using _Apply = typename _Ty::_Unsigned_type;
};
template <>
struct _Make_unsigned_like_impl<false> {
template <class _Ty>
using _Apply = make_unsigned_t<_Ty>;
};
template <class _Ty>
using _Make_unsigned_like_t = typename _Make_unsigned_like_impl<_Integer_class<_Ty>>::template _Apply<_Ty>;
template <_Integer_like _Ty>
_NODISCARD constexpr auto _To_unsigned_like(const _Ty _Value) noexcept {
return static_cast<_Make_unsigned_like_t<_Ty>>(_Value);
}
template <bool _Is_integer_class>
struct _Make_signed_like_impl {
template <class _Ty>
using _Apply = typename _Ty::_Signed_type;
};
template <>
struct _Make_signed_like_impl<false> {
template <class _Ty>
using _Apply = make_signed_t<_Ty>;
};
template <class _Ty>
using _Make_signed_like_t = typename _Make_signed_like_impl<_Integer_class<_Ty>>::template _Apply<_Ty>;
_EXPORT_STD template <class _Ty>
concept incrementable = regular<_Ty> && weakly_incrementable<_Ty> && requires(_Ty __t) {
{ __t++ } -> same_as<_Ty>;
};
template <bool _Iterator_category_present>
struct _Iter_concept_impl2 {
template <class _It, class _Traits>
using _Apply = typename _Traits::iterator_category;
};
template <>
struct _Iter_concept_impl2<false> {
template <class _It, class _Traits>
requires _Is_from_primary<iterator_traits<_It>>
using _Apply = random_access_iterator_tag;
};
template <bool _Iterator_concept_present>
struct _Iter_concept_impl1 {
template <class _It, class _Traits>
using _Apply = typename _Traits::iterator_concept;
};
template <>
struct _Iter_concept_impl1<false> {
template <class _It, class _Traits>
using _Apply = typename _Iter_concept_impl2<_Has_member_iterator_category<_Traits>>::template _Apply<_It, _Traits>;
};
template <class _It, class _Traits = conditional_t<_Is_from_primary<iterator_traits<_It>>, _It, iterator_traits<_It>>>
using _Iter_concept =
typename _Iter_concept_impl1<_Has_member_iterator_concept<_Traits>>::template _Apply<_It, _Traits>;
// clang-format off
_EXPORT_STD template <class _It>
concept input_iterator = input_or_output_iterator<_It> && indirectly_readable<_It>
&& requires { typename _Iter_concept<_It>; }
&& derived_from<_Iter_concept<_It>, input_iterator_tag>;
_EXPORT_STD template <class _It, class _Ty>
concept output_iterator = input_or_output_iterator<_It> && indirectly_writable<_It, _Ty>
&& requires(_It __i, _Ty&& __t) {
*__i++ = static_cast<_Ty&&>(__t);
};
_EXPORT_STD template <class _It>
concept forward_iterator = input_iterator<_It> && derived_from<_Iter_concept<_It>, forward_iterator_tag>
&& incrementable<_It> && sentinel_for<_It, _It>;
_EXPORT_STD template <class _It>
concept bidirectional_iterator = forward_iterator<_It> && derived_from<_Iter_concept<_It>, bidirectional_iterator_tag>
&& requires(_It __i) {
{ --__i } -> same_as<_It&>;
{ __i-- } -> same_as<_It>;
};
_EXPORT_STD template <class _It>
concept random_access_iterator = bidirectional_iterator<_It>
&& derived_from<_Iter_concept<_It>, random_access_iterator_tag> && totally_ordered<_It>
&& sized_sentinel_for<_It, _It> && requires(_It __i, const _It __j, const iter_difference_t<_It> __n) {
{ __i += __n } -> same_as<_It&>;
{ __j + __n } -> same_as<_It>;
{ __n + __j } -> same_as<_It>;
{ __i -= __n } -> same_as<_It&>;
{ __j - __n } -> same_as<_It>;
{ __j[__n] } -> same_as<iter_reference_t<_It>>;
};
_EXPORT_STD template <class _It>
concept contiguous_iterator = random_access_iterator<_It>
&& derived_from<_Iter_concept<_It>, contiguous_iterator_tag>
&& is_lvalue_reference_v<iter_reference_t<_It>>
&& same_as<iter_value_t<_It>, remove_cvref_t<iter_reference_t<_It>>>
&& requires(const _It& __i) {
{ _STD to_address(__i) } -> same_as<add_pointer_t<iter_reference_t<_It>>>;
};
// clang-format on
_EXPORT_STD template <class _Fn, class _It>
concept indirectly_unary_invocable =
indirectly_readable<_It> && copy_constructible<_Fn> && invocable<_Fn&, _Indirect_value_t<_It>>
&& invocable<_Fn&, iter_reference_t<_It>> && invocable<_Fn&, iter_common_reference_t<_It>>
&& common_reference_with<invoke_result_t<_Fn&, _Indirect_value_t<_It>>,
invoke_result_t<_Fn&, iter_reference_t<_It>>>;
_EXPORT_STD template <class _Fn, class _It>
concept indirectly_regular_unary_invocable =
indirectly_readable<_It> && copy_constructible<_Fn> && regular_invocable<_Fn&, _Indirect_value_t<_It>>
&& regular_invocable<_Fn&, iter_reference_t<_It>> && regular_invocable<_Fn&, iter_common_reference_t<_It>>
&& common_reference_with<invoke_result_t<_Fn&, _Indirect_value_t<_It>>,
invoke_result_t<_Fn&, iter_reference_t<_It>>>;
_EXPORT_STD template <class _Fn, class _It>
concept indirect_unary_predicate =
indirectly_readable<_It> && copy_constructible<_Fn> && predicate<_Fn&, _Indirect_value_t<_It>>
&& predicate<_Fn&, iter_reference_t<_It>> && predicate<_Fn&, iter_common_reference_t<_It>>;
_EXPORT_STD template <class _Fn, class _It1, class _It2>
concept indirect_binary_predicate = indirectly_readable<_It1> && indirectly_readable<_It2> && copy_constructible<_Fn>
&& predicate<_Fn&, _Indirect_value_t<_It1>, _Indirect_value_t<_It2>>
&& predicate<_Fn&, _Indirect_value_t<_It1>, iter_reference_t<_It2>>
&& predicate<_Fn&, iter_reference_t<_It1>, _Indirect_value_t<_It2>>
&& predicate<_Fn&, iter_reference_t<_It1>, iter_reference_t<_It2>>
&& predicate<_Fn&, iter_common_reference_t<_It1>, iter_common_reference_t<_It2>>;
_EXPORT_STD template <class _Fn, class _It1, class _It2 = _It1>
concept indirect_equivalence_relation =
indirectly_readable<_It1> && indirectly_readable<_It2> && copy_constructible<_Fn>
&& equivalence_relation<_Fn&, _Indirect_value_t<_It1>, _Indirect_value_t<_It2>>
&& equivalence_relation<_Fn&, _Indirect_value_t<_It1>, iter_reference_t<_It2>>
&& equivalence_relation<_Fn&, iter_reference_t<_It1>, _Indirect_value_t<_It2>>
&& equivalence_relation<_Fn&, iter_reference_t<_It1>, iter_reference_t<_It2>>
&& equivalence_relation<_Fn&, iter_common_reference_t<_It1>, iter_common_reference_t<_It2>>;
_EXPORT_STD template <class _Fn, class _It1, class _It2 = _It1>
concept indirect_strict_weak_order =
indirectly_readable<_It1> && indirectly_readable<_It2> && copy_constructible<_Fn>
&& strict_weak_order<_Fn&, _Indirect_value_t<_It1>, _Indirect_value_t<_It2>>
&& strict_weak_order<_Fn&, _Indirect_value_t<_It1>, iter_reference_t<_It2>>
&& strict_weak_order<_Fn&, iter_reference_t<_It1>, _Indirect_value_t<_It2>>
&& strict_weak_order<_Fn&, iter_reference_t<_It1>, iter_reference_t<_It2>>
&& strict_weak_order<_Fn&, iter_common_reference_t<_It1>, iter_common_reference_t<_It2>>;
_EXPORT_STD template <class _Fn, class... _Its>
requires (indirectly_readable<_Its> && ...) && invocable<_Fn, iter_reference_t<_Its>...>
using indirect_result_t = invoke_result_t<_Fn, iter_reference_t<_Its>...>;
_EXPORT_STD template <indirectly_readable _It, indirectly_regular_unary_invocable<_It> _Proj>
struct projected {
using value_type = remove_cvref_t<indirect_result_t<_Proj&, _It>>;
indirect_result_t<_Proj&, _It> operator*() const {
_CSTD abort();
}
};
template <class _It, class _Proj>
struct _Indirect_value_impl<projected<_It, _Proj>> {
using type = invoke_result_t<_Proj&, _Indirect_value_t<_It>>;
};
template <weakly_incrementable _It, class _Proj>
struct incrementable_traits<projected<_It, _Proj>> {
using difference_type = iter_difference_t<_It>;
};
_EXPORT_STD template <class _In, class _Out>
concept indirectly_movable = indirectly_readable<_In> && indirectly_writable<_Out, iter_rvalue_reference_t<_In>>;
// clang-format off
_EXPORT_STD template <class _In, class _Out>
concept indirectly_movable_storable = indirectly_movable<_In, _Out>
&& indirectly_writable<_Out, iter_value_t<_In>>
&& movable<iter_value_t<_In>>
&& constructible_from<iter_value_t<_In>, iter_rvalue_reference_t<_In>>
&& assignable_from<iter_value_t<_In>&, iter_rvalue_reference_t<_In> >;
// clang-format on
_EXPORT_STD template <class _In, class _Out>
concept indirectly_copyable = indirectly_readable<_In> && indirectly_writable<_Out, iter_reference_t<_In>>;
// clang-format off
_EXPORT_STD template <class _In, class _Out>
concept indirectly_copyable_storable = indirectly_copyable<_In, _Out>
&& indirectly_writable<_Out, iter_value_t<_In>&>
&& indirectly_writable<_Out, const iter_value_t<_In>&>
&& indirectly_writable<_Out, iter_value_t<_In>&&>
&& indirectly_writable<_Out, const iter_value_t<_In>&&>
&& copyable<iter_value_t<_In>>
&& constructible_from<iter_value_t<_In>, iter_reference_t<_In>>
&& assignable_from<iter_value_t<_In>&, iter_reference_t<_In>>;
// clang-format on
namespace ranges {
namespace _Iter_swap {
template <class _Ty1, class _Ty2>
void iter_swap(_Ty1, _Ty2) = delete;
// clang-format off
template <class _Ty1, class _Ty2>
concept _Has_ADL = (_Has_class_or_enum_type<_Ty1> || _Has_class_or_enum_type<_Ty2>)
&& requires(_Ty1&& __t1, _Ty2&& __t2) {
iter_swap(static_cast<_Ty1&&>(__t1), static_cast<_Ty2&&>(__t2)); // intentional ADL
};
template <class _Ty1, class _Ty2>
concept _Can_swap_references = indirectly_readable<remove_reference_t<_Ty1>>
&& indirectly_readable<remove_reference_t<_Ty2>>
&& swappable_with<iter_reference_t<_Ty1>, iter_reference_t<_Ty2>>;
template <class _Ty1, class _Ty2>
concept _Symmetric_indirectly_movable_storable =
indirectly_movable_storable<remove_reference_t<_Ty1>, remove_reference_t<_Ty2>>
&& indirectly_movable_storable<remove_reference_t<_Ty2>, remove_reference_t<_Ty1>>;
// clang-format on
template <class _Xty, class _Yty>
_NODISCARD constexpr iter_value_t<remove_reference_t<_Xty>> _Iter_exchange_move(_Xty&& _XVal,
_Yty&& _YVal) noexcept(noexcept(iter_value_t<remove_reference_t<_Xty>>(_RANGES iter_move(_XVal)))) {
iter_value_t<remove_reference_t<_Xty>> _Tmp(_RANGES iter_move(_XVal));
*_XVal = _RANGES iter_move(_YVal);
return _Tmp;
}
class _Cpo {
private:
enum class _St { _None, _Custom, _Swap, _Exchange };
template <class _Ty1, class _Ty2>
_NODISCARD static consteval _Choice_t<_St> _Choose() noexcept {
if constexpr (_Has_ADL<_Ty1, _Ty2>) {
return {_St::_Custom,
noexcept(iter_swap(_STD declval<_Ty1>(), _STD declval<_Ty2>()))}; // intentional ADL
} else if constexpr (_Can_swap_references<_Ty1, _Ty2>) {
return {_St::_Swap, noexcept(_RANGES swap(*_STD declval<_Ty1>(), *_STD declval<_Ty2>()))};
} else if constexpr (_Symmetric_indirectly_movable_storable<_Ty1, _Ty2>) {
constexpr auto _Nothrow = noexcept(*_STD declval<_Ty1>() = _Iter_swap::_Iter_exchange_move(
_STD declval<_Ty2>(), _STD declval<_Ty1>()));
return {_St::_Exchange, _Nothrow};
} else {
return {_St::_None};
}
}
template <class _Ty1, class _Ty2>
static constexpr _Choice_t<_St> _Choice = _Choose<_Ty1, _Ty2>();
public:
template <class _Ty1, class _Ty2>
requires (_Choice<_Ty1, _Ty2>._Strategy != _St::_None)
constexpr void operator()(_Ty1&& _Val1, _Ty2&& _Val2) const noexcept(_Choice<_Ty1, _Ty2>._No_throw) {
constexpr _St _Strat = _Choice<_Ty1, _Ty2>._Strategy;
if constexpr (_Strat == _St::_Custom) {
(void) iter_swap(static_cast<_Ty1&&>(_Val1), static_cast<_Ty2&&>(_Val2)); // intentional ADL
} else if constexpr (_Strat == _St::_Swap) {
_RANGES swap(*static_cast<_Ty1&&>(_Val1), *static_cast<_Ty2&&>(_Val2));
} else if constexpr (_Strat == _St::_Exchange) {
*static_cast<_Ty1&&>(_Val1) =
_Iter_swap::_Iter_exchange_move(static_cast<_Ty2&&>(_Val2), static_cast<_Ty1&&>(_Val1));
} else {
static_assert(_Always_false<_Ty1>, "should be unreachable");
}
}
};
} // namespace _Iter_swap
inline namespace _Cpos {
_EXPORT_STD inline constexpr _Iter_swap::_Cpo iter_swap;
}
} // namespace ranges
// clang-format off
_EXPORT_STD template <class _It1, class _It2 = _It1>
concept indirectly_swappable = indirectly_readable<_It1> && indirectly_readable<_It2>
&& requires(const _It1 __i1, const _It2 __i2) {
_RANGES iter_swap(__i1, __i1);
_RANGES iter_swap(__i2, __i2);
_RANGES iter_swap(__i1, __i2);
_RANGES iter_swap(__i2, __i1);
};
// clang-format on