From 320c747c2dffa65feb6ee15d25ec0be70c7896fb Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 17 Mar 2018 16:53:46 -0700 Subject: [PATCH] Add a get function that can help handle duplicate key entries. --- include/ht.h | 10 ++++++++++ src/ht.c | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/ht.h b/include/ht.h index 73b669b..50e7ed0 100644 --- a/include/ht.h +++ b/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. diff --git a/src/ht.c b/src/ht.c index ba3d6e9..8ff07ac 100644 --- a/src/ht.c +++ b/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;