Commit 077376be5ef773f4c274583548e3df051892f31f
1 parent
309ecbaf
don't replace tree nodes on insert but return the existing one. Note that this i…
…s different from the behaviour of tsearch which is the POSIX equivalent of this function, but in various situations it is much more handy.
Showing
1 changed file
with
8 additions
and
7 deletions
@@ -44,13 +44,14 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -44,13 +44,14 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
44 | TR_TREE_FIND(node, search, found, comp); | 44 | TR_TREE_FIND(node, search, found, comp); |
45 | 45 | ||
46 | if (found == 0) { | 46 | if (found == 0) { |
47 | - // we found an element | ||
48 | - // as this is insert and not find this will overwrite an existing | ||
49 | - // value, but return the previous one so that it can be freed if | ||
50 | - // neccessary. | ||
51 | - void * data = node->data; | ||
52 | - node->data = (void *)search; | ||
53 | - return data; | 47 | + // This differs from tsearch, which is the posix equivalent to |
48 | + // this function in that it will not replace an existing value. | ||
49 | + // If there is a value for the given key in the tree it will be | ||
50 | + // retured. It is then up to the caller to handle the situation. | ||
51 | + // This is used in my http header code to handle multiple value | ||
52 | + // per header. There it is more useful to get the already existing | ||
53 | + // tree entry and add the new data. | ||
54 | + return node->data; | ||
54 | } else { | 55 | } else { |
55 | // not found | 56 | // not found |
56 | if (0 < found) { | 57 | if (0 < found) { |
Please
register
or
login
to post a comment