forked from vim-scripts/tags-for-std-cpp-STL-streams-...
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stl_algo.h
5504 lines (5130 loc) · 182 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, 2002, 2003, 2004, 2005, 2006
// 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 2, 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.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
/*
*
* 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 stl_algo.h
* This is an internal header file, included by other library headers.
* You should not attempt to use it directly.
*/
#ifndef _ALGO_H
#define _ALGO_H 1
#include <bits/stl_heap.h>
#include <bits/stl_tempbuf.h> // for _Temporary_buffer
#include <debug/debug.h>
// See concept_check.h for the __glibcxx_*_requires macros.
namespace std {
/**
* @brief Find the median of three values.
* @param a A value.
* @param b A value.
* @param c A value.
* @return One of @p a, @p b or @p c.
*
* If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
* then the value returned will be @c m.
* This is an SGI extension.
* @ingroup SGIextensions
*/
template<typename _Tp>
inline const _Tp&
__median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
{
// concept requirements
__glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
if (__a < __b)
if (__b < __c)
return __b;
else if (__a < __c)
return __c;
else
return __a;
else if (__a < __c)
return __a;
else if (__b < __c)
return __c;
else
return __b;
}
/**
* @brief Find the median of three values using a predicate for comparison.
* @param a A value.
* @param b A value.
* @param c A value.
* @param comp A binary predicate.
* @return One of @p a, @p b or @p c.
*
* If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
* and @p comp(m,n) are both true then the value returned will be @c m.
* This is an SGI extension.
* @ingroup SGIextensions
*/
template<typename _Tp, typename _Compare>
inline const _Tp&
__median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
{
// concept requirements
__glibcxx_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>)
if (__comp(__a, __b))
if (__comp(__b, __c))
return __b;
else if (__comp(__a, __c))
return __c;
else
return __a;
else if (__comp(__a, __c))
return __a;
else if (__comp(__b, __c))
return __c;
else
return __b;
}
/**
* @brief Apply a function to every element of a sequence.
* @param first An input iterator.
* @param last An input iterator.
* @param f A unary function object.
* @return @p f.
*
* Applies the function object @p f to each element in the range
* @p [first,last). @p f must not modify the order of the sequence.
* If @p f has a return value it is ignored.
*/
template<typename _InputIterator, typename _Function>
_Function
for_each(_InputIterator __first, _InputIterator __last, _Function __f)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_requires_valid_range(__first, __last);
for ( ; __first != __last; ++__first)
__f(*__first);
return __f;
}
/**
* @if maint
* This is an overload used by find() for the Input Iterator case.
* @endif
*/
template<typename _InputIterator, typename _Tp>
inline _InputIterator
__find(_InputIterator __first, _InputIterator __last,
const _Tp& __val, input_iterator_tag)
{
while (__first != __last && !(*__first == __val))
++__first;
return __first;
}
/**
* @if maint
* This is an overload used by find_if() for the Input Iterator case.
* @endif
*/
template<typename _InputIterator, typename _Predicate>
inline _InputIterator
__find_if(_InputIterator __first, _InputIterator __last,
_Predicate __pred, input_iterator_tag)
{
while (__first != __last && !__pred(*__first))
++__first;
return __first;
}
/**
* @if maint
* This is an overload used by find() for the RAI case.
* @endif
*/
template<typename _RandomAccessIterator, typename _Tp>
_RandomAccessIterator
__find(_RandomAccessIterator __first, _RandomAccessIterator __last,
const _Tp& __val, random_access_iterator_tag)
{
typename iterator_traits<_RandomAccessIterator>::difference_type
__trip_count = (__last - __first) >> 2;
for ( ; __trip_count > 0 ; --__trip_count)
{
if (*__first == __val)
return __first;
++__first;
if (*__first == __val)
return __first;
++__first;
if (*__first == __val)
return __first;
++__first;
if (*__first == __val)
return __first;
++__first;
}
switch (__last - __first)
{
case 3:
if (*__first == __val)
return __first;
++__first;
case 2:
if (*__first == __val)
return __first;
++__first;
case 1:
if (*__first == __val)
return __first;
++__first;
case 0:
default:
return __last;
}
}
/**
* @if maint
* This is an overload used by find_if() for the RAI case.
* @endif
*/
template<typename _RandomAccessIterator, typename _Predicate>
_RandomAccessIterator
__find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
_Predicate __pred, random_access_iterator_tag)
{
typename iterator_traits<_RandomAccessIterator>::difference_type
__trip_count = (__last - __first) >> 2;
for ( ; __trip_count > 0 ; --__trip_count)
{
if (__pred(*__first))
return __first;
++__first;
if (__pred(*__first))
return __first;
++__first;
if (__pred(*__first))
return __first;
++__first;
if (__pred(*__first))
return __first;
++__first;
}
switch (__last - __first)
{
case 3:
if (__pred(*__first))
return __first;
++__first;
case 2:
if (__pred(*__first))
return __first;
++__first;
case 1:
if (__pred(*__first))
return __first;
++__first;
case 0:
default:
return __last;
}
}
/**
* @if maint
* This is an overload of find() for streambuf iterators.
* @endif
*/
template<typename _CharT>
typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
istreambuf_iterator<_CharT> >::__type
find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>,
const _CharT&);
/**
* @brief Find the first occurrence of a value in a sequence.
* @param first An input iterator.
* @param last An input iterator.
* @param val The value to find.
* @return The first iterator @c i in the range @p [first,last)
* such that @c *i == @p val, or @p last if no such iterator exists.
*/
template<typename _InputIterator, typename _Tp>
inline _InputIterator
find(_InputIterator __first, _InputIterator __last,
const _Tp& __val)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_function_requires(_EqualOpConcept<
typename iterator_traits<_InputIterator>::value_type, _Tp>)
__glibcxx_requires_valid_range(__first, __last);
return std::__find(__first, __last, __val,
std::__iterator_category(__first));
}
/**
* @brief Find the first element in a sequence for which a predicate is true.
* @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 true, or @p last if no such iterator exists.
*/
template<typename _InputIterator, typename _Predicate>
inline _InputIterator
find_if(_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(__first, __last, __pred,
std::__iterator_category(__first));
}
/**
* @brief Find two adjacent values in a sequence that are equal.
* @param first A forward iterator.
* @param last A forward iterator.
* @return The first iterator @c i such that @c i and @c i+1 are both
* valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
* or @p last if no such iterator exists.
*/
template<typename _ForwardIterator>
_ForwardIterator
adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
__glibcxx_function_requires(_EqualityComparableConcept<
typename iterator_traits<_ForwardIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
if (__first == __last)
return __last;
_ForwardIterator __next = __first;
while(++__next != __last)
{
if (*__first == *__next)
return __first;
__first = __next;
}
return __last;
}
/**
* @brief Find two adjacent values in a sequence using a predicate.
* @param first A forward iterator.
* @param last A forward iterator.
* @param binary_pred A binary predicate.
* @return The first iterator @c i such that @c i and @c i+1 are both
* valid iterators in @p [first,last) and such that
* @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
* exists.
*/
template<typename _ForwardIterator, typename _BinaryPredicate>
_ForwardIterator
adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
_BinaryPredicate __binary_pred)
{
// concept requirements
__glibcxx_function_requires(_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);
if (__first == __last)
return __last;
_ForwardIterator __next = __first;
while(++__next != __last)
{
if (__binary_pred(*__first, *__next))
return __first;
__first = __next;
}
return __last;
}
/**
* @brief Count the number of copies of a value in a sequence.
* @param first An input iterator.
* @param last An input iterator.
* @param value The value to be counted.
* @return The number of iterators @c i in the range @p [first,last)
* for which @c *i == @p value
*/
template<typename _InputIterator, typename _Tp>
typename iterator_traits<_InputIterator>::difference_type
count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_function_requires(_EqualOpConcept<
typename iterator_traits<_InputIterator>::value_type, _Tp>)
__glibcxx_requires_valid_range(__first, __last);
typename iterator_traits<_InputIterator>::difference_type __n = 0;
for ( ; __first != __last; ++__first)
if (*__first == __value)
++__n;
return __n;
}
/**
* @brief Count the elements of a sequence for which a predicate is true.
* @param first An input iterator.
* @param last An input iterator.
* @param pred A predicate.
* @return The number of iterators @c i in the range @p [first,last)
* for which @p pred(*i) is true.
*/
template<typename _InputIterator, typename _Predicate>
typename iterator_traits<_InputIterator>::difference_type
count_if(_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);
typename iterator_traits<_InputIterator>::difference_type __n = 0;
for ( ; __first != __last; ++__first)
if (__pred(*__first))
++__n;
return __n;
}
/**
* @brief Search a sequence for a matching sub-sequence.
* @param first1 A forward iterator.
* @param last1 A forward iterator.
* @param first2 A forward iterator.
* @param last2 A forward iterator.
* @return The first 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.
*
* 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>
_ForwardIterator1
search(_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);
// Test for empty ranges
if (__first1 == __last1 || __first2 == __last2)
return __first1;
// Test for a pattern of length 1.
_ForwardIterator2 __tmp(__first2);
++__tmp;
if (__tmp == __last2)
return std::find(__first1, __last1, *__first2);
// General case.
_ForwardIterator2 __p1, __p;
__p1 = __first2; ++__p1;
_ForwardIterator1 __current = __first1;
while (__first1 != __last1)
{
__first1 = std::find(__first1, __last1, *__first2);
if (__first1 == __last1)
return __last1;
__p = __p1;
__current = __first1;
if (++__current == __last1)
return __last1;
while (*__current == *__p)
{
if (++__p == __last2)
return __first1;
if (++__current == __last1)
return __last1;
}
++__first1;
}
return __first1;
}
/**
* @brief Search a sequence for a matching sub-sequence using a predicate.
* @param first1 A forward iterator.
* @param last1 A forward iterator.
* @param first2 A forward iterator.
* @param last2 A forward iterator.
* @param predicate A binary predicate.
* @return The first iterator @c i in the range
* @p [first1,last1-(last2-first2)) such that
* @p predicate(*(i+N),*(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 @p predicate to determine equality, and returns an iterator
* to the first element of the sub-sequence, or @p last1 if no such
* iterator exists.
*
* @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
*/
template<typename _ForwardIterator1, typename _ForwardIterator2,
typename _BinaryPredicate>
_ForwardIterator1
search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2, _ForwardIterator2 __last2,
_BinaryPredicate __predicate)
{
// 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);
// Test for empty ranges
if (__first1 == __last1 || __first2 == __last2)
return __first1;
// Test for a pattern of length 1.
_ForwardIterator2 __tmp(__first2);
++__tmp;
if (__tmp == __last2)
{
while (__first1 != __last1 && !__predicate(*__first1, *__first2))
++__first1;
return __first1;
}
// General case.
_ForwardIterator2 __p1, __p;
__p1 = __first2; ++__p1;
_ForwardIterator1 __current = __first1;
while (__first1 != __last1)
{
while (__first1 != __last1)
{
if (__predicate(*__first1, *__first2))
break;
++__first1;
}
while (__first1 != __last1 && !__predicate(*__first1, *__first2))
++__first1;
if (__first1 == __last1)
return __last1;
__p = __p1;
__current = __first1;
if (++__current == __last1)
return __last1;
while (__predicate(*__current, *__p))
{
if (++__p == __last2)
return __first1;
if (++__current == __last1)
return __last1;
}
++__first1;
}
return __first1;
}
/**
* @if maint
* This is an uglified
* search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
* overloaded for forward iterators.
* @endif
*/
template<typename _ForwardIterator, typename _Integer, typename _Tp>
_ForwardIterator
__search_n(_ForwardIterator __first, _ForwardIterator __last,
_Integer __count, const _Tp& __val,
std::forward_iterator_tag)
{
__first = std::find(__first, __last, __val);
while (__first != __last)
{
typename iterator_traits<_ForwardIterator>::difference_type
__n = __count;
_ForwardIterator __i = __first;
++__i;
while (__i != __last && __n != 1 && *__i == __val)
{
++__i;
--__n;
}
if (__n == 1)
return __first;
if (__i == __last)
return __last;
__first = std::find(++__i, __last, __val);
}
return __last;
}
/**
* @if maint
* This is an uglified
* search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
* overloaded for random access iterators.
* @endif
*/
template<typename _RandomAccessIter, typename _Integer, typename _Tp>
_RandomAccessIter
__search_n(_RandomAccessIter __first, _RandomAccessIter __last,
_Integer __count, const _Tp& __val,
std::random_access_iterator_tag)
{
typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
_DistanceType;
_DistanceType __tailSize = __last - __first;
const _DistanceType __pattSize = __count;
if (__tailSize < __pattSize)
return __last;
const _DistanceType __skipOffset = __pattSize - 1;
_RandomAccessIter __lookAhead = __first + __skipOffset;
__tailSize -= __pattSize;
while (1) // the main loop...
{
// __lookAhead here is always pointing to the last element of next
// possible match.
while (!(*__lookAhead == __val)) // the skip loop...
{
if (__tailSize < __pattSize)
return __last; // Failure
__lookAhead += __pattSize;
__tailSize -= __pattSize;
}
_DistanceType __remainder = __skipOffset;
for (_RandomAccessIter __backTrack = __lookAhead - 1;
*__backTrack == __val; --__backTrack)
{
if (--__remainder == 0)
return (__lookAhead - __skipOffset); // Success
}
if (__remainder > __tailSize)
return __last; // Failure
__lookAhead += __remainder;
__tailSize -= __remainder;
}
}
/**
* @brief Search a sequence for a number of consecutive values.
* @param first A forward iterator.
* @param last A forward iterator.
* @param count The number of consecutive values.
* @param val The value to find.
* @return The first iterator @c i in the range @p [first,last-count)
* such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
* or @p last if no such iterator exists.
*
* Searches the range @p [first,last) for @p count consecutive elements
* equal to @p val.
*/
template<typename _ForwardIterator, typename _Integer, typename _Tp>
_ForwardIterator
search_n(_ForwardIterator __first, _ForwardIterator __last,
_Integer __count, const _Tp& __val)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
__glibcxx_function_requires(_EqualOpConcept<
typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
__glibcxx_requires_valid_range(__first, __last);
if (__count <= 0)
return __first;
if (__count == 1)
return std::find(__first, __last, __val);
return std::__search_n(__first, __last, __count, __val,
std::__iterator_category(__first));
}
/**
* @if maint
* This is an uglified
* search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
* _BinaryPredicate)
* overloaded for forward iterators.
* @endif
*/
template<typename _ForwardIterator, typename _Integer, typename _Tp,
typename _BinaryPredicate>
_ForwardIterator
__search_n(_ForwardIterator __first, _ForwardIterator __last,
_Integer __count, const _Tp& __val,
_BinaryPredicate __binary_pred, std::forward_iterator_tag)
{
while (__first != __last && !__binary_pred(*__first, __val))
++__first;
while (__first != __last)
{
typename iterator_traits<_ForwardIterator>::difference_type
__n = __count;
_ForwardIterator __i = __first;
++__i;
while (__i != __last && __n != 1 && __binary_pred(*__i, __val))
{
++__i;
--__n;
}
if (__n == 1)
return __first;
if (__i == __last)
return __last;
__first = ++__i;
while (__first != __last && !__binary_pred(*__first, __val))
++__first;
}
return __last;
}
/**
* @if maint
* This is an uglified
* search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
* _BinaryPredicate)
* overloaded for random access iterators.
* @endif
*/
template<typename _RandomAccessIter, typename _Integer, typename _Tp,
typename _BinaryPredicate>
_RandomAccessIter
__search_n(_RandomAccessIter __first, _RandomAccessIter __last,
_Integer __count, const _Tp& __val,
_BinaryPredicate __binary_pred, std::random_access_iterator_tag)
{
typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
_DistanceType;
_DistanceType __tailSize = __last - __first;
const _DistanceType __pattSize = __count;
if (__tailSize < __pattSize)
return __last;
const _DistanceType __skipOffset = __pattSize - 1;
_RandomAccessIter __lookAhead = __first + __skipOffset;
__tailSize -= __pattSize;
while (1) // the main loop...
{
// __lookAhead here is always pointing to the last element of next
// possible match.
while (!__binary_pred(*__lookAhead, __val)) // the skip loop...
{
if (__tailSize < __pattSize)
return __last; // Failure
__lookAhead += __pattSize;
__tailSize -= __pattSize;
}
_DistanceType __remainder = __skipOffset;
for (_RandomAccessIter __backTrack = __lookAhead - 1;
__binary_pred(*__backTrack, __val); --__backTrack)
{
if (--__remainder == 0)
return (__lookAhead - __skipOffset); // Success
}
if (__remainder > __tailSize)
return __last; // Failure
__lookAhead += __remainder;
__tailSize -= __remainder;
}
}
/**
* @brief Search a sequence for a number of consecutive values using a
* predicate.
* @param first A forward iterator.
* @param last A forward iterator.
* @param count The number of consecutive values.
* @param val The value to find.
* @param binary_pred A binary predicate.
* @return The first iterator @c i in the range @p [first,last-count)
* such that @p binary_pred(*(i+N),val) is true for each @c N in the
* range @p [0,count), or @p last if no such iterator exists.
*
* Searches the range @p [first,last) for @p count consecutive elements
* for which the predicate returns true.
*/
template<typename _ForwardIterator, typename _Integer, typename _Tp,
typename _BinaryPredicate>
_ForwardIterator
search_n(_ForwardIterator __first, _ForwardIterator __last,
_Integer __count, const _Tp& __val,
_BinaryPredicate __binary_pred)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
__glibcxx_requires_valid_range(__first, __last);
if (__count <= 0)
return __first;
if (__count == 1)
{
while (__first != __last && !__binary_pred(*__first, __val))
++__first;
return __first;
}
return std::__search_n(__first, __last, __count, __val, __binary_pred,
std::__iterator_category(__first));
}
/**
* @brief Swap the elements of two sequences.
* @param first1 A forward iterator.
* @param last1 A forward iterator.
* @param first2 A forward iterator.
* @return An iterator equal to @p first2+(last1-first1).
*
* Swaps each element in the range @p [first1,last1) with the
* corresponding element in the range @p [first2,(last1-first1)).
* The ranges must not overlap.
*/
template<typename _ForwardIterator1, typename _ForwardIterator2>
_ForwardIterator2
swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2)
{
// concept requirements
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator1>)
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator2>)
__glibcxx_function_requires(_ConvertibleConcept<
typename iterator_traits<_ForwardIterator1>::value_type,
typename iterator_traits<_ForwardIterator2>::value_type>)
__glibcxx_function_requires(_ConvertibleConcept<
typename iterator_traits<_ForwardIterator2>::value_type,
typename iterator_traits<_ForwardIterator1>::value_type>)
__glibcxx_requires_valid_range(__first1, __last1);
for ( ; __first1 != __last1; ++__first1, ++__first2)
std::iter_swap(__first1, __first2);
return __first2;
}
/**
* @brief Perform an operation on a sequence.
* @param first An input iterator.
* @param last An input iterator.
* @param result An output iterator.
* @param unary_op A unary operator.
* @return An output iterator equal to @p result+(last-first).
*
* Applies the operator to each element in the input range and assigns
* the results to successive elements of the output sequence.
* Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
* range @p [0,last-first).
*
* @p unary_op must not alter its argument.
*/
template<typename _InputIterator, typename _OutputIterator,
typename _UnaryOperation>
_OutputIterator
transform(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _UnaryOperation __unary_op)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
// "the type returned by a _UnaryOperation"
__typeof__(__unary_op(*__first))>)
__glibcxx_requires_valid_range(__first, __last);
for ( ; __first != __last; ++__first, ++__result)
*__result = __unary_op(*__first);
return __result;
}
/**
* @brief Perform an operation on corresponding elements of two sequences.
* @param first1 An input iterator.
* @param last1 An input iterator.
* @param first2 An input iterator.
* @param result An output iterator.
* @param binary_op A binary operator.
* @return An output iterator equal to @p result+(last-first).
*
* Applies the operator to the corresponding elements in the two
* input ranges and assigns the results to successive elements of the
* output sequence.
* Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
* @c N in the range @p [0,last1-first1).
*
* @p binary_op must not alter either of its arguments.
*/
template<typename _InputIterator1, typename _InputIterator2,
typename _OutputIterator, typename _BinaryOperation>
_OutputIterator
transform(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _OutputIterator __result,
_BinaryOperation __binary_op)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
// "the type returned by a _BinaryOperation"
__typeof__(__binary_op(*__first1,*__first2))>)
__glibcxx_requires_valid_range(__first1, __last1);
for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
*__result = __binary_op(*__first1, *__first2);
return __result;
}
/**
* @brief Replace each occurrence of one value in a sequence with another
* value.
* @param first A forward iterator.
* @param last A forward iterator.
* @param old_value The value to be replaced.
* @param new_value The replacement value.
* @return replace() returns no value.
*
* For each iterator @c i in the range @p [first,last) if @c *i ==
* @p old_value then the assignment @c *i = @p new_value is performed.
*/
template<typename _ForwardIterator, typename _Tp>
void
replace(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __old_value, const _Tp& __new_value)
{
// concept requirements
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator>)
__glibcxx_function_requires(_EqualOpConcept<
typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
__glibcxx_function_requires(_ConvertibleConcept<_Tp,
typename iterator_traits<_ForwardIterator>::value_type>)