/* Inserts |item| into |table|, replacing any duplicate item. Returns |NULL| if |item| was inserted without replacing a duplicate, or if a memory allocation error occurred. Otherwise, returns the item that was replaced. */ void * tavl_replace (struct tavl_table *table, void *item) { void **p = tavl_probe (table, item); if (p == NULL || *p == item) return NULL; else { void *r = *p; *p = item; return r; } }
/* Attempts to insert |item| into |tree|. If |item| is inserted successfully, it is returned and |trav| is initialized to its location. If a duplicate is found, it is returned and |trav| is initialized to its location. No replacement of the item occurs. If a memory allocation failure occurs, |NULL| is returned and |trav| is initialized to the null item. */ void *tavl_t_insert(struct tavl_traverser *trav, struct tavl_table *tree, void *item) { void **p; assert(trav != NULL && tree != NULL && item != NULL); p = tavl_probe(tree, item); if (p != NULL) { trav->tavl_table = tree; trav->tavl_node = ((struct tavl_node *) ((char *)p - offsetof(struct tavl_node, tavl_data))); return *p; }
/* Inserts |item| into |table|. Returns |NULL| if |item| was successfully inserted or if a memory allocation error occurred. Otherwise, returns the duplicate item. */ void *tavl_insert(struct tavl_table *table, void *item) { void **p = tavl_probe(table, item); return p == NULL || *p == item ? NULL : *p; }