libresman

annotate src/rbtree.c @ 21:fe0dbdfbe403

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