From a2c8decf85ab93e515f23d5d9110763b8bbba218 Mon Sep 17 00:00:00 2001
From: Orestis <orestis.malaspinas@pm.me>
Date: Wed, 19 Jan 2022 07:46:06 +0100
Subject: [PATCH] with function pointer+

---
 examples/hashmap/hm.c | 54 +++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/examples/hashmap/hm.c b/examples/hashmap/hm.c
index 97c1303..0490034 100644
--- a/examples/hashmap/hm.c
+++ b/examples/hashmap/hm.c
@@ -19,27 +19,22 @@ static int rehash(char *key) {
     return 1;
 }
 
-static int find_index(hm h, char key[MAX_LEN], bool insert) {
+static bool search_condition(hm h, char key[MAX_LEN], int index) {
+    return strncmp(h.table[index].key, key, MAX_LEN) == 0 &&
+           h.table[index].state == occupied;
+}
+
+static bool set_condition(hm h, char key[MAX_LEN], int index) {
+    return search_condition(h, key, index) || h.table[index].state != occupied;
+}
+
+static int find_index(
+    hm h, char key[MAX_LEN], bool (*condition)(hm, char[], int)) {
     int try   = 0;
     int index = hash(h, key);
     while (try < h.capacity) {
-        switch (h.table[index].state) {
-            case occupied:
-                if (strncmp(h.table[index].key, key, MAX_LEN) == 0) {
-                    return index;
-                }
-                break;
-            case empty:
-                return index;
-                break;
-            case deleted:
-                if (insert) {
-                    return index;
-                }
-                break;
-            default:
-                return -1;
-                break;
+        if (condition(h, key, index)) {
+            return index;
         }
         index = (index + rehash(key)) % h.capacity;
         try += 1;
@@ -72,7 +67,7 @@ bool hm_set(hm *h, char *key, char *value) {
         return false;
     }
 
-    int index = find_index(*h, key, true);
+    int index = find_index(*h, key, set_condition);
     if (index < 0) {
         return false;
     }
@@ -85,16 +80,16 @@ bool hm_set(hm *h, char *key, char *value) {
 }
 
 bool hm_get(hm h, char *key, char *value) {
-    int index = find_index(h, key, false);
-    if (index >= 0 && h.table[index].state == occupied) {
+    int index = find_index(h, key, search_condition);
+    if (index >= 0) {
         strncpy(value, h.table[index].value, MAX_LEN);
         return true;
     }
     return false;
 }
 bool hm_remove(hm *h, char *key, char *value) {
-    int index = find_index(*h, key, false);
-    if (index >= 0 && h->table[index].state == occupied) {
+    int index = find_index(*h, key, search_condition);
+    if (index >= 0) {
         h->table[index].state = deleted;
         strncpy(value, h->table[index].value, MAX_LEN);
         h->size -= 1;
@@ -104,8 +99,8 @@ bool hm_remove(hm *h, char *key, char *value) {
 }
 
 bool hm_search(hm h, char *key) {
-    int index = find_index(h, key, false);
-    return (index >= 0 && h.table[index].state == occupied);
+    int index = find_index(h, key, search_condition);
+    return (index >= 0);
 }
 
 void hm_print(hm h) {
@@ -116,8 +111,13 @@ void hm_print(hm h) {
         printf("The hashmap is empty.\n");
     }
     for (int i = 0; i < h.capacity; ++i) {
-        printf("index: %d, key: %s, value: %s\n", i, h.table[i].key,
-            h.table[i].value);
+        if (h.table[i].state == occupied) {
+            printf("index: %d, key: %s, value: %s\n", i, h.table[i].key,
+                h.table[i].value);
+
+        } else {
+            printf("index: %d, key: {none}, value: {none}\n", i);
+        }
     }
 }
 
-- 
GitLab