-
Notifications
You must be signed in to change notification settings - Fork 25
/
ska_sort.hpp
1445 lines (1348 loc) · 47.7 KB
/
ska_sort.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
// Copyright Malte Skarupke 2016.
// Distributed under the Boost Software License, Version 1.0.
// (See http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include <cstdint>
#include <algorithm>
#include <type_traits>
#include <tuple>
#include <utility>
namespace detail
{
template<typename count_type, typename It, typename OutIt, typename ExtractKey>
void counting_sort_impl(It begin, It end, OutIt out_begin, ExtractKey && extract_key)
{
count_type counts[256] = {};
for (It it = begin; it != end; ++it)
{
++counts[extract_key(*it)];
}
count_type total = 0;
for (count_type & count : counts)
{
count_type old_count = count;
count = total;
total += old_count;
}
for (; begin != end; ++begin)
{
std::uint8_t key = extract_key(*begin);
out_begin[counts[key]++] = std::move(*begin);
}
}
template<typename It, typename OutIt, typename ExtractKey>
void counting_sort_impl(It begin, It end, OutIt out_begin, ExtractKey && extract_key)
{
counting_sort_impl<std::uint64_t>(begin, end, out_begin, extract_key);
}
inline bool to_unsigned_or_bool(bool b)
{
return b;
}
inline unsigned char to_unsigned_or_bool(unsigned char c)
{
return c;
}
inline unsigned char to_unsigned_or_bool(signed char c)
{
return static_cast<unsigned char>(c) + 128;
}
inline unsigned char to_unsigned_or_bool(char c)
{
return static_cast<unsigned char>(c);
}
inline std::uint16_t to_unsigned_or_bool(char16_t c)
{
return static_cast<std::uint16_t>(c);
}
inline std::uint32_t to_unsigned_or_bool(char32_t c)
{
return static_cast<std::uint32_t>(c);
}
inline std::uint32_t to_unsigned_or_bool(wchar_t c)
{
return static_cast<std::uint32_t>(c);
}
inline unsigned short to_unsigned_or_bool(short i)
{
return static_cast<unsigned short>(i) + static_cast<unsigned short>(1 << (sizeof(short) * 8 - 1));
}
inline unsigned short to_unsigned_or_bool(unsigned short i)
{
return i;
}
inline unsigned int to_unsigned_or_bool(int i)
{
return static_cast<unsigned int>(i) + static_cast<unsigned int>(1 << (sizeof(int) * 8 - 1));
}
inline unsigned int to_unsigned_or_bool(unsigned int i)
{
return i;
}
inline unsigned long to_unsigned_or_bool(long l)
{
return static_cast<unsigned long>(l) + static_cast<unsigned long>(1l << (sizeof(long) * 8 - 1));
}
inline unsigned long to_unsigned_or_bool(unsigned long l)
{
return l;
}
inline unsigned long long to_unsigned_or_bool(long long l)
{
return static_cast<unsigned long long>(l) + static_cast<unsigned long long>(1ll << (sizeof(long long) * 8 - 1));
}
inline unsigned long long to_unsigned_or_bool(unsigned long long l)
{
return l;
}
inline std::uint32_t to_unsigned_or_bool(float f)
{
union
{
float f;
std::uint32_t u;
} as_union = { f };
std::uint32_t sign_bit = -std::int32_t(as_union.u >> 31);
return as_union.u ^ (sign_bit | 0x80000000);
}
inline std::uint64_t to_unsigned_or_bool(double f)
{
union
{
double d;
std::uint64_t u;
} as_union = { f };
std::uint64_t sign_bit = -std::int64_t(as_union.u >> 63);
return as_union.u ^ (sign_bit | 0x8000000000000000);
}
template<typename T>
inline size_t to_unsigned_or_bool(T * ptr)
{
return reinterpret_cast<size_t>(ptr);
}
template<size_t>
struct SizedRadixSorter;
template<>
struct SizedRadixSorter<1>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
counting_sort_impl(begin, end, buffer_begin, [&](auto && o)
{
return to_unsigned_or_bool(extract_key(o));
});
return true;
}
static constexpr size_t pass_count = 2;
};
template<>
struct SizedRadixSorter<2>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
std::ptrdiff_t num_elements = end - begin;
if (num_elements <= (1ll << 32))
return sort_inline<uint32_t>(begin, end, buffer_begin, buffer_begin + num_elements, extract_key);
else
return sort_inline<uint64_t>(begin, end, buffer_begin, buffer_begin + num_elements, extract_key);
}
template<typename count_type, typename It, typename OutIt, typename ExtractKey>
static bool sort_inline(It begin, It end, OutIt out_begin, OutIt out_end, ExtractKey && extract_key)
{
count_type counts0[256] = {};
count_type counts1[256] = {};
for (It it = begin; it != end; ++it)
{
uint16_t key = to_unsigned_or_bool(extract_key(*it));
++counts0[key & 0xff];
++counts1[(key >> 8) & 0xff];
}
count_type total0 = 0;
count_type total1 = 0;
for (int i = 0; i < 256; ++i)
{
count_type old_count0 = counts0[i];
count_type old_count1 = counts1[i];
counts0[i] = total0;
counts1[i] = total1;
total0 += old_count0;
total1 += old_count1;
}
for (It it = begin; it != end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it));
out_begin[counts0[key]++] = std::move(*it);
}
for (OutIt it = out_begin; it != out_end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 8;
begin[counts1[key]++] = std::move(*it);
}
return false;
}
static constexpr size_t pass_count = 3;
};
template<>
struct SizedRadixSorter<4>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
std::ptrdiff_t num_elements = end - begin;
if (num_elements <= (1ll << 32))
return sort_inline<uint32_t>(begin, end, buffer_begin, buffer_begin + num_elements, extract_key);
else
return sort_inline<uint64_t>(begin, end, buffer_begin, buffer_begin + num_elements, extract_key);
}
template<typename count_type, typename It, typename OutIt, typename ExtractKey>
static bool sort_inline(It begin, It end, OutIt out_begin, OutIt out_end, ExtractKey && extract_key)
{
count_type counts0[256] = {};
count_type counts1[256] = {};
count_type counts2[256] = {};
count_type counts3[256] = {};
for (It it = begin; it != end; ++it)
{
uint32_t key = to_unsigned_or_bool(extract_key(*it));
++counts0[key & 0xff];
++counts1[(key >> 8) & 0xff];
++counts2[(key >> 16) & 0xff];
++counts3[(key >> 24) & 0xff];
}
count_type total0 = 0;
count_type total1 = 0;
count_type total2 = 0;
count_type total3 = 0;
for (int i = 0; i < 256; ++i)
{
count_type old_count0 = counts0[i];
count_type old_count1 = counts1[i];
count_type old_count2 = counts2[i];
count_type old_count3 = counts3[i];
counts0[i] = total0;
counts1[i] = total1;
counts2[i] = total2;
counts3[i] = total3;
total0 += old_count0;
total1 += old_count1;
total2 += old_count2;
total3 += old_count3;
}
for (It it = begin; it != end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it));
out_begin[counts0[key]++] = std::move(*it);
}
for (OutIt it = out_begin; it != out_end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 8;
begin[counts1[key]++] = std::move(*it);
}
for (It it = begin; it != end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 16;
out_begin[counts2[key]++] = std::move(*it);
}
for (OutIt it = out_begin; it != out_end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 24;
begin[counts3[key]++] = std::move(*it);
}
return false;
}
static constexpr size_t pass_count = 5;
};
template<>
struct SizedRadixSorter<8>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
std::ptrdiff_t num_elements = end - begin;
if (num_elements <= (1ll << 32))
return sort_inline<uint32_t>(begin, end, buffer_begin, buffer_begin + num_elements, extract_key);
else
return sort_inline<uint64_t>(begin, end, buffer_begin, buffer_begin + num_elements, extract_key);
}
template<typename count_type, typename It, typename OutIt, typename ExtractKey>
static bool sort_inline(It begin, It end, OutIt out_begin, OutIt out_end, ExtractKey && extract_key)
{
count_type counts0[256] = {};
count_type counts1[256] = {};
count_type counts2[256] = {};
count_type counts3[256] = {};
count_type counts4[256] = {};
count_type counts5[256] = {};
count_type counts6[256] = {};
count_type counts7[256] = {};
for (It it = begin; it != end; ++it)
{
uint64_t key = to_unsigned_or_bool(extract_key(*it));
++counts0[key & 0xff];
++counts1[(key >> 8) & 0xff];
++counts2[(key >> 16) & 0xff];
++counts3[(key >> 24) & 0xff];
++counts4[(key >> 32) & 0xff];
++counts5[(key >> 40) & 0xff];
++counts6[(key >> 48) & 0xff];
++counts7[(key >> 56) & 0xff];
}
count_type total0 = 0;
count_type total1 = 0;
count_type total2 = 0;
count_type total3 = 0;
count_type total4 = 0;
count_type total5 = 0;
count_type total6 = 0;
count_type total7 = 0;
for (int i = 0; i < 256; ++i)
{
count_type old_count0 = counts0[i];
count_type old_count1 = counts1[i];
count_type old_count2 = counts2[i];
count_type old_count3 = counts3[i];
count_type old_count4 = counts4[i];
count_type old_count5 = counts5[i];
count_type old_count6 = counts6[i];
count_type old_count7 = counts7[i];
counts0[i] = total0;
counts1[i] = total1;
counts2[i] = total2;
counts3[i] = total3;
counts4[i] = total4;
counts5[i] = total5;
counts6[i] = total6;
counts7[i] = total7;
total0 += old_count0;
total1 += old_count1;
total2 += old_count2;
total3 += old_count3;
total4 += old_count4;
total5 += old_count5;
total6 += old_count6;
total7 += old_count7;
}
for (It it = begin; it != end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it));
out_begin[counts0[key]++] = std::move(*it);
}
for (OutIt it = out_begin; it != out_end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 8;
begin[counts1[key]++] = std::move(*it);
}
for (It it = begin; it != end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 16;
out_begin[counts2[key]++] = std::move(*it);
}
for (OutIt it = out_begin; it != out_end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 24;
begin[counts3[key]++] = std::move(*it);
}
for (It it = begin; it != end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 32;
out_begin[counts4[key]++] = std::move(*it);
}
for (OutIt it = out_begin; it != out_end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 40;
begin[counts5[key]++] = std::move(*it);
}
for (It it = begin; it != end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 48;
out_begin[counts6[key]++] = std::move(*it);
}
for (OutIt it = out_begin; it != out_end; ++it)
{
std::uint8_t key = to_unsigned_or_bool(extract_key(*it)) >> 56;
begin[counts7[key]++] = std::move(*it);
}
return false;
}
static constexpr size_t pass_count = 9;
};
template<typename>
struct RadixSorter;
template<>
struct RadixSorter<bool>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
size_t false_count = 0;
for (It it = begin; it != end; ++it)
{
if (!extract_key(*it))
++false_count;
}
size_t true_position = false_count;
false_count = 0;
for (; begin != end; ++begin)
{
if (extract_key(*begin))
buffer_begin[true_position++] = std::move(*begin);
else
buffer_begin[false_count++] = std::move(*begin);
}
return true;
}
static constexpr size_t pass_count = 2;
};
template<>
struct RadixSorter<signed char> : SizedRadixSorter<sizeof(signed char)>
{
};
template<>
struct RadixSorter<unsigned char> : SizedRadixSorter<sizeof(unsigned char)>
{
};
template<>
struct RadixSorter<signed short> : SizedRadixSorter<sizeof(signed short)>
{
};
template<>
struct RadixSorter<unsigned short> : SizedRadixSorter<sizeof(unsigned short)>
{
};
template<>
struct RadixSorter<signed int> : SizedRadixSorter<sizeof(signed int)>
{
};
template<>
struct RadixSorter<unsigned int> : SizedRadixSorter<sizeof(unsigned int)>
{
};
template<>
struct RadixSorter<signed long> : SizedRadixSorter<sizeof(signed long)>
{
};
template<>
struct RadixSorter<unsigned long> : SizedRadixSorter<sizeof(unsigned long)>
{
};
template<>
struct RadixSorter<signed long long> : SizedRadixSorter<sizeof(signed long long)>
{
};
template<>
struct RadixSorter<unsigned long long> : SizedRadixSorter<sizeof(unsigned long long)>
{
};
template<>
struct RadixSorter<float> : SizedRadixSorter<sizeof(float)>
{
};
template<>
struct RadixSorter<double> : SizedRadixSorter<sizeof(double)>
{
};
template<>
struct RadixSorter<char> : SizedRadixSorter<sizeof(char)>
{
};
template<>
struct RadixSorter<wchar_t> : SizedRadixSorter<sizeof(wchar_t)>
{
};
template<>
struct RadixSorter<char16_t> : SizedRadixSorter<sizeof(char16_t)>
{
};
template<>
struct RadixSorter<char32_t> : SizedRadixSorter<sizeof(char32_t)>
{
};
template<typename K, typename V>
struct RadixSorter<std::pair<K, V>>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
bool first_result = RadixSorter<V>::sort(begin, end, buffer_begin, [&](auto && o)
{
return extract_key(o).second;
});
auto extract_first = [&](auto && o)
{
return extract_key(o).first;
};
if (first_result)
{
return !RadixSorter<K>::sort(buffer_begin, buffer_begin + (end - begin), begin, extract_first);
}
else
{
return RadixSorter<K>::sort(begin, end, buffer_begin, extract_first);
}
}
static constexpr size_t pass_count = RadixSorter<K>::pass_count + RadixSorter<V>::pass_count;
};
template<typename K, typename V>
struct RadixSorter<const std::pair<K, V> &>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
bool first_result = RadixSorter<V>::sort(begin, end, buffer_begin, [&](auto && o) -> const V &
{
return extract_key(o).second;
});
auto extract_first = [&](auto && o) -> const K &
{
return extract_key(o).first;
};
if (first_result)
{
return !RadixSorter<K>::sort(buffer_begin, buffer_begin + (end - begin), begin, extract_first);
}
else
{
return RadixSorter<K>::sort(begin, end, buffer_begin, extract_first);
}
}
static constexpr size_t pass_count = RadixSorter<K>::pass_count + RadixSorter<V>::pass_count;
};
template<size_t I, size_t S, typename Tuple>
struct TupleRadixSorter
{
using NextSorter = TupleRadixSorter<I + 1, S, Tuple>;
using ThisSorter = RadixSorter<typename std::tuple_element<I, Tuple>::type>;
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt out_begin, OutIt out_end, ExtractKey && extract_key)
{
bool which = NextSorter::sort(begin, end, out_begin, out_end, extract_key);
auto extract_i = [&](auto && o)
{
return std::get<I>(extract_key(o));
};
if (which)
return !ThisSorter::sort(out_begin, out_end, begin, extract_i);
else
return ThisSorter::sort(begin, end, out_begin, extract_i);
}
static constexpr size_t pass_count = ThisSorter::pass_count + NextSorter::pass_count;
};
template<size_t I, size_t S, typename Tuple>
struct TupleRadixSorter<I, S, const Tuple &>
{
using NextSorter = TupleRadixSorter<I + 1, S, const Tuple &>;
using ThisSorter = RadixSorter<typename std::tuple_element<I, Tuple>::type>;
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt out_begin, OutIt out_end, ExtractKey && extract_key)
{
bool which = NextSorter::sort(begin, end, out_begin, out_end, extract_key);
auto extract_i = [&](auto && o) -> decltype(auto)
{
return std::get<I>(extract_key(o));
};
if (which)
return !ThisSorter::sort(out_begin, out_end, begin, extract_i);
else
return ThisSorter::sort(begin, end, out_begin, extract_i);
}
static constexpr size_t pass_count = ThisSorter::pass_count + NextSorter::pass_count;
};
template<size_t I, typename Tuple>
struct TupleRadixSorter<I, I, Tuple>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It, It, OutIt, OutIt, ExtractKey &&)
{
return false;
}
static constexpr size_t pass_count = 0;
};
template<size_t I, typename Tuple>
struct TupleRadixSorter<I, I, const Tuple &>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It, It, OutIt, OutIt, ExtractKey &&)
{
return false;
}
static constexpr size_t pass_count = 0;
};
template<typename... Args>
struct RadixSorter<std::tuple<Args...>>
{
using SorterImpl = TupleRadixSorter<0, sizeof...(Args), std::tuple<Args...>>;
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
return SorterImpl::sort(begin, end, buffer_begin, buffer_begin + (end - begin), extract_key);
}
static constexpr size_t pass_count = SorterImpl::pass_count;
};
template<typename... Args>
struct RadixSorter<const std::tuple<Args...> &>
{
using SorterImpl = TupleRadixSorter<0, sizeof...(Args), const std::tuple<Args...> &>;
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
return SorterImpl::sort(begin, end, buffer_begin, buffer_begin + (end - begin), extract_key);
}
static constexpr size_t pass_count = SorterImpl::pass_count;
};
template<typename T, size_t S>
struct RadixSorter<std::array<T, S>>
{
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
auto buffer_end = buffer_begin + (end - begin);
bool which = false;
for (size_t i = S; i > 0; --i)
{
auto extract_i = [&, i = i - 1](auto && o)
{
return extract_key(o)[i];
};
if (which)
which = !RadixSorter<T>::sort(buffer_begin, buffer_end, begin, extract_i);
else
which = RadixSorter<T>::sort(begin, end, buffer_begin, extract_i);
}
return which;
}
static constexpr size_t pass_count = RadixSorter<T>::pass_count * S;
};
template<typename T>
struct RadixSorter<const T> : RadixSorter<T>
{
};
template<typename T>
struct RadixSorter<T &> : RadixSorter<const T &>
{
};
template<typename T>
struct RadixSorter<T &&> : RadixSorter<T>
{
};
template<typename T>
struct RadixSorter<const T &> : RadixSorter<T>
{
};
template<typename T>
struct RadixSorter<const T &&> : RadixSorter<T>
{
};
// these structs serve two purposes
// 1. they serve as illustration for how to implement the to_radix_sort_key function
// 2. they help produce better error messages. with these overloads you get the
// error message "no matching function for call to to_radix_sort(your_type)"
// without these examples, you'd get the error message "to_radix_sort_key was
// not declared in this scope" which is a much less useful error message
struct ExampleStructA { int i; };
struct ExampleStructB { float f; };
inline int to_radix_sort_key(ExampleStructA a) { return a.i; }
inline float to_radix_sort_key(ExampleStructB b) { return b.f; }
template<typename T, typename Enable = void>
struct FallbackRadixSorter : RadixSorter<decltype(to_radix_sort_key(std::declval<T>()))>
{
using base = RadixSorter<decltype(to_radix_sort_key(std::declval<T>()))>;
template<typename It, typename OutIt, typename ExtractKey>
static bool sort(It begin, It end, OutIt buffer_begin, ExtractKey && extract_key)
{
return base::sort(begin, end, buffer_begin, [&](auto && a) -> decltype(auto)
{
return to_radix_sort_key(extract_key(a));
});
}
};
template<typename...>
struct nested_void
{
using type = void;
};
template<typename... Args>
using void_t = typename nested_void<Args...>::type;
template<typename T>
struct has_subscript_operator_impl
{
template<typename U, typename = decltype(std::declval<U>()[0])>
static std::true_type test(int);
template<typename>
static std::false_type test(...);
using type = decltype(test<T>(0));
};
template<typename T>
using has_subscript_operator = typename has_subscript_operator_impl<T>::type;
template<typename T>
struct FallbackRadixSorter<T, void_t<decltype(to_unsigned_or_bool(std::declval<T>()))>>
: RadixSorter<decltype(to_unsigned_or_bool(std::declval<T>()))>
{
};
template<typename T>
struct RadixSorter : FallbackRadixSorter<T>
{
};
template<typename T>
size_t radix_sort_pass_count = RadixSorter<T>::pass_count;
template<typename It, typename Func>
inline void unroll_loop_four_times(It begin, size_t iteration_count, Func && to_call)
{
size_t loop_count = iteration_count / 4;
size_t remainder_count = iteration_count - loop_count * 4;
for (; loop_count > 0; --loop_count)
{
to_call(begin);
++begin;
to_call(begin);
++begin;
to_call(begin);
++begin;
to_call(begin);
++begin;
}
switch(remainder_count)
{
case 3:
to_call(begin);
++begin;
case 2:
to_call(begin);
++begin;
case 1:
to_call(begin);
}
}
template<typename It, typename F>
inline It custom_std_partition(It begin, It end, F && func)
{
for (;; ++begin)
{
if (begin == end)
return end;
if (!func(*begin))
break;
}
It it = begin;
for(++it; it != end; ++it)
{
if (!func(*it))
continue;
std::iter_swap(begin, it);
++begin;
}
return begin;
}
struct PartitionInfo
{
PartitionInfo()
: count(0)
{
}
union
{
size_t count;
size_t offset;
};
size_t next_offset;
};
template<size_t>
struct UnsignedForSize;
template<>
struct UnsignedForSize<1>
{
typedef uint8_t type;
};
template<>
struct UnsignedForSize<2>
{
typedef uint16_t type;
};
template<>
struct UnsignedForSize<4>
{
typedef uint32_t type;
};
template<>
struct UnsignedForSize<8>
{
typedef uint64_t type;
};
template<typename T>
struct SubKey;
template<size_t Size>
struct SizedSubKey
{
template<typename T>
static auto sub_key(T && value, void *)
{
return to_unsigned_or_bool(value);
}
typedef SubKey<void> next;
using sub_key_type = typename UnsignedForSize<Size>::type;
};
template<typename T>
struct SubKey<const T> : SubKey<T>
{
};
template<typename T>
struct SubKey<T &> : SubKey<T>
{
};
template<typename T>
struct SubKey<T &&> : SubKey<T>
{
};
template<typename T>
struct SubKey<const T &> : SubKey<T>
{
};
template<typename T>
struct SubKey<const T &&> : SubKey<T>
{
};
template<typename T, typename Enable = void>
struct FallbackSubKey
: SubKey<decltype(to_radix_sort_key(std::declval<T>()))>
{
using base = SubKey<decltype(to_radix_sort_key(std::declval<T>()))>;
template<typename U>
static decltype(auto) sub_key(U && value, void * data)
{
return base::sub_key(to_radix_sort_key(value), data);
}
};
template<typename T>
struct FallbackSubKey<T, void_t<decltype(to_unsigned_or_bool(std::declval<T>()))>>
: SubKey<decltype(to_unsigned_or_bool(std::declval<T>()))>
{
};
template<typename T>
struct SubKey : FallbackSubKey<T>
{
};
template<>
struct SubKey<bool>
{
template<typename T>
static bool sub_key(T && value, void *)
{
return value;
}
typedef SubKey<void> next;
using sub_key_type = bool;
};
template<>
struct SubKey<void>;
template<>
struct SubKey<unsigned char> : SizedSubKey<sizeof(unsigned char)>
{
};
template<>
struct SubKey<unsigned short> : SizedSubKey<sizeof(unsigned short)>
{
};
template<>
struct SubKey<unsigned int> : SizedSubKey<sizeof(unsigned int)>
{
};
template<>
struct SubKey<unsigned long> : SizedSubKey<sizeof(unsigned long)>
{
};
template<>
struct SubKey<unsigned long long> : SizedSubKey<sizeof(unsigned long long)>
{
};
template<typename T>
struct SubKey<T *> : SizedSubKey<sizeof(T *)>
{
};
template<typename F, typename S, typename Current>
struct PairSecondSubKey : Current
{
static decltype(auto) sub_key(const std::pair<F, S> & value, void * sort_data)
{
return Current::sub_key(value.second, sort_data);
}
using next = typename std::conditional<std::is_same<SubKey<void>, typename Current::next>::value, SubKey<void>, PairSecondSubKey<F, S, typename Current::next>>::type;
};
template<typename F, typename S, typename Current>
struct PairFirstSubKey : Current
{
static decltype(auto) sub_key(const std::pair<F, S> & value, void * sort_data)
{
return Current::sub_key(value.first, sort_data);
}
using next = typename std::conditional<std::is_same<SubKey<void>, typename Current::next>::value, PairSecondSubKey<F, S, SubKey<S>>, PairFirstSubKey<F, S, typename Current::next>>::type;
};
template<typename F, typename S>
struct SubKey<std::pair<F, S>> : PairFirstSubKey<F, S, SubKey<F>>
{
};
template<size_t Index, typename First, typename... More>
struct TypeAt : TypeAt<Index - 1, More..., void>
{
};
template<typename First, typename... More>
struct TypeAt<0, First, More...>
{
typedef First type;
};
template<size_t Index, typename Current, typename First, typename... More>
struct TupleSubKey;
template<size_t Index, typename Next, typename First, typename... More>
struct NextTupleSubKey
{
using type = TupleSubKey<Index, Next, First, More...>;
};
template<size_t Index, typename First, typename Second, typename... More>
struct NextTupleSubKey<Index, SubKey<void>, First, Second, More...>
{
using type = TupleSubKey<Index + 1, SubKey<Second>, Second, More...>;
};
template<size_t Index, typename First>
struct NextTupleSubKey<Index, SubKey<void>, First>
{
using type = SubKey<void>;
};
template<size_t Index, typename Current, typename First, typename... More>
struct TupleSubKey : Current
{
template<typename Tuple>
static decltype(auto) sub_key(const Tuple & value, void * sort_data)
{
return Current::sub_key(std::get<Index>(value), sort_data);
}
using next = typename NextTupleSubKey<Index, typename Current::next, First, More...>::type;
};
template<size_t Index, typename Current, typename First>
struct TupleSubKey<Index, Current, First> : Current
{
template<typename Tuple>
static decltype(auto) sub_key(const Tuple & value, void * sort_data)
{
return Current::sub_key(std::get<Index>(value), sort_data);
}
using next = typename NextTupleSubKey<Index, typename Current::next, First>::type;
};
template<typename First, typename... More>
struct SubKey<std::tuple<First, More...>> : TupleSubKey<0, SubKey<First>, First, More...>
{
};
struct BaseListSortData
{
size_t current_index;