Commit ff6613d

mo khan <mo.khan@gmail.com>
2020-08-03 20:27:51
Store Tuples in the hash
1 parent e291a2c
src/02/04/hash.c
@@ -1,53 +1,44 @@
 #include "hash.h"
+#include "tuple.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-static int to_hash(int key)
+Hash *hash_init(int size)
 {
-  return key % 13;
+  Hash *hash = malloc(sizeof(Hash));
+  hash->size = size;
+  hash->buckets = calloc(size, sizeof(Node));
+  return hash;
 }
 
-Hash *hash_init(int buckets)
+int hash_index(Hash *hash, int key)
 {
-  Hash *hash = malloc(sizeof(Hash));
-  /*hash->head = node_initialize(NULL);*/
-  /*Node *current = hash->head;*/
-
-  /*for (int i = 1; i < buckets; i++) {*/
-    /*current->next = node_initialize(NULL);*/
-    /*current = current->next;*/
-  /*}*/
-  return hash;
+  return key % hash->size;
 }
 
 void *hash_get(Hash *hash, int key)
 {
-  /*int bucket = to_hash(key);*/
-  /*Node *node = node_at(hash->head, bucket);*/
-  /*node_inspect(hash->head);*/
-  /*printf("  ");*/
-  /*node_inspect(node);*/
-
-  /*if (!node->value) {*/
-    /*return NULL;*/
-  /*}*/
-
-  /*Tuple *t = (Tuple *)node->value;*/
-  /*if (t->key == key) {*/
-    /*return node->value;*/
-  /*}*/
+  int bucket = hash_index(hash, key);
+  Node *list = hash->buckets + bucket;
+  if (list && list->data)
+    return ((Tuple *)list->data)->value;
+  else
+    return NULL;
+}
 
-  /*return node->value;*/
-  return NULL;
+void hash_set(Hash *hash, int key, void *value)
+{
+  int bucket = hash_index(hash, key);
+  Node *node = list_initialize(tuple_initialize(key, value));
+  hash->buckets[bucket] = *node;
+  hash_inspect(hash);
 }
 
-void hash_set(Hash *hash, int key, void **value)
+void hash_inspect(Hash *hash)
 {
-  int bucket = to_hash(key);
-  /*Node *node = node_at(hash->head, bucket);*/
-  /*Tuple *tuple = malloc(sizeof(Tuple));*/
-  /*tuple->key = key;*/
-  /*tuple->value = value;*/
-  /*list_add(node, tuple);*/
+  for (int i = 0; i < hash->size; i++) {
+    printf("%2d: ", i);
+    list_inspect(hash->buckets + i);
+  }
 }
src/02/04/hash.h
@@ -1,14 +1,11 @@
 #include "list.h"
 
 typedef struct {
-  int key;
-  void *value;
-} Tuple;
-
-typedef struct {
-  Node *head;
+  Node *buckets;
+  int size;
 } Hash;
 
 Hash *hash_init(int buckets);
 void *hash_get(Hash *hash, int key);
-void hash_set(Hash *hash, int key, void **value);
+void hash_set(Hash *hash, int key, void *value);
+void hash_inspect(Hash *hash);
src/02/04/hash_test.c
@@ -21,10 +21,10 @@ Ensure(HashTable, when_getting_a_value_for_a_key_that_has_not_been_inserted) {
 Ensure(HashTable, when_getting_a_values_for_a_key_that_has_been_inserted) {
   int key = 7;
   int value = 100;
-  /*Hash *hash = hash_init(13);*/
+  Hash *hash = hash_init(13);
 
-  /*hash_set(hash, key, value);*/
-  /*assert_that(hash_get(hash, key), is_equal_to(value));*/
+  hash_set(hash, key, value);
+  assert_that(hash_get(hash, key), is_equal_to(value));
 }
 
 Ensure(HashTable, when_a_hash_collision_occurs) {
@@ -40,14 +40,15 @@ Ensure(HashTable, when_a_hash_collision_occurs) {
 TestSuite *hash_table_tests() {
   TestSuite *suite = create_test_suite();
 
-  /*add_test_with_context(suite, HashTable, when_initializing_a_hash);*/
-  /*add_test_with_context(suite, HashTable, when_getting_a_value_for_a_key_that_has_not_been_inserted);*/
-  /*add_test_with_context(suite, HashTable, when_getting_a_values_for_a_key_that_has_been_inserted);*/
+  add_test_with_context(suite, HashTable, when_initializing_a_hash);
+  add_test_with_context(suite, HashTable, when_getting_a_value_for_a_key_that_has_not_been_inserted);
+  add_test_with_context(suite, HashTable, when_getting_a_values_for_a_key_that_has_been_inserted);
   /*add_test_with_context(suite, HashTable, when_a_hash_collision_occurs);*/
   return suite;
 }
 
 extern TestSuite *list_tests();
+extern TestSuite *tuple_tests();
 
 int main(int argc, char **argv) {
   TestSuite *suite = create_test_suite();
src/02/04/list.c
@@ -41,7 +41,7 @@ int list_size(Node *head) {
   return i;
 }
 
-void inspect(Node *self) {
+void list_inspect(Node *self) {
   if (!self)
     return;
 
src/02/04/tuple.c
@@ -1,7 +1,7 @@
 #include "stdlib.h"
 #include "tuple.h"
 
-Tuple *tuple_initialize(int key, int value)
+Tuple *tuple_initialize(int key, void *value)
 {
   Tuple *tuple = malloc(sizeof(Tuple));
   tuple->key = key;
src/02/04/tuple.h
@@ -1,7 +1,7 @@
 typedef struct {
   int key;
-  int value;
+  void *value;
 } Tuple;
 
-Tuple *tuple_initialize(int key, int value);
+Tuple *tuple_initialize(int key, void *value);
 void tuple_destroy(Tuple *tuple);