From 8aec952c5dc4aa31cd0e5dd26bc4fa09c8658759 Mon Sep 17 00:00:00 2001
From: "dario.genga" <dario.genga@etu.hesge.ch>
Date: Tue, 18 Jan 2022 13:14:27 +0100
Subject: [PATCH] Add hashmap create and destroy methods

---
 hashtable.c | 15 +++++++++++++--
 main.c      |  4 ++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/hashtable.c b/hashtable.c
index 0d286e4..23c17ce 100644
--- a/hashtable.c
+++ b/hashtable.c
@@ -9,11 +9,22 @@
 #include <string.h>
 
 hm *hm_create(unsigned int length) {
-    return NULL;
+    hm *hashmap = malloc(sizeof(hm));
+    hashmap->length = length;
+    hashmap->entries = malloc(sizeof(entry) * length);
+    for (int i = 0; i < length; i++) {
+        hashmap->entries[i] = malloc(sizeof(entry));
+    }
+
+    return hashmap;
 }
 
 void hm_destroy(hm **hm) {
-
+    for (int i = 0; i < (*hm)->length; i++) {
+        free((*hm)->entries[i]);
+    }
+    free((*hm)->entries);
+    free((*hm));
 }
 
 hm *hm_set(hm *hm, const char *const key, const char *const value) {
diff --git a/main.c b/main.c
index df0280e..09ae288 100644
--- a/main.c
+++ b/main.c
@@ -5,7 +5,11 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include "hashtable.h"
 
 int main() {
+    hm *hashmap = hm_create(10);
+    hm_destroy(&hashmap);
+
     return EXIT_SUCCESS;
 }
-- 
GitLab