From d1f9b8a334549a206870096db4631822dbd189ac Mon Sep 17 00:00:00 2001 From: William Bell Date: Fri, 20 Jun 2025 03:00:56 +0100 Subject: [PATCH] work on hashmap --- src/hashmap/hashmap.c | 73 +++++++++++++++++++++++++++++++++++++---- src/hashmap/hashmap.h | 10 +++--- src/runtime/null/null.c | 5 +++ src/runtime/runtime.h | 2 +- 4 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 src/runtime/null/null.c diff --git a/src/hashmap/hashmap.c b/src/hashmap/hashmap.c index 4635899..e64b7e2 100644 --- a/src/hashmap/hashmap.c +++ b/src/hashmap/hashmap.c @@ -4,9 +4,9 @@ #include #include "../memory.h" -struct table *createTable(int size) +struct hashmap *createTable(int size) { - struct table *t = (struct table *)ar_alloc(sizeof(struct table)); + struct hashmap *t = (struct hashmap *)ar_alloc(sizeof(struct hashmap)); t->size = size; t->list = (struct node **)ar_alloc(sizeof(struct node *) * size); int i; @@ -15,14 +15,39 @@ struct table *createTable(int size) return t; } -int hashCode(struct table *t, int key) +void resize_hashmap(struct hashmap *t) +{ + int old_size = t->size; + int new_size = old_size * 2; + + struct node **old_list = t->list; + + // Create new list + t->list = (struct node **)ar_alloc(sizeof(struct node *) * new_size); + for (int i = 0; i < new_size; i++) + t->list[i] = NULL; + + t->size = new_size; + t->count = 0; + + // Rehash old entries into new list + for (int i = 0; i < old_size; i++) { + struct node *temp = old_list[i]; + while (temp) { + insert(t, temp->key, temp->val); // Will increment count + temp = temp->next; + } + } +} + +int hashCode(struct hashmap *t, int key) { if (key < 0) return -(key % t->size); return key % t->size; } -int remove(struct table *t, int key) +int remove(struct hashmap *t, int key) { int pos = hashCode(t, key); struct node *list = t->list[pos]; @@ -46,12 +71,42 @@ int remove(struct table *t, int key) return 0; } -void insert(struct table *t, int key, void* val) +void resize(struct hashmap *t) { + int old_size = t->size; + int new_size = old_size * 2; + + struct node **old_list = t->list; + + // Create new list + t->list = (struct node **)ar_alloc(sizeof(struct node *) * new_size); + for (int i = 0; i < new_size; i++) + t->list[i] = NULL; + + t->size = new_size; + t->count = 0; + + // Rehash old entries into new list + for (int i = 0; i < old_size; i++) { + struct node *temp = old_list[i]; + while (temp) { + insert(t, temp->key, temp->val); // Will increment count + temp = temp->next; + } + } +} + +void insert(struct hashmap *t, int key, void* val) +{ + if ((t->count + 1) > t->size * 0.75) { + resize(t); + } + int pos = hashCode(t, key); struct node *list = t->list[pos]; - struct node *newNode = (struct node *)ar_alloc(sizeof(struct node)); struct node *temp = list; + + // Check if key exists → overwrite while (temp) { if (temp->key == key) @@ -61,13 +116,17 @@ void insert(struct table *t, int key, void* val) } temp = temp->next; } + + // Insert new node + struct node *newNode = (struct node *)ar_alloc(sizeof(struct node)); newNode->key = key; newNode->val = val; newNode->next = list; t->list[pos] = newNode; + t->count++; } -void *lookup(struct table *t, int key) +void *lookup(struct hashmap *t, int key) { int pos = hashCode(t, key); struct node *list = t->list[pos]; diff --git a/src/hashmap/hashmap.h b/src/hashmap/hashmap.h index 2f8c101..2b1943f 100644 --- a/src/hashmap/hashmap.h +++ b/src/hashmap/hashmap.h @@ -8,19 +8,19 @@ struct node void *val; struct node *next; }; -struct table +struct hashmap { size_t size; size_t count; struct node **list; }; -struct table *createTable(int size); +struct hashmap *createTable(int size); -int hashCode(struct table *t, int key); +int hashCode(struct hashmap *t, int key); -int remove(struct table *t, int key); +int remove(struct hashmap *t, int key); -void insert(struct table *t, int key, void* val); +void insert(struct hashmap *t, int key, void* val); #endif // HASHMAP_H \ No newline at end of file diff --git a/src/runtime/null/null.c b/src/runtime/null/null.c new file mode 100644 index 0000000..d87dd49 --- /dev/null +++ b/src/runtime/null/null.c @@ -0,0 +1,5 @@ +#include "../runtime.h" + +void run_null(Translated *translated, RuntimeState *state) { + +} \ No newline at end of file diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 0c7b6dd..1960c06 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -9,7 +9,7 @@ typedef struct { typedef struct { - + } ArObject; void run_instruction(Translated *translated, RuntimeState *state);