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