3dphotoshoot

annotate src/rbtree.c @ 22:d7fe157c402d

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