mirror of
https://github.com/samba-team/samba.git
synced 2025-03-27 22:50:26 +03:00
when inserting data in the tree, if there was already a node with the
same key then replace the data in the node with the new data and return the pointer to the previous data held in the node. this allows a caller to avoid having to first check if a node already exists before inserting a possibly duplicate/colliding entry and lets the caller do whatever it needs to do after the fact. (This used to be ctdb commit 6634cabb910c26400780d51727ff2d1ba5e16e36)
This commit is contained in:
parent
a85054adcf
commit
49f0317b21
@ -530,9 +530,10 @@ trbt_create_node(trbt_tree_t *tree, trbt_node_t *parent, uint32_t key, void *dat
|
||||
|
||||
/* 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;
|
||||
@ -544,16 +545,23 @@ trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data)
|
||||
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){
|
||||
@ -587,7 +595,7 @@ trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data)
|
||||
/* node will now point to the newly created node */
|
||||
node->rb_color=TRBT_RED;
|
||||
trbt_insert_case1(tree, node);
|
||||
return 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *
|
||||
|
@ -37,10 +37,19 @@ typedef struct _trbt_tree_t {
|
||||
|
||||
|
||||
|
||||
|
||||
/* 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);
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user