ok now it works nicely in VR
[vrtris] / src / rbtree.c
1 /*
2 rbtree - simple balanced binary search tree (red-black tree) library.
3 Copyright (C) 2011-2014  John Tsiombikas <nuclear@member.fsf.org>
4
5 rbtree is free software, feel free to use, modify, and redistribute it, under
6 the terms of the 3-clause BSD license. See COPYING for details.
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <string.h>
12 #include "rbtree.h"
13
14 #define INT2PTR(x)      ((void*)(intptr_t)(x))
15 #define PTR2INT(x)      ((int)(intptr_t)(x))
16
17 struct rbtree {
18         struct rbnode *root;
19
20         rb_alloc_func_t alloc;
21         rb_free_func_t free;
22
23         rb_cmp_func_t cmp;
24         rb_del_func_t del;
25         void *del_cls;
26
27         struct rbnode *rstack, *iter;
28 };
29
30 static int cmpaddr(const void *ap, const void *bp);
31 static int cmpint(const void *ap, const void *bp);
32
33 static int count_nodes(struct rbnode *node);
34 static void del_tree(struct rbnode *node, void (*delfunc)(struct rbnode*, void*), void *cls);
35 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data);
36 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key);
37 /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key);*/
38 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls);
39
40 struct rbtree *rb_create(rb_cmp_func_t cmp_func)
41 {
42         struct rbtree *rb;
43
44         if(!(rb = malloc(sizeof *rb))) {
45                 return 0;
46         }
47         if(rb_init(rb, cmp_func) == -1) {
48                 free(rb);
49                 return 0;
50         }
51         return rb;
52 }
53
54 void rb_free(struct rbtree *rb)
55 {
56         if(rb) {
57                 rb_destroy(rb);
58                 free(rb);
59         }
60 }
61
62
63 int rb_init(struct rbtree *rb, rb_cmp_func_t cmp_func)
64 {
65         memset(rb, 0, sizeof *rb);
66
67         if(!cmp_func) {
68                 rb->cmp = cmpaddr;
69         } else if(cmp_func == RB_KEY_INT) {
70                 rb->cmp = cmpint;
71         } else if(cmp_func == RB_KEY_STRING) {
72                 rb->cmp = (rb_cmp_func_t)strcmp;
73         } else {
74                 rb->cmp = cmp_func;
75         }
76
77         rb->alloc = malloc;
78         rb->free = free;
79         return 0;
80 }
81
82 void rb_destroy(struct rbtree *rb)
83 {
84         del_tree(rb->root, rb->del, rb->del_cls);
85 }
86
87 void rb_set_allocator(struct rbtree *rb, rb_alloc_func_t alloc, rb_free_func_t free)
88 {
89         rb->alloc = alloc;
90         rb->free = free;
91 }
92
93
94 void rb_set_compare_func(struct rbtree *rb, rb_cmp_func_t func)
95 {
96         rb->cmp = func;
97 }
98
99 void rb_set_delete_func(struct rbtree *rb, rb_del_func_t func, void *cls)
100 {
101         rb->del = func;
102         rb->del_cls = cls;
103 }
104
105
106 void rb_clear(struct rbtree *rb)
107 {
108         del_tree(rb->root, rb->del, rb->del_cls);
109         rb->root = 0;
110 }
111
112 int rb_copy(struct rbtree *dest, struct rbtree *src)
113 {
114         struct rbnode *node;
115
116         rb_clear(dest);
117         rb_begin(src);
118         while((node = rb_next(src))) {
119                 if(rb_insert(dest, node->key, node->data) == -1) {
120                         return -1;
121                 }
122         }
123         return 0;
124 }
125
126 int rb_size(struct rbtree *rb)
127 {
128         return count_nodes(rb->root);
129 }
130
131 int rb_insert(struct rbtree *rb, void *key, void *data)
132 {
133         rb->root = insert(rb, rb->root, key, data);
134         rb->root->red = 0;
135         return 0;
136 }
137
138 int rb_inserti(struct rbtree *rb, int key, void *data)
139 {
140         rb->root = insert(rb, rb->root, INT2PTR(key), data);
141         rb->root->red = 0;
142         return 0;
143 }
144
145
146 int rb_delete(struct rbtree *rb, void *key)
147 {
148         if((rb->root = delete(rb, rb->root, key))) {
149                 rb->root->red = 0;
150         }
151         return 0;
152 }
153
154 int rb_deletei(struct rbtree *rb, int key)
155 {
156         if((rb->root = delete(rb, rb->root, INT2PTR(key)))) {
157                 rb->root->red = 0;
158         }
159         return 0;
160 }
161
162
163 struct rbnode *rb_find(struct rbtree *rb, void *key)
164 {
165         struct rbnode *node = rb->root;
166
167         while(node) {
168                 int cmp = rb->cmp(key, node->key);
169                 if(cmp == 0) {
170                         return node;
171                 }
172                 node = cmp < 0 ? node->left : node->right;
173         }
174         return 0;
175 }
176
177 struct rbnode *rb_findi(struct rbtree *rb, int key)
178 {
179         return rb_find(rb, INT2PTR(key));
180 }
181
182
183 void rb_foreach(struct rbtree *rb, void (*func)(struct rbnode*, void*), void *cls)
184 {
185         traverse(rb->root, func, cls);
186 }
187
188
189 struct rbnode *rb_root(struct rbtree *rb)
190 {
191         return rb->root;
192 }
193
194 void rb_begin(struct rbtree *rb)
195 {
196         rb->rstack = 0;
197         rb->iter = rb->root;
198 }
199
200 #define push(sp, x)             ((x)->next = (sp), (sp) = (x))
201 #define pop(sp)                 ((sp) = (sp)->next)
202 #define top(sp)                 (sp)
203
204 struct rbnode *rb_next(struct rbtree *rb)
205 {
206         struct rbnode *res = 0;
207
208         while(rb->rstack || rb->iter) {
209                 if(rb->iter) {
210                         push(rb->rstack, rb->iter);
211                         rb->iter = rb->iter->left;
212                 } else {
213                         rb->iter = top(rb->rstack);
214                         pop(rb->rstack);
215                         res = rb->iter;
216                         rb->iter = rb->iter->right;
217                         break;
218                 }
219         }
220         return res;
221 }
222
223 void *rb_node_key(struct rbnode *node)
224 {
225         return node ? node->key : 0;
226 }
227
228 int rb_node_keyi(struct rbnode *node)
229 {
230         return node ? PTR2INT(node->key) : 0;
231 }
232
233 void *rb_node_data(struct rbnode *node)
234 {
235         return node ? node->data : 0;
236 }
237
238 static int cmpaddr(const void *ap, const void *bp)
239 {
240         return ap < bp ? -1 : (ap > bp ? 1 : 0);
241 }
242
243 static int cmpint(const void *ap, const void *bp)
244 {
245         return PTR2INT(ap) - PTR2INT(bp);
246 }
247
248
249 /* ---- left-leaning 2-3 red-black implementation ---- */
250
251 /* helper prototypes */
252 static int is_red(struct rbnode *tree);
253 static void color_flip(struct rbnode *tree);
254 static struct rbnode *rot_left(struct rbnode *a);
255 static struct rbnode *rot_right(struct rbnode *a);
256 static struct rbnode *find_min(struct rbnode *tree);
257 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree);
258 /*static struct rbnode *move_red_right(struct rbnode *tree);*/
259 static struct rbnode *move_red_left(struct rbnode *tree);
260 static struct rbnode *fix_up(struct rbnode *tree);
261
262 static int count_nodes(struct rbnode *node)
263 {
264         if(!node)
265                 return 0;
266
267         return 1 + count_nodes(node->left) + count_nodes(node->right);
268 }
269
270 static void del_tree(struct rbnode *node, rb_del_func_t delfunc, void *cls)
271 {
272         if(!node)
273                 return;
274
275         del_tree(node->left, delfunc, cls);
276         del_tree(node->right, delfunc, cls);
277
278         if(delfunc) {
279                 delfunc(node, cls);
280         }
281         free(node);
282 }
283
284 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data)
285 {
286         int cmp;
287
288         if(!tree) {
289                 struct rbnode *node = rb->alloc(sizeof *node);
290                 node->red = 1;
291                 node->key = key;
292                 node->data = data;
293                 node->left = node->right = 0;
294                 return node;
295         }
296
297         cmp = rb->cmp(key, tree->key);
298
299         if(cmp < 0) {
300                 tree->left = insert(rb, tree->left, key, data);
301         } else if(cmp > 0) {
302                 tree->right = insert(rb, tree->right, key, data);
303         } else {
304                 tree->data = data;
305         }
306
307         /* fix right-leaning reds */
308         if(is_red(tree->right)) {
309                 tree = rot_left(tree);
310         }
311         /* fix two reds in a row */
312         if(is_red(tree->left) && is_red(tree->left->left)) {
313                 tree = rot_right(tree);
314         }
315
316         /* if 4-node, split it by color inversion */
317         if(is_red(tree->left) && is_red(tree->right)) {
318                 color_flip(tree);
319         }
320
321         return tree;
322 }
323
324 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key)
325 {
326         int cmp;
327
328         if(!tree) {
329                 return 0;
330         }
331
332         cmp = rb->cmp(key, tree->key);
333
334         if(cmp < 0) {
335                 if(!is_red(tree->left) && !is_red(tree->left->left)) {
336                         tree = move_red_left(tree);
337                 }
338                 tree->left = delete(rb, tree->left, key);
339         } else {
340                 /* need reds on the right */
341                 if(is_red(tree->left)) {
342                         tree = rot_right(tree);
343                 }
344
345                 /* found it at the bottom (XXX what certifies left is null?) */
346                 if(cmp == 0 && !tree->right) {
347                         if(rb->del) {
348                                 rb->del(tree, rb->del_cls);
349                         }
350                         rb->free(tree);
351                         return 0;
352                 }
353
354                 if(!is_red(tree->right) && !is_red(tree->right->left)) {
355                         tree = move_red_left(tree);
356                 }
357
358                 if(key == tree->key) {
359                         struct rbnode *rmin = find_min(tree->right);
360                         tree->key = rmin->key;
361                         tree->data = rmin->data;
362                         tree->right = del_min(rb, tree->right);
363                 } else {
364                         tree->right = delete(rb, tree->right, key);
365                 }
366         }
367
368         return fix_up(tree);
369 }
370
371 /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key)
372 {
373         int cmp;
374
375         if(!node)
376                 return 0;
377
378         if((cmp = rb->cmp(key, node->key)) == 0) {
379                 return node;
380         }
381         return find(rb, cmp < 0 ? node->left : node->right, key);
382 }*/
383
384 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls)
385 {
386         if(!node)
387                 return;
388
389         traverse(node->left, func, cls);
390         func(node, cls);
391         traverse(node->right, func, cls);
392 }
393
394 /* helpers */
395
396 static int is_red(struct rbnode *tree)
397 {
398         return tree && tree->red;
399 }
400
401 static void color_flip(struct rbnode *tree)
402 {
403         tree->red = !tree->red;
404         tree->left->red = !tree->left->red;
405         tree->right->red = !tree->right->red;
406 }
407
408 static struct rbnode *rot_left(struct rbnode *a)
409 {
410         struct rbnode *b = a->right;
411         a->right = b->left;
412         b->left = a;
413         b->red = a->red;
414         a->red = 1;
415         return b;
416 }
417
418 static struct rbnode *rot_right(struct rbnode *a)
419 {
420         struct rbnode *b = a->left;
421         a->left = b->right;
422         b->right = a;
423         b->red = a->red;
424         a->red = 1;
425         return b;
426 }
427
428 static struct rbnode *find_min(struct rbnode *tree)
429 {
430         if(!tree)
431                 return 0;
432
433         while(tree->left) {
434                 tree = tree->left;
435         }
436         return tree;
437 }
438
439 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree)
440 {
441         if(!tree->left) {
442                 if(rb->del) {
443                         rb->del(tree->left, rb->del_cls);
444                 }
445                 rb->free(tree->left);
446                 return 0;
447         }
448
449         /* make sure we've got red (3/4-nodes) at the left side so we can delete at the bottom */
450         if(!is_red(tree->left) && !is_red(tree->left->left)) {
451                 tree = move_red_left(tree);
452         }
453         tree->left = del_min(rb, tree->left);
454
455         /* fix right-reds, red-reds, and split 4-nodes on the way up */
456         return fix_up(tree);
457 }
458
459 #if 0
460 /* push a red link on this node to the right */
461 static struct rbnode *move_red_right(struct rbnode *tree)
462 {
463         /* flipping it makes both children go red, so we have a red to the right */
464         color_flip(tree);
465
466         /* if after the flip we've got a red-red situation to the left, fix it */
467         if(is_red(tree->left->left)) {
468                 tree = rot_right(tree);
469                 color_flip(tree);
470         }
471         return tree;
472 }
473 #endif
474
475 /* push a red link on this node to the left */
476 static struct rbnode *move_red_left(struct rbnode *tree)
477 {
478         /* flipping it makes both children go red, so we have a red to the left */
479         color_flip(tree);
480
481         /* if after the flip we've got a red-red on the right-left, fix it */
482         if(is_red(tree->right->left)) {
483                 tree->right = rot_right(tree->right);
484                 tree = rot_left(tree);
485                 color_flip(tree);
486         }
487         return tree;
488 }
489
490 static struct rbnode *fix_up(struct rbnode *tree)
491 {
492         /* fix right-leaning */
493         if(is_red(tree->right)) {
494                 tree = rot_left(tree);
495         }
496         /* change invalid red-red pairs into a proper 4-node */
497         if(is_red(tree->left) && is_red(tree->left->left)) {
498                 tree = rot_right(tree);
499         }
500         /* split 4-nodes */
501         if(is_red(tree->left) && is_red(tree->right)) {
502                 color_flip(tree);
503         }
504         return tree;
505 }