tinywebd

view src/rbtree.c @ 6:4f191dbfac7e

commited a bunch of missing files from previous commits
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Apr 2015 01:57:25 +0300
parents
children
line source
1 /*
2 rbtree - simple balanced binary search tree (red-black tree) library.
3 Copyright (C) 2011-2014 John Tsiombikas <nuclear@member.fsf.org>
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"
14 #define INT2PTR(x) ((void*)(intptr_t)(x))
15 #define PTR2INT(x) ((int)(intptr_t)(x))
17 struct rbtree {
18 struct rbnode *root;
20 rb_alloc_func_t alloc;
21 rb_free_func_t free;
23 rb_cmp_func_t cmp;
24 rb_del_func_t del;
25 void *del_cls;
27 struct rbnode *rstack, *iter;
28 };
30 static int cmpaddr(const void *ap, const void *bp);
31 static int cmpint(const void *ap, const void *bp);
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);
40 struct rbtree *rb_create(rb_cmp_func_t cmp_func)
41 {
42 struct rbtree *rb;
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 }
54 void rb_free(struct rbtree *rb)
55 {
56 rb_destroy(rb);
57 free(rb);
58 }
61 int rb_init(struct rbtree *rb, rb_cmp_func_t cmp_func)
62 {
63 memset(rb, 0, sizeof *rb);
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 }
75 rb->alloc = malloc;
76 rb->free = free;
77 return 0;
78 }
80 void rb_destroy(struct rbtree *rb)
81 {
82 del_tree(rb->root, rb->del, rb->del_cls);
83 }
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 }
92 void rb_set_compare_func(struct rbtree *rb, rb_cmp_func_t func)
93 {
94 rb->cmp = func;
95 }
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 }
104 void rb_clear(struct rbtree *rb)
105 {
106 del_tree(rb->root, rb->del, rb->del_cls);
107 rb->root = 0;
108 }
110 int rb_copy(struct rbtree *dest, struct rbtree *src)
111 {
112 struct rbnode *node;
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 }
124 int rb_size(struct rbtree *rb)
125 {
126 return count_nodes(rb->root);
127 }
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 }
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 }
144 int rb_delete(struct rbtree *rb, void *key)
145 {
146 rb->root = delete(rb, rb->root, key);
147 rb->root->red = 0;
148 return 0;
149 }
151 int rb_deletei(struct rbtree *rb, int key)
152 {
153 rb->root = delete(rb, rb->root, INT2PTR(key));
154 rb->root->red = 0;
155 return 0;
156 }
159 struct rbnode *rb_find(struct rbtree *rb, void *key)
160 {
161 struct rbnode *node = rb->root;
163 while(node) {
164 int cmp = rb->cmp(key, node->key);
165 if(cmp == 0) {
166 return node;
167 }
168 node = cmp < 0 ? node->left : node->right;
169 }
170 return 0;
171 }
173 struct rbnode *rb_findi(struct rbtree *rb, int key)
174 {
175 return rb_find(rb, INT2PTR(key));
176 }
179 void rb_foreach(struct rbtree *rb, void (*func)(struct rbnode*, void*), void *cls)
180 {
181 traverse(rb->root, func, cls);
182 }
185 struct rbnode *rb_root(struct rbtree *rb)
186 {
187 return rb->root;
188 }
190 void rb_begin(struct rbtree *rb)
191 {
192 rb->rstack = 0;
193 rb->iter = rb->root;
194 }
196 #define push(sp, x) ((x)->next = (sp), (sp) = (x))
197 #define pop(sp) ((sp) = (sp)->next)
198 #define top(sp) (sp)
200 struct rbnode *rb_next(struct rbtree *rb)
201 {
202 struct rbnode *res = 0;
204 while(rb->rstack || rb->iter) {
205 if(rb->iter) {
206 push(rb->rstack, rb->iter);
207 rb->iter = rb->iter->left;
208 } else {
209 rb->iter = top(rb->rstack);
210 pop(rb->rstack);
211 res = rb->iter;
212 rb->iter = rb->iter->right;
213 break;
214 }
215 }
216 return res;
217 }
219 void *rb_node_key(struct rbnode *node)
220 {
221 return node ? node->key : 0;
222 }
224 int rb_node_keyi(struct rbnode *node)
225 {
226 return node ? PTR2INT(node->key) : 0;
227 }
229 void *rb_node_data(struct rbnode *node)
230 {
231 return node ? node->data : 0;
232 }
234 static int cmpaddr(const void *ap, const void *bp)
235 {
236 return ap < bp ? -1 : (ap > bp ? 1 : 0);
237 }
239 static int cmpint(const void *ap, const void *bp)
240 {
241 return PTR2INT(ap) - PTR2INT(bp);
242 }
245 /* ---- left-leaning 2-3 red-black implementation ---- */
247 /* helper prototypes */
248 static int is_red(struct rbnode *tree);
249 static void color_flip(struct rbnode *tree);
250 static struct rbnode *rot_left(struct rbnode *a);
251 static struct rbnode *rot_right(struct rbnode *a);
252 static struct rbnode *find_min(struct rbnode *tree);
253 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree);
254 /*static struct rbnode *move_red_right(struct rbnode *tree);*/
255 static struct rbnode *move_red_left(struct rbnode *tree);
256 static struct rbnode *fix_up(struct rbnode *tree);
258 static int count_nodes(struct rbnode *node)
259 {
260 if(!node)
261 return 0;
263 return 1 + count_nodes(node->left) + count_nodes(node->right);
264 }
266 static void del_tree(struct rbnode *node, rb_del_func_t delfunc, void *cls)
267 {
268 if(!node)
269 return;
271 del_tree(node->left, delfunc, cls);
272 del_tree(node->right, delfunc, cls);
274 if(delfunc) {
275 delfunc(node, cls);
276 }
277 free(node);
278 }
280 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data)
281 {
282 int cmp;
284 if(!tree) {
285 struct rbnode *node = rb->alloc(sizeof *node);
286 node->red = 1;
287 node->key = key;
288 node->data = data;
289 node->left = node->right = 0;
290 return node;
291 }
293 cmp = rb->cmp(key, tree->key);
295 if(cmp < 0) {
296 tree->left = insert(rb, tree->left, key, data);
297 } else if(cmp > 0) {
298 tree->right = insert(rb, tree->right, key, data);
299 } else {
300 tree->data = data;
301 }
303 /* fix right-leaning reds */
304 if(is_red(tree->right)) {
305 tree = rot_left(tree);
306 }
307 /* fix two reds in a row */
308 if(is_red(tree->left) && is_red(tree->left->left)) {
309 tree = rot_right(tree);
310 }
312 /* if 4-node, split it by color inversion */
313 if(is_red(tree->left) && is_red(tree->right)) {
314 color_flip(tree);
315 }
317 return tree;
318 }
320 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key)
321 {
322 int cmp;
324 if(!tree) {
325 return 0;
326 }
328 cmp = rb->cmp(key, tree->key);
330 if(cmp < 0) {
331 if(!is_red(tree->left) && !is_red(tree->left->left)) {
332 tree = move_red_left(tree);
333 }
334 tree->left = delete(rb, tree->left, key);
335 } else {
336 /* need reds on the right */
337 if(is_red(tree->left)) {
338 tree = rot_right(tree);
339 }
341 /* found it at the bottom (XXX what certifies left is null?) */
342 if(cmp == 0 && !tree->right) {
343 if(rb->del) {
344 rb->del(tree, rb->del_cls);
345 }
346 rb->free(tree);
347 return 0;
348 }
350 if(!is_red(tree->right) && !is_red(tree->right->left)) {
351 tree = move_red_left(tree);
352 }
354 if(key == tree->key) {
355 struct rbnode *rmin = find_min(tree->right);
356 tree->key = rmin->key;
357 tree->data = rmin->data;
358 tree->right = del_min(rb, tree->right);
359 } else {
360 tree->right = delete(rb, tree->right, key);
361 }
362 }
364 return fix_up(tree);
365 }
367 /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key)
368 {
369 int cmp;
371 if(!node)
372 return 0;
374 if((cmp = rb->cmp(key, node->key)) == 0) {
375 return node;
376 }
377 return find(rb, cmp < 0 ? node->left : node->right, key);
378 }*/
380 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls)
381 {
382 if(!node)
383 return;
385 traverse(node->left, func, cls);
386 func(node, cls);
387 traverse(node->right, func, cls);
388 }
390 /* helpers */
392 static int is_red(struct rbnode *tree)
393 {
394 return tree && tree->red;
395 }
397 static void color_flip(struct rbnode *tree)
398 {
399 tree->red = !tree->red;
400 tree->left->red = !tree->left->red;
401 tree->right->red = !tree->right->red;
402 }
404 static struct rbnode *rot_left(struct rbnode *a)
405 {
406 struct rbnode *b = a->right;
407 a->right = b->left;
408 b->left = a;
409 b->red = a->red;
410 a->red = 1;
411 return b;
412 }
414 static struct rbnode *rot_right(struct rbnode *a)
415 {
416 struct rbnode *b = a->left;
417 a->left = b->right;
418 b->right = a;
419 b->red = a->red;
420 a->red = 1;
421 return b;
422 }
424 static struct rbnode *find_min(struct rbnode *tree)
425 {
426 if(!tree)
427 return 0;
429 while(tree->left) {
430 tree = tree->left;
431 }
432 return tree;
433 }
435 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree)
436 {
437 if(!tree->left) {
438 if(rb->del) {
439 rb->del(tree->left, rb->del_cls);
440 }
441 rb->free(tree->left);
442 return 0;
443 }
445 /* make sure we've got red (3/4-nodes) at the left side so we can delete at the bottom */
446 if(!is_red(tree->left) && !is_red(tree->left->left)) {
447 tree = move_red_left(tree);
448 }
449 tree->left = del_min(rb, tree->left);
451 /* fix right-reds, red-reds, and split 4-nodes on the way up */
452 return fix_up(tree);
453 }
455 #if 0
456 /* push a red link on this node to the right */
457 static struct rbnode *move_red_right(struct rbnode *tree)
458 {
459 /* flipping it makes both children go red, so we have a red to the right */
460 color_flip(tree);
462 /* if after the flip we've got a red-red situation to the left, fix it */
463 if(is_red(tree->left->left)) {
464 tree = rot_right(tree);
465 color_flip(tree);
466 }
467 return tree;
468 }
469 #endif
471 /* push a red link on this node to the left */
472 static struct rbnode *move_red_left(struct rbnode *tree)
473 {
474 /* flipping it makes both children go red, so we have a red to the left */
475 color_flip(tree);
477 /* if after the flip we've got a red-red on the right-left, fix it */
478 if(is_red(tree->right->left)) {
479 tree->right = rot_right(tree->right);
480 tree = rot_left(tree);
481 color_flip(tree);
482 }
483 return tree;
484 }
486 static struct rbnode *fix_up(struct rbnode *tree)
487 {
488 /* fix right-leaning */
489 if(is_red(tree->right)) {
490 tree = rot_left(tree);
491 }
492 /* change invalid red-red pairs into a proper 4-node */
493 if(is_red(tree->left) && is_red(tree->left->left)) {
494 tree = rot_right(tree);
495 }
496 /* split 4-nodes */
497 if(is_red(tree->left) && is_red(tree->right)) {
498 color_flip(tree);
499 }
500 return tree;
501 }