rbtree

annotate src/rbtree.c @ 15:1b77b72688fe

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