예제 #1
0
파일: c_rb.c 프로젝트: AmeyaVS/cstl
clib_error  
insert_c_rb(struct clib_rb* pTree, void* k, size_t key_size, void* v, size_t value_size) {

    clib_error rc = CLIB_ERROR_SUCCESS;
    struct clib_rb_node* x;
	struct clib_rb_node* y;
	struct clib_rb_node* z;

    x = (struct clib_rb_node*)malloc (sizeof(struct clib_rb_node));
    if ( x == (struct clib_rb_node*)0  ) 
        return CLIB_ERROR_MEMORY;

    x->left    = rb_sentinel;
    x->right   = rb_sentinel;
    x->color   = clib_red;

    x->key     = new_clib_object ( k, key_size );
    if ( v ) {
        x->value   = new_clib_object ( v, value_size );
    } else {
        x->value =  (struct clib_object*)0;
    }

    y = pTree->root;
    z = (struct clib_rb_node*)0 ;

    while (y != rb_sentinel) {
        int c = 0;
        void* cur_key;
		void* new_key;

        get_raw_clib_object ( y->key, &cur_key );
        get_raw_clib_object ( x->key, &new_key );

        c = (pTree->compare_fn) ( new_key , cur_key);
        free ( cur_key );
        free ( new_key );
        if (c == 0) {
            /* TODO : Delete node here */
            return CLIB_RBTREE_KEY_DUPLICATE;
        }
        z = y;
        if ( c < 0 )
            y = y->left;
        else
            y = y->right;
    }    
    x->parent = z;
    if (z) {
        int c = 0;
        void* cur_key;
		void* new_key;
        get_raw_clib_object ( z->key, &cur_key );
        get_raw_clib_object ( x->key, &new_key );

        c = pTree->compare_fn( new_key, cur_key);
        free ( cur_key );
        free ( new_key );
        if (c < 0) {
            z->left = x;
        } else {
            z->right = x;
        }
    }
    else
        pTree->root = x;

    __rb_insert_fixup (pTree, x);

    debug_verify_properties ( pTree);
    return rc;
}
예제 #2
0
	void insert(const key_type& key, const mapped_type& value) {
		rb_tree_node* node = __buy_node(key, value);
		__bst_insert(node);
		node->color = red;
		__rb_insert_fixup(node);
	}