-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsortlib.hpp
2064 lines (1901 loc) · 61 KB
/
sortlib.hpp
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
// filename: sortlib.hpp
// author: baobaobear
// create date: 2019-09-20
// This library is compatible with C++03
#ifndef _BAOBAO_SORTLIB_HPP_
#define _BAOBAO_SORTLIB_HPP_
#pragma once
#include <cstddef>
#include <iterator>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#if __cplusplus >= 201103L || _MSC_VER >= 1600
#include <cstdint>
#include <type_traits>
#else
typedef unsigned int uint32_t;
typedef int int32_t;
#endif
namespace baobao
{
enum
{
qsort_insertion_sort_threshold = 40,
shellsort_insertion_sort_threshold = 32,
find_swap_bound_optimize_threshold = 256,
merge_insertion_sort_threshold = 32,
merge_2_part_insertion_sort_threshold = 32,
timsort_last_run_threshold = 64,
timsort_min_run = 32,
timsort_insert_gap = 8,
merge_sort_alloc_buffer = 1,
merge_sort_stack_buffer_size = 16384
};
namespace util
{
template<class T, T V>
struct value_constant
{
static const T value = V;
};
#if __cplusplus >= 201103L || _MSC_VER >= 1600
template< class T >
struct is_integral
: baobao::util::value_constant<bool, std::is_integral<T>::value>
{
};
template< class T >
struct is_floating_point
: baobao::util::value_constant<bool, std::is_floating_point<T>::value>
{
};
template< class T >
struct is_pointer
: baobao::util::value_constant<bool, std::is_pointer<T>::value>
{
};
#else
template< class T >
struct is_integral
: baobao::util::value_constant<bool, false>
{
};
template<>
struct is_integral<char>
: baobao::util::value_constant<bool, true>
{
};
template<>
struct is_integral<unsigned char>
: baobao::util::value_constant<bool, true>
{
};
template<>
struct is_integral<short>
: baobao::util::value_constant<bool, true>
{
};
template<>
struct is_integral<unsigned short>
: baobao::util::value_constant<bool, true>
{
};
template<>
struct is_integral<int>
: baobao::util::value_constant<bool, true>
{
};
template<>
struct is_integral<unsigned>
: baobao::util::value_constant<bool, true>
{
};
template<>
struct is_integral<long>
: baobao::util::value_constant<bool, true>
{
};
template<>
struct is_integral<unsigned long>
: baobao::util::value_constant<bool, true>
{
};
template< class T >
struct is_floating_point
: baobao::util::value_constant<bool, false>
{
};
template<>
struct is_floating_point<float>
: baobao::util::value_constant<bool, true>
{
};
template<>
struct is_floating_point<double>
: baobao::util::value_constant<bool, true>
{
};
template< class T >
struct is_pointer
: baobao::util::value_constant<bool, false>
{
};
template< class T >
struct is_pointer<T*>
: baobao::util::value_constant<bool, true>
{
};
#endif
template< class T >
struct is_arithmetic
: baobao::util::value_constant<bool, baobao::util::is_integral<T>::value || baobao::util::is_floating_point<T>::value>
{
};
template< class T >
struct is_scalar
: baobao::util::value_constant<bool, baobao::util::is_arithmetic<T>::value || baobao::util::is_pointer<T>::value>
{
};
template<class T, class Comp>
inline void make_mid_pivot(T& l, T& mid, T& r, Comp compare)
{
// one swap version
//if (compare(l, mid))
//{
// if (compare(r, mid))
// {
// if (compare(l, r))
// {
// std::swap(r, mid);
// }
// else
// {
// std::swap(l, mid);
// }
// }
//}
//else if (compare(mid, l))
//{
// if (compare(mid, r))
// {
// if (compare(l, r))
// {
// std::swap(l, mid);
// }
// else
// {
// std::swap(r, mid);
// }
// }
//}
// insert sort version
if (compare(r, mid))
{
if (compare(mid, l))
{
std::swap(l, r); // optional
return;
}
std::swap(mid, r);
}
if (compare(mid, l))
{
std::swap(mid, l);
if (compare(r, mid))
std::swap(mid, r);
}
}
template <class T, class Comp>
inline bool object_equal(const T& a, const T& b, Comp compare)
{
return !(compare(a, b) || compare(b, a));
}
uint32_t fake_rand_simple()
{
static uint32_t s_rnd = 0xfffffff;
return ++s_rnd;
}
struct taus88
{
typedef uint32_t result_type;
static result_type min() { return 0; }
static result_type max() { return 0xFFFFFFFFUL; }
static const uint32_t seed_mul = 134775813;
static const int init_iter = 16;
uint32_t s[3];
taus88() { init(time(0)); }
taus88(uint32_t seed) { init(seed); }
void init (uint32_t seed) { init(seed, (seed + 1) * seed_mul + 1, ~seed * seed_mul + 1); }
void init (uint32_t seed1, uint32_t seed2, uint32_t seed3)
{
s[0] = seed1;
s[1] = seed2;
s[2] = seed3;
for (int i = 0; i < init_iter; ++i) (*this)();
}
inline uint32_t tausworthe(uint32_t arg, uint32_t stage1, uint32_t stage2, uint32_t stage3, uint32_t limit) {
return ((arg & limit) << stage1) ^ (((arg << stage2) ^ arg) >> stage3);
}
uint32_t operator()() {
s[0] = tausworthe(s[0], 12, 13, 19, 4294967294UL);
s[1] = tausworthe(s[1], 4, 2, 25, 4294967288UL);
s[2] = tausworthe(s[2], 17, 3, 11, 4294967280UL);
return (s[0] ^ s[1] ^ s[2]);
}
static taus88& instance()
{
static taus88 rng;
return rng;
}
};
template<class _Eg32>
struct uint32_distributor
{
_Eg32& eg;
uint32_t r16, r16s;
uint32_t r8, r8s;
uint32_distributor(_Eg32& _eg) : eg(_eg) {
r16s = 0;
r8s = 0;
}
uint32_t i32(uint32_t n) {
++n;
if ((n & -n) == n) {
uint32_t r = (uint32_t)eg();
return r & (n - 1);
}
uint32_t m = n;
while (m & (m - 1)) m += m & -m;
--m;
for (;;) {
uint32_t r = (uint32_t)eg();
r &= m;
if (r < n) return r;
}
}
uint32_t i16(uint32_t n) {
++n;
if (r16s == 0) {
r16 = eg(); r16s = 2;
}
if ((n & -n) == n) {
--r16s;
uint32_t r = r16 & 0xffff;
r16 >>= 16;
return r & (n - 1);
}
uint32_t m = n;
while (m & (m - 1)) m += m & -m;
--m;
for (;;) {
--r16s;
uint32_t r = r16 & 0xffff;
r16 >>= 16;
r &= m;
if (r < n) return r;
if (r16s == 0) {
r16 = eg(); r16s = 2;
}
}
}
uint32_t i8(uint32_t n) {
++n;
if (r8s == 0) {
r8 = eg(); r8s = 4;
}
if ((n & -n) == n) {
--r8s;
uint32_t r = r8 & 0xff;
r8 >>= 8;
return r & (n - 1);
}
uint32_t m = n;
while (m & (m - 1)) m += m & -m;
--m;
for (;;) {
--r8s;
uint32_t r = r8 & 0xff;
r8 >>= 8;
r &= m;
if (r < n) return r;
if (r8s == 0) {
r8 = eg(); r8s = 4;
}
}
}
uint32_t distribution(uint32_t n) {
if (n <= 0)
return 0;
if (n <= 0xffff)
{
if (n <= 0xff)
return i8(n);
else
return i16(n);
}
else
return i32(n);
}
uint32_t distribution(uint32_t a, uint32_t b) {
return distribution(b - a) + a;
}
};
void rand_seed(uint32_t seed)
{
baobao::util::taus88::instance() = baobao::util::taus88(seed);
}
uint32_t rand_uint32(int32_t n)
{
static uint32_distributor<taus88> dis(baobao::util::taus88::instance());
return (uint32_t)dis.distribution(n);
}
} // namespace util
namespace internal
{
// stable sort
template <class RandomAccessIterator, class Comp>
void insert_sort(RandomAccessIterator beg, RandomAccessIterator end, Comp compare)
{
for (RandomAccessIterator i = beg + 1; i < end; ++i)
{
if (compare(*i, *(i - 1)))
{
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (;j != beg && compare(val, *(j - 1)); --j)
{
*j = *(j - 1);
}
*j = val;
}
}
}
// stable sort
template <class RandomAccessIterator, class Comp>
inline void unguarded_insert_sort(RandomAccessIterator beg, RandomAccessIterator end, Comp compare)
{
for (RandomAccessIterator i = beg; i < end; ++i)
{
if (compare(*i, *(i - 1)))
{
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (;compare(val, *(j - 1)); --j)
{
*j = *(j - 1);
}
*j = val;
}
}
}
// len <= 40
template <class RandomAccessIterator, class Comp>
void q_insert_sort(RandomAccessIterator beg, RandomAccessIterator end, Comp compare)
{
const int incre = 13;
for (RandomAccessIterator i = beg + incre, j = beg; i < end; ++i, ++j)
{
if (compare(*i, *j))
{
std::swap(*i, *j);
}
}
for (RandomAccessIterator i = beg + 1; i < end; ++i)
{
if (compare(*i, *(i - 1)))
{
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (;j != beg && compare(val, *(j - 1)); --j)
{
*j = *(j - 1);
}
*j = val;
}
}
}
// len <= 40
template <class RandomAccessIterator, class Comp>
void unguarded_q_insert_sort(RandomAccessIterator beg, RandomAccessIterator end, Comp compare)
{
const int incre = 13;
for (RandomAccessIterator i = beg + incre, j = beg; i < end; ++i, ++j)
{
if (compare(*i, *j))
{
std::swap(*i, *j);
}
}
for (RandomAccessIterator i = beg + 1; i < end; ++i)
{
if (compare(*i, *(i - 1)))
{
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (; compare(val, *(j - 1)); --j)
{
*j = *(j - 1);
}
*j = val;
}
}
}
// stable sort
template <class RandomAccessIterator, class Comp>
inline void insert_sort_part(RandomAccessIterator beg, RandomAccessIterator mid, RandomAccessIterator end, Comp compare)
{
for (RandomAccessIterator i = mid; i < end; ++i)
{
if (compare(*i, *(i - 1)))
{
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (;j != beg && compare(val, *(j - 1)); --j)
{
*j = *(j - 1);
}
*j = val;
}
}
}
// stable sort
template <class RandomAccessIterator, class Comp>
inline void insert_sort_part_rev(RandomAccessIterator beg, RandomAccessIterator mid, RandomAccessIterator end, Comp compare)
{
for (RandomAccessIterator i = mid; i < end; ++i)
{
if (!compare(*i, *(i - 1)))
{
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (;j != beg && !compare(val, *(j - 1)); --j)
{
*j = *(j - 1);
}
*j = val;
}
}
}
// stable sort
template <class RandomAccessIterator, class Comp>
inline void unguarded_insert_single(RandomAccessIterator i, Comp compare)
{
if (compare(*i, *(i - 1)))
{
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (; compare(val, *(j - 1)); --j)
{
*j = *(j - 1);
}
*j = val;
}
}
// stable sort
template <class RandomAccessIterator, class Comp>
inline void unguarded_insert_single_rev(RandomAccessIterator i, Comp compare)
{
if (!compare(*i, *(i - 1)))
{
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (; !compare(val, *(j - 1)); --j)
{
*j = *(j - 1);
}
*j = val;
}
}
template <class RandomAccessIterator, class Comp>
RandomAccessIterator insert_sort_limit(RandomAccessIterator beg, RandomAccessIterator end, Comp compare, int limit)
{
for (RandomAccessIterator i = beg + 1; i < end; ++i)
{
if (!compare(*i, *(i - 1)))
{
++limit;
continue;
}
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (;j != beg && compare(val, *(j - 1)); --j)
{
if (--limit <= 0)
{
*j = val;
return i;
}
*j = *(j - 1);
}
*j = val;
}
return end;
}
template <class RandomAccessIterator, class Comp>
RandomAccessIterator unguarded_insert_sort_limit(RandomAccessIterator beg, RandomAccessIterator end, Comp compare, int limit)
{
for (RandomAccessIterator i = beg + 1; i < end; ++i)
{
if (!compare(*i, *(i - 1)))
{
++limit;
continue;
}
typename std::iterator_traits<RandomAccessIterator>::value_type val = *i;
RandomAccessIterator j = i - 1;
*i = *j;
for (; compare(val, *(j - 1)); --j)
{
if (--limit <= 0)
{
*j = val;
return i;
}
*j = *(j - 1);
}
*j = val;
}
return end;
}
template <class RandomAccessIterator, class Comp>
void shell_sort(RandomAccessIterator beg, RandomAccessIterator end, Comp compare)
{
if (end - beg > shellsort_insertion_sort_threshold)
{
typedef typename std::iterator_traits<RandomAccessIterator>::difference_type diff_type;
typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
diff_type len = end - beg;
diff_type incre_list[61] = { 0, 9, 34, 182, 836, 4025, 19001, 90358, 428481, 2034035, 9651787, 45806244, 217378076, 1031612713 };
const double incre_factor = 2.9; // simple and fast enought
if (!util::is_scalar<value_type>::value)
{
incre_list[1] = 10;
}
diff_type incre_index = 0;
for (diff_type i = 1; i < 60; ++i)
{
diff_type mul = incre_list[i];
if (mul * incre_factor >= len)
{
incre_index = i;
break;
}
if (!util::is_scalar<value_type>::value || incre_list[i + 1] == 0)
{
incre_list[i + 1] = (diff_type)(mul * incre_factor);
}
}
for (; incre_index > 0; --incre_index)
{
bool swaped = false;
diff_type incre = incre_list[incre_index];
for (diff_type i = incre; i < len; i++)
{
if (compare(*(beg + i), *(beg + i - incre)))
{
value_type val = *(beg + i);
*(beg + i) = *(beg + i - incre);
diff_type pos = i - incre;
for (; pos >= incre && compare(val, *(beg + pos - incre)); pos -= incre)
{
*(beg + pos) = *(beg + pos - incre);
}
*(beg + pos) = val;
swaped = true;
}
}
if (!swaped)
{
RandomAccessIterator last = insert_sort_limit(beg, end, compare, 1);
if (last == end)
{
return;
}
if (last - beg >= incre << 1)
{
beg += ((last - beg) / incre - 1) * incre;
len = end - beg;
}
}
}
insert_sort(beg, beg + incre_list[1], compare);
unguarded_insert_sort(beg + incre_list[1], end, compare);
}
else
{
insert_sort(beg, end, compare);
}
}
template <class RandomAccessIterator, class Comp>
void max_heapify_p(RandomAccessIterator first, RandomAccessIterator target, RandomAccessIterator last, Comp compare)
{
typename std::iterator_traits<RandomAccessIterator>::value_type temp = *target;
--first;
RandomAccessIterator son;
for (; (son = target + (target - first)) <= last; target = son)
{
if (son < last && compare(*son, *(son + 1)))
++son;
if (compare(temp, *son))
*target = *son;
else
break;
}
*target = temp;
}
template <class RandomAccessIterator, class Comp>
void heap_sort_p(RandomAccessIterator beg, RandomAccessIterator end, Comp compare)
{
if (end - beg > 1)
{
for (RandomAccessIterator i = beg + (end - beg) / 2; i >= beg; --i)
max_heapify_p(beg, i, end - 1, compare);
for (--end; end > beg; )
{
std::swap(*beg, *end--);
max_heapify_p(beg, beg, end, compare);
}
}
}
template <class RandomAccessIterator, class Comp>
void max_heapify_1(RandomAccessIterator arr, size_t index, size_t last, Comp compare)
{
typename std::iterator_traits<RandomAccessIterator>::value_type temp = arr[index];
size_t child;
for (; (child = index << 1) <= last; index = child)
{
if (child < last && compare(*(arr + child), *(arr + child + 1)))
++child;
if (compare(temp, *(arr + child)))
*(arr + index) = *(arr + child);
else
break;
}
*(arr + index) = temp;
}
template <class RandomAccessIterator, class Comp>
void heap_sort_1(RandomAccessIterator beg, RandomAccessIterator end, Comp compare)
{
if (end - beg > 1)
{
size_t length = (size_t)(end - beg);
RandomAccessIterator parr = beg - 1;
for (size_t i = length / 2; i > 0; --i)
max_heapify_1(parr, i, length, compare);
for (size_t i = length - 1; i > 0; --i)
{
std::swap(*beg, *(beg + i));
max_heapify_1(parr, 1, i, compare);
}
}
}
// stable sort
template <bool safecopy, class RandomAccessIterator, class RandomAccessBufferIterator, class Comp>
void merge_2_part_force(RandomAccessBufferIterator buf, RandomAccessIterator beg, RandomAccessIterator mid, RandomAccessIterator end, Comp compare)
{
if (mid - beg <= end - mid)
{
if (safecopy)
{
RandomAccessBufferIterator t = buf;
for (RandomAccessIterator s = beg; s != mid; ++s, ++t)
{
*t = *s;
}
}
else
memcpy((char*)buf, (char*)&*beg, (char*)&*mid - (char*)&*beg);
RandomAccessBufferIterator start1 = buf, start1_end = buf + (mid - beg);
RandomAccessIterator start2 = mid, k = beg;
if (start1 < start1_end && start2 < end)
{
while (true)
{
if (compare(*start2, *start1))
{
*k++ = *start2++;
if (start2 >= end)
break;
}
else
{
*k++ = *start1++;
if (start1 >= start1_end)
break;
}
}
}
while (start1 < start1_end)
{
*k++ = *start1++;
}
}
else
{
if (safecopy)
{
RandomAccessBufferIterator t = buf;
for (RandomAccessIterator s = mid; s != end; ++s, ++t)
{
*t = *s;
}
}
else
memcpy((char*)buf, (char*)&*mid, (char*)&*end - (char*)&*mid);
RandomAccessBufferIterator start1 = buf + (end - mid) - 1, start1_end = buf;
RandomAccessIterator start2 = mid - 1, k = end;
if (start1 >= start1_end && start2 >= beg)
{
while (true)
{
if (compare(*start1, *start2))
{
*--k = *start2--;
if (start2 < beg)
break;
}
else
{
*--k = *start1--;
if (start1 < start1_end)
break;
}
}
}
while (start1 >= start1_end)
{
*--k = *start1--;
}
}
}
// stable sort
template <bool safecopy, class RandomAccessIterator, class RandomAccessBufferIterator, class Comp>
void merge_2_part(RandomAccessBufferIterator buf, RandomAccessIterator beg, RandomAccessIterator mid, RandomAccessIterator end, Comp compare)
{
if (!compare(*mid, *(mid - 1)))
{
return;
}
while (beg < mid && !compare(*mid, *beg))
{
++beg;
}
if (beg < mid)
{
RandomAccessIterator mid_last = mid - 1, last = end - 1;
while (mid_last < last && !compare(*last, *mid_last))
{
--last;
}
if (mid_last == last)
{
return;
}
end = last + 1;
}
else
{
return;
}
if (end - beg < merge_2_part_insertion_sort_threshold)
{
insert_sort_part(beg, mid, end, compare);
return;
}
merge_2_part_force<safecopy>(buf, beg, mid, end, compare);
}
template <class RandomAccessIterator>
void swap_2_part_with_same_length(RandomAccessIterator beg, RandomAccessIterator mid)
{
for (RandomAccessIterator l = beg, r = mid; l < mid; ++l, ++r)
{
std::swap(*l, *r);
}
}
template <bool safecopy, class RandomAccessIterator, class RandomAccessBufferIterator>
RandomAccessIterator swap_2_part_with_buffer(RandomAccessBufferIterator buf, size_t bufsize, RandomAccessIterator beg, RandomAccessIterator mid, RandomAccessIterator end)
{
RandomAccessIterator ret = beg + (end - mid);
while (true)
{
if (mid - beg <= end - mid)
{
if (mid - beg > 0)
{
if ((size_t)(mid - beg) > bufsize)
{
swap_2_part_with_same_length(beg, mid);
RandomAccessIterator m = mid;
mid += mid - beg;
beg = m;
}
else
{
if (safecopy)
{
RandomAccessBufferIterator t = buf;
for (RandomAccessIterator s = beg; s != mid; ++s, ++t)
{
*t = *s;
}
RandomAccessIterator m = beg;
for (RandomAccessIterator s = mid; s < end; ++s, ++m)
{
*m = *s;
}
for (RandomAccessBufferIterator it = buf; it < t; ++m, ++it)
{
*m = *it;
}
}
else
{
memcpy((char*)buf, (char*)&*beg, (char*)&*mid - (char*)&*beg);
memmove((char*)&*beg, (char*)&*mid, (char*)&*end - (char*)&*mid);
memcpy((char*)&*(beg + (end - mid)), (char*)buf, (char*)&*mid - (char*)&*beg);
}
return ret;
}
}
else
return ret;
}
else
{
if (end - mid > 0)
{
if ((size_t)(end - mid) > bufsize)
{
swap_2_part_with_same_length(mid - (end - mid), mid);
RandomAccessIterator m = mid;
mid -= end - mid;
end = m;
}
else
{
if (safecopy)
{
RandomAccessBufferIterator t = buf;
for (RandomAccessIterator s = mid; s != end; ++s, ++t)
{
*t = *s;
}
RandomAccessIterator m = end - 1;
for (RandomAccessIterator s = mid - 1; s >= beg; --s, --m)
{
*m = *s;
}
for (--t; t >= buf; --m, --t)
{
*m = *t;
}
}
else
{
memcpy((char*)buf, (char*)&*mid, (char*)&*end - (char*)&*mid);
memmove((char*)&*(end - (mid - beg)), (char*)&*beg, (char*)&*mid - (char*)&*beg);
memcpy((char*)&*beg, (char*)buf, (char*)&*end - (char*)&*mid);
}
return ret;
}
}
else
return ret;
}
}
}
template <class RandomAccessIterator, class Comp>
std::pair<RandomAccessIterator, RandomAccessIterator> find_swap_bound(RandomAccessIterator beg, RandomAccessIterator mid, RandomAccessIterator end, Comp compare)
{
if (end - beg < find_swap_bound_optimize_threshold)
{
RandomAccessIterator pivot_r = mid + (end - mid) / 2 + 1;
RandomAccessIterator pivot_l = std::upper_bound(beg, mid, *pivot_r, compare);
pivot_r = std::lower_bound(pivot_r, end, *pivot_l, compare);
return std::make_pair(pivot_l, pivot_r);
}
else
{
typedef typename std::iterator_traits<RandomAccessIterator>::difference_type diff_type;
RandomAccessIterator pivot_l = beg;
RandomAccessIterator pivot_l_map = std::lower_bound(mid, end, *pivot_l, compare);
if (pivot_l_map - mid > (end - mid) / 2 || mid - pivot_l <= pivot_l_map - mid)
{
return std::make_pair(pivot_l, pivot_l_map);
}
RandomAccessIterator pivot_r = std::lower_bound(beg, mid, *(end - 1), compare);
RandomAccessIterator pivot_r_map = std::lower_bound(mid, end, *pivot_r, compare);
if (mid - pivot_r > (mid - beg) / 2 || mid - pivot_r >= pivot_r_map - mid)
{
return std::make_pair(pivot_r, pivot_r_map);
}