-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoctosort.h
363 lines (286 loc) · 13.4 KB
/
octosort.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
/*
Copyright (C) 2014-2021 Igor van den Hoven [email protected]
*/
/*
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
octosort 1.0
*/
/*
octosort is based on WikiSort and quadsort
WikiSort: https://github.com/BonzaiThePenguin/WikiSort
quadsort: https://github.com/scandum/quadsort
searches: https://github.com/scandum/binary_search
*/
#ifndef OCTOSORT_H
#define OCTOSORT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//#define cmp(a,b) (*(a) > *(b))
typedef int CMPFUNC (const void *a, const void *b);
// Set to 1 to see how it performs when given more memory
#define DYNAMIC_CACHE 0
// utilities
#define SWAP(value1, value2) {swap = value1;value1 = value2;value2 = swap;}
#define PULL(_to) \
pull[pull_index].range = new_range(A.start, B.end); \
pull[pull_index].count = count; \
pull[pull_index].from = index; \
pull[pull_index].to = _to
// not as fast as math.h's sqrt() but it's portable
size_t monobound_sqrt(const size_t size)
{
size_t bot, mid, top, sum;
bot = 0;
top = 65536;
while (top > 1)
{
mid = top / 2;
sum = bot + mid;
if (sum * sum <= size)
{
bot += mid;
}
top -= mid;
}
return bot;
}
size_t Min(const size_t a, const size_t b)
{
return a < b ? a : b;
}
size_t Max(const size_t a, const size_t b)
{
return a > b ? a : b;
}
// 63 -> 32, 64 -> 64, etc. this comes from Hacker's Delight
size_t FloorPowerOfTwo (const size_t value)
{
size_t x = value;
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
#if __LP64__
x = x | (x >> 32);
#endif
return x - (x >> 1);
}
// structure to represent ranges within the array
typedef struct
{
size_t start;
size_t end;
}
Range;
size_t range_length(Range range)
{
return range.end - range.start;
}
Range new_range(const size_t start, const size_t end)
{
return (Range) {start, end};
}
// calculate how to scale the index value to the range within the array
// the bottom-up merge sort only operates on values that are powers of two,
// so scale down to that power of two, then use a fraction to scale back again
typedef struct
{
size_t size;
size_t power_of_two;
size_t numerator;
size_t decimal;
size_t denominator;
size_t decimal_step;
size_t numerator_step;
}
WikiIterator;
void WikiIterator_begin(WikiIterator *me)
{
me->numerator = me->decimal = 0;
}
Range WikiIterator_nextRange(WikiIterator *me)
{
size_t start = me->decimal;
me->decimal += me->decimal_step;
me->numerator += me->numerator_step;
if (me->numerator >= me->denominator)
{
me->numerator -= me->denominator;
me->decimal++;
}
return new_range(start, me->decimal);
}
size_t WikiIterator_finished(WikiIterator *me)
{
return (me->decimal >= me->size);
}
size_t WikiIterator_nextLevel(WikiIterator *me)
{
me->decimal_step += me->decimal_step;
me->numerator_step += me->numerator_step;
if (me->numerator_step >= me->denominator)
{
me->numerator_step -= me->denominator;
me->decimal_step++;
}
return (me->decimal_step < me->size);
}
size_t WikiIterator_length(WikiIterator *me)
{
return me->decimal_step;
}
WikiIterator WikiIterator_new(size_t size2, size_t min_level)
{
WikiIterator me;
me.size = size2;
me.power_of_two = FloorPowerOfTwo(me.size);
me.denominator = me.power_of_two/min_level;
me.numerator_step = me.size % me.denominator;
me.decimal_step = me.size/me.denominator;
WikiIterator_begin(&me);
return me;
}
//////////////////////////////////////////////////////////
//┌────────────────────────────────────────────────────┐//
//│ █████┐ ██████┐ ██████┐████████┐ │//
//│ ██┌──██┐ ██┌──██┐└─██┌─┘└──██┌──┘ │//
//│ └█████┌┘ ██████┌┘ ██│ ██│ │//
//│ ██┌──██┐ ██┌──██┐ ██│ ██│ │//
//│ └█████┌┘ ██████┌┘██████┐ ██│ │//
//│ └────┘ └─────┘ └─────┘ └─┘ │//
//└────────────────────────────────────────────────────┘//
//////////////////////////////////////////////////////////
#undef VAR
#undef FUNC
#undef STRUCT
#define VAR char
#define FUNC(NAME) NAME##8
#define STRUCT(NAME) struct NAME##8
#include "octosort.c"
//////////////////////////////////////////////////////////
//┌────────────────────────────────────────────────────┐//
//│ ▄██┐ █████┐ ██████┐ ██████┐████████┐│//
//│ ████│ ██┌───┘ ██┌──██┐└─██┌─┘└──██┌──┘│//
//│ └─██│ ██████┐ ██████┌┘ ██│ ██│ │//
//│ ██│ ██┌──██┐ ██┌──██┐ ██│ ██│ │//
//│ ██████┐└█████┌┘ ██████┌┘██████┐ ██│ │//
//│ └─────┘ └────┘ └─────┘ └─────┘ └─┘ │//
//└────────────────────────────────────────────────────┘//
//////////////////////////////////////////////////////////
#undef VAR
#undef FUNC
#undef STRUCT
#define VAR short
#define FUNC(NAME) NAME##16
#define STRUCT(NAME) struct NAME##16
#include "octosort.c"
//////////////////////////////////////////////////////////
// ┌───────────────────────────────────────────────────┐//
// │ ██████┐ ██████┐ ██████┐ ██████┐████████┐ │//
// │ └────██┐└────██┐ ██┌──██┐└─██┌─┘└──██┌──┘ │//
// │ █████┌┘ █████┌┘ ██████┌┘ ██│ ██│ │//
// │ └───██┐██┌───┘ ██┌──██┐ ██│ ██│ │//
// │ ██████┌┘███████┐ ██████┌┘██████┐ ██│ │//
// │ └─────┘ └──────┘ └─────┘ └─────┘ └─┘ │//
// └───────────────────────────────────────────────────┘//
//////////////////////////////////////////////////////////
#undef VAR
#undef FUNC
#undef STRUCT
#define VAR int
#define FUNC(NAME) NAME##32
#define STRUCT(NAME) struct NAME##32
#include "octosort.c"
//////////////////////////////////////////////////////////
// ┌───────────────────────────────────────────────────┐//
// │ █████┐ ██┐ ██┐ ██████┐ ██████┐████████┐ │//
// │ ██┌───┘ ██│ ██│ ██┌──██┐└─██┌─┘└──██┌──┘ │//
// │ ██████┐ ███████│ ██████┌┘ ██│ ██│ │//
// │ ██┌──██┐└────██│ ██┌──██┐ ██│ ██│ │//
// │ └█████┌┘ ██│ ██████┌┘██████┐ ██│ │//
// │ └────┘ └─┘ └─────┘ └─────┘ └─┘ │//
// └───────────────────────────────────────────────────┘//
//////////////////////////////////////////////////////////
#undef VAR
#undef FUNC
#undef STRUCT
#define VAR long long
#define FUNC(NAME) NAME##64
#define STRUCT(NAME) struct NAME##64
#include "octosort.c"
//////////////////////////////////////////////////////////
//┌────────────────────────────────────────────────────┐//
//│ ▄██┐ ██████┐ █████┐ ██████┐ ██████┐████████┐ │//
//│ ████│ └────██┐██┌──██┐ ██┌──██┐└─██┌─┘└──██┌──┘ │//
//│ └─██│ █████┌┘└█████┌┘ ██████┌┘ ██│ ██│ │//
//│ ██│ ██┌───┘ ██┌──██┐ ██┌──██┐ ██│ ██│ │//
//│ ██████┐███████┐└█████┌┘ ██████┌┘██████┐ ██│ │//
//│ └─────┘└──────┘ └────┘ └─────┘ └─────┘ └─┘ │//
//└────────────────────────────────────────────────────┘//
//////////////////////////////////////////////////////////
#undef VAR
#undef FUNC
#undef STRUCT
#define VAR long double
#define FUNC(NAME) NAME##128
#define STRUCT(NAME) struct NAME##128
#include "octosort.c"
////////////////////////////////////////////////////////////////////////////////
//┌──────────────────────────────────────────────────────────────────────────┐//
//│ ██████┐ ██████┐████████┐ ██████┐ ███████┐ ██████┐ ██████┐ ████████┐ │//
//│ ██┌───██┐██┌────┘└──██┌──┘██┌───██┐██┌────┘██┌───██┐██┌──██┐└──██┌──┘ │//
//│ ██│ ██│██│ ██│ ██│ ██│███████┐██│ ██│██████┌┘ ██│ │//
//│ ██│ ██│██│ ██│ ██│ ██│└────██│██│ ██│██┌──██┐ ██│ │//
//│ └██████┌┘└██████┐ ██│ └██████┌┘███████│└██████┌┘██│ ██│ ██│ │//
//│ └─────┘ └─────┘ └─┘ └─────┘ └──────┘ └─────┘ └─┘ └─┘ └─┘ │//
//└──────────────────────────────────────────────────────────────────────────┘//
////////////////////////////////////////////////////////////////////////////////
void octosort(void *array, size_t nmemb, size_t size, CMPFUNC *cmp)
{
if (nmemb < 2)
{
return;
}
switch (size)
{
case sizeof(char):
return octosort8(array, nmemb, NULL, 0, cmp);
case sizeof(short):
return octosort16(array, nmemb, NULL, 0, cmp);
case sizeof(int):
return octosort32(array, nmemb, NULL, 0, cmp);
case sizeof(long long):
return octosort64(array, nmemb, NULL, 0, cmp);
case sizeof(long double):
return octosort128(array, nmemb, NULL, 0, cmp);
default:
return assert(size == sizeof(char) || size == sizeof(short) || size == sizeof(int) || size == sizeof(long long) || size == sizeof(long double));
}
}
#undef DYNAMIC_CACHE
#undef PULL
#undef SWAP
#undef VAR
#undef FUNC
#undef STRUCT
#endif