-
Notifications
You must be signed in to change notification settings - Fork 0
/
axqueue.c
333 lines (254 loc) · 7.33 KB
/
axqueue.c
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
//
// Created by easy on 19.10.23.
//
#include "axqueue.h"
#include <stdlib.h>
#include <string.h>
#define BOUND(q) ((q)->items + (q)->cap)
#define MAX(x, y) ((x) > (y) ? (x) : (y))
typedef unsigned long ulong;
struct axqueue {
void **items; // base of array
void **write; // points to next cell to be written to
void **read; // points to current cell to be read from
ulong len;
ulong cap;
void (*destroy)(void *);
};
union Long {
ulong u;
long s;
};
static ulong toItemSize(ulong n) {
return n * sizeof(void *);
}
static axqueue *sizedNew(ulong size) {
size = MAX(1, size);
axqueue *q = malloc(sizeof *q);
if (q) q->items = malloc(toItemSize(size));
if (!q || !q->items) {
free(q);
return NULL;
}
q->write = q->read = q->items;
q->len = 0;
q->cap = size;
q->destroy = NULL;
return q;
}
static axqueue *new(void) {
return sizedNew(7);
}
static void destroy(axqueue *q) {
q->read -= q->cap * (q->read >= BOUND(q));
if (q->destroy && q->len) do {
q->destroy(*q->read++);
q->read -= q->cap * (q->read >= BOUND(q));
} while (q->read != q->write);
free(q->items);
free(q);
}
static bool grow(axqueue *q) {
ulong cap = q->cap * 2;
void **items = realloc(q->items, toItemSize(cap));
if (!items) return true;
// set r/w heads to their possibly new addresses
q->write = items + (q->write - q->items);
q->read = items + (q->read - q->items);
q->items = items;
if (q->write <= q->read) { // else, write already'd have space to the right
ulong num = BOUND(q) - q->read; // items from read head to end of array
memcpy(q->read + q->cap, q->read, toItemSize(num));
q->read += q->cap;
}
q->cap = cap; // finally, save the doubled capacity
return false;
}
static bool enqueue(axqueue *q, void *val) {
if (q->len >= q->cap) {
if (grow(q)) return true;
}
*q->write++ = val;
q->write -= q->cap * (q->write >= BOUND(q)); // wrap-around
++q->len;
return false;
}
static void *dequeue(axqueue *q) {
void *val = q->len ? *q->read++ : NULL;
q->read -= q->cap * (q->read >= BOUND(q)); // wrap-around
q->len -= !!q->len;
return val;
}
static void *front(axqueue *q) {
return q->len ? *q->read : NULL;
}
static union Long normaliseIndex(axqueue *q, long index) {
union Long i = {.s = index};
i.u += (i.s < 0) * q->len; // convert negative to positive index
if (i.u < q->len) { // if index valid
void **pval = q->read + i.u;
pval -= q->cap * (pval >= BOUND(q));
return (union Long) {.s = pval - q->items};
} else { // index out of range
return (union Long) {.u = q->cap};
}
}
static void *at(axqueue *q, long index) {
ulong i = normaliseIndex(q, index).u;
return i < q->cap ? q->items[i] : NULL;
}
static bool swap(axqueue *q, long index1, long index2) {
ulong i1 = normaliseIndex(q, index1).u;
ulong i2 = normaliseIndex(q, index2).u;
if (i1 >= q->cap || i2 >= q->cap)
return true;
void *tmp = q->items[i1];
q->items[i1] = q->items[i2];
q->items[i2] = tmp;
return false;
}
static axqueue *reverse(axqueue *q) {
void **l = q->read;
void **r = q->write - 1;
r += q->cap * (r < q->items);
for (ulong i = 0, swaps = q->len / 2; i < swaps; ++i) {
void *tmp = *l;
*l = *r;
*r = tmp;
++l; --r;
l -= q->cap * (l >= BOUND(q));
r += q->cap * (r < q->items);
}
return q;
}
static axqueue *clear(axqueue *q) {
q->read -= q->cap * (q->read >= BOUND(q));
if (q->destroy && q->len) do {
q->destroy(*q->read++);
q->read -= q->cap * (q->read >= BOUND(q));
} while (q->read != q->write);
q->write = q->read = q->items;
q->len = 0;
return q;
}
static axqueue *copy(axqueue *q) {
axqueue *q2 = sizedNew(q->cap);
if (!q2) return NULL;
if (q->read < q->write || q->len == 0) {
memcpy(q2->items, q->read, toItemSize(q->len));
} else {
ulong lsize = q->write - q->items;
ulong rsize = q->len - lsize;
memcpy(q2->items, q->read, toItemSize(rsize));
memcpy(q2->items + rsize, q->items, toItemSize(lsize));
}
// q2->read implicity set to q2->items by sizedNew()
q2->write = q2->items + q->len;
q2->len = q->len;
return q2;
}
static void reverseSection(void **s, void **e) { // do not expose
while (s < e) {
void *tmp = *s;
*s = *e;
*e = tmp;
++s; --e;
}
}
static void rotate(axqueue *q, ulong n) { // do not expose
if (n == 0) return;
reverseSection(q->items, BOUND(q) - 1);
reverseSection(q->items, q->items + n - 1);
reverseSection(q->items + n, BOUND(q) - 1);
}
static bool resize(axqueue *q, ulong size) {
size = MAX(1, size);
if (size == q->cap) {
return false;
} else if (size > q->cap) {
void **items = realloc(q->items, toItemSize(size));
if (!items) return true;
// set r/w heads to their possibly new addresses
q->write = items + (q->write - q->items);
q->read = items + (q->read - q->items);
q->items = items;
if (q->write <= q->read && q->len) {
ulong num = BOUND(q) - q->read;
ulong extra = size - q->cap;
memmove(q->read + extra, q->read, toItemSize(num));
q->read += extra;
}
q->cap = size;
return false;
} else /*(size < q->cap)*/ {
ulong lost = size < q->len ? q->len - size : 0;
if (q->destroy) for (ulong i = 0; i < lost; ++i) {
q->read -= q->cap * (q->read >= BOUND(q));
q->destroy(*q->read++);
} else {
q->read += lost;
q->read -= q->cap * (q->read >= BOUND(q));
}
rotate(q, q->cap - (q->read - q->items));
void **items = realloc(q->items, toItemSize(size));
bool failed = !items;
if (failed) items = q->items;
else q->cap = size;
q->len -= lost;
q->items = items;
q->read = items;
q->write = items + q->len;
q->write -= q->cap * (q->write >= BOUND(q)); // shouldn't happen (?!)
return failed;
}
}
static axqueue *destroyItem(axqueue *q, void *val) {
if (q->destroy) q->destroy(val);
return q;
}
static axqueue *setDestructor(axqueue *q, void (*destroy)(void *)) {
q->destroy = destroy;
return q;
}
static void (*getDestructor(axqueue *q))(void *) {
return q->destroy;
}
static void **data(axqueue *q) {
if (q->write <= q->read && q->len)
rotate(q, q->cap - (q->read - q->items));
return q->read;
}
static long len(axqueue *q) {
union Long len = {q->len};
len.u = len.u << 1 >> 1; // get rid of sign bit
return len.s;
}
static long cap(axqueue *q) {
union Long cap = {q->cap};
cap.u = cap.u << 1 >> 1; // get rid of sign bit
return cap.s;
}
#ifdef AXQUEUE_NAMESPACE
#define axq AXQUEUE_NAMESPACE
#endif
const struct axqueueFn axq = {
new,
sizedNew,
destroy,
enqueue,
dequeue,
front,
len,
at,
swap,
reverse,
clear,
copy,
resize,
destroyItem,
setDestructor,
getDestructor,
data,
cap
};
#undef axq