/* insert a new node in the tree.
if there is already a node with a matching key in the tree
- we reurn an error
+ we replace it with the new data and return a pointer to the old data
+ in case the caller wants to take any special action
*/
-int
+void *
trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data)
{
trbt_node_t *node;
node = trbt_create_node(tree, NULL, key, data);
tree->tree=node;
- return 0;
+ return NULL;
}
/* it was not the new root so walk the tree until we find where to
* insert this new leaf.
*/
while(1){
- /* this node already exists, so just return an error */
+ /* this node already exists, replace data and return the
+ old data
+ */
if(key==node->key32){
- return -1;
+ void *old_data;
+
+ old_data = node->data;
+ node->data = talloc_steal(node, data);
+
+ return old_data;
}
if(key<node->key32) {
if(!node->left){
/* node will now point to the newly created node */
node->rb_color=TRBT_RED;
trbt_insert_case1(tree, node);
- return 0;
+ return NULL;
}
void *
-
+/* Create a RB tree */
trbt_tree_t *trbt_create(TALLOC_CTX *memctx);
+
+/* Lookup a node in the tree and return a pointer to data or NULL */
void *trbt_lookup32(trbt_tree_t *tree, uint32_t key);
-int trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data);
+
+/* Insert a new node into the tree. If there was already a node with this
+ key the pointer to the previous data is returned.
+ The tree will talloc_steal() the data inserted into the tree .
+*/
+void *trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data);
+
+/* Delete a node from the tree and free all data associated with it */
void trbt_delete32(trbt_tree_t *tree, uint32_t key);