rbtree
view src/rbtree.c @ 1:3b219820ebe8
removed unused typedef
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Sun, 09 Oct 2011 07:52:04 +0300 |
parents | |
children | 53afe96233f2 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "rbtree.h"
6 #define INT2PTR(x) ((void*)(x))
7 #define PTR2INT(x) ((int)(x))
9 struct rbtree {
10 struct rbnode *root;
12 rb_alloc_func_t alloc;
13 rb_free_func_t free;
15 rb_cmp_func_t cmp;
16 rb_del_func_t del;
17 void *del_cls;
19 struct rbnode *rstack, *iter;
20 };
22 static int cmpaddr(void *ap, void *bp);
23 static int cmpint(void *ap, void *bp);
25 static int count_nodes(struct rbnode *node);
26 static void del_tree(struct rbnode *node, void (*delfunc)(struct rbnode*, void*), void *cls);
27 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data);
28 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key);
29 static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key);
30 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls);
32 struct rbtree *rb_create(rb_cmp_func_t cmp_func)
33 {
34 struct rbtree *rb;
36 if(!(rb = malloc(sizeof *rb))) {
37 return 0;
38 }
39 if(rb_init(rb, cmp_func) == -1) {
40 free(rb);
41 return 0;
42 }
43 return rb;
44 }
46 void rb_free(struct rbtree *rb)
47 {
48 rb_destroy(rb);
49 free(rb);
50 }
53 int rb_init(struct rbtree *rb, rb_cmp_func_t cmp_func)
54 {
55 memset(rb, 0, sizeof *rb);
57 if(cmp_func == RB_KEY_INT) {
58 rb->cmp = cmpint;
59 } else if(cmp_func == RB_KEY_STRING) {
60 rb->cmp = (rb_cmp_func_t)strcmp;
61 } else {
62 rb->cmp = cmpaddr;
63 }
65 rb->alloc = malloc;
66 rb->free = free;
67 return 0;
68 }
70 void rb_destroy(struct rbtree *rb)
71 {
72 del_tree(rb->root, rb->del, rb->del_cls);
73 }
75 void rb_set_allocator(struct rbtree *rb, rb_alloc_func_t alloc, rb_free_func_t free)
76 {
77 rb->alloc = alloc;
78 rb->free = free;
79 }
82 void rb_set_compare_func(struct rbtree *rb, rb_cmp_func_t func)
83 {
84 rb->cmp = func;
85 }
87 void rb_set_delete_func(struct rbtree *rb, rb_del_func_t func, void *cls)
88 {
89 rb->del = func;
90 rb->del_cls = cls;
91 }
93 int rb_size(struct rbtree *rb)
94 {
95 return count_nodes(rb->root);
96 }
98 int rb_insert(struct rbtree *rb, void *key, void *data)
99 {
100 rb->root = insert(rb, rb->root, key, data);
101 rb->root->red = 0;
102 return 0;
103 }
105 int rb_inserti(struct rbtree *rb, int key, void *data)
106 {
107 rb->root = insert(rb, rb->root, INT2PTR(key), data);
108 rb->root->red = 0;
109 return 0;
110 }
113 int rb_delete(struct rbtree *rb, void *key)
114 {
115 rb->root = delete(rb, rb->root, key);
116 rb->root->red = 0;
117 return 0;
118 }
120 int rb_deletei(struct rbtree *rb, int key)
121 {
122 rb->root = delete(rb, rb->root, INT2PTR(key));
123 rb->root->red = 0;
124 return 0;
125 }
128 void *rb_find(struct rbtree *rb, void *key)
129 {
130 return find(rb, rb->root, key);
131 }
133 void *rb_findi(struct rbtree *rb, int key)
134 {
135 return find(rb, rb->root, INT2PTR(key));
136 }
139 void rb_foreach(struct rbtree *rb, void (*func)(struct rbnode*, void*), void *cls)
140 {
141 traverse(rb->root, func, cls);
142 }
145 struct rbnode *rb_root(struct rbtree *rb)
146 {
147 return rb->root;
148 }
150 void rb_begin(struct rbtree *rb)
151 {
152 rb->rstack = 0;
153 rb->iter = rb->root;
154 }
156 #define push(sp, x) ((x)->next = (sp), (sp) = (x))
157 #define pop(sp) ((sp) = (sp)->next)
158 #define top(sp) (sp)
160 struct rbnode *rb_next(struct rbtree *rb)
161 {
162 struct rbnode *res = 0;
164 while(rb->rstack || rb->iter) {
165 if(rb->iter) {
166 push(rb->rstack, rb->iter);
167 rb->iter = rb->iter->left;
168 } else {
169 rb->iter = top(rb->rstack);
170 pop(rb->rstack);
171 res = rb->iter;
172 rb->iter = rb->iter->right;
173 break;
174 }
175 }
176 return res;
177 }
179 void *rb_node_key(struct rbnode *node)
180 {
181 return node ? node->key : 0;
182 }
184 int rb_node_keyi(struct rbnode *node)
185 {
186 return node ? PTR2INT(node->key) : 0;
187 }
189 void *rb_node_data(struct rbnode *node)
190 {
191 return node ? node->data : 0;
192 }
194 static int cmpaddr(void *ap, void *bp)
195 {
196 return ap < bp ? -1 : (ap > bp ? 1 : 0);
197 }
199 static int cmpint(void *ap, void *bp)
200 {
201 return PTR2INT(ap) - PTR2INT(bp);
202 }
205 /* ---- left-leaning 2-3 red-black implementation ---- */
207 /* helper prototypes */
208 static int is_red(struct rbnode *tree);
209 static void color_flip(struct rbnode *tree);
210 static struct rbnode *rot_left(struct rbnode *a);
211 static struct rbnode *rot_right(struct rbnode *a);
212 static struct rbnode *find_min(struct rbnode *tree);
213 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree);
214 /*static struct rbnode *move_red_right(struct rbnode *tree);*/
215 static struct rbnode *move_red_left(struct rbnode *tree);
216 static struct rbnode *fix_up(struct rbnode *tree);
218 static int count_nodes(struct rbnode *node)
219 {
220 if(!node)
221 return 0;
223 return 1 + count_nodes(node->left) + count_nodes(node->right);
224 }
226 static void del_tree(struct rbnode *node, rb_del_func_t delfunc, void *cls)
227 {
228 if(!node)
229 return;
231 del_tree(node->left, delfunc, cls);
232 del_tree(node->right, delfunc, cls);
234 delfunc(node, cls);
235 free(node);
236 }
238 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data)
239 {
240 int cmp;
242 if(!tree) {
243 struct rbnode *node = rb->alloc(sizeof *node);
244 node->red = 1;
245 node->key = key;
246 node->data = data;
247 node->left = node->right = 0;
248 return node;
249 }
251 cmp = rb->cmp(key, tree->key);
253 if(cmp < 0) {
254 tree->left = insert(rb, tree->left, key, data);
255 } else if(cmp > 0) {
256 tree->right = insert(rb, tree->right, key, data);
257 } else {
258 tree->data = data;
259 }
261 /* fix right-leaning reds */
262 if(is_red(tree->right)) {
263 tree = rot_left(tree);
264 }
265 /* fix two reds in a row */
266 if(is_red(tree->left) && is_red(tree->left->left)) {
267 tree = rot_right(tree);
268 }
270 /* if 4-node, split it by color inversion */
271 if(is_red(tree->left) && is_red(tree->right)) {
272 color_flip(tree);
273 }
275 return tree;
276 }
278 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key)
279 {
280 int cmp;
282 if(!tree) {
283 return 0;
284 }
286 cmp = rb->cmp(key, tree->key);
288 if(cmp < 0) {
289 if(!is_red(tree->left) && !is_red(tree->left->left)) {
290 tree = move_red_left(tree);
291 }
292 tree->left = delete(rb, tree->left, key);
293 } else {
294 /* need reds on the right */
295 if(is_red(tree->left)) {
296 tree = rot_right(tree);
297 }
299 /* found it at the bottom (XXX what certifies left is null?) */
300 if(cmp == 0 && !tree->right) {
301 if(rb->del) {
302 rb->del(tree, rb->del_cls);
303 }
304 rb->free(tree);
305 return 0;
306 }
308 if(!is_red(tree->right) && !is_red(tree->right->left)) {
309 tree = move_red_left(tree);
310 }
312 if(key == tree->key) {
313 struct rbnode *rmin = find_min(tree->right);
314 tree->key = rmin->key;
315 tree->data = rmin->data;
316 tree->right = del_min(rb, tree->right);
317 } else {
318 tree->right = delete(rb, tree->right, key);
319 }
320 }
322 return fix_up(tree);
323 }
325 static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key)
326 {
327 int cmp;
329 if(!node)
330 return 0;
332 if((cmp = rb->cmp(key, node->key)) == 0) {
333 return node;
334 }
335 return find(rb, cmp < 0 ? node->left : node->right, key);
336 }
338 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls)
339 {
340 if(!node)
341 return;
343 traverse(node->left, func, cls);
344 func(node, cls);
345 traverse(node->right, func, cls);
346 }
348 /* helpers */
350 static int is_red(struct rbnode *tree)
351 {
352 return tree && tree->red;
353 }
355 static void color_flip(struct rbnode *tree)
356 {
357 tree->red = !tree->red;
358 tree->left->red = !tree->left->red;
359 tree->right->red = !tree->right->red;
360 }
362 static struct rbnode *rot_left(struct rbnode *a)
363 {
364 struct rbnode *b = a->right;
365 a->right = b->left;
366 b->left = a;
367 b->red = a->red;
368 a->red = 1;
369 return b;
370 }
372 static struct rbnode *rot_right(struct rbnode *a)
373 {
374 struct rbnode *b = a->left;
375 a->left = b->right;
376 b->right = a;
377 b->red = a->red;
378 a->red = 1;
379 return b;
380 }
382 static struct rbnode *find_min(struct rbnode *tree)
383 {
384 if(!tree || !tree->left) {
385 return tree;
386 }
387 return find_min(tree->left);
388 }
390 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree)
391 {
392 if(!tree->left) {
393 if(rb->del) {
394 rb->del(tree->left, rb->del_cls);
395 }
396 rb->free(tree->left);
397 return 0;
398 }
400 /* make sure we've got red (3/4-nodes) at the left side so we can delete at the bottom */
401 if(!is_red(tree->left) && !is_red(tree->left->left)) {
402 tree = move_red_left(tree);
403 }
404 tree->left = del_min(rb, tree->left);
406 /* fix right-reds, red-reds, and split 4-nodes on the way up */
407 return fix_up(tree);
408 }
410 #if 0
411 /* push a red link on this node to the right */
412 static struct rbnode *move_red_right(struct rbnode *tree)
413 {
414 /* flipping it makes both children go red, so we have a red to the right */
415 color_flip(tree);
417 /* if after the flip we've got a red-red situation to the left, fix it */
418 if(is_red(tree->left->left)) {
419 tree = rot_right(tree);
420 color_flip(tree);
421 }
422 return tree;
423 }
424 #endif
426 /* push a red link on this node to the left */
427 static struct rbnode *move_red_left(struct rbnode *tree)
428 {
429 /* flipping it makes both children go red, so we have a red to the left */
430 color_flip(tree);
432 /* if after the flip we've got a red-red on the right-left, fix it */
433 if(is_red(tree->right->left)) {
434 tree->right = rot_right(tree->right);
435 tree = rot_left(tree);
436 color_flip(tree);
437 }
438 return tree;
439 }
441 static struct rbnode *fix_up(struct rbnode *tree)
442 {
443 /* fix right-leaning */
444 if(is_red(tree->right)) {
445 tree = rot_left(tree);
446 }
447 /* change invalid red-red pairs into a proper 4-node */
448 if(is_red(tree->left) && is_red(tree->left->left)) {
449 tree = rot_right(tree);
450 }
451 /* split 4-nodes */
452 if(is_red(tree->left) && is_red(tree->right)) {
453 color_flip(tree);
454 }
455 return tree;
456 }