interplane.Get Omega
Read in your language

klib

[Ported from github.com/attractivechaos/klib] added block arena

Brought by drakestapleton

branch master Branches Tags History

klib / krmq.h

474 lines · 16430 bytes Raw Blame History
1/* The MIT License
2
3 Copyright (c) 2019 by Attractive Chaos <attractor@live.co.uk>
4
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24*/
25
26/* An example:
27
28#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include "krmq.h"
32
33struct my_node {
34 char key;
35 KRMQ_HEAD(struct my_node) head;
36};
37#define my_cmp(p, q) (((q)->key < (p)->key) - ((p)->key < (q)->key))
38KRMQ_INIT(my, struct my_node, head, my_cmp)
39
40int main(void) {
41 const char *str = "MNOLKQOPHIA"; // from wiki, except a duplicate
42 struct my_node *root = 0;
43 int i, l = strlen(str);
44 for (i = 0; i < l; ++i) { // insert in the input order
45 struct my_node *q, *p = malloc(sizeof(*p));
46 p->key = str[i];
47 q = krmq_insert(my, &root, p, 0);
48 if (p != q) free(p); // if already present, free
49 }
50 krmq_itr_t(my) itr;
51 krmq_itr_first(my, root, &itr); // place at first
52 do { // traverse
53 const struct my_node *p = krmq_at(&itr);
54 putchar(p->key);
55 free((void*)p); // free node
56 } while (krmq_itr_next(my, &itr));
57 putchar('\n');
58 return 0;
59}
60*/
61
62#ifndef KRMQ_H
63#define KRMQ_H
64
65#ifdef __STRICT_ANSI__
66#define inline __inline__
67#endif
68
69#define KRMQ_MAX_DEPTH 64
70
71#define krmq_size(head, p) ((p)? (p)->head.size : 0)
72#define krmq_size_child(head, q, i) ((q)->head.p[(i)]? (q)->head.p[(i)]->head.size : 0)
73
74#define KRMQ_HEAD(__type) \
75 struct { \
76 __type *p[2], *s; \
77 signed char balance; /* balance factor */ \
78 unsigned size; /* #elements in subtree */ \
79 }
80
81#define __KRMQ_FIND(suf, __scope, __type, __head, __cmp) \
82 __scope __type *krmq_find_##suf(const __type *root, const __type *x, unsigned *cnt_) { \
83 const __type *p = root; \
84 unsigned cnt = 0; \
85 while (p != 0) { \
86 int cmp; \
87 cmp = __cmp(x, p); \
88 if (cmp >= 0) cnt += krmq_size_child(__head, p, 0) + 1; \
89 if (cmp < 0) p = p->__head.p[0]; \
90 else if (cmp > 0) p = p->__head.p[1]; \
91 else break; \
92 } \
93 if (cnt_) *cnt_ = cnt; \
94 return (__type*)p; \
95 } \
96 __scope __type *krmq_interval_##suf(const __type *root, const __type *x, __type **lower, __type **upper) { \
97 const __type *p = root, *l = 0, *u = 0; \
98 while (p != 0) { \
99 int cmp; \
100 cmp = __cmp(x, p); \
101 if (cmp < 0) u = p, p = p->__head.p[0]; \
102 else if (cmp > 0) l = p, p = p->__head.p[1]; \
103 else { l = u = p; break; } \
104 } \
105 if (lower) *lower = (__type*)l; \
106 if (upper) *upper = (__type*)u; \
107 return (__type*)p; \
108 }
109
110#define __KRMQ_RMQ(suf, __scope, __type, __head, __cmp, __lt2) \
111 __scope __type *krmq_rmq_##suf(const __type *root, const __type *lo, const __type *up) { /* CLOSED interval */ \
112 const __type *p = root, *path[2][KRMQ_MAX_DEPTH], *min; \
113 int plen[2] = {0, 0}, pcmp[2][KRMQ_MAX_DEPTH], i, cmp, lca; \
114 if (root == 0) return 0; \
115 while (p) { \
116 cmp = __cmp(lo, p); \
117 path[0][plen[0]] = p, pcmp[0][plen[0]++] = cmp; \
118 if (cmp < 0) p = p->__head.p[0]; \
119 else if (cmp > 0) p = p->__head.p[1]; \
120 else break; \
121 } \
122 p = root; \
123 while (p) { \
124 cmp = __cmp(up, p); \
125 path[1][plen[1]] = p, pcmp[1][plen[1]++] = cmp; \
126 if (cmp < 0) p = p->__head.p[0]; \
127 else if (cmp > 0) p = p->__head.p[1]; \
128 else break; \
129 } \
130 for (i = 0; i < plen[0] && i < plen[1]; ++i) /* find the LCA */ \
131 if (path[0][i] == path[1][i] && pcmp[0][i] <= 0 && pcmp[1][i] >= 0) \
132 break; \
133 if (i == plen[0] || i == plen[1]) return 0; /* no elements in the closed interval */ \
134 lca = i, min = path[0][lca]; \
135 for (i = lca + 1; i < plen[0]; ++i) { \
136 if (pcmp[0][i] <= 0) { \
137 if (__lt2(path[0][i], min)) min = path[0][i]; \
138 if (path[0][i]->__head.p[1] && __lt2(path[0][i]->__head.p[1]->__head.s, min)) \
139 min = path[0][i]->__head.p[1]->__head.s; \
140 } \
141 } \
142 for (i = lca + 1; i < plen[1]; ++i) { \
143 if (pcmp[1][i] >= 0) { \
144 if (__lt2(path[1][i], min)) min = path[1][i]; \
145 if (path[1][i]->__head.p[0] && __lt2(path[1][i]->__head.p[0]->__head.s, min)) \
146 min = path[1][i]->__head.p[0]->__head.s; \
147 } \
148 } \
149 return (__type*)min; \
150 }
151
152#define __KRMQ_ROTATE(suf, __type, __head, __lt2) \
153 /* */ \
154 static inline void krmq_update_min_##suf(__type *p, const __type *q, const __type *r) { \
155 p->__head.s = !q || __lt2(p, q->__head.s)? p : q->__head.s; \
156 p->__head.s = !r || __lt2(p->__head.s, r->__head.s)? p->__head.s : r->__head.s; \
157 } \
158 /* one rotation: (a,(b,c)q)p => ((a,b)p,c)q */ \
159 static inline __type *krmq_rotate1_##suf(__type *p, int dir) { /* dir=0 to left; dir=1 to right */ \
160 int opp = 1 - dir; /* opposite direction */ \
161 __type *q = p->__head.p[opp], *s = p->__head.s; \
162 unsigned size_p = p->__head.size; \
163 p->__head.size -= q->__head.size - krmq_size_child(__head, q, dir); \
164 q->__head.size = size_p; \
165 krmq_update_min_##suf(p, p->__head.p[dir], q->__head.p[dir]); \
166 q->__head.s = s; \
167 p->__head.p[opp] = q->__head.p[dir]; \
168 q->__head.p[dir] = p; \
169 return q; \
170 } \
171 /* two consecutive rotations: (a,((b,c)r,d)q)p => ((a,b)p,(c,d)q)r */ \
172 static inline __type *krmq_rotate2_##suf(__type *p, int dir) { \
173 int b1, opp = 1 - dir; \
174 __type *q = p->__head.p[opp], *r = q->__head.p[dir], *s = p->__head.s; \
175 unsigned size_x_dir = krmq_size_child(__head, r, dir); \
176 r->__head.size = p->__head.size; \
177 p->__head.size -= q->__head.size - size_x_dir; \
178 q->__head.size -= size_x_dir + 1; \
179 krmq_update_min_##suf(p, p->__head.p[dir], r->__head.p[dir]); \
180 krmq_update_min_##suf(q, q->__head.p[opp], r->__head.p[opp]); \
181 r->__head.s = s; \
182 p->__head.p[opp] = r->__head.p[dir]; \
183 r->__head.p[dir] = p; \
184 q->__head.p[dir] = r->__head.p[opp]; \
185 r->__head.p[opp] = q; \
186 b1 = dir == 0? +1 : -1; \
187 if (r->__head.balance == b1) q->__head.balance = 0, p->__head.balance = -b1; \
188 else if (r->__head.balance == 0) q->__head.balance = p->__head.balance = 0; \
189 else q->__head.balance = b1, p->__head.balance = 0; \
190 r->__head.balance = 0; \
191 return r; \
192 }
193
194#define __KRMQ_INSERT(suf, __scope, __type, __head, __cmp, __lt2) \
195 __scope __type *krmq_insert_##suf(__type **root_, __type *x, unsigned *cnt_) { \
196 unsigned char stack[KRMQ_MAX_DEPTH]; \
197 __type *path[KRMQ_MAX_DEPTH]; \
198 __type *bp, *bq; \
199 __type *p, *q, *r = 0; /* _r_ is potentially the new root */ \
200 int i, which = 0, top, b1, path_len; \
201 unsigned cnt = 0; \
202 bp = *root_, bq = 0; \
203 /* find the insertion location */ \
204 for (p = bp, q = bq, top = path_len = 0; p; q = p, p = p->__head.p[which]) { \
205 int cmp; \
206 cmp = __cmp(x, p); \
207 if (cmp >= 0) cnt += krmq_size_child(__head, p, 0) + 1; \
208 if (cmp == 0) { \
209 if (cnt_) *cnt_ = cnt; \
210 return p; \
211 } \
212 if (p->__head.balance != 0) \
213 bq = q, bp = p, top = 0; \
214 stack[top++] = which = (cmp > 0); \
215 path[path_len++] = p; \
216 } \
217 if (cnt_) *cnt_ = cnt; \
218 x->__head.balance = 0, x->__head.size = 1, x->__head.p[0] = x->__head.p[1] = 0, x->__head.s = x; \
219 if (q == 0) *root_ = x; \
220 else q->__head.p[which] = x; \
221 if (bp == 0) return x; \
222 for (i = 0; i < path_len; ++i) ++path[i]->__head.size; \
223 for (i = path_len - 1; i >= 0; --i) { \
224 krmq_update_min_##suf(path[i], path[i]->__head.p[0], path[i]->__head.p[1]); \
225 if (path[i]->__head.s != x) break; \
226 } \
227 for (p = bp, top = 0; p != x; p = p->__head.p[stack[top]], ++top) /* update balance factors */ \
228 if (stack[top] == 0) --p->__head.balance; \
229 else ++p->__head.balance; \
230 if (bp->__head.balance > -2 && bp->__head.balance < 2) return x; /* no re-balance needed */ \
231 /* re-balance */ \
232 which = (bp->__head.balance < 0); \
233 b1 = which == 0? +1 : -1; \
234 q = bp->__head.p[1 - which]; \
235 if (q->__head.balance == b1) { \
236 r = krmq_rotate1_##suf(bp, which); \
237 q->__head.balance = bp->__head.balance = 0; \
238 } else r = krmq_rotate2_##suf(bp, which); \
239 if (bq == 0) *root_ = r; \
240 else bq->__head.p[bp != bq->__head.p[0]] = r; \
241 return x; \
242 }
243
244#define __KRMQ_ERASE(suf, __scope, __type, __head, __cmp, __lt2) \
245 __scope __type *krmq_erase_##suf(__type **root_, const __type *x, unsigned *cnt_) { \
246 __type *p, *path[KRMQ_MAX_DEPTH], fake; \
247 unsigned char dir[KRMQ_MAX_DEPTH]; \
248 int i, d = 0, cmp; \
249 unsigned cnt = 0; \
250 fake.__head.p[0] = *root_, fake.__head.p[1] = 0; \
251 if (cnt_) *cnt_ = 0; \
252 if (x) { \
253 for (cmp = -1, p = &fake; cmp; cmp = __cmp(x, p)) { \
254 int which = (cmp > 0); \
255 if (cmp > 0) cnt += krmq_size_child(__head, p, 0) + 1; \
256 dir[d] = which; \
257 path[d++] = p; \
258 p = p->__head.p[which]; \
259 if (p == 0) { \
260 if (cnt_) *cnt_ = 0; \
261 return 0; \
262 } \
263 } \
264 cnt += krmq_size_child(__head, p, 0) + 1; /* because p==x is not counted */ \
265 } else { \
266 for (p = &fake, cnt = 1; p; p = p->__head.p[0]) \
267 dir[d] = 0, path[d++] = p; \
268 p = path[--d]; \
269 } \
270 if (cnt_) *cnt_ = cnt; \
271 for (i = 1; i < d; ++i) --path[i]->__head.size; \
272 if (p->__head.p[1] == 0) { /* ((1,.)2,3)4 => (1,3)4; p=2 */ \
273 path[d-1]->__head.p[dir[d-1]] = p->__head.p[0]; \
274 } else { \
275 __type *q = p->__head.p[1]; \
276 if (q->__head.p[0] == 0) { /* ((1,2)3,4)5 => ((1)2,4)5; p=3,q=2 */ \
277 q->__head.p[0] = p->__head.p[0]; \
278 q->__head.balance = p->__head.balance; \
279 path[d-1]->__head.p[dir[d-1]] = q; \
280 path[d] = q, dir[d++] = 1; \
281 q->__head.size = p->__head.size - 1; \
282 } else { /* ((1,((.,2)3,4)5)6,7)8 => ((1,(2,4)5)3,7)8; p=6 */ \
283 __type *r; \
284 int e = d++; /* backup _d_ */\
285 for (;;) { \
286 dir[d] = 0; \
287 path[d++] = q; \
288 r = q->__head.p[0]; \
289 if (r->__head.p[0] == 0) break; \
290 q = r; \
291 } \
292 r->__head.p[0] = p->__head.p[0]; \
293 q->__head.p[0] = r->__head.p[1]; \
294 r->__head.p[1] = p->__head.p[1]; \
295 r->__head.balance = p->__head.balance; \
296 path[e-1]->__head.p[dir[e-1]] = r; \
297 path[e] = r, dir[e] = 1; \
298 for (i = e + 1; i < d; ++i) --path[i]->__head.size; \
299 r->__head.size = p->__head.size - 1; \
300 } \
301 } \
302 for (i = d - 1; i >= 0; --i) /* not sure why adding condition "path[i]->__head.s==p" doesn't work */ \
303 krmq_update_min_##suf(path[i], path[i]->__head.p[0], path[i]->__head.p[1]); \
304 while (--d > 0) { \
305 __type *q = path[d]; \
306 int which, other, b1 = 1, b2 = 2; \
307 which = dir[d], other = 1 - which; \
308 if (which) b1 = -b1, b2 = -b2; \
309 q->__head.balance += b1; \
310 if (q->__head.balance == b1) break; \
311 else if (q->__head.balance == b2) { \
312 __type *r = q->__head.p[other]; \
313 if (r->__head.balance == -b1) { \
314 path[d-1]->__head.p[dir[d-1]] = krmq_rotate2_##suf(q, which); \
315 } else { \
316 path[d-1]->__head.p[dir[d-1]] = krmq_rotate1_##suf(q, which); \
317 if (r->__head.balance == 0) { \
318 r->__head.balance = -b1; \
319 q->__head.balance = b1; \
320 break; \
321 } else r->__head.balance = q->__head.balance = 0; \
322 } \
323 } \
324 } \
325 *root_ = fake.__head.p[0]; \
326 return p; \
327 }
328
329#define krmq_free(__type, __head, __root, __free) do { \
330 __type *_p, *_q; \
331 for (_p = __root; _p; _p = _q) { \
332 if (_p->__head.p[0] == 0) { \
333 _q = _p->__head.p[1]; \
334 __free(_p); \
335 } else { \
336 _q = _p->__head.p[0]; \
337 _p->__head.p[0] = _q->__head.p[1]; \
338 _q->__head.p[1] = _p; \
339 } \
340 } \
341 } while (0)
342
343#define __KRMQ_ITR(suf, __scope, __type, __head, __cmp) \
344 struct krmq_itr_##suf { \
345 const __type *stack[KRMQ_MAX_DEPTH], **top; \
346 }; \
347 __scope void krmq_itr_first_##suf(const __type *root, struct krmq_itr_##suf *itr) { \
348 const __type *p; \
349 for (itr->top = itr->stack - 1, p = root; p; p = p->__head.p[0]) \
350 *++itr->top = p; \
351 } \
352 __scope int krmq_itr_find_##suf(const __type *root, const __type *x, struct krmq_itr_##suf *itr) { \
353 const __type *p = root; \
354 itr->top = itr->stack - 1; \
355 while (p != 0) { \
356 int cmp; \
357 *++itr->top = p; \
358 cmp = __cmp(x, p); \
359 if (cmp < 0) p = p->__head.p[0]; \
360 else if (cmp > 0) p = p->__head.p[1]; \
361 else break; \
362 } \
363 return p? 1 : 0; \
364 } \
365 __scope int krmq_itr_next_bidir_##suf(struct krmq_itr_##suf *itr, int dir) { \
366 const __type *p; \
367 if (itr->top < itr->stack) return 0; \
368 dir = !!dir; \
369 p = (*itr->top)->__head.p[dir]; \
370 if (p) { /* go down */ \
371 for (; p; p = p->__head.p[!dir]) \
372 *++itr->top = p; \
373 return 1; \
374 } else { /* go up */ \
375 do { \
376 p = *itr->top--; \
377 } while (itr->top >= itr->stack && p == (*itr->top)->__head.p[dir]); \
378 return itr->top < itr->stack? 0 : 1; \
379 } \
380 } \
381
382/**
383 * Insert a node to the tree
384 *
385 * @param suf name suffix used in KRMQ_INIT()
386 * @param proot pointer to the root of the tree (in/out: root may change)
387 * @param x node to insert (in)
388 * @param cnt number of nodes smaller than or equal to _x_; can be NULL (out)
389 *
390 * @return _x_ if not present in the tree, or the node equal to x.
391 */
392#define krmq_insert(suf, proot, x, cnt) krmq_insert_##suf(proot, x, cnt)
393
394/**
395 * Find a node in the tree
396 *
397 * @param suf name suffix used in KRMQ_INIT()
398 * @param root root of the tree
399 * @param x node value to find (in)
400 * @param cnt number of nodes smaller than or equal to _x_; can be NULL (out)
401 *
402 * @return node equal to _x_ if present, or NULL if absent
403 */
404#define krmq_find(suf, root, x, cnt) krmq_find_##suf(root, x, cnt)
405#define krmq_interval(suf, root, x, lower, upper) krmq_interval_##suf(root, x, lower, upper)
406#define krmq_rmq(suf, root, lo, up) krmq_rmq_##suf(root, lo, up)
407
408/**
409 * Delete a node from the tree
410 *
411 * @param suf name suffix used in KRMQ_INIT()
412 * @param proot pointer to the root of the tree (in/out: root may change)
413 * @param x node value to delete; if NULL, delete the first node (in)
414 *
415 * @return node removed from the tree if present, or NULL if absent
416 */
417#define krmq_erase(suf, proot, x, cnt) krmq_erase_##suf(proot, x, cnt)
418#define krmq_erase_first(suf, proot) krmq_erase_##suf(proot, 0, 0)
419
420#define krmq_itr_t(suf) struct krmq_itr_##suf
421
422/**
423 * Place the iterator at the smallest object
424 *
425 * @param suf name suffix used in KRMQ_INIT()
426 * @param root root of the tree
427 * @param itr iterator
428 */
429#define krmq_itr_first(suf, root, itr) krmq_itr_first_##suf(root, itr)
430
431/**
432 * Place the iterator at the object equal to or greater than the query
433 *
434 * @param suf name suffix used in KRMQ_INIT()
435 * @param root root of the tree
436 * @param x query (in)
437 * @param itr iterator (out)
438 *
439 * @return 1 if find; 0 otherwise. krmq_at(itr) is NULL if and only if query is
440 * larger than all objects in the tree
441 */
442#define krmq_itr_find(suf, root, x, itr) krmq_itr_find_##suf(root, x, itr)
443
444/**
445 * Move to the next object in order
446 *
447 * @param itr iterator (modified)
448 *
449 * @return 1 if there is a next object; 0 otherwise
450 */
451#define krmq_itr_next(suf, itr) krmq_itr_next_bidir_##suf(itr, 1)
452#define krmq_itr_prev(suf, itr) krmq_itr_next_bidir_##suf(itr, 0)
453
454/**
455 * Return the pointer at the iterator
456 *
457 * @param itr iterator
458 *
459 * @return pointer if present; NULL otherwise
460 */
461#define krmq_at(itr) ((itr)->top < (itr)->stack? 0 : *(itr)->top)
462
463#define KRMQ_INIT2(suf, __scope, __type, __head, __cmp, __lt2) \
464 __KRMQ_FIND(suf, __scope, __type, __head, __cmp) \
465 __KRMQ_RMQ(suf, __scope, __type, __head, __cmp, __lt2) \
466 __KRMQ_ROTATE(suf, __type, __head, __lt2) \
467 __KRMQ_INSERT(suf, __scope, __type, __head, __cmp, __lt2) \
468 __KRMQ_ERASE(suf, __scope, __type, __head, __cmp, __lt2) \
469 __KRMQ_ITR(suf, __scope, __type, __head, __cmp)
470
471#define KRMQ_INIT(suf, __type, __head, __cmp, __lt2) \
472 KRMQ_INIT2(suf,, __type, __head, __cmp, __lt2)
473
474#endif