From 2a3c11e2024395f385f6c5ae70337e0442fb683c Mon Sep 17 00:00:00 2001 From: David Maciejak Date: Tue, 2 Sep 2014 11:17:31 +0700 Subject: [PATCH] WINGs: merge hashtable duplicate code This patch is adding a new static function hashGetItem to factorize some code. Signed-off-by: Carlos R. Mafra --- WINGs/hashtable.c | 50 ++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/WINGs/hashtable.c b/WINGs/hashtable.c index c4c82144..2620c784 100644 --- a/WINGs/hashtable.c +++ b/WINGs/hashtable.c @@ -154,7 +154,7 @@ unsigned WMCountHashTable(WMHashTable * table) return table->itemCount; } -void *WMHashGet(WMHashTable * table, const void *key) +static HashItem *hashGetItem(WMHashTable *table, const void *key) { unsigned h; HashItem *item; @@ -177,44 +177,32 @@ void *WMHashGet(WMHashTable * table, const void *key) item = item->next; } } - if (item) - return (void *)item->data; - else + return item; +} + +void *WMHashGet(WMHashTable * table, const void *key) +{ + HashItem *item; + + item = hashGetItem(table, key); + if (!item) return NULL; + return (void *)item->data; } Bool WMHashGetItemAndKey(WMHashTable * table, const void *key, void **retItem, void **retKey) { - unsigned h; HashItem *item; - h = HASH(table, key); - item = table->table[h]; - - if (table->callbacks.keyIsEqual) { - while (item) { - if ((*table->callbacks.keyIsEqual) (key, item->key)) { - break; - } - item = item->next; - } - } else { - while (item) { - if (key == item->key) { - break; - } - item = item->next; - } - } - if (item) { - if (retKey) - *retKey = (void *)item->key; - if (retItem) - *retItem = (void *)item->data; - return True; - } else { + item = hashGetItem(table, key); + if (!item) return False; - } + + if (retKey) + *retKey = (void *)item->key; + if (retItem) + *retItem = (void *)item->data; + return True; } void *WMHashInsert(WMHashTable * table, const void *key, const void *data)