mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
ranges_algo.h
3565 lines (3239 loc) · 112 KB
/
ranges_algo.h
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
// Core algorithmic facilities -*- C++ -*-
// Copyright (C) 2020-2023 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 bits/ranges_algo.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{algorithm}
*/
#ifndef _RANGES_ALGO_H
#define _RANGES_ALGO_H 1
#if __cplusplus > 201703L
#include <bits/ranges_algobase.h>
#include <bits/ranges_util.h>
#include <bits/uniform_int_dist.h> // concept uniform_random_bit_generator
#if __cpp_lib_concepts
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
namespace ranges
{
namespace __detail
{
template<typename _Comp, typename _Proj>
constexpr auto
__make_comp_proj(_Comp& __comp, _Proj& __proj)
{
return [&] (auto&& __lhs, auto&& __rhs) -> bool {
using _TL = decltype(__lhs);
using _TR = decltype(__rhs);
return std::__invoke(__comp,
std::__invoke(__proj, std::forward<_TL>(__lhs)),
std::__invoke(__proj, std::forward<_TR>(__rhs)));
};
}
template<typename _Pred, typename _Proj>
constexpr auto
__make_pred_proj(_Pred& __pred, _Proj& __proj)
{
return [&] <typename _Tp> (_Tp&& __arg) -> bool {
return std::__invoke(__pred,
std::__invoke(__proj, std::forward<_Tp>(__arg)));
};
}
} // namespace __detail
struct __all_of_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
constexpr bool
operator()(_Iter __first, _Sent __last,
_Pred __pred, _Proj __proj = {}) const
{
for (; __first != __last; ++__first)
if (!(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
return false;
return true;
}
template<input_range _Range, typename _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
_Pred>
constexpr bool
operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__pred), std::move(__proj));
}
};
inline constexpr __all_of_fn all_of{};
struct __any_of_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
constexpr bool
operator()(_Iter __first, _Sent __last,
_Pred __pred, _Proj __proj = {}) const
{
for (; __first != __last; ++__first)
if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
return true;
return false;
}
template<input_range _Range, typename _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
_Pred>
constexpr bool
operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__pred), std::move(__proj));
}
};
inline constexpr __any_of_fn any_of{};
struct __none_of_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
constexpr bool
operator()(_Iter __first, _Sent __last,
_Pred __pred, _Proj __proj = {}) const
{
for (; __first != __last; ++__first)
if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
return false;
return true;
}
template<input_range _Range, typename _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
_Pred>
constexpr bool
operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__pred), std::move(__proj));
}
};
inline constexpr __none_of_fn none_of{};
template<typename _Iter, typename _Fp>
struct in_fun_result
{
[[no_unique_address]] _Iter in;
[[no_unique_address]] _Fp fun;
template<typename _Iter2, typename _F2p>
requires convertible_to<const _Iter&, _Iter2>
&& convertible_to<const _Fp&, _F2p>
constexpr
operator in_fun_result<_Iter2, _F2p>() const &
{ return {in, fun}; }
template<typename _Iter2, typename _F2p>
requires convertible_to<_Iter, _Iter2> && convertible_to<_Fp, _F2p>
constexpr
operator in_fun_result<_Iter2, _F2p>() &&
{ return {std::move(in), std::move(fun)}; }
};
template<typename _Iter, typename _Fp>
using for_each_result = in_fun_result<_Iter, _Fp>;
struct __for_each_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Proj = identity,
indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
constexpr for_each_result<_Iter, _Fun>
operator()(_Iter __first, _Sent __last, _Fun __f, _Proj __proj = {}) const
{
for (; __first != __last; ++__first)
std::__invoke(__f, std::__invoke(__proj, *__first));
return { std::move(__first), std::move(__f) };
}
template<input_range _Range, typename _Proj = identity,
indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>>
_Fun>
constexpr for_each_result<borrowed_iterator_t<_Range>, _Fun>
operator()(_Range&& __r, _Fun __f, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__f), std::move(__proj));
}
};
inline constexpr __for_each_fn for_each{};
template<typename _Iter, typename _Fp>
using for_each_n_result = in_fun_result<_Iter, _Fp>;
struct __for_each_n_fn
{
template<input_iterator _Iter, typename _Proj = identity,
indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
constexpr for_each_n_result<_Iter, _Fun>
operator()(_Iter __first, iter_difference_t<_Iter> __n,
_Fun __f, _Proj __proj = {}) const
{
if constexpr (random_access_iterator<_Iter>)
{
if (__n <= 0)
return {std::move(__first), std::move(__f)};
auto __last = __first + __n;
return ranges::for_each(std::move(__first), std::move(__last),
std::move(__f), std::move(__proj));
}
else
{
while (__n-- > 0)
{
std::__invoke(__f, std::__invoke(__proj, *__first));
++__first;
}
return {std::move(__first), std::move(__f)};
}
}
};
inline constexpr __for_each_n_fn for_each_n{};
// find, find_if and find_if_not are defined in <bits/ranges_util.h>.
struct __find_first_of_fn
{
template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
typename _Pred = ranges::equal_to,
typename _Proj1 = identity, typename _Proj2 = identity>
requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
constexpr _Iter1
operator()(_Iter1 __first1, _Sent1 __last1,
_Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
{
for (; __first1 != __last1; ++__first1)
for (auto __iter = __first2; __iter != __last2; ++__iter)
if (std::__invoke(__pred,
std::__invoke(__proj1, *__first1),
std::__invoke(__proj2, *__iter)))
return __first1;
return __first1;
}
template<input_range _Range1, forward_range _Range2,
typename _Pred = ranges::equal_to,
typename _Proj1 = identity, typename _Proj2 = identity>
requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
_Pred, _Proj1, _Proj2>
constexpr borrowed_iterator_t<_Range1>
operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
{
return (*this)(ranges::begin(__r1), ranges::end(__r1),
ranges::begin(__r2), ranges::end(__r2),
std::move(__pred),
std::move(__proj1), std::move(__proj2));
}
};
inline constexpr __find_first_of_fn find_first_of{};
struct __count_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Tp, typename _Proj = identity>
requires indirect_binary_predicate<ranges::equal_to,
projected<_Iter, _Proj>,
const _Tp*>
constexpr iter_difference_t<_Iter>
operator()(_Iter __first, _Sent __last,
const _Tp& __value, _Proj __proj = {}) const
{
iter_difference_t<_Iter> __n = 0;
for (; __first != __last; ++__first)
if (std::__invoke(__proj, *__first) == __value)
++__n;
return __n;
}
template<input_range _Range, typename _Tp, typename _Proj = identity>
requires indirect_binary_predicate<ranges::equal_to,
projected<iterator_t<_Range>, _Proj>,
const _Tp*>
constexpr range_difference_t<_Range>
operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
__value, std::move(__proj));
}
};
inline constexpr __count_fn count{};
struct __count_if_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
constexpr iter_difference_t<_Iter>
operator()(_Iter __first, _Sent __last,
_Pred __pred, _Proj __proj = {}) const
{
iter_difference_t<_Iter> __n = 0;
for (; __first != __last; ++__first)
if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
++__n;
return __n;
}
template<input_range _Range,
typename _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
_Pred>
constexpr range_difference_t<_Range>
operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__pred), std::move(__proj));
}
};
inline constexpr __count_if_fn count_if{};
// in_in_result, mismatch and search are defined in <bits/ranges_util.h>.
struct __search_n_fn
{
template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Tp,
typename _Pred = ranges::equal_to, typename _Proj = identity>
requires indirectly_comparable<_Iter, const _Tp*, _Pred, _Proj>
constexpr subrange<_Iter>
operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __count,
const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
{
if (__count <= 0)
return {__first, __first};
auto __value_comp = [&] <typename _Rp> (_Rp&& __arg) -> bool {
return std::__invoke(__pred, std::forward<_Rp>(__arg), __value);
};
if (__count == 1)
{
__first = ranges::find_if(std::move(__first), __last,
std::move(__value_comp),
std::move(__proj));
if (__first == __last)
return {__first, __first};
else
{
auto __end = __first;
return {__first, ++__end};
}
}
if constexpr (sized_sentinel_for<_Sent, _Iter>
&& random_access_iterator<_Iter>)
{
auto __tail_size = __last - __first;
auto __remainder = __count;
while (__remainder <= __tail_size)
{
__first += __remainder;
__tail_size -= __remainder;
auto __backtrack = __first;
while (__value_comp(std::__invoke(__proj, *--__backtrack)))
{
if (--__remainder == 0)
return {__first - __count, __first};
}
__remainder = __count + 1 - (__first - __backtrack);
}
auto __i = __first + __tail_size;
return {__i, __i};
}
else
{
__first = ranges::find_if(__first, __last, __value_comp, __proj);
while (__first != __last)
{
auto __n = __count;
auto __i = __first;
++__i;
while (__i != __last && __n != 1
&& __value_comp(std::__invoke(__proj, *__i)))
{
++__i;
--__n;
}
if (__n == 1)
return {__first, __i};
if (__i == __last)
return {__i, __i};
__first = ranges::find_if(++__i, __last, __value_comp, __proj);
}
return {__first, __first};
}
}
template<forward_range _Range, typename _Tp,
typename _Pred = ranges::equal_to, typename _Proj = identity>
requires indirectly_comparable<iterator_t<_Range>, const _Tp*,
_Pred, _Proj>
constexpr borrowed_subrange_t<_Range>
operator()(_Range&& __r, range_difference_t<_Range> __count,
const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__count), __value,
std::move(__pred), std::move(__proj));
}
};
inline constexpr __search_n_fn search_n{};
struct __find_end_fn
{
template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
typename _Pred = ranges::equal_to,
typename _Proj1 = identity, typename _Proj2 = identity>
requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
constexpr subrange<_Iter1>
operator()(_Iter1 __first1, _Sent1 __last1,
_Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
{
if constexpr (bidirectional_iterator<_Iter1>
&& bidirectional_iterator<_Iter2>)
{
auto __i1 = ranges::next(__first1, __last1);
auto __i2 = ranges::next(__first2, __last2);
auto __rresult
= ranges::search(reverse_iterator<_Iter1>{__i1},
reverse_iterator<_Iter1>{__first1},
reverse_iterator<_Iter2>{__i2},
reverse_iterator<_Iter2>{__first2},
std::move(__pred),
std::move(__proj1), std::move(__proj2));
auto __result_first = ranges::end(__rresult).base();
auto __result_last = ranges::begin(__rresult).base();
if (__result_last == __first1)
return {__i1, __i1};
else
return {__result_first, __result_last};
}
else
{
auto __i = ranges::next(__first1, __last1);
if (__first2 == __last2)
return {__i, __i};
auto __result_begin = __i;
auto __result_end = __i;
for (;;)
{
auto __new_range = ranges::search(__first1, __last1,
__first2, __last2,
__pred, __proj1, __proj2);
auto __new_result_begin = ranges::begin(__new_range);
auto __new_result_end = ranges::end(__new_range);
if (__new_result_begin == __last1)
return {__result_begin, __result_end};
else
{
__result_begin = __new_result_begin;
__result_end = __new_result_end;
__first1 = __result_begin;
++__first1;
}
}
}
}
template<forward_range _Range1, forward_range _Range2,
typename _Pred = ranges::equal_to,
typename _Proj1 = identity, typename _Proj2 = identity>
requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
_Pred, _Proj1, _Proj2>
constexpr borrowed_subrange_t<_Range1>
operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
{
return (*this)(ranges::begin(__r1), ranges::end(__r1),
ranges::begin(__r2), ranges::end(__r2),
std::move(__pred),
std::move(__proj1), std::move(__proj2));
}
};
inline constexpr __find_end_fn find_end{};
// adjacent_find is defined in <bits/ranges_util.h>.
struct __is_permutation_fn
{
template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
typename _Proj1 = identity, typename _Proj2 = identity,
indirect_equivalence_relation<projected<_Iter1, _Proj1>,
projected<_Iter2, _Proj2>> _Pred
= ranges::equal_to>
constexpr bool
operator()(_Iter1 __first1, _Sent1 __last1,
_Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
{
constexpr bool __sized_iters
= (sized_sentinel_for<_Sent1, _Iter1>
&& sized_sentinel_for<_Sent2, _Iter2>);
if constexpr (__sized_iters)
{
auto __d1 = ranges::distance(__first1, __last1);
auto __d2 = ranges::distance(__first2, __last2);
if (__d1 != __d2)
return false;
}
// Efficiently compare identical prefixes: O(N) if sequences
// have the same elements in the same order.
for (; __first1 != __last1 && __first2 != __last2;
++__first1, (void)++__first2)
if (!(bool)std::__invoke(__pred,
std::__invoke(__proj1, *__first1),
std::__invoke(__proj2, *__first2)))
break;
if constexpr (__sized_iters)
{
if (__first1 == __last1)
return true;
}
else
{
auto __d1 = ranges::distance(__first1, __last1);
auto __d2 = ranges::distance(__first2, __last2);
if (__d1 == 0 && __d2 == 0)
return true;
if (__d1 != __d2)
return false;
}
for (auto __scan = __first1; __scan != __last1; ++__scan)
{
auto&& __proj_scan = std::__invoke(__proj1, *__scan);
auto __comp_scan = [&] <typename _Tp> (_Tp&& __arg) -> bool {
return std::__invoke(__pred, __proj_scan,
std::forward<_Tp>(__arg));
};
if (__scan != ranges::find_if(__first1, __scan,
__comp_scan, __proj1))
continue; // We've seen this one before.
auto __matches = ranges::count_if(__first2, __last2,
__comp_scan, __proj2);
if (__matches == 0
|| ranges::count_if(__scan, __last1,
__comp_scan, __proj1) != __matches)
return false;
}
return true;
}
template<forward_range _Range1, forward_range _Range2,
typename _Proj1 = identity, typename _Proj2 = identity,
indirect_equivalence_relation<
projected<iterator_t<_Range1>, _Proj1>,
projected<iterator_t<_Range2>, _Proj2>> _Pred = ranges::equal_to>
constexpr bool
operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
{
return (*this)(ranges::begin(__r1), ranges::end(__r1),
ranges::begin(__r2), ranges::end(__r2),
std::move(__pred),
std::move(__proj1), std::move(__proj2));
}
};
inline constexpr __is_permutation_fn is_permutation{};
template<typename _Iter, typename _Out>
using copy_if_result = in_out_result<_Iter, _Out>;
struct __copy_if_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
weakly_incrementable _Out, typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
requires indirectly_copyable<_Iter, _Out>
constexpr copy_if_result<_Iter, _Out>
operator()(_Iter __first, _Sent __last, _Out __result,
_Pred __pred, _Proj __proj = {}) const
{
for (; __first != __last; ++__first)
if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
{
*__result = *__first;
++__result;
}
return {std::move(__first), std::move(__result)};
}
template<input_range _Range, weakly_incrementable _Out,
typename _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
_Pred>
requires indirectly_copyable<iterator_t<_Range>, _Out>
constexpr copy_if_result<borrowed_iterator_t<_Range>, _Out>
operator()(_Range&& __r, _Out __result,
_Pred __pred, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__result),
std::move(__pred), std::move(__proj));
}
};
inline constexpr __copy_if_fn copy_if{};
template<typename _Iter1, typename _Iter2>
using swap_ranges_result = in_in_result<_Iter1, _Iter2>;
struct __swap_ranges_fn
{
template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
input_iterator _Iter2, sentinel_for<_Iter2> _Sent2>
requires indirectly_swappable<_Iter1, _Iter2>
constexpr swap_ranges_result<_Iter1, _Iter2>
operator()(_Iter1 __first1, _Sent1 __last1,
_Iter2 __first2, _Sent2 __last2) const
{
for (; __first1 != __last1 && __first2 != __last2;
++__first1, (void)++__first2)
ranges::iter_swap(__first1, __first2);
return {std::move(__first1), std::move(__first2)};
}
template<input_range _Range1, input_range _Range2>
requires indirectly_swappable<iterator_t<_Range1>, iterator_t<_Range2>>
constexpr swap_ranges_result<borrowed_iterator_t<_Range1>,
borrowed_iterator_t<_Range2>>
operator()(_Range1&& __r1, _Range2&& __r2) const
{
return (*this)(ranges::begin(__r1), ranges::end(__r1),
ranges::begin(__r2), ranges::end(__r2));
}
};
inline constexpr __swap_ranges_fn swap_ranges{};
template<typename _Iter, typename _Out>
using unary_transform_result = in_out_result<_Iter, _Out>;
template<typename _Iter1, typename _Iter2, typename _Out>
struct in_in_out_result
{
[[no_unique_address]] _Iter1 in1;
[[no_unique_address]] _Iter2 in2;
[[no_unique_address]] _Out out;
template<typename _IIter1, typename _IIter2, typename _OOut>
requires convertible_to<const _Iter1&, _IIter1>
&& convertible_to<const _Iter2&, _IIter2>
&& convertible_to<const _Out&, _OOut>
constexpr
operator in_in_out_result<_IIter1, _IIter2, _OOut>() const &
{ return {in1, in2, out}; }
template<typename _IIter1, typename _IIter2, typename _OOut>
requires convertible_to<_Iter1, _IIter1>
&& convertible_to<_Iter2, _IIter2>
&& convertible_to<_Out, _OOut>
constexpr
operator in_in_out_result<_IIter1, _IIter2, _OOut>() &&
{ return {std::move(in1), std::move(in2), std::move(out)}; }
};
template<typename _Iter1, typename _Iter2, typename _Out>
using binary_transform_result = in_in_out_result<_Iter1, _Iter2, _Out>;
struct __transform_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
weakly_incrementable _Out,
copy_constructible _Fp, typename _Proj = identity>
requires indirectly_writable<_Out,
indirect_result_t<_Fp&,
projected<_Iter, _Proj>>>
constexpr unary_transform_result<_Iter, _Out>
operator()(_Iter __first1, _Sent __last1, _Out __result,
_Fp __op, _Proj __proj = {}) const
{
for (; __first1 != __last1; ++__first1, (void)++__result)
*__result = std::__invoke(__op, std::__invoke(__proj, *__first1));
return {std::move(__first1), std::move(__result)};
}
template<input_range _Range, weakly_incrementable _Out,
copy_constructible _Fp, typename _Proj = identity>
requires indirectly_writable<_Out,
indirect_result_t<_Fp&,
projected<iterator_t<_Range>, _Proj>>>
constexpr unary_transform_result<borrowed_iterator_t<_Range>, _Out>
operator()(_Range&& __r, _Out __result, _Fp __op, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__result),
std::move(__op), std::move(__proj));
}
template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
weakly_incrementable _Out, copy_constructible _Fp,
typename _Proj1 = identity, typename _Proj2 = identity>
requires indirectly_writable<_Out,
indirect_result_t<_Fp&,
projected<_Iter1, _Proj1>,
projected<_Iter2, _Proj2>>>
constexpr binary_transform_result<_Iter1, _Iter2, _Out>
operator()(_Iter1 __first1, _Sent1 __last1,
_Iter2 __first2, _Sent2 __last2,
_Out __result, _Fp __binary_op,
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
{
for (; __first1 != __last1 && __first2 != __last2;
++__first1, (void)++__first2, ++__result)
*__result = std::__invoke(__binary_op,
std::__invoke(__proj1, *__first1),
std::__invoke(__proj2, *__first2));
return {std::move(__first1), std::move(__first2), std::move(__result)};
}
template<input_range _Range1, input_range _Range2,
weakly_incrementable _Out, copy_constructible _Fp,
typename _Proj1 = identity, typename _Proj2 = identity>
requires indirectly_writable<_Out,
indirect_result_t<_Fp&,
projected<iterator_t<_Range1>, _Proj1>,
projected<iterator_t<_Range2>, _Proj2>>>
constexpr binary_transform_result<borrowed_iterator_t<_Range1>,
borrowed_iterator_t<_Range2>, _Out>
operator()(_Range1&& __r1, _Range2&& __r2, _Out __result, _Fp __binary_op,
_Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
{
return (*this)(ranges::begin(__r1), ranges::end(__r1),
ranges::begin(__r2), ranges::end(__r2),
std::move(__result), std::move(__binary_op),
std::move(__proj1), std::move(__proj2));
}
};
inline constexpr __transform_fn transform{};
struct __replace_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Tp1, typename _Tp2, typename _Proj = identity>
requires indirectly_writable<_Iter, const _Tp2&>
&& indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>,
const _Tp1*>
constexpr _Iter
operator()(_Iter __first, _Sent __last,
const _Tp1& __old_value, const _Tp2& __new_value,
_Proj __proj = {}) const
{
for (; __first != __last; ++__first)
if (std::__invoke(__proj, *__first) == __old_value)
*__first = __new_value;
return __first;
}
template<input_range _Range,
typename _Tp1, typename _Tp2, typename _Proj = identity>
requires indirectly_writable<iterator_t<_Range>, const _Tp2&>
&& indirect_binary_predicate<ranges::equal_to,
projected<iterator_t<_Range>, _Proj>,
const _Tp1*>
constexpr borrowed_iterator_t<_Range>
operator()(_Range&& __r,
const _Tp1& __old_value, const _Tp2& __new_value,
_Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
__old_value, __new_value, std::move(__proj));
}
};
inline constexpr __replace_fn replace{};
struct __replace_if_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Tp, typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
requires indirectly_writable<_Iter, const _Tp&>
constexpr _Iter
operator()(_Iter __first, _Sent __last,
_Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
{
for (; __first != __last; ++__first)
if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
*__first = __new_value;
return std::move(__first);
}
template<input_range _Range, typename _Tp, typename _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
_Pred>
requires indirectly_writable<iterator_t<_Range>, const _Tp&>
constexpr borrowed_iterator_t<_Range>
operator()(_Range&& __r,
_Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__pred), __new_value, std::move(__proj));
}
};
inline constexpr __replace_if_fn replace_if{};
template<typename _Iter, typename _Out>
using replace_copy_result = in_out_result<_Iter, _Out>;
struct __replace_copy_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Tp1, typename _Tp2, output_iterator<const _Tp2&> _Out,
typename _Proj = identity>
requires indirectly_copyable<_Iter, _Out>
&& indirect_binary_predicate<ranges::equal_to,
projected<_Iter, _Proj>, const _Tp1*>
constexpr replace_copy_result<_Iter, _Out>
operator()(_Iter __first, _Sent __last, _Out __result,
const _Tp1& __old_value, const _Tp2& __new_value,
_Proj __proj = {}) const
{
for (; __first != __last; ++__first, (void)++__result)
if (std::__invoke(__proj, *__first) == __old_value)
*__result = __new_value;
else
*__result = *__first;
return {std::move(__first), std::move(__result)};
}
template<input_range _Range, typename _Tp1, typename _Tp2,
output_iterator<const _Tp2&> _Out, typename _Proj = identity>
requires indirectly_copyable<iterator_t<_Range>, _Out>
&& indirect_binary_predicate<ranges::equal_to,
projected<iterator_t<_Range>, _Proj>,
const _Tp1*>
constexpr replace_copy_result<borrowed_iterator_t<_Range>, _Out>
operator()(_Range&& __r, _Out __result,
const _Tp1& __old_value, const _Tp2& __new_value,
_Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__result), __old_value,
__new_value, std::move(__proj));
}
};
inline constexpr __replace_copy_fn replace_copy{};
template<typename _Iter, typename _Out>
using replace_copy_if_result = in_out_result<_Iter, _Out>;
struct __replace_copy_if_fn
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Tp, output_iterator<const _Tp&> _Out,
typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
requires indirectly_copyable<_Iter, _Out>
constexpr replace_copy_if_result<_Iter, _Out>
operator()(_Iter __first, _Sent __last, _Out __result,
_Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
{
for (; __first != __last; ++__first, (void)++__result)
if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
*__result = __new_value;
else
*__result = *__first;
return {std::move(__first), std::move(__result)};
}
template<input_range _Range,
typename _Tp, output_iterator<const _Tp&> _Out,
typename _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
_Pred>
requires indirectly_copyable<iterator_t<_Range>, _Out>
constexpr replace_copy_if_result<borrowed_iterator_t<_Range>, _Out>
operator()(_Range&& __r, _Out __result,
_Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__result), std::move(__pred),
__new_value, std::move(__proj));
}
};
inline constexpr __replace_copy_if_fn replace_copy_if{};
struct __generate_n_fn
{
template<input_or_output_iterator _Out, copy_constructible _Fp>
requires invocable<_Fp&>
&& indirectly_writable<_Out, invoke_result_t<_Fp&>>
constexpr _Out
operator()(_Out __first, iter_difference_t<_Out> __n, _Fp __gen) const
{
for (; __n > 0; --__n, (void)++__first)
*__first = std::__invoke(__gen);
return __first;
}
};
inline constexpr __generate_n_fn generate_n{};
struct __generate_fn
{
template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent,
copy_constructible _Fp>
requires invocable<_Fp&>
&& indirectly_writable<_Out, invoke_result_t<_Fp&>>
constexpr _Out
operator()(_Out __first, _Sent __last, _Fp __gen) const
{
for (; __first != __last; ++__first)
*__first = std::__invoke(__gen);
return __first;
}
template<typename _Range, copy_constructible _Fp>
requires invocable<_Fp&> && output_range<_Range, invoke_result_t<_Fp&>>
constexpr borrowed_iterator_t<_Range>
operator()(_Range&& __r, _Fp __gen) const
{
return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__gen));
}
};
inline constexpr __generate_fn generate{};
struct __remove_if_fn
{
template<permutable _Iter, sentinel_for<_Iter> _Sent,
typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
constexpr subrange<_Iter>
operator()(_Iter __first, _Sent __last,
_Pred __pred, _Proj __proj = {}) const
{
__first = ranges::find_if(__first, __last, __pred, __proj);
if (__first == __last)
return {__first, __first};
auto __result = __first;
++__first;
for (; __first != __last; ++__first)
if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
{
*__result = std::move(*__first);
++__result;
}
return {__result, __first};
}
template<forward_range _Range, typename _Proj = identity,
indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
_Pred>
requires permutable<iterator_t<_Range>>
constexpr borrowed_subrange_t<_Range>
operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
{
return (*this)(ranges::begin(__r), ranges::end(__r),
std::move(__pred), std::move(__proj));
}
};
inline constexpr __remove_if_fn remove_if{};