-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathalgo.h
2744 lines (2597 loc) · 78.6 KB
/
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
#ifndef MYTINYSTL_ALGO_H_
#define MYTINYSTL_ALGO_H_
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4244)
#endif
// 这个头文件包含了 mystl 的一系列算法
#include <cstddef>
#include <ctime>
#include "algobase.h"
#include "memory.h"
#include "heap_algo.h"
#include "functional.h"
namespace mystl
{
/*****************************************************************************************/
// all_of
// 检查[first, last)内是否全部元素都满足一元操作 unary_pred 为 true 的情况,满足则返回 true
/*****************************************************************************************/
template <class InputIter, class UnaryPredicate>
bool all_of(InputIter first, InputIter last, UnaryPredicate unary_pred)
{
for (; first != last; ++first)
{
if (!unary_pred(*first))
return false;
}
return true;
}
/*****************************************************************************************/
// any_of
// 检查[first, last)内是否存在某个元素满足一元操作 unary_pred 为 true 的情况,满足则返回 true
/*****************************************************************************************/
template <class InputIter, class UnaryPredicate>
bool any_of(InputIter first, InputIter last, UnaryPredicate unary_pred)
{
for (; first != last; ++first)
{
if (unary_pred(*first))
return true;
}
return false;
}
/*****************************************************************************************/
// none_of
// 检查[first, last)内是否全部元素都不满足一元操作 unary_pred 为 true 的情况,满足则返回 true
/*****************************************************************************************/
template <class InputIter, class UnaryPredicate>
bool none_of(InputIter first, InputIter last, UnaryPredicate unary_pred)
{
for (; first != last; ++first)
{
if (unary_pred(*first))
return false;
}
return true;
}
/*****************************************************************************************/
// count
// 对[first, last)区间内的元素与给定值进行比较,缺省使用 operator==,返回元素相等的个数
/*****************************************************************************************/
template <class InputIter, class T>
size_t count(InputIter first, InputIter last, const T& value)
{
size_t n = 0;
for (; first != last; ++first)
{
if (*first == value)
++n;
}
return n;
}
/*****************************************************************************************/
// count_if
// 对[first, last)区间内的每个元素都进行一元 unary_pred 操作,返回结果为 true 的个数
/*****************************************************************************************/
template <class InputIter, class UnaryPredicate>
size_t count_if(InputIter first, InputIter last, UnaryPredicate unary_pred)
{
size_t n = 0;
for (; first != last; ++first)
{
if (unary_pred(*first))
++n;
}
return n;
}
/*****************************************************************************************/
// find
// 在[first, last)区间内找到等于 value 的元素,返回指向该元素的迭代器
/*****************************************************************************************/
template <class InputIter, class T>
InputIter
find(InputIter first, InputIter last, const T& value)
{
while (first != last && *first != value)
++first;
return first;
}
/*****************************************************************************************/
// find_if
// 在[first, last)区间内找到第一个令一元操作 unary_pred 为 true 的元素并返回指向该元素的迭代器
/*****************************************************************************************/
template <class InputIter, class UnaryPredicate>
InputIter
find_if(InputIter first, InputIter last, UnaryPredicate unary_pred)
{
while (first != last && !unary_pred(*first))
++first;
return first;
}
/*****************************************************************************************/
// find_if_not
// 在[first, last)区间内找到第一个令一元操作 unary_pred 为 false 的元素并返回指向该元素的迭代器
/*****************************************************************************************/
template <class InputIter, class UnaryPredicate>
InputIter
find_if_not(InputIter first, InputIter last, UnaryPredicate unary_pred)
{
while (first != last && unary_pred(*first))
++first;
return first;
}
/*****************************************************************************************/
// search
// 在[first1, last1)中查找[first2, last2)的首次出现点
/*****************************************************************************************/
template <class ForwardIter1, class ForwardIter2>
ForwardIter1
search(ForwardIter1 first1, ForwardIter1 last1,
ForwardIter2 first2, ForwardIter2 last2)
{
auto d1 = mystl::distance(first1, last1);
auto d2 = mystl::distance(first2, last2);
if (d1 < d2)
return last1;
auto current1 = first1;
auto current2 = first2;
while (current2 != last2)
{
if (*current1 == *current2)
{
++current1;
++current2;
}
else
{
if (d1 == d2)
{
return last1;
}
else
{
current1 = ++first1;
current2 = first2;
--d1;
}
}
}
return first1;
}
// 重载版本使用函数对象 comp 代替比较操作
template <class ForwardIter1, class ForwardIter2, class Compared>
ForwardIter1
search(ForwardIter1 first1, ForwardIter1 last1,
ForwardIter2 first2, ForwardIter2 last2, Compared comp)
{
auto d1 = mystl::distance(first1, last1);
auto d2 = mystl::distance(first2, last2);
if (d1 < d2)
return last1;
auto current1 = first1;
auto current2 = first2;
while (current2 != last2)
{
if (comp(*current1, *current2))
{
++current1;
++current2;
}
else
{
if (d1 == d2)
{
return last1;
}
else
{
current1 = ++first1;
current2 = first2;
--d1;
}
}
}
return first1;
}
/*****************************************************************************************/
// search_n
// 在[first, last)中查找连续 n 个 value 所形成的子序列,返回一个迭代器指向该子序列的起始处
/*****************************************************************************************/
template <class ForwardIter, class Size, class T>
ForwardIter
search_n(ForwardIter first, ForwardIter last, Size n, const T& value)
{
if (n <= 0)
{
return first;
}
else
{
first = mystl::find(first, last, value);
while (first != last)
{
auto m = n - 1;
auto i = first;
++i;
while (i != last && m != 0 && *i == value)
{
++i;
--m;
}
if (m == 0)
{
return first;
}
else
{
first = mystl::find(i, last, value);
}
}
return last;
}
}
// 重载版本使用函数对象 comp 代替比较操作
template <class ForwardIter, class Size, class T, class Compared>
ForwardIter
search_n(ForwardIter first, ForwardIter last,
Size n, const T& value, Compared comp)
{
if (n <= 0)
{
return first;
}
else
{
while (first != last)
{
if (comp(*first, value))
break;
++first;
}
while (first != last)
{
auto m = n - 1;
auto i = first;
++i;
while (i != last && m != 0 && comp(*i, value))
{
++i;
--m;
}
if (m == 0)
{
return first;
}
else
{
while (i != last)
{
if (comp(*i, value))
break;
++i;
}
first = i;
}
}
return last;
}
}
/*****************************************************************************************/
// find_end
// 在[first1, last1)区间中查找[first2, last2)最后一次出现的地方,若不存在返回 last1
/*****************************************************************************************/
// find_end_dispatch 的 forward_iterator_tag 版本
template <class ForwardIter1, class ForwardIter2>
ForwardIter1
find_end_dispatch(ForwardIter1 first1, ForwardIter1 last1,
ForwardIter2 first2, ForwardIter2 last2,
forward_iterator_tag, forward_iterator_tag)
{
if (first2 == last2)
{
return last1;
}
else
{
auto result = last1;
while (true)
{
// 利用 search 查找某个子序列的首次出现点,找不到则返回 last1
auto new_result = mystl::search(first1, last1, first2, last2);
if (new_result == last1)
{
return result;
}
else
{
result = new_result;
first1 = new_result;
++first1;
}
}
}
}
// find_end_dispatch 的 bidirectional_iterator_tag 版本
template <class BidirectionalIter1, class BidirectionalIter2>
BidirectionalIter1
find_end_dispatch(BidirectionalIter1 first1, BidirectionalIter1 last1,
BidirectionalIter2 first2, BidirectionalIter2 last2,
bidirectional_iterator_tag, bidirectional_iterator_tag)
{
typedef reverse_iterator<BidirectionalIter1> reviter1;
typedef reverse_iterator<BidirectionalIter2> reviter2;
reviter1 rlast1(first1);
reviter2 rlast2(first2);
reviter1 rresult = mystl::search(reviter1(last1), rlast1, reviter2(last2), rlast2);
if (rresult == rlast1)
{
return last1;
}
else
{
auto result = rresult.base();
mystl::advance(result, -mystl::distance(first2, last2));
return result;
}
}
template <class ForwardIter1, class ForwardIter2>
ForwardIter1
find_end(ForwardIter1 first1, ForwardIter1 last1,
ForwardIter2 first2, ForwardIter2 last2)
{
typedef typename iterator_traits<ForwardIter1>::iterator_category Category1;
typedef typename iterator_traits<ForwardIter2>::iterator_category Category2;
return mystl::find_end_dispatch(first1, last1, first2, last2, Category1(), Category2());
}
// 重载版本使用函数对象 comp 代替比较操作
// find_end_dispatch 的 forward_iterator_tag 版本
template <class ForwardIter1, class ForwardIter2, class Compared>
ForwardIter1
find_end_dispatch(ForwardIter1 first1, ForwardIter1 last1,
ForwardIter2 first2, ForwardIter2 last2,
forward_iterator_tag, forward_iterator_tag, Compared comp)
{
if (first2 == last2)
{
return last1;
}
else
{
auto result = last1;
while (true)
{
// 利用 search 查找某个子序列的首次出现点,找不到则返回 last1
auto new_result = mystl::search(first1, last1, first2, last2, comp);
if (new_result == last1)
{
return result;
}
else
{
result = new_result;
first1 = new_result;
++first1;
}
}
}
}
// find_end_dispatch 的 bidirectional_iterator_tag 版本
template <class BidirectionalIter1, class BidirectionalIter2, class Compared>
BidirectionalIter1
find_end_dispatch(BidirectionalIter1 first1, BidirectionalIter1 last1,
BidirectionalIter2 first2, BidirectionalIter2 last2,
bidirectional_iterator_tag, bidirectional_iterator_tag, Compared comp)
{
typedef reverse_iterator<BidirectionalIter1> reviter1;
typedef reverse_iterator<BidirectionalIter2> reviter2;
reviter1 rlast1(first1);
reviter2 rlast2(first2);
reviter1 rresult = mystl::search(reviter1(last1), rlast1, reviter2(last2), rlast2, comp);
if (rresult == rlast1)
{
return last1;
}
else
{
auto result = rresult.base();
mystl::advance(result, -mystl::distance(first2, last2));
return result;
}
}
template <class ForwardIter1, class ForwardIter2, class Compared>
ForwardIter1
find_end(ForwardIter1 first1, ForwardIter1 last1,
ForwardIter2 first2, ForwardIter2 last2, Compared comp)
{
typedef typename iterator_traits<ForwardIter1>::iterator_category Category1;
typedef typename iterator_traits<ForwardIter2>::iterator_category Category2;
return mystl::find_end_dispatch(first1, last1, first2, last2, Category1(), Category2(), comp);
}
/*****************************************************************************************/
// find_first_of
// 在[first1, last1)中查找[first2, last2)中的某些元素,返回指向第一次出现的元素的迭代器
/*****************************************************************************************/
template <class InputIter, class ForwardIter>
InputIter
find_first_of(InputIter first1, InputIter last1,
ForwardIter first2, ForwardIter last2)
{
for (; first1 != last1; ++first1)
{
for (auto iter = first2; iter != last2; ++iter)
{
if (*first1 == *iter)
return first1;
}
}
return last1;
}
// 重载版本使用函数对象 comp 代替比较操作
template <class InputIter, class ForwardIter, class Compared>
InputIter
find_first_of(InputIter first1, InputIter last1,
ForwardIter first2, ForwardIter last2, Compared comp)
{
for (; first1 != last1; ++first1)
{
for (auto iter = first2; iter != last2; ++iter)
{
if (comp(*first1, *iter))
return first1;
}
}
return last1;
}
/*****************************************************************************************/
// for_each
// 使用一个函数对象 f 对[first, last)区间内的每个元素执行一个 operator() 操作,但不能改变元素内容
// f() 可返回一个值,但该值会被忽略
/*****************************************************************************************/
template <class InputIter, class Function>
Function for_each(InputIter first, InputIter last, Function f)
{
for (; first != last; ++first)
{
f(*first);
}
return f;
}
/*****************************************************************************************/
// adjacent_find
// 找出第一对匹配的相邻元素,缺省使用 operator== 比较,如果找到返回一个迭代器,指向这对元素的第一个元素
/*****************************************************************************************/
template <class ForwardIter>
ForwardIter adjacent_find(ForwardIter first, ForwardIter last)
{
if (first == last) return last;
auto next = first;
while (++next != last)
{
if (*first == *next) return first;
first = next;
}
return last;
}
// 重载版本使用函数对象 comp 代替比较操作
template <class ForwardIter, class Compared>
ForwardIter adjacent_find(ForwardIter first, ForwardIter last, Compared comp)
{
if (first == last) return last;
auto next = first;
while (++next != last)
{
if (comp(*first, *next)) return first;
first = next;
}
return last;
}
/*****************************************************************************************/
// lower_bound
// 在[first, last)中查找第一个不小于 value 的元素,并返回指向它的迭代器,若没有则返回 last
/*****************************************************************************************/
// lbound_dispatch 的 forward_iterator_tag 版本
template <class ForwardIter, class T>
ForwardIter
lbound_dispatch(ForwardIter first, ForwardIter last,
const T& value, forward_iterator_tag)
{
auto len = mystl::distance(first, last);
auto half = len;
ForwardIter middle;
while (len > 0)
{
half = len >> 1;
middle = first;
mystl::advance(middle, half);
if (*middle < value)
{
first = middle;
++first;
len = len - half - 1;
}
else
{
len = half;
}
}
return first;
}
// lbound_dispatch 的 random_access_iterator_tag 版本
template <class RandomIter, class T>
RandomIter
lbound_dispatch(RandomIter first, RandomIter last,
const T& value, random_access_iterator_tag)
{
auto len = last - first;
auto half = len;
RandomIter middle;
while (len > 0)
{
half = len >> 1;
middle = first + half;
if (*middle < value)
{
first = middle + 1;
len = len - half - 1;
}
else
{
len = half;
}
}
return first;
}
template <class ForwardIter, class T>
ForwardIter
lower_bound(ForwardIter first, ForwardIter last, const T& value)
{
return mystl::lbound_dispatch(first, last, value, iterator_category(first));
}
// 重载版本使用函数对象 comp 代替比较操作
// lbound_dispatch 的 forward_iterator_tag 版本
template <class ForwardIter, class T, class Compared>
ForwardIter
lbound_dispatch(ForwardIter first, ForwardIter last,
const T& value, forward_iterator_tag, Compared comp)
{
auto len = mystl::distance(first, last);
auto half = len;
ForwardIter middle;
while (len > 0)
{
half = len >> 1;
middle = first;
mystl::advance(middle, half);
if (comp(*middle, value))
{
first = middle;
++first;
len = len - half - 1;
}
else
{
len = half;
}
}
return first;
}
// lbound_dispatch 的 random_access_iterator_tag 版本
template <class RandomIter, class T, class Compared>
RandomIter
lbound_dispatch(RandomIter first, RandomIter last,
const T& value, random_access_iterator_tag, Compared comp)
{
auto len = last - first;
auto half = len;
RandomIter 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;
}
template <class ForwardIter, class T, class Compared>
ForwardIter
lower_bound(ForwardIter first, ForwardIter last, const T& value, Compared comp)
{
return mystl::lbound_dispatch(first, last, value, iterator_category(first), comp);
}
/*****************************************************************************************/
// upper_bound
// 在[first, last)中查找第一个大于value 的元素,并返回指向它的迭代器,若没有则返回 last
/*****************************************************************************************/
// ubound_dispatch 的 forward_iterator_tag 版本
template <class ForwardIter, class T>
ForwardIter
ubound_dispatch(ForwardIter first, ForwardIter last,
const T& value, forward_iterator_tag)
{
auto len = mystl::distance(first, last);
auto half = len;
ForwardIter middle;
while (len > 0)
{
half = len >> 1;
middle = first;
mystl::advance(middle, half);
if (value < *middle)
{
len = half;
}
else
{
first = middle;
++first;
len = len - half - 1;
}
}
return first;
}
// ubound_dispatch 的 random_access_iterator_tag 版本
template <class RandomIter, class T>
RandomIter
ubound_dispatch(RandomIter first, RandomIter last,
const T& value, random_access_iterator_tag)
{
auto len = last - first;
auto half = len;
RandomIter middle;
while (len > 0)
{
half = len >> 1;
middle = first + half;
if (value < *middle)
{
len = half;
}
else
{
first = middle + 1;
len = len - half - 1;
}
}
return first;
}
template <class ForwardIter, class T>
ForwardIter
upper_bound(ForwardIter first, ForwardIter last, const T& value)
{
return mystl::ubound_dispatch(first, last, value, iterator_category(first));
}
// 重载版本使用函数对象 comp 代替比较操作
// ubound_dispatch 的 forward_iterator_tag 版本
template <class ForwardIter, class T, class Compared>
ForwardIter
ubound_dispatch(ForwardIter first, ForwardIter last,
const T& value, forward_iterator_tag, Compared comp)
{
auto len = mystl::distance(first, last);
auto half = len;
ForwardIter middle;
while (len > 0)
{
half = len >> 1;
middle = first;
mystl::advance(middle, half);
if (comp(value, *middle))
{
len = half;
}
else
{
first = middle;
++first;
len = len - half - 1;
}
}
return first;
}
// ubound_dispatch 的 random_access_iterator_tag 版本
template <class RandomIter, class T, class Compared>
RandomIter
ubound_dispatch(RandomIter first, RandomIter last,
const T& value, random_access_iterator_tag, Compared comp)
{
auto len = last - first;
auto half = len;
RandomIter 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;
}
template <class ForwardIter, class T, class Compared>
ForwardIter
upper_bound(ForwardIter first, ForwardIter last, const T& value, Compared comp)
{
return mystl::ubound_dispatch(first, last, value, iterator_category(first), comp);
}
/*****************************************************************************************/
// binary_search
// 二分查找,如果在[first, last)内有等同于 value 的元素,返回 true,否则返回 false
/*****************************************************************************************/
template <class ForwardIter, class T>
bool binary_search(ForwardIter first, ForwardIter last, const T& value)
{
auto i = mystl::lower_bound(first, last, value);
return i != last && !(value < *i);
}
// 重载版本使用函数对象 comp 代替比较操作
template <class ForwardIter, class T, class Compared>
bool binary_search(ForwardIter first, ForwardIter last, const T& value, Compared comp)
{
auto i = mystl::lower_bound(first, last, value);
return i != last && !comp(value, *i);
}
/*****************************************************************************************/
// equal_range
// 查找[first,last)区间中与 value 相等的元素所形成的区间,返回一对迭代器指向区间首尾
// 第一个迭代器指向第一个不小于 value 的元素,第二个迭代器指向第一个大于 value 的元素
/*****************************************************************************************/
// erange_dispatch 的 forward_iterator_tag 版本
template <class ForwardIter, class T>
mystl::pair<ForwardIter, ForwardIter>
erange_dispatch(ForwardIter first, ForwardIter last,
const T& value, forward_iterator_tag)
{
auto len = mystl::distance(first, last);
auto half = len;
ForwardIter middle, left, right;
while (len > 0)
{
half = len >> 1;
middle = first;
mystl::advance(middle, half);
if (*middle < value)
{
first = middle;
++first;
len = len - half - 1;
}
else if (value < *middle)
{
len = half;
}
else
{
left = mystl::lower_bound(first, last, value);
mystl::advance(first, len);
right = mystl::upper_bound(++middle, first, value);
return mystl::pair<ForwardIter, ForwardIter>(left, right);
}
}
return mystl::pair<ForwardIter, ForwardIter>(last, last);
}
// erange_dispatch 的 random_access_iterator_tag 版本
template <class RandomIter, class T>
mystl::pair<RandomIter, RandomIter>
erange_dispatch(RandomIter first, RandomIter last,
const T& value, random_access_iterator_tag)
{
auto len = last - first;
auto half = len;
RandomIter middle, left, right;
while (len > 0)
{
half = len >> 1;
middle = first + half;
if (*middle < value)
{
first = middle + 1;
len = len - half - 1;
}
else if (value < *middle)
{
len = half;
}
else
{
left = mystl::lower_bound(first, middle, value);
right = mystl::upper_bound(++middle, first + len, value);
return mystl::pair<RandomIter, RandomIter>(left, right);
}
}
return mystl::pair<RandomIter, RandomIter>(last, last);
}
template <class ForwardIter, class T>
mystl::pair<ForwardIter, ForwardIter>
equal_range(ForwardIter first, ForwardIter last, const T& value)
{
return mystl::erange_dispatch(first, last, value, iterator_category(first));
}
// 重载版本使用函数对象 comp 代替比较操作
// erange_dispatch 的 forward iterator 版本
template <class ForwardIter, class T, class Compared>
mystl::pair<ForwardIter, ForwardIter>
erange_dispatch(ForwardIter first, ForwardIter last,
const T& value, forward_iterator_tag, Compared comp)
{
auto len = mystl::distance(first, last);
auto half = len;
ForwardIter middle, left, right;
while (len > 0)
{
half = len >> 1;
middle = first;
mystl::advance(middle, half);
if (comp(*middle, value))
{
first = middle;
++first;
len = len - half - 1;
}
else if (comp(value, *middle))
{
len = half;
}
else
{
left = mystl::lower_bound(first, last, value, comp);
mystl::advance(first, len);
right = mystl::upper_bound(++middle, first, value, comp);
return mystl::pair<ForwardIter, ForwardIter>(left, right);
}
}
return mystl::pair<ForwardIter, ForwardIter>(last, last);
}
// erange_dispatch 的 random access iterator 版本
template <class RandomIter, class T, class Compared>
mystl::pair<RandomIter, RandomIter>
erange_dispatch(RandomIter first, RandomIter last,
const T& value, random_access_iterator_tag, Compared comp)
{
auto len = last - first;
auto half = len;
RandomIter middle, left, right;
while (len > 0)
{
half = len >> 1;
middle = first + half;
if (comp(*middle, value))
{
first = middle + 1;
len = len - half - 1;
}
else if (comp(value, *middle))
{
len = half;
}
else
{
left = mystl::lower_bound(first, middle, value, comp);
right = mystl::upper_bound(++middle, first + len, value, comp);
return mystl::pair<RandomIter, RandomIter>(left, right);
}
}
return mystl::pair<RandomIter, RandomIter>(last, last);
}
template <class ForwardIter, class T, class Compared>
mystl::pair<ForwardIter, ForwardIter>
equal_range(ForwardIter first, ForwardIter last, const T& value, Compared comp)
{
return mystl::erange_dispatch(first, last, value, iterator_category(first), comp);
}
/*****************************************************************************************/
// generate
// 将函数对象 gen 的运算结果对[first, last)内的每个元素赋值
/*****************************************************************************************/
template <class ForwardIter, class Generator>
void generate(ForwardIter first, ForwardIter last, Generator gen)
{
for (; first != last; ++first)
{
*first = gen();
}
}
/*****************************************************************************************/
// generate_n
// 用函数对象 gen 连续对 n 个元素赋值
/*****************************************************************************************/
template <class ForwardIter, class Size, class Generator>
void generate_n(ForwardIter first, Size n, Generator gen)
{
for (; n > 0; --n, ++first)
{
*first = gen();
}
}
/*****************************************************************************************/
// includes
// 判断序列一S1 是否包含序列二S2
/*****************************************************************************************/
template <class InputIter1, class InputIter2>
bool includes(InputIter1 first1, InputIter1 last1,
InputIter2 first2, InputIter2 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 InputIter1, class InputIter2, class Compared>
bool includes(InputIter1 first1, InputIter1 last1,
InputIter2 first2, InputIter2 last2, Compared comp)