From 120db2838c80ccbb4ea674443a4706356d4e8856 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Fri, 20 May 2016 00:14:39 +0200 Subject: [PATCH] Use a real hashing function. --- src/hashtable-simple.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/hashtable-simple.c b/src/hashtable-simple.c index ac85a41..c2aaed9 100644 --- a/src/hashtable-simple.c +++ b/src/hashtable-simple.c @@ -15,7 +15,13 @@ typedef struct { } HASHTABLE; int hash_of_key(HASHTABLE* hashtable, char * key) { - return 0; + int val=0; + int idx=0; + while (key[idx] != '\0') { + val ^= key[idx]; + idx++; + } + return (val % hashtable->capacity); } HASHTABLE* hashtable_create(int capacity) { @@ -88,7 +94,7 @@ int hashtable_set(HASHTABLE* hashtable, char* key, void * value) { (void*)cell->value, (void*)cell->next); } - printf("hashtable_set -- done\n"); + printf("hashtable_set(...) -- done\n"); return 0; } @@ -98,6 +104,7 @@ void * hashtable_get(HASHTABLE* hashtable, char* key) { HASHCELL* cell; printf("hashtable_get(%p, %p) -- \n", hashtable, key); idx = hash_of_key(hashtable, key); + printf("hashtable_get(...) -- index [%d/%d]\n", idx, hashtable->capacity); cell = hashtable->data[idx]; while(cell) { printf("hashtable_get(...) -- cell@%p{key=%s, value=%p, next=%p}\n", @@ -134,6 +141,7 @@ int main(int argc, char** argv) { printf("h['charlie'] <- %d\n", z); hashtable_set(hashtable, "alice", &y); printf("h['alice'] <- %d\n", y); + value = hashtable_get(hashtable, "alice"); printf("h['alice'] ? %d\n", *value); value = hashtable_get(hashtable, "bob");