diff --git a/docs/technical/rbtree.txt b/docs/technical/rbtree.txt index 36685a4..9d22f55 100644 --- a/docs/technical/rbtree.txt +++ b/docs/technical/rbtree.txt @@ -39,8 +39,7 @@ Functions `rbtree_insert()`:: Creates and inserts a new node in the tree. + - If provided, calls `rbtree->delete_fn` if a node should be updated. + - Returns nonzero if a new node was inserted or updated, zero otherwise. + 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. NOTE: The memory pointed to by the 'key' pointer is *not* copied so you must ensure that it has a infinite lifetime. diff --git a/src/rbtree.c b/src/rbtree.c index 4634662..f2acb2c 100644 --- a/src/rbtree.c +++ b/src/rbtree.c @@ -211,10 +211,10 @@ int rbtree_insert(rbtree *tree, const void *key) { int dir = 0, last = 0; if (!tree || !tree->cmp_fn) - return 0; + return -1; if (!tree->root) { - tree->root = node_alloc(key); + tree->root = q = node_alloc(key); goto done; } @@ -259,12 +259,6 @@ int rbtree_insert(rbtree *tree, const void *key) { 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]; done: @@ -275,7 +269,7 @@ done: rb_assert(tree); #endif - return 1; + return q && q->key == key; } void* rbtree_delete(rbtree *tree, const void *key) {