rayzor

annotate src/rbtree.c @ 17:79609d482762

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