From 721ef4b6a93a01e59b80f34d40cc651a73c78ac4 Mon Sep 17 00:00:00 2001
From: David Maciejak <david.maciejak@gmail.com>
Date: Tue, 2 Sep 2014 11:17:31 +0700
Subject: [PATCH 1/3] WINGs: merge hashtable duplicate code

This patch is adding a new static function hashGetItem to factorize some code.
---
 WINGs/hashtable.c | 50 +++++++++++++++++++-------------------------------
 1 file changed, 19 insertions(+), 31 deletions(-)

diff --git a/WINGs/hashtable.c b/WINGs/hashtable.c
index c4c8214..2620c78 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)
-- 
1.8.3.2

