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 / kavl-lite.h

306 lines · 9859 bytes Raw Blame History
1/* The MIT License
2
3 Copyright (c) 2021 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 "kavl-lite.h"
32
33struct my_node {
34 char key;
35 KAVLL_HEAD(struct my_node) head;
36};
37#define my_cmp(p, q) (((q)->key < (p)->key) - ((p)->key < (q)->key))
38KAVLL_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 = my_insert(&root, p);
48 if (p != q) free(p); // if already present, free
49 }
50 my_itr_t itr;
51 my_itr_first(root, &itr); // place at first
52 do { // traverse
53 const struct my_node *p = kavll_at(&itr);
54 putchar(p->key);
55 free((void*)p); // free node
56 } while (my_itr_next(&itr));
57 putchar('\n');
58 return 0;
59}
60*/
61
62#ifndef KAVL_LITE_H
63#define KAVL_LITE_H
64
65#ifdef __STRICT_ANSI__
66#define inline __inline__
67#endif
68
69#define KAVLL_MAX_DEPTH 64
70
71#define KAVLL_HEAD(__type) \
72 struct { \
73 __type *p[2]; \
74 signed char balance; /* balance factor */ \
75 }
76
77#define __KAVLL_FIND(pre, __scope, __type, __head, __cmp) \
78 __scope __type *pre##_find(const __type *root, const __type *x) { \
79 const __type *p = root; \
80 while (p != 0) { \
81 int cmp; \
82 cmp = __cmp(x, p); \
83 if (cmp < 0) p = p->__head.p[0]; \
84 else if (cmp > 0) p = p->__head.p[1]; \
85 else break; \
86 } \
87 return (__type*)p; \
88 }
89
90#define __KAVLL_ROTATE(pre, __type, __head) \
91 /* one rotation: (a,(b,c)q)p => ((a,b)p,c)q */ \
92 static inline __type *pre##_rotate1(__type *p, int dir) { /* dir=0 to left; dir=1 to right */ \
93 int opp = 1 - dir; /* opposite direction */ \
94 __type *q = p->__head.p[opp]; \
95 p->__head.p[opp] = q->__head.p[dir]; \
96 q->__head.p[dir] = p; \
97 return q; \
98 } \
99 /* two consecutive rotations: (a,((b,c)r,d)q)p => ((a,b)p,(c,d)q)r */ \
100 static inline __type *pre##_rotate2(__type *p, int dir) { \
101 int b1, opp = 1 - dir; \
102 __type *q = p->__head.p[opp], *r = q->__head.p[dir]; \
103 p->__head.p[opp] = r->__head.p[dir]; \
104 r->__head.p[dir] = p; \
105 q->__head.p[dir] = r->__head.p[opp]; \
106 r->__head.p[opp] = q; \
107 b1 = dir == 0? +1 : -1; \
108 if (r->__head.balance == b1) q->__head.balance = 0, p->__head.balance = -b1; \
109 else if (r->__head.balance == 0) q->__head.balance = p->__head.balance = 0; \
110 else q->__head.balance = b1, p->__head.balance = 0; \
111 r->__head.balance = 0; \
112 return r; \
113 }
114
115#define __KAVLL_INSERT(pre, __scope, __type, __head, __cmp) \
116 __scope __type *pre##_insert(__type **root_, __type *x) { \
117 unsigned char stack[KAVLL_MAX_DEPTH]; \
118 __type *path[KAVLL_MAX_DEPTH]; \
119 __type *bp, *bq; \
120 __type *p, *q, *r = 0; /* _r_ is potentially the new root */ \
121 int which = 0, top, b1, path_len; \
122 bp = *root_, bq = 0; \
123 /* find the insertion location */ \
124 for (p = bp, q = bq, top = path_len = 0; p; q = p, p = p->__head.p[which]) { \
125 int cmp; \
126 cmp = __cmp(x, p); \
127 if (cmp == 0) return p; \
128 if (p->__head.balance != 0) \
129 bq = q, bp = p, top = 0; \
130 stack[top++] = which = (cmp > 0); \
131 path[path_len++] = p; \
132 } \
133 x->__head.balance = 0, x->__head.p[0] = x->__head.p[1] = 0; \
134 if (q == 0) *root_ = x; \
135 else q->__head.p[which] = x; \
136 if (bp == 0) return x; \
137 for (p = bp, top = 0; p != x; p = p->__head.p[stack[top]], ++top) /* update balance factors */ \
138 if (stack[top] == 0) --p->__head.balance; \
139 else ++p->__head.balance; \
140 if (bp->__head.balance > -2 && bp->__head.balance < 2) return x; /* no re-balance needed */ \
141 /* re-balance */ \
142 which = (bp->__head.balance < 0); \
143 b1 = which == 0? +1 : -1; \
144 q = bp->__head.p[1 - which]; \
145 if (q->__head.balance == b1) { \
146 r = pre##_rotate1(bp, which); \
147 q->__head.balance = bp->__head.balance = 0; \
148 } else r = pre##_rotate2(bp, which); \
149 if (bq == 0) *root_ = r; \
150 else bq->__head.p[bp != bq->__head.p[0]] = r; \
151 return x; \
152 }
153
154#define __KAVLL_ERASE(pre, __scope, __type, __head, __cmp) \
155 __scope __type *pre##_erase(__type **root_, const __type *x) { \
156 __type *p, *path[KAVLL_MAX_DEPTH], fake; \
157 unsigned char dir[KAVLL_MAX_DEPTH]; \
158 int d = 0, cmp; \
159 fake.__head.p[0] = *root_, fake.__head.p[1] = 0; \
160 if (x) { \
161 for (cmp = -1, p = &fake; cmp; cmp = __cmp(x, p)) { \
162 int which = (cmp > 0); \
163 dir[d] = which; \
164 path[d++] = p; \
165 p = p->__head.p[which]; \
166 if (p == 0) return 0; \
167 } \
168 } else { \
169 for (p = &fake; p; p = p->__head.p[0]) \
170 dir[d] = 0, path[d++] = p; \
171 p = path[--d]; \
172 } \
173 if (p->__head.p[1] == 0) { /* ((1,.)2,3)4 => (1,3)4; p=2 */ \
174 path[d-1]->__head.p[dir[d-1]] = p->__head.p[0]; \
175 } else { \
176 __type *q = p->__head.p[1]; \
177 if (q->__head.p[0] == 0) { /* ((1,2)3,4)5 => ((1)2,4)5; p=3 */ \
178 q->__head.p[0] = p->__head.p[0]; \
179 q->__head.balance = p->__head.balance; \
180 path[d-1]->__head.p[dir[d-1]] = q; \
181 path[d] = q, dir[d++] = 1; \
182 } else { /* ((1,((.,2)3,4)5)6,7)8 => ((1,(2,4)5)3,7)8; p=6 */ \
183 __type *r; \
184 int e = d++; /* backup _d_ */\
185 for (;;) { \
186 dir[d] = 0; \
187 path[d++] = q; \
188 r = q->__head.p[0]; \
189 if (r->__head.p[0] == 0) break; \
190 q = r; \
191 } \
192 r->__head.p[0] = p->__head.p[0]; \
193 q->__head.p[0] = r->__head.p[1]; \
194 r->__head.p[1] = p->__head.p[1]; \
195 r->__head.balance = p->__head.balance; \
196 path[e-1]->__head.p[dir[e-1]] = r; \
197 path[e] = r, dir[e] = 1; \
198 } \
199 } \
200 while (--d > 0) { \
201 __type *q = path[d]; \
202 int which, other, b1 = 1, b2 = 2; \
203 which = dir[d], other = 1 - which; \
204 if (which) b1 = -b1, b2 = -b2; \
205 q->__head.balance += b1; \
206 if (q->__head.balance == b1) break; \
207 else if (q->__head.balance == b2) { \
208 __type *r = q->__head.p[other]; \
209 if (r->__head.balance == -b1) { \
210 path[d-1]->__head.p[dir[d-1]] = pre##_rotate2(q, which); \
211 } else { \
212 path[d-1]->__head.p[dir[d-1]] = pre##_rotate1(q, which); \
213 if (r->__head.balance == 0) { \
214 r->__head.balance = -b1; \
215 q->__head.balance = b1; \
216 break; \
217 } else r->__head.balance = q->__head.balance = 0; \
218 } \
219 } \
220 } \
221 *root_ = fake.__head.p[0]; \
222 return p; \
223 }
224
225#define kavll_free(__type, __head, __root, __free) do { \
226 __type *_p, *_q; \
227 for (_p = __root; _p; _p = _q) { \
228 if (_p->__head.p[0] == 0) { \
229 _q = _p->__head.p[1]; \
230 __free(_p); \
231 } else { \
232 _q = _p->__head.p[0]; \
233 _p->__head.p[0] = _q->__head.p[1]; \
234 _q->__head.p[1] = _p; \
235 } \
236 } \
237 } while (0)
238
239#define kavll_size(__type, __head, __root, __cnt) do { \
240 __type *_p, *_q; \
241 *(__cnt) = 0; \
242 for (_p = __root; _p; _p = _q) { \
243 if (_p->__head.p[0] == 0) { \
244 _q = _p->__head.p[1]; \
245 ++*(__cnt); \
246 } else { \
247 _q = _p->__head.p[0]; \
248 _p->__head.p[0] = _q->__head.p[1]; \
249 _q->__head.p[1] = _p; \
250 } \
251 } \
252 } while (0)
253
254#define __KAVLL_ITR(pre, __scope, __type, __head, __cmp) \
255 typedef struct pre##_itr_t { \
256 const __type *stack[KAVLL_MAX_DEPTH], **top, *right; /* _right_ points to the right child of *top */ \
257 } pre##_itr_t; \
258 __scope void pre##_itr_first(const __type *root, struct pre##_itr_t *itr) { \
259 const __type *p; \
260 for (itr->top = itr->stack - 1, p = root; p; p = p->__head.p[0]) \
261 *++itr->top = p; \
262 itr->right = (*itr->top)->__head.p[1]; \
263 } \
264 __scope int pre##_itr_find(const __type *root, const __type *x, struct pre##_itr_t *itr) { \
265 const __type *p = root; \
266 itr->top = itr->stack - 1; \
267 while (p != 0) { \
268 int cmp; \
269 cmp = __cmp(x, p); \
270 if (cmp < 0) *++itr->top = p, p = p->__head.p[0]; \
271 else if (cmp > 0) p = p->__head.p[1]; \
272 else break; \
273 } \
274 if (p) { \
275 *++itr->top = p; \
276 itr->right = p->__head.p[1]; \
277 return 1; \
278 } else if (itr->top >= itr->stack) { \
279 itr->right = (*itr->top)->__head.p[1]; \
280 return 0; \
281 } else return 0; \
282 } \
283 __scope int pre##_itr_next(struct pre##_itr_t *itr) { \
284 for (;;) { \
285 const __type *p; \
286 for (p = itr->right, --itr->top; p; p = p->__head.p[0]) \
287 *++itr->top = p; \
288 if (itr->top < itr->stack) return 0; \
289 itr->right = (*itr->top)->__head.p[1]; \
290 return 1; \
291 } \
292 }
293
294#define kavll_at(itr) ((itr)->top < (itr)->stack? 0 : *(itr)->top)
295
296#define KAVLL_INIT2(pre, __scope, __type, __head, __cmp) \
297 __KAVLL_FIND(pre, __scope, __type, __head, __cmp) \
298 __KAVLL_ROTATE(pre, __type, __head) \
299 __KAVLL_INSERT(pre, __scope, __type, __head, __cmp) \
300 __KAVLL_ERASE(pre, __scope, __type, __head, __cmp) \
301 __KAVLL_ITR(pre, __scope, __type, __head, __cmp)
302
303#define KAVLL_INIT(pre, __type, __head, __cmp) \
304 KAVLL_INIT2(pre,, __type, __head, __cmp)
305
306#endif