Add a get function that can help handle duplicate key entries.
This commit is contained in:
parent
c318aae32c
commit
320c747c2d
10
include/ht.h
10
include/ht.h
|
@ -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.
|
||||
*/
|
||||
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.
|
||||
* @param ht the hash table to remove a value from.
|
||||
|
|
15
src/ht.c
15
src/ht.c
|
@ -84,6 +84,21 @@ void* ht_get(ht* ht, const void* key) {
|
|||
|
||||
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) {
|
||||
ht_node** search_ptr;
|
||||
unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
|
||||
|
|
Loading…
Reference in New Issue
Block a user