-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALGO.H
2384 lines (2164 loc) · 73.8 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
/*
*
* 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.
*
*/
#ifndef ALGO_H
#define ALGO_H
#include <stdlib.h>
#include <bool.h>
#include <pair.h>
#include <iterator.h>
#include <algobase.h>
#include <heap.h>
#include <tempbuf.h>
template <class T>
inline const T& __median(const T& a, const T& b, const T& c) {
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;
}
template <class T, class Compare>
inline const T& __median(const T& a, const T& b, const T& c, Compare comp) {
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;
}
template <class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f) {
while (first != last) f(*first++);
return f;
}
template <class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T& value) {
while (first != last && *first != value) ++first;
return first;
}
template <class InputIterator, class Predicate>
InputIterator find_if(InputIterator first, InputIterator last,
Predicate pred) {
while (first != last && !pred(*first)) ++first;
return first;
}
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;
first = next;
}
return last;
}
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last,
BinaryPredicate binary_pred) {
if (first == last) return last;
ForwardIterator next = first;
while(++next != last) {
if (binary_pred(*first, *next)) return first;
first = next;
}
return last;
}
template <class InputIterator, class T, class Size>
void count(InputIterator first, InputIterator last, const T& value,
Size& n) {
while (first != last)
if (*first++ == value) ++n;
}
template <class InputIterator, class Predicate, class Size>
void count_if(InputIterator first, InputIterator last, Predicate pred,
Size& n) {
while (first != last)
if (pred(*first++)) ++n;
}
template <class ForwardIterator1, class ForwardIterator2, class Distance1,
class Distance2>
ForwardIterator1 __search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
Distance1*, Distance2*) {
Distance1 d1 = 0;
distance(first1, last1, d1);
Distance2 d2 = 0;
distance(first2, last2, d2);
if (d1 < d2) return last1;
ForwardIterator1 current1 = first1;
ForwardIterator2 current2 = first2;
while (current2 != last2)
if (*current1++ != *current2++)
if (d1-- == d2)
return last1;
else {
current1 = ++first1;
current2 = first2;
}
return (current2 == last2) ? first1 : last1;
}
template <class ForwardIterator1, class ForwardIterator2>
inline ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
return __search(first1, last1, first2, last2, distance_type(first1),
distance_type(first2));
}
template <class ForwardIterator1, class ForwardIterator2,
class BinaryPredicate, class Distance1, class Distance2>
ForwardIterator1 __search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate binary_pred, Distance1*, Distance2*) {
Distance1 d1 = 0;
distance(first1, last1, d1);
Distance2 d2 = 0;
distance(first2, last2, d2);
if (d1 < d2) return last1;
ForwardIterator1 current1 = first1;
ForwardIterator2 current2 = first2;
while (current2 != last2)
if (!binary_pred(*current1++, *current2++))
if (d1-- == d2)
return last1;
else {
current1 = ++first1;
current2 = first2;
}
return (current2 == last2) ? first1 : last1;
}
template <class ForwardIterator1, class ForwardIterator2,
class BinaryPredicate>
inline ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate binary_pred) {
return __search(first1, last1, first2, last2, binary_pred,
distance_type(first1), distance_type(first2));
}
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2) {
while (first1 != last1) iter_swap(first1++, first2++);
return first2;
}
template <class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator transform(InputIterator first, InputIterator last,
OutputIterator result, UnaryOperation op) {
while (first != last) *result++ = op(*first++);
return result;
}
template <class InputIterator1, class InputIterator2, class OutputIterator,
class BinaryOperation>
OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperation binary_op) {
while (first1 != last1) *result++ = binary_op(*first1++, *first2++);
return result;
}
template <class ForwardIterator, class T>
void replace(ForwardIterator first, ForwardIterator last, const T& old_value,
const T& new_value) {
while (first != last) {
if (*first == old_value) *first = new_value;
++first;
}
}
template <class ForwardIterator, class Predicate, class T>
void replace_if(ForwardIterator first, ForwardIterator last, Predicate pred,
const T& new_value) {
while (first != last) {
if (pred(*first)) *first = new_value;
++first;
}
}
template <class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy(InputIterator first, InputIterator last,
OutputIterator result, const T& old_value,
const T& new_value) {
while (first != last) {
*result++ = *first == old_value ? new_value : *first;
++first;
}
return result;
}
template <class Iterator, class OutputIterator, class Predicate, class T>
OutputIterator replace_copy_if(Iterator first, Iterator last,
OutputIterator result, Predicate pred,
const T& new_value) {
while (first != last) {
*result++ = pred(*first) ? new_value : *first;
++first;
}
return result;
}
template <class ForwardIterator, class Generator>
void generate(ForwardIterator first, ForwardIterator last, Generator gen) {
while (first != last) *first++ = gen();
}
template <class OutputIterator, class Size, class Generator>
OutputIterator generate_n(OutputIterator first, Size n, Generator gen) {
while (n-- > 0) *first++ = gen();
return first;
}
template <class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy(InputIterator first, InputIterator last,
OutputIterator result, const T& value) {
while (first != last) {
if (*first != value) *result++ = *first;
++first;
}
return result;
}
template <class InputIterator, class OutputIterator, class Predicate>
OutputIterator remove_copy_if(InputIterator first, InputIterator last,
OutputIterator result, Predicate pred) {
while (first != last) {
if (!pred(*first)) *result++ = *first;
++first;
}
return result;
}
template <class ForwardIterator, class T>
ForwardIterator remove(ForwardIterator first, ForwardIterator last,
const T& value) {
first = find(first, last, value);
ForwardIterator next = first;
return first == last ? first : remove_copy(++next, last, first, value);
}
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);
}
template <class InputIterator, class ForwardIterator>
ForwardIterator __unique_copy(InputIterator first, InputIterator last,
ForwardIterator result, forward_iterator_tag) {
*result = *first;
while (++first != last)
if (*result != *first) *++result = *first;
return ++result;
}
template <class InputIterator, class BidirectionalIterator>
inline BidirectionalIterator __unique_copy(InputIterator first,
InputIterator last,
BidirectionalIterator result,
bidirectional_iterator_tag) {
return __unique_copy(first, last, result, forward_iterator_tag());
}
template <class InputIterator, class RandomAccessIterator>
inline RandomAccessIterator __unique_copy(InputIterator first,
InputIterator last,
RandomAccessIterator result,
random_access_iterator_tag) {
return __unique_copy(first, last, result, forward_iterator_tag());
}
template <class InputIterator, class OutputIterator, class T>
OutputIterator __unique_copy(InputIterator first, InputIterator last,
OutputIterator result, T*) {
T value = *first;
*result = value;
while (++first != last)
if (value != *first) {
value = *first;
*++result = value;
}
return ++result;
}
template <class InputIterator, class OutputIterator>
inline OutputIterator __unique_copy(InputIterator first, InputIterator last,
OutputIterator result,
output_iterator_tag) {
return __unique_copy(first, last, result, value_type(first));
}
template <class InputIterator, class OutputIterator>
inline OutputIterator unique_copy(InputIterator first, InputIterator last,
OutputIterator result) {
if (first == last) return result;
return __unique_copy(first, last, result, iterator_category(result));
}
template <class InputIterator, class ForwardIterator, class BinaryPredicate>
ForwardIterator __unique_copy(InputIterator first, InputIterator last,
ForwardIterator result,
BinaryPredicate binary_pred,
forward_iterator_tag) {
*result = *first;
while (++first != last)
if (!binary_pred(*result, *first)) *++result = *first;
return ++result;
}
template <class InputIterator, class BidirectionalIterator,
class BinaryPredicate>
inline BidirectionalIterator __unique_copy(InputIterator first,
InputIterator last,
BidirectionalIterator result,
BinaryPredicate binary_pred,
bidirectional_iterator_tag) {
return __unique_copy(first, last, result, binary_pred,
forward_iterator_tag());
}
template <class InputIterator, class RandomAccessIterator,
class BinaryPredicate>
inline RandomAccessIterator __unique_copy(InputIterator first,
InputIterator last,
RandomAccessIterator result,
BinaryPredicate binary_pred,
random_access_iterator_tag) {
return __unique_copy(first, last, result, binary_pred,
forward_iterator_tag());
}
template <class InputIterator, class OutputIterator, class BinaryPredicate,
class T>
OutputIterator __unique_copy(InputIterator first, InputIterator last,
OutputIterator result,
BinaryPredicate binary_pred, T*) {
T value = *first;
*result = value;
while (++first != last)
if (!binary_pred(value, *first)) {
value = *first;
*++result = value;
}
return ++result;
}
template <class InputIterator, class OutputIterator, class BinaryPredicate>
inline OutputIterator __unique_copy(InputIterator first, InputIterator last,
OutputIterator result,
BinaryPredicate binary_pred,
output_iterator_tag) {
return __unique_copy(first, last, result, binary_pred, value_type(first));
}
template <class InputIterator, class OutputIterator, class BinaryPredicate>
inline OutputIterator unique_copy(InputIterator first, InputIterator last,
OutputIterator result,
BinaryPredicate binary_pred) {
if (first == last) return result;
return __unique_copy(first, last, result, binary_pred,
iterator_category(result));
}
template <class ForwardIterator>
ForwardIterator unique(ForwardIterator first, ForwardIterator last) {
first = adjacent_find(first, last);
return unique_copy(first, last, first);
}
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique(ForwardIterator first, ForwardIterator last,
BinaryPredicate binary_pred) {
first = adjacent_find(first, last, binary_pred);
return unique_copy(first, last, first, binary_pred);
}
template <class BidirectionalIterator>
void __reverse(BidirectionalIterator first, BidirectionalIterator last,
bidirectional_iterator_tag) {
while (true)
if (first == last || first == --last)
return;
else
iter_swap(first++, last);
}
template <class RandomAccessIterator>
void __reverse(RandomAccessIterator first, RandomAccessIterator last,
random_access_iterator_tag) {
while (first < last) iter_swap(first++, --last);
}
template <class BidirectionalIterator>
inline void reverse(BidirectionalIterator first, BidirectionalIterator last) {
__reverse(first, last, iterator_category(first));
}
template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy(BidirectionalIterator first,
BidirectionalIterator last,
OutputIterator result) {
while (first != last) *result++ = *--last;
return result;
}
template <class ForwardIterator, class Distance>
void __rotate(ForwardIterator first, ForwardIterator middle,
ForwardIterator last, Distance*, forward_iterator_tag) {
for (ForwardIterator i = middle; ;) {
iter_swap(first++, i++);
if (first == middle) {
if (i == last) return;
middle = i;
} else if (i == last)
i = middle;
}
}
template <class BidirectionalIterator, class Distance>
void __rotate(BidirectionalIterator first, BidirectionalIterator middle,
BidirectionalIterator last, Distance*,
bidirectional_iterator_tag) {
reverse(first, middle);
reverse(middle, last);
reverse(first, last);
}
template <class EuclideanRingElement>
EuclideanRingElement __gcd(EuclideanRingElement m, EuclideanRingElement n)
{
while (n != 0) {
EuclideanRingElement t = m % n;
m = n;
n = t;
}
return m;
}
template <class RandomAccessIterator, class Distance, class T>
void __rotate_cycle(RandomAccessIterator first, RandomAccessIterator last,
RandomAccessIterator initial, Distance shift, T*) {
T value = *initial;
RandomAccessIterator ptr1 = initial;
RandomAccessIterator ptr2 = ptr1 + shift;
while (ptr2 != initial) {
*ptr1 = *ptr2;
ptr1 = ptr2;
if (last - ptr2 > shift)
ptr2 += shift;
else
ptr2 = first + (shift - (last - ptr2));
}
*ptr1 = value;
}
template <class RandomAccessIterator, class Distance>
void __rotate(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last, Distance*,
random_access_iterator_tag) {
Distance n = __gcd(last - first, middle - first);
while (n--)
__rotate_cycle(first, last, first + n, middle - first,
value_type(first));
}
template <class ForwardIterator>
inline void rotate(ForwardIterator first, ForwardIterator middle,
ForwardIterator last) {
if (first == middle || middle == last) return;
__rotate(first, middle, last, distance_type(first),
iterator_category(first));
}
template <class ForwardIterator, class OutputIterator>
OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle,
ForwardIterator last, OutputIterator result) {
return copy(first, middle, copy(middle, last, result));
}
unsigned long __long_random(unsigned long);
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(__long_random((i - first) + 1)));
}
template <class RandomAccessIterator>
inline void random_shuffle(RandomAccessIterator first,
RandomAccessIterator last) {
__random_shuffle(first, last, distance_type(first));
}
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 BidirectionalIterator, class Predicate>
BidirectionalIterator partition(BidirectionalIterator first,
BidirectionalIterator last, Predicate pred) {
while (true) {
while (true)
if (first == last)
return first;
else if (pred(*first))
++first;
else
break;
--last;
while (true)
if (first == last)
return first;
else if (!pred(*last))
--last;
else
break;
iter_swap(first, last);
++first;
}
}
template <class ForwardIterator, class Predicate, class Distance>
ForwardIterator __inplace_stable_partition(ForwardIterator first,
ForwardIterator last,
Predicate pred, Distance len) {
if (len == 1) return pred(*first) ? last : first;
ForwardIterator middle = first;
advance(middle, len / 2);
ForwardIterator
first_cut = __inplace_stable_partition(first, middle, pred, len / 2);
ForwardIterator
second_cut = __inplace_stable_partition(middle, last, pred,
len - len / 2);
rotate(first_cut, middle, second_cut);
len = 0;
distance(middle, second_cut, len);
advance(first_cut, len);
return first_cut;
}
template <class ForwardIterator, class Pointer, class Predicate,
class Distance, class T>
ForwardIterator __stable_partition_adaptive(ForwardIterator first,
ForwardIterator last,
Predicate pred, Distance len,
Pointer buffer,
Distance buffer_size,
Distance& fill_pointer, T*) {
if (len <= buffer_size) {
len = 0;
ForwardIterator result1 = first;
Pointer result2 = buffer;
while (first != last && len < fill_pointer)
if (pred(*first))
*result1++ = *first++;
else {
*result2++ = *first++;
++len;
}
if (first != last) {
raw_storage_iterator<Pointer, T> result3 = result2;
while (first != last)
if (pred(*first))
*result1++ = *first++;
else {
*result3++ = *first++;
++len;
}
fill_pointer = len;
}
copy(buffer, buffer + len, result1);
return result1;
}
ForwardIterator middle = first;
advance(middle, len / 2);
ForwardIterator first_cut = __stable_partition_adaptive
(first, middle, pred, len / 2, buffer, buffer_size, fill_pointer,
(T*)0);
ForwardIterator second_cut = __stable_partition_adaptive
(middle, last, pred, len - len / 2, buffer, buffer_size,
fill_pointer, (T*)0);
rotate(first_cut, middle, second_cut);
len = 0;
distance(middle, second_cut, len);
advance(first_cut, len);
return first_cut;
}
template <class ForwardIterator, class Predicate, class Pointer,
class Distance>
ForwardIterator __stable_partition(ForwardIterator first, ForwardIterator last,
Predicate pred, Distance len,
pair<Pointer, Distance> p) {
if (p.first == 0)
return __inplace_stable_partition(first, last, pred, len);
Distance fill_pointer = 0;
ForwardIterator result =
__stable_partition_adaptive(first, last, pred, len, p.first, p.second,
fill_pointer, value_type(first));
destroy(p.first, p.first + fill_pointer);
return_temporary_buffer(p.first);
return result;
}
template <class ForwardIterator, class Predicate, class Distance>
inline ForwardIterator __stable_partition_aux(ForwardIterator first,
ForwardIterator last,
Predicate pred, Distance*) {
Distance len = 0;
distance(first, last, len);
return __stable_partition(first, last, pred, len,
get_temporary_buffer(len, value_type(first)));
}
template <class ForwardIterator, class Predicate>
inline ForwardIterator stable_partition(ForwardIterator first,
ForwardIterator last,
Predicate pred) {
return __stable_partition_aux(first, last, pred, distance_type(first));
}
template <class RandomAccessIterator, class T>
RandomAccessIterator __unguarded_partition(RandomAccessIterator first,
RandomAccessIterator last,
T pivot) {
while (1) {
while (*first < pivot) ++first;
--last;
while (pivot < *last) --last;
if (!(first < last)) return first;
iter_swap(first, last);
++first;
}
}
template <class RandomAccessIterator, class T, class Compare>
RandomAccessIterator __unguarded_partition(RandomAccessIterator first,
RandomAccessIterator last,
T pivot, Compare comp) {
while (1) {
while (comp(*first, pivot)) ++first;
--last;
while (comp(pivot, *last)) --last;
if (!(first < last)) return first;
iter_swap(first, last);
++first;
}
}
const int __stl_threshold = 16;
template <class RandomAccessIterator, class T>
void __quick_sort_loop_aux(RandomAccessIterator first,
RandomAccessIterator last, T*) {
while (last - first > __stl_threshold) {
RandomAccessIterator cut = __unguarded_partition
(first, last, T(__median(*first, *(first + (last - first)/2),
*(last - 1))));
if (cut - first >= last - cut) {
__quick_sort_loop(cut, last);
last = cut;
} else {
__quick_sort_loop(first, cut);
first = cut;
}
}
}
template <class RandomAccessIterator>
inline void __quick_sort_loop(RandomAccessIterator first,
RandomAccessIterator last) {
__quick_sort_loop_aux(first, last, value_type(first));
}
template <class RandomAccessIterator, class T, class Compare>
void __quick_sort_loop_aux(RandomAccessIterator first,
RandomAccessIterator last, T*, Compare comp) {
while (last - first > __stl_threshold) {
RandomAccessIterator cut = __unguarded_partition
(first, last, T(__median(*first, *(first + (last - first)/2),
*(last - 1), comp)), comp);
if (cut - first >= last - cut) {
__quick_sort_loop(cut, last, comp);
last = cut;
} else {
__quick_sort_loop(first, cut, comp);
first = cut;
}
}
}
template <class RandomAccessIterator, class Compare>
inline void __quick_sort_loop(RandomAccessIterator first,
RandomAccessIterator last, Compare comp) {
__quick_sort_loop_aux(first, last, value_type(first), comp);
}
template <class RandomAccessIterator, class T>
void __unguarded_linear_insert(RandomAccessIterator last, T value) {
RandomAccessIterator next = last;
--next;
while (value < *next) {
*last = *next;
last = next--;
}
*last = value;
}
template <class RandomAccessIterator, class T, class Compare>
void __unguarded_linear_insert(RandomAccessIterator last, T value,
Compare comp) {
RandomAccessIterator next = last;
--next;
while (comp(value , *next)) {
*last = *next;
last = next--;
}
*last = value;
}
template <class RandomAccessIterator, class T>
inline void __linear_insert(RandomAccessIterator first,
RandomAccessIterator last, T*) {
T value = *last;
if (value < *first) {
copy_backward(first, last, last + 1);
*first = value;
} else
__unguarded_linear_insert(last, value);
}
template <class RandomAccessIterator, class T, class Compare>
inline void __linear_insert(RandomAccessIterator first,
RandomAccessIterator last, T*, Compare comp) {
T value = *last;
if (comp(value, *first)) {
copy_backward(first, last, last + 1);
*first = value;
} else
__unguarded_linear_insert(last, value, comp);
}
template <class RandomAccessIterator>
void __insertion_sort(RandomAccessIterator first, RandomAccessIterator last) {
if (first == last) return;
for (RandomAccessIterator i = first + 1; i != last; ++i)
__linear_insert(first, i, value_type(first));
}
template <class RandomAccessIterator, class Compare>
void __insertion_sort(RandomAccessIterator first,
RandomAccessIterator last, Compare comp) {
if (first == last) return;
for (RandomAccessIterator i = first + 1; i != last; ++i)
__linear_insert(first, i, value_type(first), comp);
}
template <class RandomAccessIterator, class T>
void __unguarded_insertion_sort_aux(RandomAccessIterator first,
RandomAccessIterator last, T*) {
for (RandomAccessIterator i = first; i != last; ++i)
__unguarded_linear_insert(i, T(*i));
}
template <class RandomAccessIterator>
inline void __unguarded_insertion_sort(RandomAccessIterator first,
RandomAccessIterator last) {
__unguarded_insertion_sort_aux(first, last, value_type(first));
}
template <class RandomAccessIterator, class T, class Compare>
void __unguarded_insertion_sort_aux(RandomAccessIterator first,
RandomAccessIterator last,
T*, Compare comp) {
for (RandomAccessIterator i = first; i != last; ++i)
__unguarded_linear_insert(i, T(*i), comp);
}
template <class RandomAccessIterator, class Compare>
inline void __unguarded_insertion_sort(RandomAccessIterator first,
RandomAccessIterator last,
Compare comp) {
__unguarded_insertion_sort_aux(first, last, value_type(first), comp);
}
template <class RandomAccessIterator>
void __final_insertion_sort(RandomAccessIterator first,
RandomAccessIterator last) {
if (last - first > __stl_threshold) {
__insertion_sort(first, first + __stl_threshold);
__unguarded_insertion_sort(first + __stl_threshold, last);
} else
__insertion_sort(first, last);
}
template <class RandomAccessIterator, class Compare>
void __final_insertion_sort(RandomAccessIterator first,
RandomAccessIterator last, Compare comp) {
if (last - first > __stl_threshold) {
__insertion_sort(first, first + __stl_threshold, comp);
__unguarded_insertion_sort(first + __stl_threshold, last, comp);
} else
__insertion_sort(first, last, comp);
}
template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last) {
__quick_sort_loop(first, last);
__final_insertion_sort(first, last);
}
template <class RandomAccessIterator, class Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last,
Compare comp) {
__quick_sort_loop(first, last, comp);
__final_insertion_sort(first, last, comp);
}
template <class RandomAccessIterator>
void __inplace_stable_sort(RandomAccessIterator first,
RandomAccessIterator last) {
if (last - first < 15) {
__insertion_sort(first, last);
return;
}
RandomAccessIterator middle = first + (last - first) / 2;
__inplace_stable_sort(first, middle);
__inplace_stable_sort(middle, last);
__merge_without_buffer(first, middle, last, middle - first, last - middle);
}
template <class RandomAccessIterator, class Compare>
void __inplace_stable_sort(RandomAccessIterator first,
RandomAccessIterator last, Compare comp) {
if (last - first < 15) {
__insertion_sort(first, last, comp);
return;
}
RandomAccessIterator middle = first + (last - first) / 2;
__inplace_stable_sort(first, middle, comp);
__inplace_stable_sort(middle, last, comp);
__merge_without_buffer(first, middle, last, middle - first,
last - middle, comp);
}
template <class RandomAccessIterator1, class RandomAccessIterator2,
class Distance>
void __merge_sort_loop(RandomAccessIterator1 first,
RandomAccessIterator1 last,
RandomAccessIterator2 result, Distance step_size) {
Distance two_step = 2 * step_size;
while (last - first >= two_step) {
result = merge(first, first + step_size,
first + step_size, first + two_step, result);
first += two_step;
}
step_size = min(Distance(last - first), step_size);
merge(first, first + step_size, first + step_size, last, result);
}
template <class RandomAccessIterator1, class RandomAccessIterator2,
class Distance, class Compare>
void __merge_sort_loop(RandomAccessIterator1 first,
RandomAccessIterator1 last,
RandomAccessIterator2 result, Distance step_size,
Compare comp) {
Distance two_step = 2 * step_size;
while (last - first >= two_step) {
result = merge(first, first + step_size,
first + step_size, first + two_step, result, comp);
first += two_step;
}
step_size = min(Distance(last - first), step_size);
merge(first, first + step_size, first + step_size, last, result, comp);
}
const int __stl_chunk_size = 7;
template <class RandomAccessIterator, class Distance>
void __chunk_insertion_sort(RandomAccessIterator first,
RandomAccessIterator last, Distance chunk_size) {
while (last - first >= chunk_size) {
__insertion_sort(first, first + chunk_size);
first += chunk_size;
}
__insertion_sort(first, last);
}
template <class RandomAccessIterator, class Distance, class Compare>
void __chunk_insertion_sort(RandomAccessIterator first,
RandomAccessIterator last,
Distance chunk_size, Compare comp) {
while (last - first >= chunk_size) {
__insertion_sort(first, first + chunk_size, comp);
first += chunk_size;
}
__insertion_sort(first, last, comp);
}
template <class RandomAccessIterator, class Pointer, class Distance, class T>
void __merge_sort_with_buffer(RandomAccessIterator first,
RandomAccessIterator last,
Pointer buffer, Distance*, T*) {
Distance len = last - first;
Pointer buffer_last = buffer + len;
Distance step_size = __stl_chunk_size;
__chunk_insertion_sort(first, last, step_size);
while (step_size < len) {
__merge_sort_loop(first, last, buffer, step_size);
step_size *= 2;
__merge_sort_loop(buffer, buffer_last, first, step_size);
step_size *= 2;
}
}
template <class RandomAccessIterator, class Pointer, class Distance, class T,
class Compare>
void __merge_sort_with_buffer(RandomAccessIterator first,
RandomAccessIterator last, Pointer buffer,
Distance*, T*, Compare comp) {
Distance len = last - first;
Pointer buffer_last = buffer + len;
Distance step_size = __stl_chunk_size;
__chunk_insertion_sort(first, last, step_size, comp);
while (step_size < len) {
__merge_sort_loop(first, last, buffer, step_size, comp);
step_size *= 2;
__merge_sort_loop(buffer, buffer_last, first, step_size, comp);
step_size *= 2;
}
}
template <class RandomAccessIterator, class Pointer, class Distance, class T>
void __stable_sort_adaptive(RandomAccessIterator first,
RandomAccessIterator last, Pointer buffer,
Distance buffer_size, T*) {
Distance len = (last - first + 1) / 2;
RandomAccessIterator middle = first + len;