This repository has been archived by the owner on May 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathalgorithm
4436 lines (4050 loc) · 131 KB
/
algorithm
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 standard header
#pragma once
#ifndef _ALGORITHM_
#define _ALGORITHM_
#ifndef RC_INVOKED
#include <xmemory>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma push_macro("new")
#undef new
#pragma warning(disable: 4244)
_STD_BEGIN
// COMMON SORT PARAMETERS
const int _ISORT_MAX = 32; // maximum size for insertion sort
// TEMPLATE FUNCTION for_each
template<class _InIt,
class _Fn1> inline
void _For_each(_InIt _First, _InIt _Last, _Fn1& _Func)
{ // perform function for each element
for (; _First != _Last; ++_First)
_Func(*_First);
}
template<class _InIt,
class _Fn1> inline
_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{ // perform function for each element
_DEBUG_RANGE_PTR(_First, _Last, _Func);
_For_each(_Unchecked(_First), _Unchecked(_Last), _Func);
return (_STD move(_Func));
}
// TEMPLATE FUNCTION find_if
template<class _InIt,
class _Pr> inline
_InIt _Find_if(_InIt _First, _InIt _Last, _Pr _Pred)
{ // find first satisfying _Pred
for (; _First != _Last; ++_First)
if (_Pred(*_First))
break;
return (_First);
}
template<class _InIt,
class _Pr> inline
_InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
{ // find first satisfying _Pred
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_Rechecked(_First,
_Find_if(_Unchecked(_First), _Unchecked(_Last), _Pred)));
}
// TEMPLATE FUNCTION adjacent_find WITH PRED
template<class _FwdIt,
class _Pr> inline
_FwdIt _Adjacent_find(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
{ // find first satisfying _Pred with successor
if (_First != _Last)
for (_FwdIt _Firstb; (void)(_Firstb = _First), ++_First != _Last; )
if (_Pred(*_Firstb, *_First))
return (_Firstb);
return (_Last);
}
template<class _FwdIt,
class _Pr> inline
_FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
{ // find first satisfying _Pred with successor
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER_IF(_First != _Last && _STD next(_First) != _Last, _Pred);
return (_Rechecked(_First,
_Adjacent_find(_Unchecked(_First), _Unchecked(_Last), _Pred)));
}
// TEMPLATE FUNCTION adjacent_find
template<class _FwdIt> inline
_FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last)
{ // find first matching successor
return (_STD adjacent_find(_First, _Last, equal_to<>()));
}
// TEMPLATE FUNCTION count_if
template<class _InIt,
class _Pr> inline
typename iterator_traits<_InIt>::difference_type
_Count_if(_InIt _First, _InIt _Last, _Pr _Pred)
{ // count elements satisfying _Pred
typename iterator_traits<_InIt>::difference_type _Count = 0;
for (; _First != _Last; ++_First)
if (_Pred(*_First))
++_Count;
return (_Count);
}
template<class _InIt,
class _Pr> inline
typename iterator_traits<_InIt>::difference_type
count_if(_InIt _First, _InIt _Last, _Pr _Pred)
{ // count elements satisfying _Pred
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_Count_if(_Unchecked(_First), _Unchecked(_Last), _Pred));
}
// TEMPLATE FUNCTION mismatch WITH PRED
template<class _InIt1,
class _InIt2,
class _Pr> inline
pair<_InIt1, _InIt2>
_Mismatch(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred)
{ // return [_First1, _Last1)/[_First2, ...) mismatch using _Pred
for (; _First1 != _Last1 && _Pred(*_First1, *_First2); )
{ // point past match
++_First1;
++_First2;
}
return (pair<_InIt1, _InIt2>(_First1, _First2));
}
#if _ITERATOR_DEBUG_LEVEL == 0
template<class _InIt1,
class _InIt2,
class _Pr> inline
pair<_InIt1, _InIt2>
mismatch(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred)
{ // return [_First1, _Last1)/[_First2, ...) mismatch using _Pred
pair<_UNCHECKED_TYPE(_InIt1), _InIt2> _Ans(
_Mismatch(_Unchecked(_First1), _Unchecked(_Last1),
_First2, _Pred));
return (pair<_InIt1, _InIt2>(
_Rechecked(_First1, _Ans.first),
_Ans.second));
}
#else /* _ITERATOR_DEBUG_LEVEL == 0 */
template<class _InIt1,
class _InIt2,
class _Pr> inline
pair<_InIt1, _InIt2>
_Mismatch2(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred, true_type)
{ // return [_First1, _Last1)/[_First2, ...) mismatch, checked input
return (_Mismatch(_First1, _Last1,
_First2, _Pred));
}
template<class _InIt1,
class _InIt2,
class _Pr> inline
_SCL_INSECURE_DEPRECATE
pair<_InIt1, _InIt2>
_Mismatch2(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred, false_type)
{ // return [_First1, _Last1)/[_First2, ...) mismatch, unchecked input
return (_Mismatch(_First1, _Last1,
_First2, _Pred));
}
template<class _InIt1,
class _InIt2,
class _Pr> inline
pair<_InIt1, _InIt2>
mismatch(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred)
{ // return [_First1, _Last1)/[_First2, ...) mismatch using _Pred
_DEBUG_RANGE_PTR(_First1, _Last1, _First2);
_DEBUG_POINTER_IF(_First1 != _Last1, _Pred);
pair<_UNCHECKED_TYPE(_InIt1), _InIt2> _Ans(
_Mismatch2(_Unchecked(_First1), _Unchecked(_Last1),
_First2, _Pred, _Is_checked(_First2)));
return (pair<_InIt1, _InIt2>(
_Rechecked(_First1, _Ans.first),
_Ans.second));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template<class _InIt1,
class _InTy,
size_t _InSize,
class _Pr> inline
pair<_InIt1, _InTy *>
mismatch(_InIt1 _First1, _InIt1 _Last1,
_InTy (&_First2)[_InSize], _Pr _Pred)
{ // return [_First1, _Last1)/[_First2, ...) mismatch using _Pred
pair<_InIt1, _Array_iterator<_InTy, _InSize> > _Ans(
_STD mismatch(_First1, _Last1,
_Array_iterator<_InTy, _InSize>(_First2), _Pred));
return (pair<_InIt1, _InTy *>(
_Ans.first,
_Unchecked(_Ans.second)));
}
#endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
#endif /* _ITERATOR_DEBUG_LEVEL == 0 */
// TEMPLATE FUNCTION mismatch
template<class _InIt1,
class _InIt2> inline
pair<_InIt1, _InIt2>
mismatch(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2)
{ // return [_First1, _Last1)/[_First2, ...) mismatch
return (_STD mismatch(_First1, _Last1, _First2,
equal_to<>()));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template<class _InIt1,
class _InTy,
size_t _InSize> inline
pair<_InIt1, _InTy *>
mismatch(_InIt1 _First1, _InIt1 _Last1,
_InTy (&_First2)[_InSize])
{ // return [_First1, _Last1)/[_First2, ...) mismatch, array input
return (_STD mismatch(_First1, _Last1, _First2,
equal_to<>()));
}
#endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
// TEMPLATE FUNCTION mismatch WITH TWO RANGES, PRED
template<class _InIt1,
class _InIt2,
class _Pr> inline
pair<_InIt1, _InIt2>
_Mismatch(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2, _Pr _Pred)
{ // return [_First1, _Last1)/[_First2, _Last2) mismatch using _Pred
for (; _First1 != _Last1 && _First2 != _Last2
&& _Pred(*_First1, *_First2); )
{ // point past match
++_First1;
++_First2;
}
return (pair<_InIt1, _InIt2>(_First1, _First2));
}
template<class _InIt1,
class _InIt2,
class _Pr> inline
pair<_InIt1, _InIt2>
mismatch(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2, _Pr _Pred)
{ // return [_First1, _Last1)/[_First2, _Last2) mismatch using _Pred
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_RANGE(_First2, _Last2);
_DEBUG_POINTER_IF(_First1 != _Last1 && _First2 != _Last2, _Pred);
pair<_UNCHECKED_TYPE(_InIt1), _UNCHECKED_TYPE(_InIt2)> _Ans(
_Mismatch(_Unchecked(_First1), _Unchecked(_Last1),
_Unchecked(_First2), _Unchecked(_Last2), _Pred));
return (pair<_InIt1, _InIt2>(
_Rechecked(_First1, _Ans.first),
_Rechecked(_First2, _Ans.second)));
}
// TEMPLATE FUNCTION mismatch WITH TWO RANGES
template<class _InIt1,
class _InIt2> inline
pair<_InIt1, _InIt2>
mismatch(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2)
{ // return [_First1, _Last1)/[_First2, _Last2) mismatch
return (_STD mismatch(_First1, _Last1, _First2, _Last2,
equal_to<>()));
}
// TEMPLATE FUNCTION all_of
template<class _InIt,
class _Pr> inline
bool _All_of(_InIt _First, _InIt _Last, _Pr _Pred)
{ // test if all elements satisfy _Pred
for (; _First != _Last; ++_First)
if (!_Pred(*_First))
return (false);
return (true);
}
template<class _InIt,
class _Pr> inline
bool all_of(_InIt _First, _InIt _Last, _Pr _Pred)
{ // test if all elements satisfy _Pred
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_All_of(_Unchecked(_First), _Unchecked(_Last), _Pred));
}
// TEMPLATE FUNCTION any_of
template<class _InIt,
class _Pr> inline
bool _Any_of(_InIt _First, _InIt _Last, _Pr _Pred)
{ // test if any element satisfies _Pred
for (; _First != _Last; ++_First)
if (_Pred(*_First))
return (true);
return (false);
}
template<class _InIt,
class _Pr> inline
bool any_of(_InIt _First, _InIt _Last, _Pr _Pred)
{ // test if any element satisfies _Pred
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_Any_of(_Unchecked(_First), _Unchecked(_Last), _Pred));
}
// TEMPLATE FUNCTION none_of
template<class _InIt,
class _Pr> inline
bool _None_of(_InIt _First, _InIt _Last, _Pr _Pred)
{ // test if no elements satisfy _Pred
for (; _First != _Last; ++_First)
if (_Pred(*_First))
return (false);
return (true);
}
template<class _InIt,
class _Pr> inline
bool none_of(_InIt _First, _InIt _Last, _Pr _Pred)
{ // test if no elements satisfy _Pred
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_None_of(_Unchecked(_First), _Unchecked(_Last), _Pred));
}
// TEMPLATE FUNCTION find_if_not
template<class _InIt,
class _Pr> inline
_InIt _Find_if_not(_InIt _First, _InIt _Last, _Pr _Pred)
{ // find first element that satisfies !_Pred
for (; _First != _Last; ++_First)
if (!_Pred(*_First))
break;
return (_First);
}
template<class _InIt,
class _Pr> inline
_InIt find_if_not(_InIt _First, _InIt _Last, _Pr _Pred)
{ // find first element that satisfies !_Pred
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_Rechecked(_First,
_Find_if_not(_Unchecked(_First), _Unchecked(_Last), _Pred)));
}
// TEMPLATE FUNCTION copy_if
template<class _InIt,
class _OutIt,
class _Pr> inline
_OutIt _Copy_if(_InIt _First, _InIt _Last, _OutIt _Dest,
_Pr _Pred)
{ // copy each satisfying _Pred
for (; _First != _Last; ++_First)
if (_Pred(*_First))
{ // validate _Dest and copy
_DEBUG_POINTER(_Dest);
*_Dest++ = *_First;
}
return (_Dest);
}
#if _ITERATOR_DEBUG_LEVEL == 0
template<class _InIt,
class _OutIt,
class _Pr> inline
_OutIt copy_if(_InIt _First, _InIt _Last, _OutIt _Dest,
_Pr _Pred)
{ // copy each satisfying _Pred
return (_Copy_if(_Unchecked(_First), _Unchecked(_Last),
_Dest, _Pred));
}
#else /* _ITERATOR_DEBUG_LEVEL == 0 */
template<class _InIt,
class _OutIt,
class _Pr> inline
_OutIt _Copy_if(_InIt _First, _InIt _Last, _OutIt _Dest,
_Pr _Pred, true_type)
{ // copy each satisfying _Pred, checked dest
return (_Copy_if(_First, _Last,
_Dest, _Pred));
}
template<class _InIt,
class _OutIt,
class _Pr> inline
_SCL_INSECURE_DEPRECATE
_OutIt _Copy_if(_InIt _First, _InIt _Last, _OutIt _Dest,
_Pr _Pred, false_type)
{ // copy each satisfying _Pred, unchecked dest
return (_Copy_if(_First, _Last,
_Dest, _Pred));
}
template<class _InIt,
class _OutIt,
class _Pr> inline
_OutIt copy_if(_InIt _First, _InIt _Last, _OutIt _Dest,
_Pr _Pred)
{ // copy each satisfying _Pred
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_Copy_if(_Unchecked(_First), _Unchecked(_Last),
_Dest, _Pred, _Is_checked(_Dest)));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template<class _InIt,
class _OutTy,
size_t _OutSize,
class _Pr> inline
_OutTy *copy_if(_InIt _First, _InIt _Last, _OutTy (&_Dest)[_OutSize],
_Pr _Pred)
{ // copy each satisfying _Pred, array dest
return (_Unchecked(
_STD copy_if(_First, _Last,
_Array_iterator<_OutTy, _OutSize>(_Dest), _Pred)));
}
#endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
#endif /* _ITERATOR_DEBUG_LEVEL == 0 */
// TEMPLATE FUNCTION partition_copy
template<class _InIt,
class _OutIt1,
class _OutIt2,
class _Pr> inline
pair<_OutIt1, _OutIt2>
_Partition_copy(_InIt _First, _InIt _Last,
_OutIt1 _Dest1, _OutIt2 _Dest2, _Pr _Pred)
{ // copy true partition *_Dest1++, false to *_Dest2++
for (; _First != _Last; ++_First)
if (_Pred(*_First))
{ // validate _Dest1 and store
_DEBUG_POINTER(_Dest1);
*_Dest1++ = *_First;
}
else
{ // validate _Dest2 and store
_DEBUG_POINTER(_Dest2);
*_Dest2++ = *_First;
}
return (pair<_OutIt1, _OutIt2>( _Dest1, _Dest2));
}
#if _ITERATOR_DEBUG_LEVEL == 0
template<class _InIt,
class _OutIt1,
class _OutIt2,
class _Pr> inline
pair<_OutIt1, _OutIt2>
partition_copy(_InIt _First, _InIt _Last,
_OutIt1 _Dest1, _OutIt2 _Dest2, _Pr _Pred)
{ // copy true partition *_Dest1++, false to *_Dest2++
return (pair<_OutIt1, _OutIt2>(
_Partition_copy(_Unchecked(_First), _Unchecked(_Last),
_Dest1, _Dest2, _Pred)));
}
#else /* _ITERATOR_DEBUG_LEVEL == 0 */
template<class _InIt,
class _OutIt1,
class _OutIt2,
class _Pr> inline
pair<_OutIt1, _OutIt2>
_Partition_copy(_InIt _First, _InIt _Last,
_OutIt1 _Dest1, _OutIt2 _Dest2, _Pr _Pred,
true_type, true_type)
{ // copy true partition *_Dest1++, false to *_Dest2++, checked dest
return (pair<_OutIt1, _OutIt2>(
_Partition_copy(_First, _Last,
_Dest1, _Dest2, _Pred)));
}
template<class _InIt,
class _OutIt1,
class _OutIt2,
class _Pr> inline
_SCL_INSECURE_DEPRECATE
pair<_OutIt1, _OutIt2>
_Partition_copy(_InIt _First, _InIt _Last,
_OutIt1 _Dest1, _OutIt2 _Dest2, _Pr _Pred,
true_type, false_type)
{ // copy true partition *_Dest1++, false to *_Dest2++, unchecked dest
return (pair<_OutIt1, _OutIt2>(
_Partition_copy(_First, _Last,
_Dest1, _Dest2, _Pred)));
}
template<class _InIt,
class _OutIt1,
class _OutIt2,
class _Pr> inline
_SCL_INSECURE_DEPRECATE
pair<_OutIt1, _OutIt2>
_Partition_copy(_InIt _First, _InIt _Last,
_OutIt1 _Dest1, _OutIt2 _Dest2, _Pr _Pred,
false_type, true_type)
{ // copy true partition *_Dest1++, false to *_Dest2++, unchecked dest
return (pair<_OutIt1, _OutIt2>(
_Partition_copy(_First, _Last,
_Dest1, _Dest2, _Pred)));
}
template<class _InIt,
class _OutIt1,
class _OutIt2,
class _Pr> inline
_SCL_INSECURE_DEPRECATE
pair<_OutIt1, _OutIt2>
_Partition_copy(_InIt _First, _InIt _Last,
_OutIt1 _Dest1, _OutIt2 _Dest2, _Pr _Pred,
false_type, false_type)
{ // copy true partition *_Dest1++, false to *_Dest2++, unchecked dest
return (pair<_OutIt1, _OutIt2>(
_Partition_copy(_First, _Last,
_Dest1, _Dest2, _Pred)));
}
template<class _InIt,
class _OutIt1,
class _OutIt2,
class _Pr> inline
pair<_OutIt1, _OutIt2>
partition_copy(_InIt _First, _InIt _Last,
_OutIt1 _Dest1, _OutIt2 _Dest2, _Pr _Pred)
{ // copy true partition *_Dest1++, false to *_Dest2++
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (pair<_OutIt1, _OutIt2>(
_Partition_copy(_Unchecked(_First), _Unchecked(_Last),
_Dest1, _Dest2, _Pred,
_Is_checked(_Dest1), _Is_checked(_Dest2))));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template<class _InIt,
class _OutTy1,
size_t _OutSize1,
class _OutIt2,
class _Pr> inline
pair<_OutTy1 *, _OutIt2>
partition_copy(_InIt _First, _InIt _Last,
_OutTy1 (&_Dest1)[_OutSize1], _OutIt2 _Dest2, _Pr _Pred)
{ // copy true partition *_Dest1++, false to *_Dest2++, array dest
pair<_Array_iterator<_OutTy1, _OutSize1>, _OutIt2> _Ans =
_STD partition_copy(_First, _Last,
_Array_iterator<_OutTy1, _OutSize1>(_Dest1), _Dest2, _Pred);
return (pair<_OutTy1 *, _OutIt2>(
_Unchecked(_Ans.first),
_Ans.second));
}
template<class _InIt,
class _OutIt1,
class _OutTy2,
size_t _OutSize2,
class _Pr> inline
pair<_OutIt1, _OutTy2 *>
partition_copy(_InIt _First, _InIt _Last,
_OutIt1 _Dest1, _OutTy2 (&_Dest2)[_OutSize2], _Pr _Pred)
{ // copy true partition *_Dest1++, false to *_Dest2++, array dest
pair<_OutIt1, _Array_iterator<_OutTy2, _OutSize2> > _Ans =
_STD partition_copy(_First, _Last,
_Dest1, _Array_iterator<_OutTy2, _OutSize2>(_Dest2), _Pred);
return (pair<_OutIt1, _OutTy2 *>(
_Ans.first,
_Unchecked(_Ans.second)));
}
template<class _InIt,
class _OutTy1,
size_t _OutSize1,
class _OutTy2,
size_t _OutSize2,
class _Pr> inline
pair<_OutTy1 *, _OutTy2 *>
partition_copy(_InIt _First, _InIt _Last,
_OutTy1 (&_Dest1)[_OutSize1], _OutTy2 (&_Dest2)[_OutSize2],
_Pr _Pred)
{ // copy true partition *_Dest1++, false to *_Dest2++, array dest
pair<_Array_iterator<_OutTy1, _OutSize1>,
_Array_iterator<_OutTy2, _OutSize2> > _Ans =
_STD partition_copy(_First, _Last,
_Array_iterator<_OutTy1, _OutSize1>(_Dest1),
_Array_iterator<_OutTy2, _OutSize2>(_Dest2), _Pred);
return (pair<_OutTy1 *, _OutTy2 *>(
_Unchecked(_Ans.first),
_Unchecked(_Ans.second)));
}
#endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
#endif /* _ITERATOR_DEBUG_LEVEL == 0 */
// TEMPLATE FUNCTION is_partitioned
template<class _InIt,
class _Pr> inline
bool _Is_partitioned(_InIt _First, _InIt _Last, _Pr _Pred)
{ // test if [_First, _Last) partitioned by _Pred
for (; _First != _Last; ++_First)
if (!_Pred(*_First))
break; // skip true partition
for (; _First != _Last; ++_First)
if (_Pred(*_First))
return (false); // found out of place element
return (true);
}
template<class _InIt,
class _Pr> inline
bool is_partitioned(_InIt _First, _InIt _Last, _Pr _Pred)
{ // test if [_First, _Last) partitioned by _Pred
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_Is_partitioned(_Unchecked(_First), _Unchecked(_Last),
_Pred));
}
// TEMPLATE FUNCTION partition_point
template<class _FwdIt,
class _Diff,
class _Pr> inline
_FwdIt _Partition_point(_FwdIt _First, _FwdIt _Last, _Pr _Pred, _Diff *)
{ // find beginning of false partition in [_First, _Last)
_Diff _Count = 0;
_Distance(_First, _Last, _Count);
while (0 < _Count)
{ // divide and conquer, find half that contains answer
_Diff _Count2 = _Count / 2;
_FwdIt _Mid = _First;
_STD advance(_Mid, _Count2);
if (_Pred(*_Mid))
{ // try top half
_First = ++_Mid;
_Count -= _Count2 + 1;
}
else
_Count = _Count2;
}
return (_First);
}
template<class _FwdIt,
class _Pr> inline
_FwdIt partition_point(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
{ // find beginning of false partition in [_First, _Last)
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_Rechecked(_First,
_Partition_point(_Unchecked(_First), _Unchecked(_Last), _Pred,
_Dist_type(_First))));
}
// TEMPLATE FUNCTION search WITH PRED
template<class _FwdIt1,
class _FwdIt2,
class _Diff1,
class _Diff2,
class _Pr> inline
_FwdIt1 _Search(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred, _Diff1 *, _Diff2 *,
forward_iterator_tag, forward_iterator_tag)
{ // find first [_First2, _Last2) satisfying _Pred, arbitrary iterators
for (; ; ++_First1)
{ // loop until match or end of a sequence
_FwdIt1 _Mid1 = _First1;
for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1, (void)++_Mid2)
if (_Mid2 == _Last2)
return (_First1);
else if (_Mid1 == _Last1)
return (_Last1);
else if (!_Pred(*_Mid1, *_Mid2))
break;
}
}
template<class _FwdIt1,
class _FwdIt2,
class _Diff1,
class _Diff2,
class _Pr> inline
_FwdIt1 _Search(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred, _Diff1 *, _Diff2 *,
random_access_iterator_tag, random_access_iterator_tag)
{ // find first [_First2, _Last2) satisfying _Pred, random-access iterators
_Diff1 _Count1 = _Last1 - _First1;
_Diff2 _Count2 = _Last2 - _First2;
for (; _Count2 <= _Count1; ++_First1, (void)--_Count1)
{ // room for match, try it
_FwdIt1 _Mid1 = _First1;
for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1, (void)++_Mid2)
if (_Mid2 == _Last2)
return (_First1);
else if (!_Pred(*_Mid1, *_Mid2))
break;
}
return (_Last1);
}
template<class _FwdIt1,
class _FwdIt2,
class _Pr> inline
_FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
{ // find first [_First2, _Last2) satisfying _Pred
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_RANGE(_First2, _Last2);
_DEBUG_POINTER_IF(_First1 != _Last1 && _First2 != _Last2, _Pred);
return (_Rechecked(_First1,
_Search(_Unchecked(_First1), _Unchecked(_Last1),
_Unchecked(_First2), _Unchecked(_Last2), _Pred,
_Dist_type(_First1), _Dist_type(_First2),
_Iter_cat(_First1), _Iter_cat(_First2))));
}
// TEMPLATE FUNCTION search
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2)
{ // find first [_First2, _Last2) match
return (_STD search(_First1, _Last1, _First2, _Last2,
equal_to<>()));
}
// TEMPLATE FUNCTION search_n WITH PRED
template<class _FwdIt,
class _Diff,
class _Ty,
class _Pr> inline
_FwdIt _Search_n(_FwdIt _First, _FwdIt _Last,
_Diff _Count, const _Ty& _Val, _Pr _Pred, forward_iterator_tag)
{ // find first _Count * _Val satisfying _Pred, forward iterators
if (_Count <= 0)
return (_First);
for (; _First != _Last; ++_First)
if (_Pred(*_First, _Val))
{ // found start of possible match, check it out
_FwdIt _Mid = _First;
for (_Diff _Count1 = _Count; ; )
if (--_Count1 == 0)
return (_First); // found rest of match, report it
else if (++_Mid == _Last)
return (_Last); // short match at end
else if (!_Pred(*_Mid, _Val))
{ // short match not at end
break;
}
_First = _Mid; // pick up just beyond failed match
}
return (_Last);
}
template<class _FwdIt,
class _Diff,
class _Ty,
class _Pr> inline
_FwdIt _Search_n(_FwdIt _First, _FwdIt _Last,
_Diff _Count, const _Ty& _Val, _Pr _Pred, random_access_iterator_tag)
{ // find first _Count * _Val satisfying _Pred, random-access iterators
if (_Count <= 0)
return (_First);
_FwdIt _Oldfirst = _First;
for (_Diff _Inc = 0; _Count <= _Last - _Oldfirst; )
{ // enough room, look for a match
_First = _Oldfirst + _Inc;
if (_Pred(*_First, _Val))
{ // found part of possible match, check it out
_Diff _Count1 = _Count;
_FwdIt _Mid = _First;
for (; _Oldfirst != _First && _Pred(_First[-1], _Val);
--_First)
--_Count1; // back up over any skipped prefix
if (_Count1 <= _Last - _Mid)
for (; ; )
{ // enough left, test suffix
if (--_Count1 == 0)
return (_First); // found rest of match, report it
else if (!_Pred(*++_Mid, _Val))
{ // short match not at end
break;
}
}
_Oldfirst = ++_Mid; // failed match, take small jump
_Inc = 0;
}
else
{ // no match, take big jump and back up as needed
_Oldfirst = _First + 1;
_Inc = _Count - 1;
}
}
return (_Last);
}
template<class _FwdIt,
class _Diff,
class _Ty,
class _Pr> inline
_FwdIt search_n(_FwdIt _First, _FwdIt _Last,
_Diff _Count, const _Ty& _Val, _Pr _Pred)
{ // find first _Count * _Val satisfying _Pred
_DEBUG_RANGE_PTR(_First, _Last, _Pred);
return (_Rechecked(_First,
_Search_n(_Unchecked(_First), _Unchecked(_Last), _Count, _Val,
_Pred, _Iter_cat(_First))));
}
// TEMPLATE FUNCTION search_n
template<class _FwdIt,
class _Diff,
class _Ty> inline
_FwdIt search_n(_FwdIt _First, _FwdIt _Last,
_Diff _Count, const _Ty& _Val)
{ // find first _Count * _Val match
return (_STD search_n(_First, _Last, _Count, _Val,
equal_to<>()));
}
// TEMPLATE FUNCTION find_end WITH PRED
template<class _FwdIt1,
class _FwdIt2,
class _Diff1,
class _Diff2,
class _Pr> inline
_FwdIt1 _Find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred, _Diff1 *, _Diff2 *)
{ // find last [_First2, _Last2) satisfying _Pred
_Diff1 _Count1 = 0;
_Distance(_First1, _Last1, _Count1);
_Diff2 _Count2 = 0;
_Distance(_First2, _Last2, _Count2);
_FwdIt1 _Ans = _Last1;
if (0 < _Count2)
{ // validate _Pred and test
_DEBUG_POINTER_IF(_Count2 <= _Count1, _Pred);
for (; _Count2 <= _Count1; ++_First1, (void)--_Count1)
{ // room for match, try it
_FwdIt1 _Mid1 = _First1;
for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1)
if (!_Pred(*_Mid1, *_Mid2))
break;
else if (++_Mid2 == _Last2)
{ // potential answer, save it
_Ans = _First1;
break;
}
}
}
return (_Ans);
}
template<class _FwdIt1,
class _FwdIt2,
class _Pr> inline
_FwdIt1 find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
{ // find last [_First2, _Last2) satisfying _Pred
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_RANGE(_First2, _Last2);
return (_Rechecked(_First1,
_Find_end(_Unchecked(_First1), _Unchecked(_Last1),
_Unchecked(_First2), _Unchecked(_Last2), _Pred,
_Dist_type(_First1), _Dist_type(_First2))));
}
// TEMPLATE FUNCTION find_end
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt1 find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2)
{ // find last [_First2, _Last2) match
return (_STD find_end(_First1, _Last1, _First2, _Last2,
equal_to<>()));
}
// TEMPLATE FUNCTION find_first_of WITH PRED
template<class _FwdIt1,
class _FwdIt2,
class _Pr> inline
_FwdIt1 _Find_first_of(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
{ // look for one of [_First2, _Last2) satisfying _Pred with element
for (; _First1 != _Last1; ++_First1)
for (_FwdIt2 _Mid2 = _First2; _Mid2 != _Last2; ++_Mid2)
if (_Pred(*_First1, *_Mid2))
return (_First1);
return (_First1);
}
template<class _FwdIt1,
class _FwdIt2,
class _Pr> inline
_FwdIt1 find_first_of(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
{ // look for one of [_First2, _Last2) satisfying _Pred with element
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_RANGE(_First2, _Last2);
_DEBUG_POINTER_IF(_First1 != _Last1 && _First2 != _Last2, _Pred);
return (_Rechecked(_First1,
_Find_first_of(_Unchecked(_First1), _Unchecked(_Last1),
_Unchecked(_First2), _Unchecked(_Last2), _Pred)));
}
// TEMPLATE FUNCTION find_first_of
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt1 find_first_of(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2)
{ // look for one of [_First2, _Last2) that matches element
return (_STD find_first_of(_First1, _Last1, _First2, _Last2,
equal_to<>()));
}
// TEMPLATE FUNCTION swap_ranges
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt2 _Swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _Dest)
{ // swap [_First1, _Last1) with [_Dest, ...)
for (; _First1 != _Last1; ++_First1, (void)++_Dest)
_STD iter_swap(_First1, _Dest);
return (_Dest);
}
#if _ITERATOR_DEBUG_LEVEL == 0
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt2 swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _Dest)
{ // swap [_First1, _Last1) with [_Dest, ...)
return (_Swap_ranges(_Unchecked(_First1), _Unchecked(_Last1),
_Dest));
}
#else /* _ITERATOR_DEBUG_LEVEL == 0 */
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt2 _Swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _Dest,
forward_iterator_tag, forward_iterator_tag)
{ // swap [_First1, _Last1) with [_Dest, ...), arbitrary iterators
return (_Swap_ranges(_First1, _Last1,
_Dest));
}
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt2 _Swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _Dest,
random_access_iterator_tag, random_access_iterator_tag)
{ // swap [_First1, _Last1) with [_Dest, ...), random-access iterators
_FwdIt2 _Ans = _Dest + (_Last1 - _First1); // also checks range
_Swap_ranges(_First1, _Last1,
_Unchecked(_Dest));
return (_Ans);
}
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt2 _Swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _Dest, true_type)
{ // swap [_First1, _Last1) with [_Dest, ...), checked dest
return (_Swap_ranges(_First1, _Last1,
_Dest, _Iter_cat(_First1), _Iter_cat(_Dest)));
}
template<class _FwdIt1,
class _FwdIt2> inline
_SCL_INSECURE_DEPRECATE
_FwdIt2 _Swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _Dest, false_type)
{ // swap [_First1, _Last1) with [_Dest, ...), unchecked dest
return (_Swap_ranges(_First1, _Last1,
_Dest, _Iter_cat(_First1), _Iter_cat(_Dest)));
}
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt2 swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _Dest)
{ // swap [_First1, _Last1) with [_Dest, ...)
_DEBUG_RANGE_PTR(_First1, _Last1, _Dest);
return (_Swap_ranges(_Unchecked(_First1), _Unchecked(_Last1),
_Dest, _Is_checked(_Dest)));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template<class _FwdIt1,
class _OutTy,
size_t _OutSize> inline
_OutTy *swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1,
_OutTy (&_Dest)[_OutSize])
{ // swap [_First1, _Last1) with [_Dest, ...), array dest
return (_Unchecked(