Browse Source

don't replace tree nodes on insert but return the existing one. Note that this is different from the behaviour of tsearch which is the POSIX equivalent of this function, but in various situations it is much more handy.

1.0.0
Georg Hopp 12 years ago
parent
commit
077376be5e
  1. 15
      src/tree/insert.c

15
src/tree/insert.c

@ -44,13 +44,14 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp)
TR_TREE_FIND(node, search, found, comp);
if (found == 0) {
// we found an element
// as this is insert and not find this will overwrite an existing
// value, but return the previous one so that it can be freed if
// neccessary.
void * data = node->data;
node->data = (void *)search;
return data;
// This differs from tsearch, which is the posix equivalent to
// this function in that it will not replace an existing value.
// If there is a value for the given key in the tree it will be
// retured. It is then up to the caller to handle the situation.
// This is used in my http header code to handle multiple value
// per header. There it is more useful to get the already existing
// tree entry and add the new data.
return node->data;
} else {
// not found
if (0 < found) {

Loading…
Cancel
Save