nuclear@21: #include nuclear@21: #include nuclear@23: #ifndef _MSC_VER nuclear@21: #include nuclear@23: #endif nuclear@21: #include nuclear@21: #include "rbtree.h" nuclear@21: nuclear@21: #define INT2PTR(x) ((void*)(intptr_t)(x)) nuclear@21: #define PTR2INT(x) ((int)(intptr_t)(x)) nuclear@21: nuclear@21: struct rbtree { nuclear@21: struct rbnode *root; nuclear@21: nuclear@21: rb_alloc_func_t alloc; nuclear@21: rb_free_func_t free; nuclear@21: nuclear@21: rb_cmp_func_t cmp; nuclear@21: rb_del_func_t del; nuclear@21: void *del_cls; nuclear@21: nuclear@21: struct rbnode *rstack, *iter; nuclear@21: }; nuclear@21: nuclear@21: static int cmpaddr(const void *ap, const void *bp); nuclear@21: static int cmpint(const void *ap, const void *bp); nuclear@21: nuclear@21: static int count_nodes(struct rbnode *node); nuclear@21: static void del_tree(struct rbnode *node, void (*delfunc)(struct rbnode*, void*), void *cls); nuclear@21: static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data); nuclear@21: static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key); nuclear@21: /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key);*/ nuclear@21: static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls); nuclear@21: nuclear@21: struct rbtree *rb_create(rb_cmp_func_t cmp_func) nuclear@21: { nuclear@21: struct rbtree *rb; nuclear@21: nuclear@21: if(!(rb = malloc(sizeof *rb))) { nuclear@21: return 0; nuclear@21: } nuclear@21: if(rb_init(rb, cmp_func) == -1) { nuclear@21: free(rb); nuclear@21: return 0; nuclear@21: } nuclear@21: return rb; nuclear@21: } nuclear@21: nuclear@21: void rb_free(struct rbtree *rb) nuclear@21: { nuclear@21: rb_destroy(rb); nuclear@21: free(rb); nuclear@21: } nuclear@21: nuclear@21: nuclear@21: int rb_init(struct rbtree *rb, rb_cmp_func_t cmp_func) nuclear@21: { nuclear@21: memset(rb, 0, sizeof *rb); nuclear@21: nuclear@21: if(cmp_func == RB_KEY_INT) { nuclear@21: rb->cmp = cmpint; nuclear@21: } else if(cmp_func == RB_KEY_STRING) { nuclear@21: rb->cmp = (rb_cmp_func_t)strcmp; nuclear@21: } else { nuclear@21: rb->cmp = cmpaddr; nuclear@21: } nuclear@21: nuclear@21: rb->alloc = malloc; nuclear@21: rb->free = free; nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: void rb_destroy(struct rbtree *rb) nuclear@21: { nuclear@21: del_tree(rb->root, rb->del, rb->del_cls); nuclear@21: } nuclear@21: nuclear@21: void rb_set_allocator(struct rbtree *rb, rb_alloc_func_t alloc, rb_free_func_t free) nuclear@21: { nuclear@21: rb->alloc = alloc; nuclear@21: rb->free = free; nuclear@21: } nuclear@21: nuclear@21: nuclear@21: void rb_set_compare_func(struct rbtree *rb, rb_cmp_func_t func) nuclear@21: { nuclear@21: rb->cmp = func; nuclear@21: } nuclear@21: nuclear@21: void rb_set_delete_func(struct rbtree *rb, rb_del_func_t func, void *cls) nuclear@21: { nuclear@21: rb->del = func; nuclear@21: rb->del_cls = cls; nuclear@21: } nuclear@21: nuclear@21: nuclear@21: void rb_clear(struct rbtree *rb) nuclear@21: { nuclear@21: del_tree(rb->root, rb->del, rb->del_cls); nuclear@21: rb->root = 0; nuclear@21: } nuclear@21: nuclear@21: int rb_copy(struct rbtree *dest, struct rbtree *src) nuclear@21: { nuclear@21: struct rbnode *node; nuclear@21: nuclear@21: rb_clear(dest); nuclear@21: rb_begin(src); nuclear@21: while((node = rb_next(src))) { nuclear@21: if(rb_insert(dest, node->key, node->data) == -1) { nuclear@21: return -1; nuclear@21: } nuclear@21: } nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: int rb_size(struct rbtree *rb) nuclear@21: { nuclear@21: return count_nodes(rb->root); nuclear@21: } nuclear@21: nuclear@21: int rb_insert(struct rbtree *rb, void *key, void *data) nuclear@21: { nuclear@21: rb->root = insert(rb, rb->root, key, data); nuclear@21: rb->root->red = 0; nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: int rb_inserti(struct rbtree *rb, int key, void *data) nuclear@21: { nuclear@21: rb->root = insert(rb, rb->root, INT2PTR(key), data); nuclear@21: rb->root->red = 0; nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: nuclear@21: int rb_delete(struct rbtree *rb, void *key) nuclear@21: { nuclear@21: rb->root = delete(rb, rb->root, key); nuclear@21: rb->root->red = 0; nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: int rb_deletei(struct rbtree *rb, int key) nuclear@21: { nuclear@21: rb->root = delete(rb, rb->root, INT2PTR(key)); nuclear@21: rb->root->red = 0; nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: nuclear@21: void *rb_find(struct rbtree *rb, void *key) nuclear@21: { nuclear@21: struct rbnode *node = rb->root; nuclear@21: nuclear@21: while(node) { nuclear@21: int cmp = rb->cmp(key, node->key); nuclear@21: if(cmp == 0) { nuclear@21: return node->data; nuclear@21: } nuclear@21: node = cmp < 0 ? node->left : node->right; nuclear@21: } nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: void *rb_findi(struct rbtree *rb, int key) nuclear@21: { nuclear@21: return rb_find(rb, INT2PTR(key)); nuclear@21: } nuclear@21: nuclear@21: nuclear@21: void rb_foreach(struct rbtree *rb, void (*func)(struct rbnode*, void*), void *cls) nuclear@21: { nuclear@21: traverse(rb->root, func, cls); nuclear@21: } nuclear@21: nuclear@21: nuclear@21: struct rbnode *rb_root(struct rbtree *rb) nuclear@21: { nuclear@21: return rb->root; nuclear@21: } nuclear@21: nuclear@21: void rb_begin(struct rbtree *rb) nuclear@21: { nuclear@21: rb->rstack = 0; nuclear@21: rb->iter = rb->root; nuclear@21: } nuclear@21: nuclear@21: #define push(sp, x) ((x)->next = (sp), (sp) = (x)) nuclear@21: #define pop(sp) ((sp) = (sp)->next) nuclear@21: #define top(sp) (sp) nuclear@21: nuclear@21: struct rbnode *rb_next(struct rbtree *rb) nuclear@21: { nuclear@21: struct rbnode *res = 0; nuclear@21: nuclear@21: while(rb->rstack || rb->iter) { nuclear@21: if(rb->iter) { nuclear@21: push(rb->rstack, rb->iter); nuclear@21: rb->iter = rb->iter->left; nuclear@21: } else { nuclear@21: rb->iter = top(rb->rstack); nuclear@21: pop(rb->rstack); nuclear@21: res = rb->iter; nuclear@21: rb->iter = rb->iter->right; nuclear@21: break; nuclear@21: } nuclear@21: } nuclear@21: return res; nuclear@21: } nuclear@21: nuclear@21: void *rb_node_key(struct rbnode *node) nuclear@21: { nuclear@21: return node ? node->key : 0; nuclear@21: } nuclear@21: nuclear@21: int rb_node_keyi(struct rbnode *node) nuclear@21: { nuclear@21: return node ? PTR2INT(node->key) : 0; nuclear@21: } nuclear@21: nuclear@21: void *rb_node_data(struct rbnode *node) nuclear@21: { nuclear@21: return node ? node->data : 0; nuclear@21: } nuclear@21: nuclear@21: static int cmpaddr(const void *ap, const void *bp) nuclear@21: { nuclear@21: return ap < bp ? -1 : (ap > bp ? 1 : 0); nuclear@21: } nuclear@21: nuclear@21: static int cmpint(const void *ap, const void *bp) nuclear@21: { nuclear@21: return PTR2INT(ap) - PTR2INT(bp); nuclear@21: } nuclear@21: nuclear@21: nuclear@21: /* ---- left-leaning 2-3 red-black implementation ---- */ nuclear@21: nuclear@21: /* helper prototypes */ nuclear@21: static int is_red(struct rbnode *tree); nuclear@21: static void color_flip(struct rbnode *tree); nuclear@21: static struct rbnode *rot_left(struct rbnode *a); nuclear@21: static struct rbnode *rot_right(struct rbnode *a); nuclear@21: static struct rbnode *find_min(struct rbnode *tree); nuclear@21: static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree); nuclear@21: /*static struct rbnode *move_red_right(struct rbnode *tree);*/ nuclear@21: static struct rbnode *move_red_left(struct rbnode *tree); nuclear@21: static struct rbnode *fix_up(struct rbnode *tree); nuclear@21: nuclear@21: static int count_nodes(struct rbnode *node) nuclear@21: { nuclear@21: if(!node) nuclear@21: return 0; nuclear@21: nuclear@21: return 1 + count_nodes(node->left) + count_nodes(node->right); nuclear@21: } nuclear@21: nuclear@21: static void del_tree(struct rbnode *node, rb_del_func_t delfunc, void *cls) nuclear@21: { nuclear@21: if(!node) nuclear@21: return; nuclear@21: nuclear@21: del_tree(node->left, delfunc, cls); nuclear@21: del_tree(node->right, delfunc, cls); nuclear@21: nuclear@21: if(delfunc) { nuclear@21: delfunc(node, cls); nuclear@21: } nuclear@21: free(node); nuclear@21: } nuclear@21: nuclear@21: static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data) nuclear@21: { nuclear@21: int cmp; nuclear@21: nuclear@21: if(!tree) { nuclear@21: struct rbnode *node = rb->alloc(sizeof *node); nuclear@21: node->red = 1; nuclear@21: node->key = key; nuclear@21: node->data = data; nuclear@21: node->left = node->right = 0; nuclear@21: return node; nuclear@21: } nuclear@21: nuclear@21: cmp = rb->cmp(key, tree->key); nuclear@21: nuclear@21: if(cmp < 0) { nuclear@21: tree->left = insert(rb, tree->left, key, data); nuclear@21: } else if(cmp > 0) { nuclear@21: tree->right = insert(rb, tree->right, key, data); nuclear@21: } else { nuclear@21: tree->data = data; nuclear@21: } nuclear@21: nuclear@21: /* fix right-leaning reds */ nuclear@21: if(is_red(tree->right)) { nuclear@21: tree = rot_left(tree); nuclear@21: } nuclear@21: /* fix two reds in a row */ nuclear@21: if(is_red(tree->left) && is_red(tree->left->left)) { nuclear@21: tree = rot_right(tree); nuclear@21: } nuclear@21: nuclear@21: /* if 4-node, split it by color inversion */ nuclear@21: if(is_red(tree->left) && is_red(tree->right)) { nuclear@21: color_flip(tree); nuclear@21: } nuclear@21: nuclear@21: return tree; nuclear@21: } nuclear@21: nuclear@21: static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key) nuclear@21: { nuclear@21: int cmp; nuclear@21: nuclear@21: if(!tree) { nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: cmp = rb->cmp(key, tree->key); nuclear@21: nuclear@21: if(cmp < 0) { nuclear@21: if(!is_red(tree->left) && !is_red(tree->left->left)) { nuclear@21: tree = move_red_left(tree); nuclear@21: } nuclear@21: tree->left = delete(rb, tree->left, key); nuclear@21: } else { nuclear@21: /* need reds on the right */ nuclear@21: if(is_red(tree->left)) { nuclear@21: tree = rot_right(tree); nuclear@21: } nuclear@21: nuclear@21: /* found it at the bottom (XXX what certifies left is null?) */ nuclear@21: if(cmp == 0 && !tree->right) { nuclear@21: if(rb->del) { nuclear@21: rb->del(tree, rb->del_cls); nuclear@21: } nuclear@21: rb->free(tree); nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: if(!is_red(tree->right) && !is_red(tree->right->left)) { nuclear@21: tree = move_red_left(tree); nuclear@21: } nuclear@21: nuclear@21: if(key == tree->key) { nuclear@21: struct rbnode *rmin = find_min(tree->right); nuclear@21: tree->key = rmin->key; nuclear@21: tree->data = rmin->data; nuclear@21: tree->right = del_min(rb, tree->right); nuclear@21: } else { nuclear@21: tree->right = delete(rb, tree->right, key); nuclear@21: } nuclear@21: } nuclear@21: nuclear@21: return fix_up(tree); nuclear@21: } nuclear@21: nuclear@21: /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key) nuclear@21: { nuclear@21: int cmp; nuclear@21: nuclear@21: if(!node) nuclear@21: return 0; nuclear@21: nuclear@21: if((cmp = rb->cmp(key, node->key)) == 0) { nuclear@21: return node; nuclear@21: } nuclear@21: return find(rb, cmp < 0 ? node->left : node->right, key); nuclear@21: }*/ nuclear@21: nuclear@21: static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls) nuclear@21: { nuclear@21: if(!node) nuclear@21: return; nuclear@21: nuclear@21: traverse(node->left, func, cls); nuclear@21: func(node, cls); nuclear@21: traverse(node->right, func, cls); nuclear@21: } nuclear@21: nuclear@21: /* helpers */ nuclear@21: nuclear@21: static int is_red(struct rbnode *tree) nuclear@21: { nuclear@21: return tree && tree->red; nuclear@21: } nuclear@21: nuclear@21: static void color_flip(struct rbnode *tree) nuclear@21: { nuclear@21: tree->red = !tree->red; nuclear@21: tree->left->red = !tree->left->red; nuclear@21: tree->right->red = !tree->right->red; nuclear@21: } nuclear@21: nuclear@21: static struct rbnode *rot_left(struct rbnode *a) nuclear@21: { nuclear@21: struct rbnode *b = a->right; nuclear@21: a->right = b->left; nuclear@21: b->left = a; nuclear@21: b->red = a->red; nuclear@21: a->red = 1; nuclear@21: return b; nuclear@21: } nuclear@21: nuclear@21: static struct rbnode *rot_right(struct rbnode *a) nuclear@21: { nuclear@21: struct rbnode *b = a->left; nuclear@21: a->left = b->right; nuclear@21: b->right = a; nuclear@21: b->red = a->red; nuclear@21: a->red = 1; nuclear@21: return b; nuclear@21: } nuclear@21: nuclear@21: static struct rbnode *find_min(struct rbnode *tree) nuclear@21: { nuclear@23: struct rbnode *node = tree; nuclear@21: nuclear@21: if(!tree) nuclear@21: return 0; nuclear@21: nuclear@21: while(node->left) { nuclear@21: node = node->left; nuclear@21: } nuclear@21: return node; nuclear@21: } nuclear@21: nuclear@21: static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree) nuclear@21: { nuclear@21: if(!tree->left) { nuclear@21: if(rb->del) { nuclear@21: rb->del(tree->left, rb->del_cls); nuclear@21: } nuclear@21: rb->free(tree->left); nuclear@21: return 0; nuclear@21: } nuclear@21: nuclear@21: /* make sure we've got red (3/4-nodes) at the left side so we can delete at the bottom */ nuclear@21: if(!is_red(tree->left) && !is_red(tree->left->left)) { nuclear@21: tree = move_red_left(tree); nuclear@21: } nuclear@21: tree->left = del_min(rb, tree->left); nuclear@21: nuclear@21: /* fix right-reds, red-reds, and split 4-nodes on the way up */ nuclear@21: return fix_up(tree); nuclear@21: } nuclear@21: nuclear@21: #if 0 nuclear@21: /* push a red link on this node to the right */ nuclear@21: static struct rbnode *move_red_right(struct rbnode *tree) nuclear@21: { nuclear@21: /* flipping it makes both children go red, so we have a red to the right */ nuclear@21: color_flip(tree); nuclear@21: nuclear@21: /* if after the flip we've got a red-red situation to the left, fix it */ nuclear@21: if(is_red(tree->left->left)) { nuclear@21: tree = rot_right(tree); nuclear@21: color_flip(tree); nuclear@21: } nuclear@21: return tree; nuclear@21: } nuclear@21: #endif nuclear@21: nuclear@21: /* push a red link on this node to the left */ nuclear@21: static struct rbnode *move_red_left(struct rbnode *tree) nuclear@21: { nuclear@21: /* flipping it makes both children go red, so we have a red to the left */ nuclear@21: color_flip(tree); nuclear@21: nuclear@21: /* if after the flip we've got a red-red on the right-left, fix it */ nuclear@21: if(is_red(tree->right->left)) { nuclear@21: tree->right = rot_right(tree->right); nuclear@21: tree = rot_left(tree); nuclear@21: color_flip(tree); nuclear@21: } nuclear@21: return tree; nuclear@21: } nuclear@21: nuclear@21: static struct rbnode *fix_up(struct rbnode *tree) nuclear@21: { nuclear@21: /* fix right-leaning */ nuclear@21: if(is_red(tree->right)) { nuclear@21: tree = rot_left(tree); nuclear@21: } nuclear@21: /* change invalid red-red pairs into a proper 4-node */ nuclear@21: if(is_red(tree->left) && is_red(tree->left->left)) { nuclear@21: tree = rot_right(tree); nuclear@21: } nuclear@21: /* split 4-nodes */ nuclear@21: if(is_red(tree->left) && is_red(tree->right)) { nuclear@21: color_flip(tree); nuclear@21: } nuclear@21: return tree; nuclear@21: }