-
Notifications
You must be signed in to change notification settings - Fork 39
/
stl_algo.h
1450 lines (1332 loc) · 50.1 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
#pragma once
#include "Algorithms/heap/heap_algorithm.h"// for partial_sort()
#include "Function/function_adapter.h"
#include "Iterator/stl_iterator.h"
#include "Utils/stl_tempbuf.h"
namespace MiniSTL {
// adjacent_find:找出一组满足条件的相邻元素
template<class ForwardIterator>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last) {
if (first == last) return last;
ForwardIterator next = first;
while (++next != last) {
if (*first == *next) {
return first;
} else {
first = next;
}
}
return last;
}
template<class ForwardIterator, class BinaryPredicate = equal_to<value_type_t<ForwardIterator>>>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last,
const BinaryPredicate &binary_pred) {
if (first == last) return last;
ForwardIterator next = first;
while (++next != last) {
if (binary_pred(*first, *next)) {
return first;
} else {
first = next;
}
}
return last;
}
// count:运用equality判断有几个与value相等的元素
template<class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type count(
InputIterator first, InputIterator last, T value) {
typename iterator_traits<InputIterator>::difference_type count = 0;
for (; first != last; ++first) {
if (*first == value) count++;
}
return count;
}
// count_if:将指定操作实施于整个区间,并将“令pred的计算结果为true”的元素个数返回
template<class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type count_if(
InputIterator first, InputIterator last, Predicate pred) {
typename iterator_traits<InputIterator>::difference_type count = 0;
for (; first != last; ++first) {
if (pred(*first)) count++;
}
return count;
}
// count:运用equality查找元素
template<class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, T value) {
while (first != last && *first != value) {
++first;
}
return first;
}
// find_if:将指向“令pred的计算结果为true”的第一个元素的迭代器返回
template<class InputIterator, class Predicate>
InputIterator find_if(InputIterator first, InputIterator last, Predicate pred) {
while (first != last && !pred(*first)) {
++first;
}
return first;
}
// find_end:在区间S1[first1,last1)中找出区间S2[first2,last2)的最后出现点,若不存在,返回last1
//显然,若迭代器支持逆向查找则较为便利
template<class ForwardIterator1, class ForwardIterator2>
inline ForwardIterator1 find_end(ForwardIterator1 first1,
ForwardIterator1 last1,
ForwardIterator2 first2,
ForwardIterator2 last2) {
return _find_end(first1, last1, first2, last2, iterator_category_t<ForwardIterator1>(), iterator_category_t<ForwardIterator2>());
}
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 _find_end(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
forward_iterator_tag, forward_iterator_tag) {
if (first2 == last2) {
return last1;
} else {
ForwardIterator1 result = last1;
while (true) {
ForwardIterator1 new_result = search(
first1, last1, first2, last2);//利用search查找首次出现点
if (new_result == last1) {
return result;
} else {
result = new_result;//更新result
first1 = new_result;
++first1;//更换搜索位置
}
}
}
}
template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator1 _find_end(BidirectionalIterator1 first1,
BidirectionalIterator1 last1,
BidirectionalIterator1 first2,
BidirectionalIterator1 last2,
bidirectional_iterator_tag,
bidirectional_iterator_tag) {
//采用反向迭代器
using reviter1 = __reverse_iterator<BidirectionalIterator1>;
using reviter2 = __reverse_iterator<BidirectionalIterator2>;
reviter1 rlast1(first1);
reviter2 rlast2(first2);
reviter1 rresult = search(reviter1(last1), rlast1, reviter2(last2), rlast2);
if (rresult == rlast1) {
return last1;
} else {
BidirectionalIterator1 result = rresult.base();
advance(result, -distance(first2, last2));//回归子序列头部
return result;
}
}
// find_first_of:以S2区间内某些元素为目标,寻找它们在S1区间内首次出现地点。
template<class InputIterator, class ForwardIterator>
InputIterator find_first_of(InputIterator first1, InputIterator last1,
ForwardIterator first2, ForwardIterator last2) {
for (; first1 != last1; ++first1) {
for (ForwardIterator iter = first2; iter != last2; ++iter) {
if (*first1 == *iter) {
return first1;
}
}
}
return last1;
}
// for_each:将f作用于区间元素之上,不能改变元素,欲改变应当使用transform
//具备一个通常被忽略的返回值
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f) {
for (; first != last; ++first) {
f(*first);
}
return f;
}
// generate:将gen的运算结果填写于[first,last),采用assignment
template<class InputIterator, class Generator>
void generate(InputIterator first, InputIterator last, Generator gen) {
for (; first != last; ++first) {
*first = gen();
}
}
// generate_n:将gen的运算结果填写于起点为first,长度为n的区间内,采用assignment
template<class OutputIterator, class Size, class Generator>
OutputIterator generate_n(OutputIterator first, Size n, Generator gen) {
for (; n > 0; --n, ++first) {
*first = gen();
}
return first;
}
// include:判断S2是否被S1所包含,要求有序,默认为升序
template<class InputIterator1, class InputIterator2>
bool include(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
InputIterator2 last2) {
while (first1 != last1 && first2 != last2) {
if (*first2 < *first1) {//一个不在则不可能包含
return false;
} else if (*first1 < *first2) {
++first1;
} else {
++first1;
++first2;
}
}
return first2 == last2;
}
// comp必须满足等价判定表达式(严格弱序或升序)
template<class InputIterator1, class InputIterator2, class Compare>
bool include(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
InputIterator2 last2, Compare comp) {
while (first1 != last1 && first2 != last2) {
if (comp(*first2, *first1)) {
return false;
} else if (comp(*first1, *first2)) {
++first1;
} else {
++first1;
++first2;
}
}
return first2 == last2;
}
// max_element:返回指向序列中数值最大元素的迭代器
template<class ForwardIterator>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last) {
if (first == last) {
return first;
}
ForwardIterator result = first;
while (++first != last) {
if (*result < *first) {
result = first;
}
}
return result;
}
template<class ForwardIterator, class Compare>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last,
Compare comp) {
if (first == last) {
return first;
}
ForwardIterator result = first;
while (++first != last) {
if (comp(*result, *first)) {
result = first;
}
}
return result;
}
// min_element:返回指向序列中数值最小元素的迭代器
template<class ForwardIterator>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last) {
if (first == last) {
return first;
}
ForwardIterator result = first;
while (++first != last) {
if (*first < *result) {
result = first;
}
}
return result;
}
template<class ForwardIterator, class Compare>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
Compare comp) {
if (first == last) {
return first;
}
ForwardIterator result = first;
while (++first != last) {
if (comp(*first, *result)) {
result = first;
}
}
return result;
}
// merge:将有序的S1与S2合并置于另一段空间,合并结果仍然有序,返回一个迭代器指向新区间的last
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result) {
while (first1 != last1 && first2 != last2) {
if (*first2 < *first1) {
*result++ = *first2;
++first2;
} else {
*result++ = *first1;
++first1;
}
}
return copy(first2, last2, copy(first1, last1, result));
}
template<class InputIterator1, class InputIterator2, class OutputIterator,
class Compare>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp) {
while (first1 != last1 && first2 != last2) {
if (comp(*first2, *first1)) {
*result++ = *first2;
++first2;
} else {
*result++ = *first1;
++first1;
}
}
return copy(first2, last2, copy(first1, last1, result));
}
// partition:将被pred判定为true的移动到前列,unstable
// TODO::need complete stable_partition()
template<class BidirectionalIterator, class Predicate>
BidirectionalIterator partition(BidirectionalIterator first,
BidirectionalIterator last, Predicate pred) {
while (true) {
while (true) {
if (first == last) {
return first;
}
if (!pred(*first)) {// 找到需要交换的头部元素
break;
}
++first;
}
do {
if (first == --last) {
return first;
}
} while (!pred(*last));// 找到需要交换的尾部元素
iter_swap(first, last);
++first;
}
}
// remove_copy:将结果拷贝到result
template<class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy(InputIterator first, InputIterator last,
OutputIterator result, T value) {
for (; first != last; ++first) {
if (*first != value) {
*result++ = *first;
}
}
return result;
}
// remove:本身并不做remove操作,只是将元素后移
template<class ForwardIterator, class T>
ForwardIterator remove(ForwardIterator first, ForwardIterator last, T value) {
first = find(first, last, value);//循序找出第一个相等元素
ForwardIterator next = first;
return first == last ? first
: remove_copy(++next, last, first, value);//利用remove_copy (TODO::存在优化空间)
}
// remove_copy_if:在remove_copy的基础上加入了谓词
template<class InputIterator, class OutputIterator, class Predicate>
OutputIterator remove_copy_if(InputIterator first, InputIterator last,
OutputIterator result, Predicate pred) {
for (; first != last; ++first) {
if (!pred(*first)) {
*result++ = *first;
}
}
return result;
}
// remove_if:在remove的基础上加入了谓词
template<class ForwardIterator, class Predicate>
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last,
Predicate pred) {
first = find_if(first, last, pred);
ForwardIterator next = first;
return first == last ? first : remove_copy_if(++next, last, first, pred);
}
// replace:将[first,last)区间内所有old_value替换为new_value
template<class ForwardIterator, class T>
void replace(ForwardIterator first, ForwardIterator last, const T &old_value,
const T &new_value) {
for (; first != last; ++first) {
if (*first == old_value) {
*first = new_value;
}
}
}
// replace_copy:将结果拷贝到result
template<class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy(InputIterator first, InputIterator last,
OutputIterator result, const T &old_value,
const T &new_value) {
for (; first != last; ++first, ++result) {
*result = (*first == old_value ? new_value : *first);
}
return result;
}
// replace_if:所有被pred判定为true的元素将被取代为new_value
template<class ForwardIterator, class Predicate, class T>
void replace_if(ForwardIterator first, ForwardIterator last,
Predicate pred, const T &new_value) {
for (; first != last; ++first) {
if (pred(*first)) {
*first = new_value;
}
}
}
// replace_copy_if:在replace_copy的基础上加入了谓词
template<class InputIterator, class OutputIterator, class Predicate, class T>
OutputIterator replace_copy_if(InputIterator first, InputIterator last,
OutputIterator result, Predicate pred,
const T &new_value) {
for (; first != last; ++first, ++result) {
*result = pred(*first) ? new_value : *first;
}
return result;
}
// reverse:将指定区间颠倒重排,迭代器的性质会对效率产生影响
template<class BidirectionalIterator>
inline void reverse(BidirectionalIterator first, BidirectionalIterator last) {
_reverse(first, last, iterator_category_t<BidirectionalIterator>());
}
template<class BidirectionalIterator>
void _reverse(BidirectionalIterator first, BidirectionalIterator last,
bidirectional_iterator_tag) {
while (true) {
if (first == last || first == --last) {//空集或仅有一个,注意此时last已自减
return;
} else {
iter_swap(first++, last);
}
}
}
template<class RandomAccessIterator>
void _reverse(RandomAccessIterator first, RandomAccessIterator last,
random_access_iterator_tag) {
while (first < last) {//只有random_access_iterator_tag支持operator<
iter_swap(first++, --last);
}
}
//reverse_copy:将指定区间颠倒重排,迭代器的性质会对效率产生影响
template<class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy(BidirectionalIterator first, BidirectionalIterator last,
OutputIterator result) {
while (first != last) {
--last;
*result = *last;
++result;
}
return result;
}
//rotate:将区间[first,middle)与[middle,last)置换,二者的长度可不相等
//根据迭代器性质演变出不同的算法
template<class ForwardIterator>
inline ForwardIterator rotate(ForwardIterator first, ForwardIterator middle,
ForwardIterator last) {
if (first == middle) {
return last;
}
if (last == middle) {
return first;
}
return _rotate(first, middle, last, difference_type_t<ForwardIterator>(),
iterator_category_t<ForwardIterator>(), pointer_t<ForwardIterator>());
}
// rotate-forward
template<class ForwardIterator, class Distance, class T>
ForwardIterator _rotate(ForwardIterator first, ForwardIterator middle,
ForwardIterator last, Distance, forward_iterator_tag, T *) {
ForwardIterator first2 = middle;
do {// 尝试交换两个区间的头部元素
swap(*first++, *first2++);
if (first == middle) {// 若前半部分较短,更新中点
middle = first2;
}
} while (first2 != last);
ForwardIterator new_middle = first;
// 尝试翻转剩下的区间
first2 = middle;
while (first2 != last) {
swap(*first++, *first2++);
if (first == middle) {
middle = first2;
} else if (first2 == last) {
first2 = middle;
}
}
return new_middle;
}
// rotate-bidrectional
template<class BidrectionalIterator, class Distance, class T>
BidrectionalIterator _rotate(BidrectionalIterator first, BidrectionalIterator middle,
BidrectionalIterator last, Distance, bidirectional_iterator_tag, T *) {
reverse(first, middle);
reverse(middle, last);
while (first != middle && middle != last) {
swap(*first++, *--last);
}
if (first == middle) {
reverse(middle, last);
return last;
} else {
reverse(first, middle);
return first;
}
}
// swap_ranges:将区间S1[first,last)内的元素与以first2为起点,长度与S1相同的区间作交换,返回指向S2中最后一个交换的元素的下一位置
//若S2实际长度较小或S1、S2重叠,执行结果未可预期
template<class ForwardIterato1, class ForwardIterator2>
ForwardIterator2 swap_ranges(ForwardIterato1 first1, ForwardIterato1 last1,
ForwardIterator2 first2) {
for (; first1 != last1; ++first1, ++first2) {
iter_swap(first1, first2);
}
return first2;
}
// gcd:求取最大公约数(TODO::效率不如减损术,存在优化空间)
template<class EuclideanRingElement>
EuclideanRingElement gcd(EuclideanRingElement m, EuclideanRingElement n) {
while (n) {
EuclideanRingElement t = m % n;
m = n;
n = t;
}
return m;
}
// rotate-randomaccess
template<class RandomAccessIterator, class Distance, class T>
RandomAccessIterator _rotate(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last, Distance,
random_access_iterator_tag, T *) {
Distance n = last - first;
Distance k = middle - first;
Distance l = n - k;
RandomAccessIterator result = first + (last - middle);
if (k == 0) {
return last;
} else if (k == l) {
swap_ranges(first, middle, middle);
return result;
}
Distance d = gcd(n, k);
for (Distance i = 0; i < d; i++) {
T tmp = *first;
RandomAccessIterator p = first;
if (k < l) {
for (Distance j = 0; j < l / d; j++) {
if (p > first + l) {
*p = *(p - l);
p -= l;
}
*p = *(p + k);
p += k;
}
} else {
for (Distance j = 0; j < k / d - 1; j++) {
if (p < last - k) {
*p = *(p + k);
p += k;
}
*p = *(p - l);
p -= l;
}
}
*p = tmp;
++first;
}
return result;
}
// rotate_copy:并不需要执行rotate,只需要copy的时候注意次序即可
template<class ForwardIterator, class OutputIterator>
OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle,
ForwardIterator last, OutputIterator result) {
return copy(first, middle, copy(middle, last, result));
}
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate, class Distance1,
class Distance2>
inline ForwardIterator1 _search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
const BinaryPredicate &pred,
Distance1, Distance2) {
Distance1 d1 = distance(first1, last1);
Distance2 d2 = distance(first2, last2);
if (d1 < d2) {//若S2长度大于S1,不可能属于
return last1;
}
ForwardIterator1 cur1 = first1;
ForwardIterator2 cur2 = first2;
while (cur2 != last2) { //遍历S2
if (pred(*cur1, *cur2)) {//当前相同,对比下一个
++cur1;
++cur2;
} else {
if (d1 == d2) {//若序列长度一致,则不可能成功
return last1;
} else {
cur1 = ++first1;//调整序列,再来一次
cur2 = first2;
--d1;//排除了一个元素
}
}
}
return first1;
}
// search:在区间S1中查找区间S2首次出现的位置,若不匹配则返回last
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate = equal_to<value_type_t<ForwardIterator1>>>
inline ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2,
ForwardIterator2 last2,
const BinaryPredicate &pred = BinaryPredicate()) {
return _search(first1, last1, first2, last2, pred, difference_type_t<ForwardIterator1>(),
difference_type_t<ForwardIterator2>());
}
template<class ForwardIterator, class Integer, class T, class BinaryPredicate = equal_to<T>>
ForwardIterator search_n(ForwardIterator first, ForwardIterator last,
Integer count, const T &value,
const BinaryPredicate &binary_pred = BinaryPredicate()) {
if (count < 0) {
return first;
} else {
while (first != last) {
if (binary_pred(*first, value)) {
break;
}
++first;
}
while (first != last) {
Integer n = count - 1;// value应当再出现n次
ForwardIterator i = first;
++i;
while (i != last && n != 0 && binary_pred(*i, value)) {// 接下来依然符合条件
++i;
--n;
}
if (n == 0) {
return first;
} else {
while (i != last) {
if (binary_pred(*i, value)) {
break;
}
++i;
}
first = i;
}
}
return last;
}
}
// transform:
//第一版本:以仿函数op作用于[first,last),并以其结果产生一个新序列
//第二版本:以仿函数binary_op作用于一双元素之上,其中一个来自[first1,last),另一个来自[first2,...)
template<class InputIterator, class OutputIterator, class UnaryOperator>
OutputIterator transform(InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperator op) {
for (; first1 != last1; ++first1, ++result) {
*result = op(*first1);
}
return result;
}
template<class InputIterator1, class InputIterator2, class OutputIterator,
class BinaryOperator>
OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperator binary_op) {
for (; first1 != last1; ++first1, ++first2, ++result) {
*result = binary_op(*first1, *first2);
}
return result;
}
// forward
template<class InputIterator, class ForwardIterator, class BinaryPredicate>
ForwardIterator _unique_copy(InputIterator first, InputIterator last,
ForwardIterator result, const BinaryPredicate &pred, forward_iterator_tag) {
*result = *first;//记录第一个元素
while (++first != last) {
if (!pred(*result, *first)) {//不同则记录
*++result = *first;
}
}
return ++result;
}
template<class InputIterator, class OutputIterator, class BinaryPredicate, class T>
OutputIterator _unique_copy_aux(InputIterator first, InputIterator last,
OutputIterator result, const BinaryPredicate &pred, T *) {
T value = *first;
*result = value;
while (++first != last) {
if (!pred(*result, *first)) {
*++result = *first;
}
}
return ++result;
}
// OutputIterator存在限制,必须了解value_type
template<class InputIterator, class OutputIterator, class BinaryPredicate>
OutputIterator _unique_copy(InputIterator first, InputIterator last,
OutputIterator result, const BinaryPredicate &pred, output_iterator_tag) {
return _unique_copy_aux(first, last, result, pred, pointer_t<InputIterator>());
}
// unique_copy:提供copy操作,返回新序列的尾端
// 根据输出迭代器性质作不同的处理
template<class InputIterator, class OutputIterator, class BinaryPredicate = equal_to<value_type_t<InputIterator>>>
OutputIterator unique_copy(InputIterator first, InputIterator last,
OutputIterator result, const BinaryPredicate &pred = BinaryPredicate()) {
if (first == last) {
return result;
}
return _unique_copy(first, last, result, pred, iterator_category_t<OutputIterator>());
}
// unique:只移处相邻重复元素的去重操作
template<class ForwardIterator, class BinaryPredicate = equal_to<value_type_t<ForwardIterator>>>
ForwardIterator unique(ForwardIterator first, ForwardIterator last, const BinaryPredicate &pred = BinaryPredicate()) {
first = adjacent_find(first, last, pred);//找到相邻重复元素的起点
return unique_copy(first, last, first, pred);
}
//lower_bound:二分查找的一个版本,返回指向第一个不小于value的元素的迭代器。亦可认为是第一个可插入位置
template<class ForwardIterator, class T, class Compare = less<T>>
inline ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
const T &value, const Compare &comp = Compare()) {
return _lower_bound(first, last, value, comp,
difference_type_t<ForwardIterator>(),
iterator_category_t<ForwardIterator>());
}
template<class ForwardIterator, class T, class Compare, class Distance>
ForwardIterator _lower_bound(ForwardIterator first, ForwardIterator last,
const T &value, const Compare &comp, Distance, forward_iterator_tag) {
Distance len = 0;
distance(first, last, len);//求取长度
Distance half;
ForwardIterator middle;
while (len > 0) {
half = len >> 1;
middle = first;
advance(middle, half);//令middle指向中间位置
if (comp(*middle, value)) {
first = middle;
++first;//令first指向middle下一位置
len = len - half - 1;
} else {
len = half;
}
}
return first;
}
template<class RandomAccessIterator, class T, class Compare, class Distance>
RandomAccessIterator _lower_bound(RandomAccessIterator first,
RandomAccessIterator last, const T &value, const Compare &comp,
Distance, random_access_iterator_tag) {
Distance len = last - first;
Distance half;
RandomAccessIterator middle;
while (len > 0) {
half = len >> 1;
middle = first + half;
if (comp(*middle, value)) {
first = middle + 1;
len = len - half - 1;
} else {
len = half;
}
}
return first;
}
// upper_bound:二分查找的一个版本,返回可插入的最后一个位置
template<class ForwardIterator, class T, class Compare = less<T>>
inline ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last,
const T &value, const Compare &comp = Compare()) {
return _upper_bound(first, last, value, comp, difference_type_t<ForwardIterator>(),
iterator_category_t<ForwardIterator>());
}
template<class ForwardIterator, class T, class Compare, class Distance>
ForwardIterator _upper_bound(ForwardIterator first, ForwardIterator last,
const T &value, const Compare &comp, Distance,
forward_iterator_tag) {
Distance len = 0;
distance(first, last, len);//求取长度
Distance half;
ForwardIterator middle;
while (len > 0) {
half = len >> 1;
middle = first;
advance(middle, half);//令middle指向中间位置
if (comp(value, *middle)) {
len = half;
} else {
first = middle;
++first;
len = len - half - 1;
}
}
return first;
}
template<class RandomAccessIterator, class T, class Compare, class Distance>
RandomAccessIterator _upper_bound(RandomAccessIterator first,
RandomAccessIterator last, const T &value,
const Compare &comp,
Distance, random_access_iterator_tag) {
Distance len = last - first;
Distance half;
RandomAccessIterator middle;
while (len > 0) {
half = len >> 1;
middle = first + half;
if (comp(value, *middle)) {
len = half;
} else {
first = middle + 1;
len = len - half - 1;
}
}
return first;
}
// binary_search:在区间内查找元素,存在则返回true,否则返回false
//本质上只需要调用lower_bound即可
template<class ForwardIterator, class T>
bool binary_search(ForwardIterator first, ForwardIterator last,
const T &value) {
ForwardIterator i = lower_bound(first, last, value);
return i != last && !(value < *i);//这里用的是等价判定而非相等判定
}
// next_permutation:字典序下的下一个排列
//算法精要:从后向前,找出一组相邻元素,记第一元素为*i,第二元素为*ii,此二者满足*i<*ii
//再次从后向前,找出第一个大于*i的元素,记为*j,将i与j对调,再将ii之后的元素颠倒重排即可
template<class BidirectionIterator, class Pred = less<value_type_t<BidirectionIterator>>>
bool next_permutation(BidirectionIterator first, BidirectionIterator last, Pred pred = less<value_type_t<BidirectionIterator>>()) {
if (first == last) {
return false;
}
BidirectionIterator i = first;
++i;
if (i == last) {//仅有一个元素
return false;
}
i = last;
--i;
for (;;) {
BidirectionIterator ii = i;
--i;
if (pred(*i, *ii)) {
BidirectionIterator j = last;
while (!pred(*i, *--j))
;//此时j必然存在,最不济也是ii
iter_swap(i, j);
reverse(ii, last);
return true;
}
if (i == first) {// i已移动至队首且尚未找到ii,直接颠倒全部
reverse(first, last);
return false;
}
}
}
// prev_permutation:字典序下的上一个排列
//算法精要:从后向前,找出一组相邻元素,记第一元素为*i,第二元素为*ii,此二者满足*i>*ii
//再次从后向前,找出第一个小于*i的元素,记为*j,将i与j对调,再将ii之后的元素颠倒重排即可
template<class BidirectionIterator, class Pred = less<value_type_t<BidirectionIterator>>>
bool prev_permutation(BidirectionIterator first, BidirectionIterator last, Pred pred = less<value_type_t<BidirectionIterator>>()) {
if (first == last) {
return false;
}
BidirectionIterator i = first;
++i;
if (i == last) {//仅有一个元素
return false;
}
i = last;
--i;
for (;;) {
BidirectionIterator ii = i;
--i;
if (pred(*ii, *i)) {
BidirectionIterator j = last;
while (!pred(*--j, *i))
;//此时j必然存在,最不济也是ii
iter_swap(i, j);
reverse(ii, last);
return true;
}
if (i == first) {// i已移动至队首且尚未找到ii,直接颠倒全部
reverse(first, last);
return false;
}
}
}
template<class RandomAccessIterator, class Distance>
void _random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Distance) {
if (first == last) {
return;
}
for (RandomAccessIterator i = first + 1; i != last; ++i) {
iter_swap(i, first + Distance(rand() % ((i - first) + 1)));
}
}
// random_shuffle:将区间[first,last)内的元素随机重排,即对于存在N个元素的区间,从N!的可能性中挑出一种
//存在两个版本:一个采用内部随机数生成器,另一个则是产生随机数的仿函数,值得注意的是仿函数pass-by-reference而非value,因为该仿函数存在局部状态
template<class RandomAccessIterator>
inline void random_shuffle(RandomAccessIterator first,
RandomAccessIterator last) {
_random_shuffle(first, last, difference_type_t<RandomAccessIterator>());
}
template<class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
RandomNumberGenerator &rand) {
if (first == last) {
return;
}
for (RandomAccessIterator i = first + 1; i != last; ++i) {
iter_swap(i, first + rand(i - first + 1));
}
}
template<class RandomAccessIterator, class Compare, class T>
inline void _partial_sort(RandomAccessIterator first,
RandomAccessIterator middle, RandomAccessIterator last,
const Compare &comp,
T *) {
make_heap(first, middle, comp);
for (RandomAccessIterator i = middle; i != last; ++i) {
if (comp(*i, *first)) {
pop_heap_aux(first, middle, i, T(*i), comp);
}
}
sort_heap(first, middle, comp);
}
//partial_sort:接收迭代器first,middle,last,使序列中的middle-first个元素以递增序置于[first,middle)中
//算法精要:将[first,middle)做成最大堆,然后将[middle,last)中的元素与max-heap中的元素比较
//若小于最大值,交换位置,并重新维持max-heap (在算法中的直接体现为pop_heap)
//因此当走完[first,last)时较大元素已被抽离[first,middle),最后再次以sort_heap对[first,middle)作出排序
template<class RandomAccessIterator, class Compare = less<value_type_t<RandomAccessIterator>>>
inline void partial_sort(RandomAccessIterator first,
RandomAccessIterator middle,
RandomAccessIterator last,
const Compare &comp = Compare()) {
_partial_sort(first, middle, last, comp, pointer_t<RandomAccessIterator>());
}
// TODO::need partial_sort_copy, 其行为类似于partial_sort
template<class RandomAccessIterator, class Compare, class T>
void _unguarded_linear_insert(RandomAccessIterator last, T value, const Compare &comp) {
RandomAccessIterator next = last;
--next;
while (comp(value, *next)) {
*last = *next;
last = next;
--next;
}
*last = value;
}