-
Notifications
You must be signed in to change notification settings - Fork 2
/
sort.h
1334 lines (1237 loc) · 37.3 KB
/
sort.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
#pragma once
#ifndef SORT_H
#define SORT_H
#include <iostream>
#include <cassert>
#include <bit>
#include <cstdint>
namespace Algo
{ enum : uint32_t
{
InsertionThreshold = 88,
AscendingThreshold = 8,
LargeDataThreshold = 128,
BlockSize = 64,
#if __cpp_lib_bitops >= 201907L
DoubleWordBitCount = 31,
#else
DeBruijnShiftAmoun = 58
#endif
};
/**
* The DeBruijn constant.
*/
#if __cpp_lib_bitops < 201907L
constexpr uint64_t DeBruijn64 =
0x03F79D71B4CB0A89L;
#endif
/**
* The DeBruijn map from key to integer
* square index.
*/
#if __cpp_lib_bitops < 201907L
constexpr uint8_t DeBruijnTableF[] =
{
0, 47, 1, 56, 48, 27, 2, 60,
57, 49, 41, 37, 28, 16, 3, 61,
54, 58, 35, 52, 50, 42, 21, 44,
38, 32, 29, 23, 17, 11, 4, 62,
46, 55, 26, 59, 40, 36, 15, 53,
34, 51, 20, 43, 31, 22, 10, 45,
25, 39, 14, 33, 19, 30, 9, 24,
13, 18, 8, 12, 7, 6, 5, 63
};
#endif
/**
* Fill trailing bits using prefix fill.
*
* @code
* Example:
* 10000000 >> 1
* = 01000000 | 10000000
* = 11000000 >> 2
* = 00110000 | 11000000
* = 11110000 >> 4
* = 00001111 | 11110000
* = 11111111
* @endcode
* @tparam E The type
* @param x The integer
*/
#if __cpp_lib_bitops < 201907L
constexpr void parallelPrefixFill
(
uint32_t & x
)
{
x |= x >> 1U;
x |= x >> 2U;
x |= x >> 4U;
x |= x >> 8U;
x |= x >> 16U;
}
#endif
/**
* Calculates floor of log2
*
* @authors Kim Walisch - source
* @authors Mark Dickinson - source
* @authors Ellie Moore
* @param bb bitboard to scan
* @precondition bb != 0
* @return index (0..63) of most significant one bit
*/
constexpr int log2
(
uint32_t l
)
{
assert(l != 0);
#if __cpp_lib_bitops >= 201907L
return std::countl_zero(l) ^ DoubleWordBitCount;
#else
parallelPrefixFill(l);
return DeBruijnTableF[(int)
((l * DeBruijn64) >> DeBruijnShiftAmoun)
];
#endif
}
/**
* A simple swap method.
*
* @tparam E the element type
* @param i the first element pointer
* @param j the second element pointer
*/
template<typename E>
constexpr void swap
(
E *const i,
E *const j
)
{
E const el = *i; *i = *j; *j = el;
}
/**
* A generic "sift down" method (AKA max-heapify)
*
* @tparam E the element type
* @param a the pointer to the base of the current
* sub-array
* @param i the starting index
* @param size the size of the current sub-array
*/
template<typename E, class Cmp>
inline void siftDown
(
E* const a,
const int i,
const int size,
const Cmp cmp
)
{
// Store size in
// a local variable.
const size_t n = size;
// Establish non-leaf
// boundary.
const size_t o = n >> 1U;
// Extract the element
// to sift.
E z = a[i];
// initialize temporary
// variables.
size_t x = i, l, r;
// consider only non-leaf
// nodes.
while(x < o)
{
// y is currently
// left child element.
// Note: "l" here stands
// for "left"
r = (l = (x << 1U) + 1) + 1;
E y = a[l];
// if right child is
// within the heap...
// AND
// if right child element
// is greater than left
// child element,
// THEN
// assign right child to
// y and right index to l.
// Note: "l" now stands
// for "larger"
if(r < n && cmp(y, a[r]))
y = a[l = r];
// if y is less than or
// equal to the element
// we are sifting, then
// we are done.
if(!cmp(z, y)) break;
// move y up to the
// parent index.
a[x] = y;
// Set parent index to
// be the index of
// the largest child.
x = l;
}
// Place the sifted element.
a[x] = z;
}
/**
* <h1>
* <b>
* <i>Heap Sort</i>
* </b>
* </h1>
*
* <p>
* Classical heap sort that sorts the given range
* in ascending order, building a max heap and
* continuously sifting/swapping the max element
* to the previous rightmost index.
* </p>
* @author Ellie Moore
* @tparam E the element type
* @param low a pointer to the leftmost index
* @param high a pointer to the rightmost index
*/
template<typename E, class Cmp>
inline void hSort
(
E* const low,
E* const high,
const Cmp cmp
)
{
E* r = high + 1;
E* const l = low;
// Build the heap.
int x = r - l;
for(int i =
(x >> 1U); i >= 0; --i)
siftDown(l, i, x, cmp);
// Sort.
while(l < --r)
{
const E z = *l; *l = *r;
siftDown(l, 0, --x, cmp);
*r = z;
}
}
/**
* <h1>
* <b>
* <i>Insertion Sort</i>
* </b>
* </h1>
*
* <p>
* Classical ascending insertion sort packaged with a
* "pairing" optimization to be used in the context of
* Quicksort.
* </p>
*
* <p>
* This optimization is used whenever the portion of
* the array to be sorted is padded on the left by
* a portion with lesser elements. The fact that all of
* the elements on the left are automatically less than
* the elements in the current portion allows us to skip
* the costly lower boundary check in the nested loops
* and insert two elements in one go.
* </p>
*
* @authors Josh Bloch - source
* @authors Jon Bently - source
* @authors Orson Peters - source
* @authors Ellie Moore
* @tparam E the element type
* @tparam Are we sorting optimistically?
* @param leftmost whether this is the leftmost part
* @param low a pointer to the leftmost index
* @param high a pointer to the rightmost index
* left-most partition.
*/
template
<bool NoGuard, bool Guard, bool Bail = true, typename E, class Cmp>
inline bool iSort
(
E *const low,
E *const high,
const Cmp cmp,
const bool leftmost = true
)
{
E* l = low;
E* r = high;
int moves = 0;
// We aren't guarding, jump
// straight into pair insertion
// sort.
if constexpr (Guard)
goto g1;
if constexpr (NoGuard)
goto g2;
if (leftmost)
{
g1:
// Traditional
// insertion
// sort.
for (E *i = l + 1; i <= r; ++i)
{
E t = *i, *j = i - 1;
for (; j >= l && cmp(t, *j); --j)
j[1] = *j;
j[1] = t;
if constexpr (Bail)
{
// If we have moved too
// many elements, abort.
moves += (i - 1) - j;
if(moves > AscendingThreshold)
return false;
}
}
}
else
{
g2:
// Pair insertion sort.
// Skip elements that are
// in ascending order.
do if (l++ >= r) return true;
while (!cmp(*l, *(l - 1)));
// This sort uses the sub
// array at left to avoid
// the lower bound check.
// Assumes that this is not
// the leftmost partition.
for (E *i = l; ++l <= r; i = ++l)
{
E ex = *i, ey = *l;
// Make sure that
// we insert the
// larger element
// first.
if (cmp(ey, ex))
{
ex = ey;
ey = *i;
if constexpr (Bail)
++moves;
}
// Insert the two
// in one downward
// motion.
while (cmp(ey, *--i))
i[2] = *i;
(++i)[1] = ey;
while (cmp(ex, *--i))
i[1] = *i;
i[1] = ex;
if constexpr (Bail)
{
// If we have moved too
// many elements, abort.
moves += (l - 2) - i;
if(moves > AscendingThreshold)
return false;
}
}
// For odd length arrays,
// insert the last element.
E ez = *r;
while (cmp(ez, *--r))
r[1] = *r;
r[1] = ez;
}
return true;
}
/**
* Explicit constexpr ternary.
*
* @tparam EXP the constexpr
* @tparam E the return type
* @param a the true value
* @param b the false value
*/
template<bool EXP, typename E>
constexpr E ternary
(
E a,
E b
)
{
if constexpr (EXP) return a;
else return b;
}
/**
* Scramble a few elements to help
* break patterns.
*
* @tparam E the element type
* @param i the first element pointer
* @param j the second element pointer
*/
template<typename E>
constexpr void scramble
(
E* const low,
E* const high,
const size_t len
)
{
if(len >= InsertionThreshold)
{
const int _4th = len >> 2U;
swap(low, low + _4th);
swap(high, high - _4th);
if(len > LargeDataThreshold)
{
swap(low + 1, low + (_4th + 1));
swap(low + 2, low + (_4th + 2));
swap(high - 2, high - (_4th + 2));
swap(high - 1, high - (_4th + 1));
}
}
}
/**
* Aligns the given pointer on 64-byte
* cachline.
*
* @tparam E the element type
* @param p pointer to memory to align
*/
template<typename E>
constexpr E* align
(
E* p
)
{
return reinterpret_cast<E*>((
reinterpret_cast<uintptr_t>(p) + (BlockSize - 1)
) & -uintptr_t(BlockSize));
}
/**
* <h1>
* <b>
* <i>Blipsort</i>
* </b>
* </h1>
*
* <h2>Branchless Lomuto</h2>
* <p>
* The decades-old partitioning algorithm recently
* made a resurgence when researchers discovered
* ways to remove the inner branch. Lukas Bergdoll
* and Orson Peters' method— published a little
* under two months ago— is the fastest yet. It
* employs a gap in the data to move elements
* twice per iteration rather than swapping them
* (three moves). For arithmetic and pointer types,
* Blipsort employs branchless Lomuto partitioning.
* For other, larger types, Blipsort uses branchless
* or branchful Hoare partitioning.
* </p>
*
* <h2>Pivot Selectivity</h2>
* <p>
* Blipsort carefully selects the pivot from the
* middle of five sorted candidates. These
* candidates allow the sort to determine whether
* the data in the current interval is approximately
* descending and inform its "partition left" strategy.
* </p>
*
* <h2>Insertion Sort</h2>
* <p>
* Blipsort uses Insertion sort on small intervals
* where asymptotic complexity matters less and
* instruction overhead matters more. Blipsort
* employs Java's Pair Insertion sort on every
* interval except the leftmost. Pair insertion
* sort inserts two elements at a time and doesn't
* need to perform a lower bound check, making it
* slightly faster than normal insertion sort in
* the context of quicksort.
* </p>
*
* <h2>Pivot Retention</h2>
* <p>
* Similar to PDQsort, if any of the three middlemost
* candidate pivots is equal to the rightmost element
* of the partition at left, Blipsort moves equal
* elements to the left with branchless Lomuto and
* continues to the right, solving the dutch-flag
* problem and yeilding linear time on data comprised
* of equal elements.
* </p>
*
* <h2>Optimism</h2>
* <p>
* Similar to PDQsort, if the partition is "good"
* (not highly unbalanced), Blipsort switches to
* insertion sort. If the Insertion sort makes more
* than a constant number of moves, Blipsort bails
* and resumes quicksort. This allows Blipsort to
* achieve linear time on already-sorted data.
* </p>
*
* <h2>Breaking Patterns</h2>
* <p>
* Like PDQsort, if the partition is bad, Blipsort
* scrambles some elements to break up patterns.
* </p>
*
* <h2>Rotation</h2>
* <p>
* When all of the candidate pivots are strictly
* descending, it is very likely that the interval
* is descending as well. Lomuto partitioning slows
* significantly on descending data. Therefore,
* Blipsort neglects to sort descending candidates
* and instead swap-rotates the entire interval
* before partitioning.
* </p>
*
* <h2>Custom Comparators</h2>
* <p>
* Blipsort allows its user to implement a custom
* boolean comparator. A comparator is best
* implemented with a lambda and no branches. A
* comparator implemented with a lambda can be
* inlined by an optimizing compiler, while a
* comparator implemented with a constexpr/inline
* function typically cannot.
* </p>
*
* @authors Josh Bloch - source
* @authors Jon Bently - source
* @authors Orson Peters - source
* @authors Lukas Bergdoll - source
* @authors Stefan Edelkamp - source
* @authors Armin Weiß - source
* @authors Ellie Moore
* @tparam E the element type
* @tparam Root whether this is the sort root
* @param leftmost whether this is the leftmost part
* @param low a pointer to the leftmost index
* @param high a pointer to the rightmost index
* @param height the distance of the current sort
* tree from the initial height of 2log<sub>2</sub>n
*/
template
<bool Expense, bool Block, bool Root = true, typename E, class Cmp>
inline void qSort
(
E * low,
E * high,
int height,
const Cmp cmp,
bool leftmost = true
)
{
// Tail call loop.
for(size_t x = high - low;;)
{
// If this is not the
// root node, sort the
// interval by insertion
// sort if small enough.
if constexpr (!Root)
if (x < InsertionThreshold)
{
// If we are in the Root,
// we won't be insertion
// sorting until we
// iterate on the rightmost
// part. However, we are
// not in the root here, so
// we need to be careful
// to use guarded insertion
// sort if this is the
// leftmost partition.
iSort<0,0,0>
(low, high, cmp, leftmost);
return;
}
// If this is not the root node,
// heap sort when the runtime
// trends towards quadratic.
if constexpr (!Root)
if(height < 0)
return hSort(low, high, cmp);
// Find an inexpensive
// approximation of a third of
// the interval.
const size_t y = x >> 2U,
_3rd = y + (y >> 1U),
_6th = _3rd >> 1U;
// Find an approximate
// midpoint of the interval.
E *const mid = low + (x >> 1U);
// Assign tercile indices
// to candidate pivots.
E *const sl = low + _3rd;
E *const sr = high - _3rd;
// Assign outer indices
// to candidate pivots.
E * cl = low + _6th;
E * cr = high - _6th;
// If the candidates aren't
// descending...
// Insertion sort all five
// candidate pivots in-place.
if((!cmp(*cl, *low)) ||
(!cmp(*sl, *cl)) ||
(!cmp(*mid, *sl)) ||
(!cmp(*sr, *mid)) ||
(!cmp(*cr, *sr)) ||
(!cmp(*high, *cr)))
{
if(cmp(*low, *cl))
cl = low;
if(cmp(*cr, *high))
cr = high;
if (cmp(*sl, *cl))
{
E e = *sl;
*sl = *cl;
*cl = e;
}
if (cmp(*mid, *sl))
{
E e = *mid;
*mid = *sl;
*sl = e;
if (cmp(e, *cl))
{
*sl = *cl;
*cl = e;
}
}
if (cmp(*sr, *mid))
{
E e = *sr;
*sr = *mid;
*mid = e;
if (cmp(e, *sl))
{
*mid = *sl;
*sl = e;
if (cmp(e, *cl))
{
*sl = *cl;
*cl = e;
}
}
}
if (cmp(*cr, *sr))
{
E e = *cr;
*cr = *sr;
*sr = e;
if (cmp(e, *mid))
{
*sr = *mid;
*mid = e;
if (cmp(e, *sl))
{
*mid = *sl;
*sl = e;
if (cmp(e, *cl))
{
*sl = *cl;
*cl = e;
}
}
}
}
}
// If the candidates are
// descending, then the
// interval is likely to
// be descending somewhat.
// rotate the entire interval
// around the midpoint.
// Don't worry about the
// even size case. One
// out-of-order element
// is no big deal.
else
{
E* u = low;
E* q = high;
while(u < mid)
{
E e = *u;
*u++ = *q;
*q-- = e;
}
}
// If any middle candidate
// pivot is equal to the
// rightmost element of the
// partition to the left,
// swap pivot duplicates to
// the side and sort the
// remainder. This is an
// alternative to dutch-flag
// partitioning.
if(!leftmost)
{
// Check the pivot to
// the left.
E h = *(low - 1);
if(!cmp(h, *sl) ||
!cmp(h, *mid) ||
!cmp(h, *sr))
{
E* l = low - 1,
* g = high + 1;
// skip over data
// in place.
while(cmp(h, *--g));
if(g == high)
while(!cmp(h, *++l) && l < g);
else
while(!cmp(h, *++l));
// If we are sorting
// non-arithmetic types,
// use Hoare for fewer
// moves.
if constexpr (Expense)
{
/**
* Partition left by branchful Hoare scheme
*
* During partitioning:
*
* +-------------------------------------------------------------+
* | ... == h | ... ? ... | ... > h |
* +-------------------------------------------------------------+
* ^ ^ ^ ^
* low l k high
*
* After partitioning:
*
* +-------------------------------------------------------------+
* | ... == h | > h ... |
* +-------------------------------------------------------------+
* ^ ^ ^
* low l high
*/
while(l < g)
{
swap(l, g);
while(cmp(h, *--g));
while(!cmp(h, *++l));
}
}
// If we are sorting
// arithmetic types,
// use branchless lomuto
// for fewer branches.
else
{
/**
* Partition left by branchless Lomuto scheme
*
* During partitioning:
*
* +-------------------------------------------------------------+
* | ... == h | ... > h | * | ... ? ... | ... > h |
* +-------------------------------------------------------------+
* ^ ^ ^ ^ ^
* low l k g high
*
* After partitioning:
*
* +-------------------------------------------------------------+
* | ... == h | > h ... |
* +-------------------------------------------------------------+
* ^ ^ ^
* low l high
*/
E * k = l, p = *l;
while(k < g)
{
*k = *l;
*l = *++k;
l += !cmp(h, *l);
}
*k = *l; *l = p;
}
// Advance low to the
// start of the right
// partition.
low = l;
// If we have nothing
// left to sort, return.
if(low >= high)
return;
// Calculate the interval
// width and loop.
x = high - low;
continue;
}
}
// Initialize l and k.
E *l = ternary<Expense>(low, low - 1),
*k = high + 1, * g;
// Assign midpoint to pivot
// variable.
const E p = *mid;
// If we are sorting
// non-arithmetic types, bring
// left end inside. Left end
// will be replaced and pivot
// will be swapped back later.
if constexpr(Expense)
*mid = *low;
// skip over data
// in place.
while(cmp(*++l, p));
// If we are sorting
// arithmetic types, bring
// left end inside. Left end
// will be replaced and pivot
// will be swapped back later.
if constexpr(!Expense)
*mid = *l;
// skip over data
// in place.
if(ternary<Expense>
(l == low + 1, l == low))
while(!cmp(*--k, p) && k > l);
else
while(!cmp(*--k, p));
// Will we do a significant
// amount of work during
// partitioning?
bool work =
((l - low) + (high - k))
< (x >> 1U);
// If we are sorting
// non-arithmetic types and
// conserving memory, use
// Hoare for fewer moves.
if constexpr (Expense && !Block)
{
/**
* Partition by branchful Hoare scheme
*
* During partitioning:
*
* +-------------------------------------------------------------+
* | ... < p | ... ? ... | ... >= p |
* +-------------------------------------------------------------+
* ^ ^ ^ ^
* low l k high
*
* After partitioning:
*
* +-------------------------------------------------------------+
* | ... < p | >= p ... |
* +-------------------------------------------------------------+
* ^ ^ ^
* low l high
*/
while(l < k)
{
swap(l, k);
while(cmp(*++l, p));
while(!cmp(*--k, p));
}
*low = *--l; *l = p;
}
// If we are sorting
// non-arithmetic types and
// not conserving memory, use
// Block Hoare for fewer moves
// and fewer branches.
else if constexpr (Expense)
{
/**
* Partition by branchless (Block) Hoare scheme
*
* During partitioning:
*
* +-------------------------------------------------------------+
* | ... < p | cmp | ... ? ... | cmp | ... >= p |
* +-------------------------------------------------------------+
* ^ ^ ^ ^ ^ ^
* low _low l k _high high
*
* After partitioning:
*
* +-------------------------------------------------------------+
* | ... < p | >= p ... |
* +-------------------------------------------------------------+
* ^ ^ ^
* low l high
*/
if(l < k)
{
swap(l++, k);
// Set up blocks and
// align base pointers to
// the cacheline.
uint8_t
ols[BlockSize << 1U],
oks[BlockSize << 1U];
uint8_t
* olp = align(ols),
* okp = align(oks);
// Initialize frame pointers.
E * _low = l, * _high = k;
// Initialize offset counts and
// start indices for swap routine.
size_t nl = 0, nk = 0, ls = 0, ks = 0;
while(l < k)
{
// If both blocks are empty, split
// the interval in two. Otherwise
// give the whole interval to one
// block.
size_t xx = k - l,
lspl = -(nl == 0) & (xx >> (nk == 0)),
kspl = -(nk == 0) & (xx - lspl);
// Fill the offset blocks. If the split
// for either block is larger than 64,
// crop it and unroll the loop. Otherwise,
// keep the loop fully rolled. This should
// only happen near the end of partitioning.
if(lspl >= BlockSize)
{
size_t i = -1;
do
{
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
olp[nl] = ++i; nl += !cmp(*l++, p);
} while(i < BlockSize - 1);
}