summaryrefslogtreecommitdiff
path: root/compiler/hashmap.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-11-01 23:46:08 -0400
committerIan Moffett <ian@osmora.org>2024-11-01 23:46:08 -0400
commita515dfb3b8f8e999362db7a6b52b3104c03b750a (patch)
treed0180f0cbc39d9c3e367af30791ad774e4d419ff /compiler/hashmap.c
Import quark sources
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'compiler/hashmap.c')
-rw-r--r--compiler/hashmap.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/compiler/hashmap.c b/compiler/hashmap.c
new file mode 100644
index 0000000..0fb2e14
--- /dev/null
+++ b/compiler/hashmap.c
@@ -0,0 +1,39 @@
+/*
+ * Tiny hashmap implementation.
+ * Copyright (c) 2023-2024, Quinn Stephens and the OSMORA team.
+ * Provided under the BSD 3-Clause license.
+ */
+
+#include <stddef.h>
+#include "hashmap.h"
+
+void
+hashmap_add(struct hashmap *map, struct hashmap_entry *entry)
+{
+ list_prepend(&map->rows[entry->hash % map->n_rows], &entry->list_entry);
+}
+
+struct hashmap_entry *
+hashmap_find(struct hashmap *map, hash_t hash)
+{
+ struct hashmap_entry *head, *entry;
+
+ entry = head = (struct hashmap_entry*)(map->rows[hash % map->n_rows].head);
+ do {
+ if (entry->hash == hash) {
+ return entry;
+ }
+
+ entry = (struct hashmap_entry*)(entry->list_entry.next);
+ } while (entry != head);
+
+ return NULL;
+}
+
+void
+hashmap_init(struct hashmap *map)
+{
+ for (size_t r = 0; r < map->n_rows; r++) {
+ list_init(&map->rows[r]);
+ }
+}