-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds.h
305 lines (259 loc) · 7.29 KB
/
ds.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
#ifndef DS_H
#define DS_H
#include <string.h>
#include "acg/alloc.h"
#include "acg/minmax.h"
#define LOOP(T, I, A, B) for (T I = A; I < B; ++I)
#define LOOP0(T, I, N) LOOP(T, I, 0, N)
#define FOREACH(VAR, T, N) \
for (T *T = VAR; T - VAR < N; ++T)
typedef struct {
u32 size;
u32 off;
u32 cap;
u32 n;
} abuf;
#define DS_CAP_STR(CAP) \
CAP > 1e6f, \
CAP > 1e6f ? CAP / 1e6f : (CAP > 1e4f ? CAP / 1e3f : CAP), \
CAP > 1e6f ? "M" : (CAP > 1e4f ? "K" : "")
#define ABUF_MK_LOG(VAR, T) \
printf( \
"abuf \"" #VAR "\" of type " #T ": %.*f%s elements, %.1f %s\n", \
DS_CAP_STR(VAR->cap), \
(VAR->off + VAR->size * VAR->cap) / (1024.f * 1024.f), "MiB"), \
dump_vmem()
#define ABUF_MK_SILENT(VAR, T, CAP, ALLOCATOR) \
VAR = abuf_mk(CAP, ALLOCATOR, sizeof(T), MIN(64, sizeof(T)))
#define ABUF_MK(VAR, T, CAP, ALLOCATOR) \
ABUF_MK_SILENT(VAR, T, CAP, ALLOCATOR), \
ABUF_MK_LOG(VAR, T)
#define ABUF_MK_MB(VAR, T, MB, ALLOCATOR) \
assert((MB) < 4096), \
ABUF_MK(VAR, T, ((MB) * 1024 * 1024) / sizeof(T), ALLOCATOR)
#define ABUF_MK_TMP(VAR, T, CAP) \
ABUF_MK_SILENT(VAR, T, CAP, ALLOC_BUMP)
static abuf *abuf_mk(
const u64 cap,
allocator allocator,
u64 size,
const u64 align
) {
assert(cap);
size = align_up(size, align);
assert(size);
u64 off = MAX(size, sizeof(abuf));
u64 total = size * cap + off
+ (align); // Store a u32 here later to free
assert(total <= ~(u32)0);
void *raw = alloc(total, allocator);
abuf *buf = align_up_ptr(raw, align);
assert((char*)buf - (char*)raw < align);
*buf = (abuf) { size, off, cap };
return buf;
}
#define ABUF_GET_UNSAFE(VAR, I) \
((void*)((char*)(VAR) + (VAR)->off + (VAR)->size * (I)))
static inline void *abuf_get(abuf *abuf, u32 i)
{
assert(abuf);
assert(i < abuf->n);
return ABUF_GET_UNSAFE(abuf, i);
}
#define ABUF_HEAD_UNSAFE(VAR) ((void*)((char*)(VAR) + (VAR)->off))
#define ABUF_HEAD(VAR) abuf_get(VAR, 0)
#define ABUF_TAIL(VAR) abuf_get(VAR, (VAR)->n - 1)
#define ABUF_I(VAR, E) (((char*)(E) - (char*)ABUF_HEAD(VAR)) / (VAR)->size)
#define ABUF_NPTR(VAR) ABUF_GET_UNSAFE((VAR), (VAR)->n)
#define ABUF_NEXT_UNSAFE(VAR, E) ((void*)((char*)(E) + (VAR)->size))
#define ABUF_PREV_UNSAFE(VAR, E) ((void*)((char*)(E) - (VAR)->size))
static inline void *abuf_next(abuf *abuf, void *entry)
{
assert(abuf);
if (!abuf->n)
return NULL;
if (!entry)
return ABUF_HEAD(abuf);
char *end = entry;
char *beg = ABUF_HEAD_UNSAFE(abuf);
return (u64)(end - beg) / abuf->size < abuf->n - 1?
(void*)(end + abuf->size) : NULL;
}
static inline void *abuf_push(abuf *abuf)
{
assert(abuf);
assert(abuf->n < abuf->cap);
return (char*)ABUF_HEAD_UNSAFE(abuf) + abuf->size * abuf->n++;
}
static inline void abuf_grow(abuf *abuf, const u32 amt)
{
assert(abuf);
const u32 rem = ~(u32)0 - abuf->n;
assert(amt <= rem);
assert(abuf->n + amt <= abuf->cap);
abuf->n += amt;
}
#define ABUF_RMSWAP(VAR, E) abuf_rmswap(VAR, E, sizeof(*E))
static inline void abuf_rmswap(abuf *abuf, void *entry, u32 size)
{
assert(abuf);
switch (abuf->n) {
case 0:
panic();
default:
memcpy(entry, abuf_get(abuf, abuf->n - 1), size);
case 1:
break;
}
--abuf->n;
}
static inline void *abuf_pop(abuf *abuf)
{
assert(abuf);
assert(abuf->n);
return (char*)ABUF_HEAD_UNSAFE(abuf) + abuf->size * abuf->n--;
}
static void abuf_clear(abuf *abuf)
{
assert(abuf);
abuf->n = 0;
}
#define ABUF_ITER(VAR) for ( \
struct { u32 i; void *val; } iter = { .val = ABUF_HEAD(VAR) }; \
iter.i < VAR->n; \
iter.val = ABUF_GET_UNSAFE(VAR, ++iter.i))
#define ABUF_FOREACH_SAFE(VAR, T) \
for (T *T = abuf_next(VAR, NULL); T; T = abuf_next(VAR, T))
#define ABUF_FOREACH(VAR, T) for ( \
T *T = ABUF_HEAD_UNSAFE(VAR); \
(void*)T < ABUF_NPTR(VAR); \
T = ABUF_NEXT_UNSAFE(VAR, T))
#define ABUF_FOREACH_REV(VAR, T) for ( \
T *T = ABUF_TAIL(VAR); \
(void*)T >= ABUF_HEAD_UNSAFE(VAR); \
T = ABUF_PREV_UNSAFE(VAR, T))
#define VBUF(VAR, T, CAP) u32 VAR ## _n ; T VAR [ CAP ]
#define VBUF_MK(VAR, T, CAP) u32 VAR ## _n = 0; T VAR [ CAP ]
#define VBUF_MK_MB(VAR, T, MB) \
_Static_assert(MB < 4096, "VBUF oversized"); \
u32 VAR ## _n = 0; T VAR [ ((MB) * 1024 * 1024) / sizeof(T) ]
#define VBUF_INIT(VAR) VAR ## _n = 0
#define VBUF_I(VAR, ENTRY) ((ENTRY) - (VAR))
#define VBUF_CAP(VAR) (sizeof(VAR) / sizeof((VAR)[0]))
#define VBUF_PUSH(VAR) \
(assert(VAR ## _n < VBUF_CAP(VAR)), (VAR) + VAR ## _n++)
#define VBUF_POP(VAR) \
(assert(VAR ## _n), (VAR) + --VAR ## _n)
#define VBUF_CLEAR(VAR) (VAR ## _n = 0)
#define VBUF_CONTAINS(VAR, ENTRY) \
(VBUF_I(VAR, (ENTRY)) < VAR ## _n)
#define VBUF_RMSWAP(VAR, ENTRY) \
(assert(VBUF_CONTAINS(VAR, ENTRY)), *(ENTRY) = (VAR)[--VAR ## _n])
#define VBUF_RM(VAR, I) \
{ \
u32 i = I; \
assert(i < VAR ## _n), --VAR ## _n; \
for (; i < VAR ## _n; ++i) (VAR)[i] = (VAR)[i + 1]; \
}
#define VBUF_SWAP(VAR, T, I, J) \
{ \
assert((I) < VAR ## _n), assert((J) < VAR ## _n); \
T swap = VAR [I]; \
VAR [I] = VAR [J]; \
VAR [J] = swap; \
}
#define VBUF_ITER(VAR) for ( \
struct { u32 i; void *val; } iter = { .val = VAR }; \
iter.i < VAR ## _n; \
iter.val = ++iter.i + VAR)
#define VBUF_ENUM(VAR, E) \
E = VAR ; for (u32 i = 0; i < VAR ## _n; E = ++i + VAR)
#define VBUF_FOREACH(VAR, T) \
for (T *T = VAR; T - VAR < VAR ## _n; ++T)
#define VBUF_FOREACH_REV(VAR, T) \
for (T *T = VAR + VAR ## _n - 1; T >= VAR; --T)
#define VBUF_LOG(VAR) \
printf( \
"VBUF \"" #VAR "\": %.*f%s elements, %.1f %s\n", \
DS_CAP_STR(VBUF_CAP(VAR)), \
sizeof(VAR) / (1024.f * 1024.f), "MiB"), \
dump_vmem()
// In-place counting sort, where k is the maximum value returned by keyof()
typedef u16 (*keyof)(void*);
static void sort_count(
void *buf,
u16 k,
keyof keyof,
const u32 n,
const u32 size,
const u32 align
) {
assert(buf);
assert(k);
assert(size);
assert(align);
// Values of k should be small (otherwise, why are you using this?)
// and therefore the stack should be fine.
// This way, we're reetrant.
u16 len = k + 1;
u32 end[len * 2], *beg = end + len;
char swap[size];
memset(end, 0, sizeof(u32) * len);
memset(beg, 0, sizeof(u32) * len);
for (u32 i = 0; i < align * n; i += align) {
void *entry = (char*)buf + i;
u16 key = keyof(entry);
++end[key];
}
for (u16 i = 1; i < len; ++i) {
end[i] += end[i - 1];
beg[i] = end[i - 1];
}
for (u32 i = 0; i < align * n; i += align) {
void *entry = (char*)buf + i;
u16 key = keyof(entry);
while (end[key] != beg[key]) {
void *cmp = (char*)buf + align * --end[key];
if (key == keyof(cmp))
continue;
// Swap
void *target = (char*)buf + align * end[key];
memcpy( swap, target, size);
memcpy(target, entry, size);
memcpy( entry, swap, size);
key = keyof(entry);
}
}
}
#define ABUF_SORT_COUNT(VAR, T, K, FN) \
sort_count(ABUF_HEAD_UNSAFE(VAR), K, FN, VAR->n, sizeof(T), VAR->size)
typedef int (*ds_lt)(const void*, const void*); // Nonzero if a < b
static void sort_sel(
void *buf,
const u32 n,
const u32 size,
const u32 align,
ds_lt lt
) {
assert(buf);
assert(size);
assert(align);
void *swap[size];
for (u32 i = 0; i < align * n; i += align) {
void *I = (char*)buf + i;
void *best = I;
for (u32 j = i; j < align * n; j += align) {
void *J = (char*)buf + j;
if (lt(J, best))
best = J;
}
if (best != I) {
memcpy(swap, I, size);
memcpy( I, best, size);
memcpy(best, swap, size);
}
}
}
#define VBUF_SORT_SEL(VAR, T, FN) \
sort_sel(VAR, VAR ## _n, sizeof(T), sizeof(T), FN)
#endif