tinywebd

annotate src/rbtree.c @ 6:4f191dbfac7e

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