Add a get function that can help handle duplicate key entries.

This commit is contained in:
Danila Fedorin 2018-03-17 16:53:46 -07:00
parent c318aae32c
commit 320c747c2d
2 changed files with 25 additions and 0 deletions

View File

@ -125,6 +125,16 @@ libds_result ht_put(ht* ht, const void* key, void* value);
* @return the data, or NULL if it is not found. * @return the data, or NULL if it is not found.
*/ */
void* ht_get(ht* ht, const void* key); void* ht_get(ht* ht, const void* key);
/**
* Retreives the first value from the hash table
* that's stored with the given key and passes the compare function.
* @param ht the hash table to retreive the value from.
* @param key the key to use to find the data.
* @param data the data to compare the candidate value to.
* @param compare the comparison function used to compare adta.
* @return the data, or NULL if it is not found.
*/
void* ht_get_filter(ht* ht, const void* key, void* data, compare_func compare);
/** /**
* Removes a value from the hash table. * Removes a value from the hash table.
* @param ht the hash table to remove a value from. * @param ht the hash table to remove a value from.

View File

@ -84,6 +84,21 @@ void* ht_get(ht* ht, const void* key) {
return data; return data;
} }
void* ht_get_filter(ht* ht, const void* key, void* data, compare_func compare) {
void* found = NULL;
ht_node* search_node;
unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
search_node = ht->data[key_int];
while (search_node && found == NULL) {
if (ht->cmp_func(search_node->key, key) &&
compare(data, search_node->data)) {
found = search_node->data;
}
search_node = search_node->next;
}
return found;
}
void ht_remove(ht* ht, const void* key) { void ht_remove(ht* ht, const void* key) {
ht_node** search_ptr; ht_node** search_ptr;
unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE; unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;