Ejemplo n.º 1
0
static void btree_insert_node(ACL_BTREE *tree, BTREE_NODE *z)
{
	const char *myname = "btree_insert_node";
	BTREE_NODE *x, *y;

	btree_validate(tree);
	y = NULL;
	x = tree->root;
	while (x != NULL) {
		y = x;
		if (z->key == x->key)
			acl_msg_fatal("%s(%d): key(%u) exist",
				myname, __LINE__, z->key);
		if (z->key < x->key) {
			x = x->left;
		} else {
			x = x->right;
		}
	}

	z->parent = y;
	if (y == NULL) {
		tree->root = z;
	} else if (z->key < y->key) {
		y->left = z;
	} else {
		y->right = z;
	}
	tree->count++;
	btree_validate(tree);
}
Ejemplo n.º 2
0
static void
btree_insert_node(btree_t *tree, btree_node_t *z) {
    btree_node_t *x, *y;

    btree_validate(tree);
    y = NULL;
    x = tree->root;
    while (x != NULL) {
        y = x;
        ASSERT(z->key != x->key);
        if (z->key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    }

    z->parent = y;
    if (y == NULL) {
        tree->root = z;
    } else if (z->key < y->key) {
        y->left = z;
    } else {
        y->right = z;
    }
    tree->count++;
    btree_validate(tree);
}
Ejemplo n.º 3
0
int
btree_remove(btree_t *tree, uint32_t key, void **data)
{
    btree_node_t *x;

    btree_validate(tree);
    x = btree_search(tree->root, key);
    if (x == NULL) {
        debug_msg("Item not on tree - key %ul\n", key);
        *data = NULL;
        return FALSE;
    }

    /* Note value that gets freed is not necessarily the the same
     * as node that gets removed from tree since there is an
     * optimization to avoid pointer updates in tree which means
     * sometimes we just copy key and data from one node to
     * another.
     */

    *data = x->data;
    x = btree_delete_node(tree, x);
    xfree(x);

    return TRUE;
}
Ejemplo n.º 4
0
void *acl_btree_remove(ACL_BTREE *tree, unsigned int key)
{
	const char *myname = "acl_btree_remove";
	BTREE_NODE *x;
	void *data;

	btree_validate(tree);
	x = btree_search(tree->root, key);
	if (x == NULL) {
		acl_msg_error("%s(%d): Item not on tree - key %u\n",
			myname, __LINE__, key);
		return (NULL);
	}

	/* Note value that gets freed is not necessarily the the same
	 * as node that gets removed from tree since there is an
	 * optimization to avoid pointer updates in tree which means
	 * sometimes we just copy key and data from one node to another.  
	 */

	data = x->data;
	x = btree_delete_node(tree, x);
	acl_slice_free2(tree->slice, x);

	return (data);
}
Ejemplo n.º 5
0
void *acl_btree_find(ACL_BTREE *tree, unsigned int key)
{
	BTREE_NODE *x;

	btree_validate(tree);
	x = btree_search(tree->root, key);
	if (x != NULL) {
		return (x->data);
	}
	return (NULL);
}
Ejemplo n.º 6
0
int
btree_find(btree_t *tree, uint32_t key, void **d)
{
    btree_node_t *x;

    btree_validate(tree);
    x = btree_search(tree->root, key);
    if (x != NULL) {
        *d = x->data;
        return TRUE;
    }
    return FALSE;
}
Ejemplo n.º 7
0
static btree_node_t*
btree_delete_node(btree_t *tree, btree_node_t *z)
{
    btree_node_t *x, *y;

    btree_validate(tree);
    if (z->left == NULL || z->right == NULL) {
        y = z;
    } else {
        y = btree_successor(z);
    }

    if (y->left != NULL) {
        x = y->left;
    } else {
        x = y->right;
    }

    if (x != NULL) {
        x->parent = y->parent;
    }

    if (y->parent == NULL) {
        tree->root = x;
    } else if (y == y->parent->left) {
        y->parent->left = x;
    } else {
        y->parent->right = x;
    }

    z->key  = y->key;
    z->data = y->data;

    tree->count--;

    btree_validate(tree);
    return y;
}
Ejemplo n.º 8
0
int acl_btree_destroy(ACL_BTREE *tree)
{
	const char *myname = "acl_btree_destroy";

	btree_validate(tree);
	if (tree->root != NULL) {
		acl_msg_error("%s(%d): Tree not empty", myname, __LINE__);
		return (-1);
	}

	acl_slice_destroy(tree->slice);
	acl_myfree(tree);
	return (0);
}
Ejemplo n.º 9
0
static BTREE_NODE *btree_delete_node(ACL_BTREE *tree, BTREE_NODE *z)
{
	BTREE_NODE *x, *y;

	btree_validate(tree);
	if (z->left == NULL || z->right == NULL) {
		y = z;
	} else {
		y = btree_successor(z);
	}

	if (y->left != NULL) {
		x = y->left;
	} else {
		x = y->right;
	}

	if (x != NULL) {
		x->parent = y->parent;
	}

	if (y->parent == NULL) {
		tree->root = x;
	} else if (y == y->parent->left) {
		y->parent->left = x;
	} else {
		y->parent->right = x;
	}

	z->key  = y->key;
	z->data = y->data;

	tree->count--;

	btree_validate(tree);
	return (y);
}
Ejemplo n.º 10
0
int
btree_destroy(btree_t **tree)
{
    btree_t *t = *tree;

    btree_validate(t);
    if (t->root != NULL) {
        debug_msg("Tree not empty - cannot destroy\n");
        return FALSE;
    }

    xfree(t);
    *tree = NULL;
    return TRUE;
}
Ejemplo n.º 11
0
int acl_btree_get_max_key(ACL_BTREE *tree, unsigned int *key)
{
	BTREE_NODE *x;

	btree_validate(tree);
	if (tree->root == NULL) {
		return (-1);
	}

	x = btree_max(tree->root);
	if (x == NULL) {
		return (-1);
	}
        
	*key = x->key;
	return (0);
}
Ejemplo n.º 12
0
int
btree_get_max_key(btree_t *tree, uint32_t *key)
{
    btree_node_t *x;

    btree_validate(tree);
    if (tree->root == NULL) {
        return FALSE;
    }

    x = btree_max(tree->root);
    if (x == NULL) {
        return FALSE;
    }

    *key = x->key;
    return TRUE;
}
Ejemplo n.º 13
0
int
btree_get_next_key(btree_t *tree, uint32_t cur_key, uint32_t *next_key)
{
    btree_node_t *x;

    btree_validate(tree);
    x = btree_search(tree->root, cur_key);
    if (x == NULL) {
        return FALSE;
    }

    x = btree_successor(x);
    if (x == NULL) {
        return FALSE;
    }

    *next_key = x->key;
    return TRUE;
}
Ejemplo n.º 14
0
int acl_btree_get_next_key(ACL_BTREE *tree,
	unsigned int cur_key, unsigned int *next_key)
{
	BTREE_NODE *x;

	btree_validate(tree);
	x = btree_search(tree->root, cur_key);
	if (x == NULL) {
		return (-1);
	}
        
	x = btree_successor(x);
	if (x == NULL) {
		return (-1);
	}
        
	*next_key = x->key;
	return (0);
}
Ejemplo n.º 15
0
int
btree_add(btree_t *tree, uint32_t key, void *data)
{
    btree_node_t *x;

    btree_validate(tree);
    x = btree_search(tree->root, key);
    if (x != NULL) {
        debug_msg("Item already exists - key %ul\n", key);
        return FALSE;
    }

    x = (btree_node_t *)xmalloc(sizeof(btree_node_t));
    x->key    = key;
    x->data   = data;
    x->parent = NULL;
    x->left   = NULL;
    x->right  = NULL;
    x->magic  = BTREE_NODE_MAGIC;
    btree_insert_node(tree, x);

    return TRUE;
}
Ejemplo n.º 16
0
int acl_btree_add(ACL_BTREE *tree, unsigned int key, void *data)
{
	const char *myname = "acl_btree_add";
	BTREE_NODE *x;

	btree_validate(tree);
	x = btree_search(tree->root, key);
	if (x != NULL) {
		acl_msg_error("%s(%d): Item already exists - key %u",
			myname, __LINE__, key);
		return (-1);
	}

	x = (BTREE_NODE *) acl_slice_alloc(tree->slice);
	x->key = key;
	x->data   = data;
	x->parent = NULL;
	x->left   = NULL;
	x->right  = NULL;
	x->magic  = BTREE_NODE_MAGIC;
	btree_insert_node(tree, x);

	return (0);
}