void * bu_rb_search (struct bu_rb_tree *tree, int order, void *data) { int (*compare)(const void *, const void *); struct bu_rb_node *node; BU_CKMAG(tree, BU_RB_TREE_MAGIC, "red-black tree"); RB_CKORDER(tree, order); compare = RB_COMPARE_FUNC(tree, order); node = _rb_search(RB_ROOT(tree, order), order, compare, data); if (node == RB_NULL(tree)) return NULL; else return RB_DATA(node, order); }
/* * if key not in rbtree, alloc a node and insert * if key already in rbtree, insert into one of this node's 32-buckets * * return - 0 succeess * - -1 fail */ int rb_insert(_key_t key, ap_callback fn, void *arg, ap_key* retval) { struct rb_node *ptr = _rb_search(key); struct rb_node *tmp; int id = -1; int ret; if(NULL == ptr){ tmp = _rb_alloc_new(key, fn, arg); this_rb->root = tmp; goto out;//retval 0 tmp } if((ret = _rb_compare(key, ptr->key)) == 0){ //already in rbtree if(-1 == (id = _rb_node_add(ptr, fn, arg))){ return -1; } goto out;//retval id ptr }else{ tmp = _rb_alloc_new(key, fn, arg); if(ret > 0){ ptr->rb_right = tmp;//node->color == 0 red }else{ ptr->rb_left = tmp; } tmp->rb_parent = ptr; _rb_insert_fix(tmp); //retval 0 tmp } out: this_rb->root->color = 1; this_rb->num++; if(retval && -1 == id){ retval->i = 0; retval->node_ptr = tmp; }else if(retval){// retval->i = id; retval->node_ptr = ptr; } return 0; }
struct rb_node* rb_search(_key_t key) { struct rb_node *ptr = _rb_search(key); return _rb_compare(key, ptr->key) == 0 ? ptr : NULL; }