rbtree.c: don't call delete_fn in rbtree_insert, return a value on duplicate keys instead.
for design reasons, it's more flexible to not use callbacks for this purpose.
This commit is contained in:
parent
7dcfd815ee
commit
ae3dec912a
2 changed files with 4 additions and 11 deletions
|
|
@ -39,8 +39,7 @@ Functions
|
||||||
`rbtree_insert()`::
|
`rbtree_insert()`::
|
||||||
|
|
||||||
Creates and inserts a new node in the tree. +
|
Creates and inserts a new node in the tree. +
|
||||||
If provided, calls `rbtree->delete_fn` if a node should be updated. +
|
Returns a positive value if the node was inserted, zero if the key already exists in the tree and a negative value if an error occured.
|
||||||
Returns nonzero if a new node was inserted or updated, zero otherwise.
|
|
||||||
|
|
||||||
NOTE: The memory pointed to by the 'key' pointer is *not* copied so you must ensure that it has a infinite lifetime.
|
NOTE: The memory pointed to by the 'key' pointer is *not* copied so you must ensure that it has a infinite lifetime.
|
||||||
|
|
||||||
|
|
|
||||||
12
src/rbtree.c
12
src/rbtree.c
|
|
@ -211,10 +211,10 @@ int rbtree_insert(rbtree *tree, const void *key) {
|
||||||
int dir = 0, last = 0;
|
int dir = 0, last = 0;
|
||||||
|
|
||||||
if (!tree || !tree->cmp_fn)
|
if (!tree || !tree->cmp_fn)
|
||||||
return 0;
|
return -1;
|
||||||
|
|
||||||
if (!tree->root) {
|
if (!tree->root) {
|
||||||
tree->root = node_alloc(key);
|
tree->root = q = node_alloc(key);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -259,12 +259,6 @@ int rbtree_insert(rbtree *tree, const void *key) {
|
||||||
q = q->child[dir];
|
q = q->child[dir];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (q->key != key) {
|
|
||||||
if (tree->delete_fn)
|
|
||||||
tree->delete_fn((void*)q->key);
|
|
||||||
q->key = key;
|
|
||||||
}
|
|
||||||
|
|
||||||
tree->root = head.child[1];
|
tree->root = head.child[1];
|
||||||
|
|
||||||
done:
|
done:
|
||||||
|
|
@ -275,7 +269,7 @@ done:
|
||||||
rb_assert(tree);
|
rb_assert(tree);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 1;
|
return q && q->key == key;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* rbtree_delete(rbtree *tree, const void *key) {
|
void* rbtree_delete(rbtree *tree, const void *key) {
|
||||||
|
|
|
||||||
Reference in a new issue