-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.c
More file actions
397 lines (338 loc) · 13.1 KB
/
Copy pathsort.c
File metadata and controls
397 lines (338 loc) · 13.1 KB
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
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <tgmath.h>
#include "heap.h"
#include "stack.h"
#include "sort.h"
/* Nice clean pointer bubblesort, never fully O(n^2). */
static void __bubble_sort(char *left, char *right, size_t size, compare_fn_t cmp)
{
for (char *i = right; i > left; i -= size) // Start at the right.
for (char *j = left; j < i; j += size) // Check all elements before i if they are larger.
if ((*cmp)(i, j))
BYTE_SWAP(j, i, size); // Swap if larger.
}
/* Nice clean pointer insertion sort, never fully O(n^2). */
static void __insertion_sort(char *left, char *right, size_t size, compare_fn_t cmp)
{
// First loop that semi sorts array, moves smallest element to first position.
for (char* i = left + size; i <= right; i += size)
if (cmp(i, left))
BYTE_SWAP(i, left, size);
// Second loop sorts all elements, extremely fast if array is almosot already sorted.
for (char* i = left + (size << 1); i <= right; i += size) {
char high[size];
BYTE_ASSERT(high, i, size);
char* j = i - size;
while (cmp(high, j)) {
BYTE_ASSERT(j + size, j, size);
j -= size;
}
BYTE_ASSERT(j + size, high, size);
}
}
/* Standard sorting of three values. */
static inline char *__median_three(char *left, char *right, size_t size, compare_fn_t cmp)
{
char *mid = left + FIND_MID(right, left, size);
if (cmp(mid, left)) {
if (cmp(right, left)) {
if (cmp (right, mid)) {
BYTE_SWAP(left, right, size);
} else {
THREE_BYTE_ROTATE(left, mid, right, size);
}
}
else {
BYTE_SWAP(left, mid, size);
}
}
else {
if ((*cmp)(right, mid)) {
if ((*cmp)(right, left)) {
THREE_BYTE_ROTATE(right, mid, left, size);
} else {
BYTE_SWAP(mid, right, size);
}
}
}
return mid;
}
static char *__median_five(char *left, char *right, size_t size, compare_fn_t cmp)
{
char* mid = left + FIND_MID(right, left, size);
char* left_mid = left + FIND_FIRST_QUART(right, left, size);
char* right_mid = left + FIND_THIRD_QUART(right, left, size);
if ((*cmp)(left_mid, left))
BYTE_SWAP(left, left_mid, size);
if ((*cmp)(right, right_mid))
BYTE_SWAP(right_mid, right, size);
if ((*cmp)(right_mid, left))
BYTE_SWAP(left, right_mid, size);
if ((*cmp)(right, left_mid))
BYTE_SWAP(left_mid, right, size);
if ((*cmp)(right_mid, mid))
BYTE_SWAP(mid, right_mid, size);
if ((*cmp)(mid, left_mid))
BYTE_SWAP(left_mid, mid, size);
if ((*cmp)(right_mid, mid))
BYTE_SWAP(right_mid, mid, size);
return mid;
}
static char *__median_five_of_three(char *left, char *right, size_t size, compare_fn_t cmp)
{
char *t_first_quart = left + FIND_FIRST_QUART(right, left, size);
char *t_mid = left + FIND_MID(right, left, size);
char *t_third_quart = left + FIND_THIRD_QUART(right, left, size);
char *left_mid = __median_three(left, t_first_quart, size, cmp);
char *mid = __median_three(t_first_quart, t_mid, size, cmp);
char *right_mid = __median_three(t_mid, t_third_quart, size, cmp);
char *last = __median_three(t_third_quart, right, size, cmp);
if ((*cmp)(left_mid, left))
BYTE_SWAP(left, left_mid, size);
if ((*cmp)(right, right_mid))
BYTE_SWAP(right_mid, right, size);
if ((*cmp)(right_mid, left))
BYTE_SWAP(left, right_mid, size);
if ((*cmp)(right, left_mid))
BYTE_SWAP(left_mid, right, size);
if ((*cmp)(right_mid, mid))
BYTE_SWAP(mid, right_mid, size);
if ((*cmp)(mid, left_mid))
BYTE_SWAP(left_mid, mid, size);
if ((*cmp)(right_mid, mid))
BYTE_SWAP(right_mid, mid, size);
return mid;
}
/* Magical block partitioning based on this research paper: https://arxiv.org/pdf/1604.06697.pdf */
static void __block_partition(char *left, char *right, char **pivot_l, char **pivot_r, size_t size, compare_fn_t cmp)
{
char *mid;
char piv[size];
int diff = right - left;
const int med_5_lim = size << 13;
if (diff > med_5_lim) {
mid = __median_five_of_three(left, right, size, cmp);
} else {
mid = __median_five(left, right, size, cmp);
}
BYTE_ASSERT(piv, mid, size);
BYTE_ASSERT(mid, left + size, size);
BYTE_ASSERT(left + size, piv, size);
char *l;
char *r;
int block_size = BLOCK * size;
if (diff > (block_size << 1) + (size << 1)) { // Probably inefficient code, but no
l = left + (size << 1); // need to init if the loop doesn't run.
r = right - size;
char *offset_left[block_size]; // Left buffer.
char *offset_right[block_size]; // Right buffer.
char **num_left = offset_left; // Tmp pointer to left buffer.
char **num_right = offset_right; // Tmp pointer to right buffer.
do { // Main loop for partioning.
char *l_lim = l + block_size; // Limit for the left buffer.
if (num_left == offset_left) { // If the left buffer is empty start loop.
char *pd = l; // Tmp value for the left-most value.
do {
*num_left = pd;
bool pred = true;
if (*piv == *pd) {
if (size == 1) {
pred = true;
} else {
BYTES_EQUAL(piv + 1, pd + 1, size - 1, pred);
}
} else {
pred = false;
}
num_left += pred || cmp(piv, pd); // If the value is larger than current piv, go to the next pointer.
pd += size; // Go to the next value to the right.
} while (pd <= l_lim); // Continue until movement is equal to blocksize.
}
char *r_lim = r - block_size; // Limit for the right buffer.
if (num_right == offset_right) { // If the left buffer is empty start loop.
char *pd = r; // Tmp value for the right-most value.
do {
*num_right = pd;
bool pred = true;
if (*piv == *pd) {
if (size == 1) {
pred = true;
} else {
BYTES_EQUAL(piv + 1, pd + 1, size - 1, pred);
}
} else {
pred = false;
}
num_right += pred || cmp(pd, piv); // If the value is larger than current piv, go to the next pointer.
pd -= size; // Go to the next value to the left.
} while (pd >= r_lim); // Continue until movement is equal to blocksize.
}
// Get the size of the smallest buffer and move that many places to the left in both buffers.
int min = min(num_left - offset_left, num_right - offset_right);
num_left -= min;
num_right -= min;
// Swap the pointers so the pointers to larger values are on the right buffer and smaller on the left buffer.
for (int i = 0; i < min; i++)
BYTE_SWAP(*(num_left + i), *(num_right + i), size);
// If the pointers are equal to the buffer, this block is correct for that side and we move to the next block.
if (num_left == offset_left)
l += block_size;
if (num_right == offset_right)
r -= block_size;
} while(r - l >= block_size << 1);
l -= size;
r += size;
} else {
l = left + size;
r = right;
}
// Hoare's partition, slightly rewritten.
do {
do l += size;
while((*cmp)(l, piv));
do r -= size;
while ((*cmp)(piv, r));
if (l >= r)
break;
BYTE_SWAP(l, r, size);
} while(true);
// Move all elements equal to the pivot to the left.
char *piv_l = r;
if (right - r > med_5_lim) {
char *pd = r + size;
int k = 0;
while(k != 3 && pd <= right) {
if (cmp(pd, piv)) {
k = 0;
r += size;
BYTE_SWAP(pd, r, size);
}
k++;
pd += size;
}
}
BYTE_ASSERT(left + size, r, size);
BYTE_ASSERT(r, piv, size);
*pivot_l = piv_l;
*pivot_r = r;
}
void __heap_sort(char *left, char *right, size_t size, compare_fn_t cmp)
{
struct heap_t *heap = heap_malloc(cmp);
char *p = left;
char elems[(right - left) / size][size];
int i = 0;
while (p <= right) {
BYTE_ASSERT(*(elems + i), p, size);
heap_push(heap, *(elems + i++));
p += size;
}
p = right;
while (p >= left) {
char *elem = (char *)heap_pop(heap);
BYTE_SWAP(p, elem, size);
p -= size;
}
heap_free(heap);
}
static void __quicksort_rec(char *left, char *right, size_t size, compare_fn_t cmp)
{
if (right - left >= RUN_INSERTION * size) {
char *piv_r; // Create pointer for the pivot/split-point.
char *piv_l;
__block_partition(left, right, &piv_r, &piv_l, size, cmp); // Partition the pointer.
__quicksort_rec(piv_r + size, right, size, cmp);
__quicksort_rec(left, piv_l - size, size, cmp);
} else {
__insertion_sort(left, right, size, cmp);
}
}
static void __quicksort(char *left, char *right, size_t size, compare_fn_t cmp)
{
const int depth_limit = 2 * ilogb((double)(right - left)) + 3;
const int ins_limit = RUN_INSERTION * size;
int depth = 0;
struct stack_t stack;
stack_init(&stack, depth_limit);
stack_push(&stack, left);
stack_push(&stack, right);
int depth_stack[depth_limit];
int* d_s_top = depth_stack;
*d_s_top = 0;
++d_s_top;
do {
if (depth < depth_limit && right - left >= ins_limit) {
char *piv_r;
char *piv_l;
__block_partition(left, right, &piv_l, &piv_r, size, cmp);
if (piv_l - left > right - piv_r) {
stack_push(&stack, left);
stack_push(&stack, piv_l);
left = piv_l + size;
} else {
stack_push(&stack, piv_r + size);
stack_push(&stack, right);
right = piv_r + size;
}
depth++;
*d_s_top = depth;
++d_s_top;
} else {
if (right - left >= ins_limit)
__heap_sort(left, right, size, cmp);
else
__insertion_sort(left, right, size, cmp);
right = stack_pop(&stack);
left = stack_pop(&stack);
--d_s_top;
depth = *d_s_top;
}
} while (!stack_empty(&stack));
free(stack.items);
}
inline void insertion_sort(const void *base, size_t nmemb, size_t size, compare_fn_t cmp)
{
if (!nmemb) // Return if there are no items.
return;
char *base_ptr = (char *)base; // Cast the array to an array of bytes.
char *bound_ptr = base_ptr + size * (nmemb - 1); // Create a pointer to the last byte.
__insertion_sort(base_ptr, bound_ptr, size, cmp);
}
inline void bubble_sort(const void *base, size_t nmemb, size_t size, compare_fn_t cmp)
{
if (!nmemb) // Return if there are no items.
return;
char *base_ptr = (char *)base; // Cast the array to an array of bytes
char *bound_ptr = base_ptr + size * (nmemb - 1); // Create a pointer to the last byte.
__bubble_sort(base_ptr, bound_ptr, size, cmp);
}
inline void quicksort(const void *base, size_t nmemb, size_t size, compare_fn_t cmp)
{
if (!nmemb) // Return if there are no items.
return;
char *base_ptr = (char *)base; // Cast the array to an array of bytes.
char *left_ptr = base_ptr;
char *right_ptr = base_ptr + size * (nmemb - 1); // Create a pointer to the last byte.
__quicksort(left_ptr, right_ptr, size, cmp);
}
inline void quicksort_rec(const void *base, size_t nmemb, size_t size, compare_fn_t cmp)
{
if (!nmemb) // Return if there are no items.
return;
int depth = 0;
char *base_ptr = (char *)base; // Cast the array to an array of bytes.
char *left_ptr = base_ptr;
char *right_ptr = base_ptr + size * (nmemb - 1); // Create a pointer to the last byte.
__quicksort_rec(left_ptr, right_ptr, size, cmp);
}
inline void heap_sort(const void *base, size_t nmemb, size_t size, compare_fn_t cmp)
{
if (!nmemb) // Return if there are no items.
return;
char *base_ptr = (char *)base; // Cast the array to an array of bytes.
char *left_ptr = base_ptr;
char *right_ptr = base_ptr + size * (nmemb - 1); // Create a pointer to the last byte.
__heap_sort(left_ptr, right_ptr, size, cmp);
}