insert_at_head(& ht->buckets[bucket], & node->link);
}
-void
+bool
hash_table_replace(struct hash_table *ht, void *data, const void *key)
{
const unsigned hash_value = (*ht->hash)(key);
if ((*ht->compare)(hn->key, key) == 0) {
hn->data = data;
- return;
+ return true;
}
}
hn->key = key;
insert_at_head(& ht->buckets[bucket], & hn->link);
+ return false;
}
void
/**
* Add an element to a hash table with replacement
*
+ * \return
+ * 1 if it did replace the the value (in which case the old key is kept), 0 if
+ * it did not replace the value (in which case the new key is kept).
+ *
* \warning
* If \c key is already in the hash table, \c data will \b replace the most
* recently inserted \c data (see the warning in \c hash_table_insert) for
*
* \sa hash_table_insert
*/
-extern void hash_table_replace(struct hash_table *ht, void *data,
+extern int hash_table_replace(struct hash_table *ht, void *data,
const void *key);
/**
*/
void clear()
{
+ hash_table_call_foreach(this->ht, delete_key, NULL);
hash_table_clear(this->ht);
}
* because UINT_MAX+1 = 0.
*/
assert(value != UINT_MAX);
- hash_table_replace(this->ht,
- (void *) (intptr_t) (value + 1),
- strdup(key));
+ char *dup_key = strdup(key);
+ int result = hash_table_replace(this->ht,
+ (void *) (intptr_t) (value + 1),
+ dup_key);
+ if (result)
+ free(dup_key);
}
private: