packio-simple

annotate src/rbtree.c @ 2:4767e7769c32

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