-
Notifications
You must be signed in to change notification settings - Fork 0
/
qqsort.h
224 lines (211 loc) · 15.8 KB
/
qqsort.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
/*
* SPDX-FileCopyrightText: Copyright 2023 Denis Glazkov <glazzk.off@mail.ru>
* SPDX-License-Identifier: MIT
*
* Reimplementation of qsort function from glibc with
* more ability to inline comparison function.
*
* https://codebrowser.dev/glibc/glibc/stdlib/qsort.c.html
*/
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#define __QQSORT_SWAP(A, B, N) \
do { \
uint8_t temp; \
\
uint8_t *a = (A); \
uint8_t *b = (B); \
\
size_t i = 0; \
\
while (i++ < (N)) { \
temp = *a; \
*a++ = *b; \
*b++ = temp; \
} \
} while (0)
#define __QQSORT_STACK_PUSH(L, R, TOP) \
do { \
(TOP)->l = (L); \
(TOP)->r = (R); \
(TOP)++; \
} while (0)
#define __QQSORT_STACK_POP(L, R, TOP) \
do { \
(TOP)--; \
(L) = (TOP)->l; \
(R) = (TOP)->r; \
} while (0)
#define __QQSORT_CMP_RESULT_VARIABLE_NAME __qqsort_cmp_result
#define __QQSORT_CMP_A_VARIABLE_NAME __qqsort_cmp_a
#define __QQSORT_CMP_B_VARIABLE_NAME __qqsort_cmp_b
#define __QQSORT_CMP(A, B, CMP_CALLBACK, IF_CALLBACK, ELSE_CALLBACK) \
do { \
void *__QQSORT_CMP_A_VARIABLE_NAME = (void *) (A); \
void *__QQSORT_CMP_B_VARIABLE_NAME = (void *) (B); \
\
int __QQSORT_CMP_RESULT_VARIABLE_NAME = 0; \
CMP_CALLBACK \
\
if (__QQSORT_CMP_RESULT_VARIABLE_NAME < 0) \
IF_CALLBACK \
else \
ELSE_CALLBACK \
} while (0)
#define __QQSORT_INF_LOOP_FLAG __qqsort_infinite_loop_stop
#define __QQSORT_INF_LOOP_STOP __QQSORT_INF_LOOP_FLAG = true
#define __QQSORT_INF_LOOP __QQSORT_INF_LOOP_FLAG = false; while (!__QQSORT_INF_LOOP_FLAG)
/**
* Utility class to avoid the error of converting
* void* to any other pointer when using C++.
*/
#ifdef __cplusplus
template <typename T>
struct __QQSORT_AUTOCAST
{
constexpr __QQSORT_AUTOCAST(T *ptr) : ptr(ptr) {}
template <typename U>
constexpr operator U() { return static_cast<U>(ptr); }
T *ptr;
};
#else
#define __QQSORT_AUTOCAST(PTR) (PTR)
#endif
#define qqsortret(A) __QQSORT_CMP_RESULT_VARIABLE_NAME = (A)
#define qqsortcmp(A, B) \
A = __QQSORT_AUTOCAST(__QQSORT_CMP_A_VARIABLE_NAME); \
B = __QQSORT_AUTOCAST(__QQSORT_CMP_B_VARIABLE_NAME);
typedef struct {
uint8_t *l;
uint8_t *r;
} __qqsort_stack_node_t;
#define qqsort(ARR, LEN, SIZE, CMP) \
do { \
bool __QQSORT_INF_LOOP_FLAG; \
\
uint8_t *base = (uint8_t *) (ARR); \
size_t maxthresh = 4 * (SIZE); \
\
if ((LEN) == 0) break; \
\
if ((size_t) (LEN) > maxthresh) { \
uint8_t *l = base; \
uint8_t *r = l + (SIZE) * ((LEN) - 1); \
\
__qqsort_stack_node_t stack[sizeof(size_t) * CHAR_BIT]; \
__qqsort_stack_node_t *top = stack; \
\
__QQSORT_STACK_PUSH(NULL, NULL, top); \
\
while (stack < top) { \
uint8_t *ll; \
uint8_t *rr; \
uint8_t *mm = l + (SIZE) * ((r - l) / (SIZE) >> 1); \
\
__QQSORT_CMP(mm, l, CMP, { __QQSORT_SWAP(mm, l, (SIZE)); }, {}); \
__QQSORT_CMP(r, mm, CMP, \
{ \
__QQSORT_SWAP(mm, r, (SIZE)); \
__QQSORT_CMP(mm, l, CMP, { __QQSORT_SWAP(mm, l, (SIZE)); }, {}); \
}, \
{}); \
\
ll = l + (SIZE); \
rr = r - (SIZE); \
\
do { \
__QQSORT_INF_LOOP { \
__QQSORT_CMP(ll, mm, CMP, { ll += (SIZE); }, { __QQSORT_INF_LOOP_STOP; }); \
} \
\
__QQSORT_INF_LOOP { \
__QQSORT_CMP(mm, rr, CMP, { rr -= (SIZE); }, { __QQSORT_INF_LOOP_STOP; }); \
} \
\
if (ll < rr) { \
__QQSORT_SWAP(ll, rr, (SIZE)); \
\
if (mm == ll) { \
mm = rr; \
} else if (mm == rr) { \
mm = ll; \
} \
\
ll += (SIZE); \
rr -= (SIZE); \
} else if (ll == rr) { \
ll += (SIZE); \
rr -= (SIZE); \
break; \
} \
} while (ll <= rr); \
\
if ((size_t) (rr - l) <= maxthresh) { \
if ((size_t) (r - ll) <= maxthresh) { \
__QQSORT_STACK_POP(l, r, top); \
} else { \
l = ll; \
} \
} else if ((size_t) (r - ll) <= maxthresh) { \
r = rr; \
} else if ((rr - l) > (r - ll)) { \
__QQSORT_STACK_PUSH(l, rr, top); \
l = ll; \
} else { \
__QQSORT_STACK_PUSH(ll, r, top); \
r = rr; \
} \
} \
} \
\
{ \
uint8_t *end = base + (SIZE) * ((LEN) -1); \
uint8_t *tmp = base; \
uint8_t *thresh = end; \
uint8_t *run = tmp + (SIZE); \
\
if (thresh < base + maxthresh) { \
thresh = base + maxthresh; \
} \
\
while (run <= thresh) { \
__QQSORT_CMP(run, tmp, CMP, { tmp = run; }, {}); \
run += (SIZE); \
} \
\
if (tmp != base) { \
__QQSORT_SWAP(tmp, base, (SIZE)); \
} \
\
run = base + (SIZE); \
\
while ((run += (SIZE)) <= end) { \
tmp = run - (SIZE); \
\
__QQSORT_INF_LOOP { \
__QQSORT_CMP(run, tmp, CMP, { tmp -= (SIZE); }, { __QQSORT_INF_LOOP_STOP; }); \
} \
\
tmp += (SIZE); \
\
if (tmp != run) { \
uint8_t *trav = run + (SIZE); \
\
while (--trav >= run) { \
uint8_t *h = trav; \
uint8_t *l = trav; \
\
uint8_t c = *trav; \
\
while ((l -= (SIZE)) >= tmp) { \
*h = *l; \
h = l; \
} \
\
*h = c; \
} \
} \
} \
} \
} while (0)