mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
stl_algo.h
5904 lines (5477 loc) · 212 KB
/
stl_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
// Algorithm implementation -*- C++ -*-
// Copyright (C) 2001-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/** @file bits/stl_algo.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{algorithm}
*/
#ifndef _STL_ALGO_H
#define _STL_ALGO_H 1
#include <bits/algorithmfwd.h>
#include <bits/stl_algobase.h>
#include <bits/stl_heap.h>
#include <bits/predefined_ops.h>
#if __cplusplus >= 201103L
#include <bits/uniform_int_dist.h>
#endif
#if _GLIBCXX_HOSTED
# include <bits/stl_tempbuf.h> // for _Temporary_buffer
# if (__cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED)
# include <cstdlib> // for rand
# endif
#endif
// See concept_check.h for the __glibcxx_*_requires macros.
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Swaps the median value of *__a, *__b and *__c under __comp to *__result
template<typename _Iterator, typename _Compare>
_GLIBCXX20_CONSTEXPR
void
__move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b,
_Iterator __c, _Compare __comp)
{
if (__comp(__a, __b))
{
if (__comp(__b, __c))
std::iter_swap(__result, __b);
else if (__comp(__a, __c))
std::iter_swap(__result, __c);
else
std::iter_swap(__result, __a);
}
else if (__comp(__a, __c))
std::iter_swap(__result, __a);
else if (__comp(__b, __c))
std::iter_swap(__result, __c);
else
std::iter_swap(__result, __b);
}
/// Provided for stable_partition to use.
template<typename _InputIterator, typename _Predicate>
_GLIBCXX20_CONSTEXPR
inline _InputIterator
__find_if_not(_InputIterator __first, _InputIterator __last,
_Predicate __pred)
{
return std::__find_if(__first, __last,
__gnu_cxx::__ops::__negate(__pred));
}
/// Like find_if_not(), but uses and updates a count of the
/// remaining range length instead of comparing against an end
/// iterator.
template<typename _InputIterator, typename _Predicate, typename _Distance>
_GLIBCXX20_CONSTEXPR
_InputIterator
__find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred)
{
for (; __len; --__len, (void) ++__first)
if (!__pred(__first))
break;
return __first;
}
// set_difference
// set_intersection
// set_symmetric_difference
// set_union
// for_each
// find
// find_if
// find_first_of
// adjacent_find
// count
// count_if
// search
// search_n
/**
* This is an helper function for search_n overloaded for forward iterators.
*/
template<typename _ForwardIterator, typename _Integer,
typename _UnaryPredicate>
_GLIBCXX20_CONSTEXPR
_ForwardIterator
__search_n_aux(_ForwardIterator __first, _ForwardIterator __last,
_Integer __count, _UnaryPredicate __unary_pred,
std::forward_iterator_tag)
{
__first = std::__find_if(__first, __last, __unary_pred);
while (__first != __last)
{
typename iterator_traits<_ForwardIterator>::difference_type
__n = __count;
_ForwardIterator __i = __first;
++__i;
while (__i != __last && __n != 1 && __unary_pred(__i))
{
++__i;
--__n;
}
if (__n == 1)
return __first;
if (__i == __last)
return __last;
__first = std::__find_if(++__i, __last, __unary_pred);
}
return __last;
}
/**
* This is an helper function for search_n overloaded for random access
* iterators.
*/
template<typename _RandomAccessIter, typename _Integer,
typename _UnaryPredicate>
_GLIBCXX20_CONSTEXPR
_RandomAccessIter
__search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last,
_Integer __count, _UnaryPredicate __unary_pred,
std::random_access_iterator_tag)
{
typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
_DistanceType;
_DistanceType __tailSize = __last - __first;
_DistanceType __remainder = __count;
while (__remainder <= __tailSize) // the main loop...
{
__first += __remainder;
__tailSize -= __remainder;
// __first here is always pointing to one past the last element of
// next possible match.
_RandomAccessIter __backTrack = __first;
while (__unary_pred(--__backTrack))
{
if (--__remainder == 0)
return (__first - __count); // Success
}
__remainder = __count + 1 - (__first - __backTrack);
}
return __last; // Failure
}
template<typename _ForwardIterator, typename _Integer,
typename _UnaryPredicate>
_GLIBCXX20_CONSTEXPR
_ForwardIterator
__search_n(_ForwardIterator __first, _ForwardIterator __last,
_Integer __count,
_UnaryPredicate __unary_pred)
{
if (__count <= 0)
return __first;
if (__count == 1)
return std::__find_if(__first, __last, __unary_pred);
return std::__search_n_aux(__first, __last, __count, __unary_pred,
std::__iterator_category(__first));
}
// find_end for forward iterators.
template<typename _ForwardIterator1, typename _ForwardIterator2,
typename _BinaryPredicate>
_GLIBCXX20_CONSTEXPR
_ForwardIterator1
__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2, _ForwardIterator2 __last2,
forward_iterator_tag, forward_iterator_tag,
_BinaryPredicate __comp)
{
if (__first2 == __last2)
return __last1;
_ForwardIterator1 __result = __last1;
while (1)
{
_ForwardIterator1 __new_result
= std::__search(__first1, __last1, __first2, __last2, __comp);
if (__new_result == __last1)
return __result;
else
{
__result = __new_result;
__first1 = __new_result;
++__first1;
}
}
}
// find_end for bidirectional iterators (much faster).
template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
typename _BinaryPredicate>
_GLIBCXX20_CONSTEXPR
_BidirectionalIterator1
__find_end(_BidirectionalIterator1 __first1,
_BidirectionalIterator1 __last1,
_BidirectionalIterator2 __first2,
_BidirectionalIterator2 __last2,
bidirectional_iterator_tag, bidirectional_iterator_tag,
_BinaryPredicate __comp)
{
// concept requirements
__glibcxx_function_requires(_BidirectionalIteratorConcept<
_BidirectionalIterator1>)
__glibcxx_function_requires(_BidirectionalIteratorConcept<
_BidirectionalIterator2>)
typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
_RevIterator1 __rlast1(__first1);
_RevIterator2 __rlast2(__first2);
_RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1,
_RevIterator2(__last2), __rlast2,
__comp);
if (__rresult == __rlast1)
return __last1;
else
{
_BidirectionalIterator1 __result = __rresult.base();
std::advance(__result, -std::distance(__first2, __last2));
return __result;
}
}
/**
* @brief Find last matching subsequence in a sequence.
* @ingroup non_mutating_algorithms
* @param __first1 Start of range to search.
* @param __last1 End of range to search.
* @param __first2 Start of sequence to match.
* @param __last2 End of sequence to match.
* @return The last iterator @c i in the range
* @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
* @p *(__first2+N) for each @c N in the range @p
* [0,__last2-__first2), or @p __last1 if no such iterator exists.
*
* Searches the range @p [__first1,__last1) for a sub-sequence that
* compares equal value-by-value with the sequence given by @p
* [__first2,__last2) and returns an iterator to the __first
* element of the sub-sequence, or @p __last1 if the sub-sequence
* is not found. The sub-sequence will be the last such
* subsequence contained in [__first1,__last1).
*
* Because the sub-sequence must lie completely within the range @p
* [__first1,__last1) it must start at a position less than @p
* __last1-(__last2-__first2) where @p __last2-__first2 is the
* length of the sub-sequence. This means that the returned
* iterator @c i will be in the range @p
* [__first1,__last1-(__last2-__first2))
*/
template<typename _ForwardIterator1, typename _ForwardIterator2>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline _ForwardIterator1
find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2, _ForwardIterator2 __last2)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
__glibcxx_function_requires(_EqualOpConcept<
typename iterator_traits<_ForwardIterator1>::value_type,
typename iterator_traits<_ForwardIterator2>::value_type>)
__glibcxx_requires_valid_range(__first1, __last1);
__glibcxx_requires_valid_range(__first2, __last2);
return std::__find_end(__first1, __last1, __first2, __last2,
std::__iterator_category(__first1),
std::__iterator_category(__first2),
__gnu_cxx::__ops::__iter_equal_to_iter());
}
/**
* @brief Find last matching subsequence in a sequence using a predicate.
* @ingroup non_mutating_algorithms
* @param __first1 Start of range to search.
* @param __last1 End of range to search.
* @param __first2 Start of sequence to match.
* @param __last2 End of sequence to match.
* @param __comp The predicate to use.
* @return The last iterator @c i in the range @p
* [__first1,__last1-(__last2-__first2)) such that @c
* predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
* range @p [0,__last2-__first2), or @p __last1 if no such iterator
* exists.
*
* Searches the range @p [__first1,__last1) for a sub-sequence that
* compares equal value-by-value with the sequence given by @p
* [__first2,__last2) using comp as a predicate and returns an
* iterator to the first element of the sub-sequence, or @p __last1
* if the sub-sequence is not found. The sub-sequence will be the
* last such subsequence contained in [__first,__last1).
*
* Because the sub-sequence must lie completely within the range @p
* [__first1,__last1) it must start at a position less than @p
* __last1-(__last2-__first2) where @p __last2-__first2 is the
* length of the sub-sequence. This means that the returned
* iterator @c i will be in the range @p
* [__first1,__last1-(__last2-__first2))
*/
template<typename _ForwardIterator1, typename _ForwardIterator2,
typename _BinaryPredicate>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline _ForwardIterator1
find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2, _ForwardIterator2 __last2,
_BinaryPredicate __comp)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
typename iterator_traits<_ForwardIterator1>::value_type,
typename iterator_traits<_ForwardIterator2>::value_type>)
__glibcxx_requires_valid_range(__first1, __last1);
__glibcxx_requires_valid_range(__first2, __last2);
return std::__find_end(__first1, __last1, __first2, __last2,
std::__iterator_category(__first1),
std::__iterator_category(__first2),
__gnu_cxx::__ops::__iter_comp_iter(__comp));
}
#if __cplusplus >= 201103L
/**
* @brief Checks that a predicate is true for all the elements
* of a sequence.
* @ingroup non_mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __pred A predicate.
* @return True if the check is true, false otherwise.
*
* Returns true if @p __pred is true for each element in the range
* @p [__first,__last), and false otherwise.
*/
template<typename _InputIterator, typename _Predicate>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline bool
all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
{ return __last == std::find_if_not(__first, __last, __pred); }
/**
* @brief Checks that a predicate is false for all the elements
* of a sequence.
* @ingroup non_mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __pred A predicate.
* @return True if the check is true, false otherwise.
*
* Returns true if @p __pred is false for each element in the range
* @p [__first,__last), and false otherwise.
*/
template<typename _InputIterator, typename _Predicate>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline bool
none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
{ return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); }
/**
* @brief Checks that a predicate is true for at least one element
* of a sequence.
* @ingroup non_mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __pred A predicate.
* @return True if the check is true, false otherwise.
*
* Returns true if an element exists in the range @p
* [__first,__last) such that @p __pred is true, and false
* otherwise.
*/
template<typename _InputIterator, typename _Predicate>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline bool
any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
{ return !std::none_of(__first, __last, __pred); }
/**
* @brief Find the first element in a sequence for which a
* predicate is false.
* @ingroup non_mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __pred A predicate.
* @return The first iterator @c i in the range @p [__first,__last)
* such that @p __pred(*i) is false, or @p __last if no such iterator exists.
*/
template<typename _InputIterator, typename _Predicate>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline _InputIterator
find_if_not(_InputIterator __first, _InputIterator __last,
_Predicate __pred)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
return std::__find_if_not(__first, __last,
__gnu_cxx::__ops::__pred_iter(__pred));
}
/**
* @brief Checks whether the sequence is partitioned.
* @ingroup mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __pred A predicate.
* @return True if the range @p [__first,__last) is partioned by @p __pred,
* i.e. if all elements that satisfy @p __pred appear before those that
* do not.
*/
template<typename _InputIterator, typename _Predicate>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline bool
is_partitioned(_InputIterator __first, _InputIterator __last,
_Predicate __pred)
{
__first = std::find_if_not(__first, __last, __pred);
if (__first == __last)
return true;
++__first;
return std::none_of(__first, __last, __pred);
}
/**
* @brief Find the partition point of a partitioned range.
* @ingroup mutating_algorithms
* @param __first An iterator.
* @param __last Another iterator.
* @param __pred A predicate.
* @return An iterator @p mid such that @p all_of(__first, mid, __pred)
* and @p none_of(mid, __last, __pred) are both true.
*/
template<typename _ForwardIterator, typename _Predicate>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
_ForwardIterator
partition_point(_ForwardIterator __first, _ForwardIterator __last,
_Predicate __pred)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
typename iterator_traits<_ForwardIterator>::value_type>)
// A specific debug-mode test will be necessary...
__glibcxx_requires_valid_range(__first, __last);
typedef typename iterator_traits<_ForwardIterator>::difference_type
_DistanceType;
_DistanceType __len = std::distance(__first, __last);
while (__len > 0)
{
_DistanceType __half = __len >> 1;
_ForwardIterator __middle = __first;
std::advance(__middle, __half);
if (__pred(*__middle))
{
__first = __middle;
++__first;
__len = __len - __half - 1;
}
else
__len = __half;
}
return __first;
}
#endif
template<typename _InputIterator, typename _OutputIterator,
typename _Predicate>
_GLIBCXX20_CONSTEXPR
_OutputIterator
__remove_copy_if(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _Predicate __pred)
{
for (; __first != __last; ++__first)
if (!__pred(__first))
{
*__result = *__first;
++__result;
}
return __result;
}
/**
* @brief Copy a sequence, removing elements of a given value.
* @ingroup mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __result An output iterator.
* @param __value The value to be removed.
* @return An iterator designating the end of the resulting sequence.
*
* Copies each element in the range @p [__first,__last) not equal
* to @p __value to the range beginning at @p __result.
* remove_copy() is stable, so the relative order of elements that
* are copied is unchanged.
*/
template<typename _InputIterator, typename _OutputIterator, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline _OutputIterator
remove_copy(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, const _Tp& __value)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_function_requires(_EqualOpConcept<
typename iterator_traits<_InputIterator>::value_type, _Tp>)
__glibcxx_requires_valid_range(__first, __last);
return std::__remove_copy_if(__first, __last, __result,
__gnu_cxx::__ops::__iter_equals_val(__value));
}
/**
* @brief Copy a sequence, removing elements for which a predicate is true.
* @ingroup mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __result An output iterator.
* @param __pred A predicate.
* @return An iterator designating the end of the resulting sequence.
*
* Copies each element in the range @p [__first,__last) for which
* @p __pred returns false to the range beginning at @p __result.
*
* remove_copy_if() is stable, so the relative order of elements that are
* copied is unchanged.
*/
template<typename _InputIterator, typename _OutputIterator,
typename _Predicate>
_GLIBCXX20_CONSTEXPR
inline _OutputIterator
remove_copy_if(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _Predicate __pred)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
return std::__remove_copy_if(__first, __last, __result,
__gnu_cxx::__ops::__pred_iter(__pred));
}
#if __cplusplus >= 201103L
/**
* @brief Copy the elements of a sequence for which a predicate is true.
* @ingroup mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __result An output iterator.
* @param __pred A predicate.
* @return An iterator designating the end of the resulting sequence.
*
* Copies each element in the range @p [__first,__last) for which
* @p __pred returns true to the range beginning at @p __result.
*
* copy_if() is stable, so the relative order of elements that are
* copied is unchanged.
*/
template<typename _InputIterator, typename _OutputIterator,
typename _Predicate>
_GLIBCXX20_CONSTEXPR
_OutputIterator
copy_if(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _Predicate __pred)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
for (; __first != __last; ++__first)
if (__pred(*__first))
{
*__result = *__first;
++__result;
}
return __result;
}
template<typename _InputIterator, typename _Size, typename _OutputIterator>
_GLIBCXX20_CONSTEXPR
_OutputIterator
__copy_n(_InputIterator __first, _Size __n,
_OutputIterator __result, input_iterator_tag)
{
return std::__niter_wrap(__result,
__copy_n_a(__first, __n,
std::__niter_base(__result), true));
}
template<typename _RandomAccessIterator, typename _Size,
typename _OutputIterator>
_GLIBCXX20_CONSTEXPR
inline _OutputIterator
__copy_n(_RandomAccessIterator __first, _Size __n,
_OutputIterator __result, random_access_iterator_tag)
{ return std::copy(__first, __first + __n, __result); }
/**
* @brief Copies the range [first,first+n) into [result,result+n).
* @ingroup mutating_algorithms
* @param __first An input iterator.
* @param __n The number of elements to copy.
* @param __result An output iterator.
* @return result+n.
*
* This inline function will boil down to a call to @c memmove whenever
* possible. Failing that, if random access iterators are passed, then the
* loop count will be known (and therefore a candidate for compiler
* optimizations such as unrolling).
*/
template<typename _InputIterator, typename _Size, typename _OutputIterator>
_GLIBCXX20_CONSTEXPR
inline _OutputIterator
copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
typename iterator_traits<_InputIterator>::value_type>)
const auto __n2 = std::__size_to_integer(__n);
if (__n2 <= 0)
return __result;
__glibcxx_requires_can_increment(__first, __n2);
__glibcxx_requires_can_increment(__result, __n2);
return std::__copy_n(__first, __n2, __result,
std::__iterator_category(__first));
}
/**
* @brief Copy the elements of a sequence to separate output sequences
* depending on the truth value of a predicate.
* @ingroup mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __out_true An output iterator.
* @param __out_false An output iterator.
* @param __pred A predicate.
* @return A pair designating the ends of the resulting sequences.
*
* Copies each element in the range @p [__first,__last) for which
* @p __pred returns true to the range beginning at @p out_true
* and each element for which @p __pred returns false to @p __out_false.
*/
template<typename _InputIterator, typename _OutputIterator1,
typename _OutputIterator2, typename _Predicate>
_GLIBCXX20_CONSTEXPR
pair<_OutputIterator1, _OutputIterator2>
partition_copy(_InputIterator __first, _InputIterator __last,
_OutputIterator1 __out_true, _OutputIterator2 __out_false,
_Predicate __pred)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
for (; __first != __last; ++__first)
if (__pred(*__first))
{
*__out_true = *__first;
++__out_true;
}
else
{
*__out_false = *__first;
++__out_false;
}
return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
}
#endif // C++11
/**
* @brief Remove elements from a sequence.
* @ingroup mutating_algorithms
* @param __first An input iterator.
* @param __last An input iterator.
* @param __value The value to be removed.
* @return An iterator designating the end of the resulting sequence.
*
* All elements equal to @p __value are removed from the range
* @p [__first,__last).
*
* remove() is stable, so the relative order of elements that are
* not removed is unchanged.
*
* Elements between the end of the resulting sequence and @p __last
* are still present, but their value is unspecified.
*/
template<typename _ForwardIterator, typename _Tp>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline _ForwardIterator
remove(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
// concept requirements
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator>)
__glibcxx_function_requires(_EqualOpConcept<
typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
__glibcxx_requires_valid_range(__first, __last);
return std::__remove_if(__first, __last,
__gnu_cxx::__ops::__iter_equals_val(__value));
}
/**
* @brief Remove elements from a sequence using a predicate.
* @ingroup mutating_algorithms
* @param __first A forward iterator.
* @param __last A forward iterator.
* @param __pred A predicate.
* @return An iterator designating the end of the resulting sequence.
*
* All elements for which @p __pred returns true are removed from the range
* @p [__first,__last).
*
* remove_if() is stable, so the relative order of elements that are
* not removed is unchanged.
*
* Elements between the end of the resulting sequence and @p __last
* are still present, but their value is unspecified.
*/
template<typename _ForwardIterator, typename _Predicate>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline _ForwardIterator
remove_if(_ForwardIterator __first, _ForwardIterator __last,
_Predicate __pred)
{
// concept requirements
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator>)
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
typename iterator_traits<_ForwardIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
return std::__remove_if(__first, __last,
__gnu_cxx::__ops::__pred_iter(__pred));
}
template<typename _ForwardIterator, typename _BinaryPredicate>
_GLIBCXX20_CONSTEXPR
_ForwardIterator
__adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
_BinaryPredicate __binary_pred)
{
if (__first == __last)
return __last;
_ForwardIterator __next = __first;
while (++__next != __last)
{
if (__binary_pred(__first, __next))
return __first;
__first = __next;
}
return __last;
}
template<typename _ForwardIterator, typename _BinaryPredicate>
_GLIBCXX20_CONSTEXPR
_ForwardIterator
__unique(_ForwardIterator __first, _ForwardIterator __last,
_BinaryPredicate __binary_pred)
{
// Skip the beginning, if already unique.
__first = std::__adjacent_find(__first, __last, __binary_pred);
if (__first == __last)
return __last;
// Do the real copy work.
_ForwardIterator __dest = __first;
++__first;
while (++__first != __last)
if (!__binary_pred(__dest, __first))
*++__dest = _GLIBCXX_MOVE(*__first);
return ++__dest;
}
/**
* @brief Remove consecutive duplicate values from a sequence.
* @ingroup mutating_algorithms
* @param __first A forward iterator.
* @param __last A forward iterator.
* @return An iterator designating the end of the resulting sequence.
*
* Removes all but the first element from each group of consecutive
* values that compare equal.
* unique() is stable, so the relative order of elements that are
* not removed is unchanged.
* Elements between the end of the resulting sequence and @p __last
* are still present, but their value is unspecified.
*/
template<typename _ForwardIterator>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline _ForwardIterator
unique(_ForwardIterator __first, _ForwardIterator __last)
{
// concept requirements
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator>)
__glibcxx_function_requires(_EqualityComparableConcept<
typename iterator_traits<_ForwardIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
return std::__unique(__first, __last,
__gnu_cxx::__ops::__iter_equal_to_iter());
}
/**
* @brief Remove consecutive values from a sequence using a predicate.
* @ingroup mutating_algorithms
* @param __first A forward iterator.
* @param __last A forward iterator.
* @param __binary_pred A binary predicate.
* @return An iterator designating the end of the resulting sequence.
*
* Removes all but the first element from each group of consecutive
* values for which @p __binary_pred returns true.
* unique() is stable, so the relative order of elements that are
* not removed is unchanged.
* Elements between the end of the resulting sequence and @p __last
* are still present, but their value is unspecified.
*/
template<typename _ForwardIterator, typename _BinaryPredicate>
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
inline _ForwardIterator
unique(_ForwardIterator __first, _ForwardIterator __last,
_BinaryPredicate __binary_pred)
{
// concept requirements
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator>)
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
typename iterator_traits<_ForwardIterator>::value_type,
typename iterator_traits<_ForwardIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
return std::__unique(__first, __last,
__gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
}
/**
* This is an uglified
* unique_copy(_InputIterator, _InputIterator, _OutputIterator,
* _BinaryPredicate)
* overloaded for forward iterators and output iterator as result.
*/
template<typename _ForwardIterator, typename _OutputIterator,
typename _BinaryPredicate>
_GLIBCXX20_CONSTEXPR
_OutputIterator
__unique_copy(_ForwardIterator __first, _ForwardIterator __last,
_OutputIterator __result, _BinaryPredicate __binary_pred,
forward_iterator_tag, output_iterator_tag)
{
// concept requirements -- iterators already checked
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
typename iterator_traits<_ForwardIterator>::value_type,
typename iterator_traits<_ForwardIterator>::value_type>)
_ForwardIterator __next = __first;
*__result = *__first;
while (++__next != __last)
if (!__binary_pred(__first, __next))
{
__first = __next;
*++__result = *__first;
}
return ++__result;
}
/**
* This is an uglified
* unique_copy(_InputIterator, _InputIterator, _OutputIterator,
* _BinaryPredicate)
* overloaded for input iterators and output iterator as result.
*/
template<typename _InputIterator, typename _OutputIterator,
typename _BinaryPredicate>
_GLIBCXX20_CONSTEXPR
_OutputIterator
__unique_copy(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _BinaryPredicate __binary_pred,
input_iterator_tag, output_iterator_tag)
{
// concept requirements -- iterators already checked
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
typename iterator_traits<_InputIterator>::value_type,
typename iterator_traits<_InputIterator>::value_type>)
typename iterator_traits<_InputIterator>::value_type __value = *__first;
__decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred))
__rebound_pred
= __gnu_cxx::__ops::__iter_comp_val(__binary_pred);
*__result = __value;
while (++__first != __last)
if (!__rebound_pred(__first, __value))
{
__value = *__first;
*++__result = __value;
}
return ++__result;
}
/**
* This is an uglified