From 07cedee4865612646016b4ff71a9953e77a378d4 Mon Sep 17 00:00:00 2001 From: Sebastian Dransfeld Date: Sun, 25 Mar 2007 16:44:11 +0000 Subject: [PATCH] Add a function to find a value in a hash. SVN revision: 29129 --- legacy/ecore/src/lib/ecore/Ecore_Data.h | 1 + legacy/ecore/src/lib/ecore/ecore_hash.c | 34 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/legacy/ecore/src/lib/ecore/Ecore_Data.h b/legacy/ecore/src/lib/ecore/Ecore_Data.h index dc265840ac..38df41d366 100644 --- a/legacy/ecore/src/lib/ecore/Ecore_Data.h +++ b/legacy/ecore/src/lib/ecore/Ecore_Data.h @@ -277,6 +277,7 @@ extern "C" { EAPI int ecore_hash_set(Ecore_Hash *hash, void *key, void *value); EAPI int ecore_hash_set_hash(Ecore_Hash *hash, Ecore_Hash *set); EAPI void *ecore_hash_remove(Ecore_Hash *hash, const void *key); + EAPI void *ecore_hash_find(Ecore_Hash *hash, Ecore_Compare_Cb compare, const void *value); EAPI void ecore_hash_dump_graph(Ecore_Hash *hash); EAPI void ecore_hash_dump_stats(Ecore_Hash *hash); diff --git a/legacy/ecore/src/lib/ecore/ecore_hash.c b/legacy/ecore/src/lib/ecore/ecore_hash.c index 753cc67083..cbf6108a51 100644 --- a/legacy/ecore/src/lib/ecore/ecore_hash.c +++ b/legacy/ecore/src/lib/ecore/ecore_hash.c @@ -537,6 +537,40 @@ ecore_hash_remove(Ecore_Hash *hash, const void *key) return ret; } +/** + * Retrieves the first value that matches + * table. + * @param hash The given hash table. + * @param key The key to search for. + * @return The value corresponding to key on success, @c NULL otherwise. + * @ingroup Ecore_Data_Hash_ADT_Data_Group + */ +EAPI void * +ecore_hash_find(Ecore_Hash *hash, Ecore_Compare_Cb compare, const void *value) +{ + unsigned int i = 0; + + CHECK_PARAM_POINTER_RETURN("hash", hash, NULL); + CHECK_PARAM_POINTER_RETURN("compare", compare, NULL); + CHECK_PARAM_POINTER_RETURN("value", value, NULL); + + while (i < ecore_prime_table[hash->size]) + { + if (hash->buckets[i]) + { + Ecore_Hash_Node *node; + + for (node = hash->buckets[i]; node; node = node->next) + { + if (!compare(node->value, value)) return node->value; + } + } + i++; + } + + return NULL; +} + /* * @brief Retrieve the node associated with key * @param hash: the hash table to search for the key