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