Skip to content
Snippets Groups Projects
Verified Commit a2c8decf authored by orestis.malaspin's avatar orestis.malaspin
Browse files

with function pointer+

parent 9db7d84a
No related branches found
No related tags found
No related merge requests found
...@@ -19,27 +19,22 @@ static int rehash(char *key) { ...@@ -19,27 +19,22 @@ static int rehash(char *key) {
return 1; 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 try = 0;
int index = hash(h, key); int index = hash(h, key);
while (try < h.capacity) { while (try < h.capacity) {
switch (h.table[index].state) { if (condition(h, key, index)) {
case occupied: return index;
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;
} }
index = (index + rehash(key)) % h.capacity; index = (index + rehash(key)) % h.capacity;
try += 1; try += 1;
...@@ -72,7 +67,7 @@ bool hm_set(hm *h, char *key, char *value) { ...@@ -72,7 +67,7 @@ bool hm_set(hm *h, char *key, char *value) {
return false; return false;
} }
int index = find_index(*h, key, true); int index = find_index(*h, key, set_condition);
if (index < 0) { if (index < 0) {
return false; return false;
} }
...@@ -85,16 +80,16 @@ bool hm_set(hm *h, char *key, char *value) { ...@@ -85,16 +80,16 @@ bool hm_set(hm *h, char *key, char *value) {
} }
bool hm_get(hm h, char *key, char *value) { bool hm_get(hm h, char *key, char *value) {
int index = find_index(h, key, false); int index = find_index(h, key, search_condition);
if (index >= 0 && h.table[index].state == occupied) { if (index >= 0) {
strncpy(value, h.table[index].value, MAX_LEN); strncpy(value, h.table[index].value, MAX_LEN);
return true; return true;
} }
return false; return false;
} }
bool hm_remove(hm *h, char *key, char *value) { bool hm_remove(hm *h, char *key, char *value) {
int index = find_index(*h, key, false); int index = find_index(*h, key, search_condition);
if (index >= 0 && h->table[index].state == occupied) { if (index >= 0) {
h->table[index].state = deleted; h->table[index].state = deleted;
strncpy(value, h->table[index].value, MAX_LEN); strncpy(value, h->table[index].value, MAX_LEN);
h->size -= 1; h->size -= 1;
...@@ -104,8 +99,8 @@ bool hm_remove(hm *h, char *key, char *value) { ...@@ -104,8 +99,8 @@ bool hm_remove(hm *h, char *key, char *value) {
} }
bool hm_search(hm h, char *key) { bool hm_search(hm h, char *key) {
int index = find_index(h, key, false); int index = find_index(h, key, search_condition);
return (index >= 0 && h.table[index].state == occupied); return (index >= 0);
} }
void hm_print(hm h) { void hm_print(hm h) {
...@@ -116,8 +111,13 @@ void hm_print(hm h) { ...@@ -116,8 +111,13 @@ void hm_print(hm h) {
printf("The hashmap is empty.\n"); printf("The hashmap is empty.\n");
} }
for (int i = 0; i < h.capacity; ++i) { for (int i = 0; i < h.capacity; ++i) {
printf("index: %d, key: %s, value: %s\n", i, h.table[i].key, if (h.table[i].state == occupied) {
h.table[i].value); 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);
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment