diff options
| author | 0scar <qgt268@alumni.ku.dk> | 2023-10-30 22:11:54 +0000 |
|---|---|---|
| committer | 0scar <qgt268@alumni.ku.dk> | 2023-10-30 22:12:34 +0000 |
| commit | 5c0311ff3095a5dd30fb15db54aa394498dd2bcf (patch) | |
| tree | 2abb0e3dc145e4372fcf07aeeb736279d00471e5 /include/engine/hashmap.h | |
| parent | 3e3fe3d42206dc14baca2f50445e6fe8bb2d2480 (diff) | |
The great formattening!
Diffstat (limited to 'include/engine/hashmap.h')
| -rw-r--r-- | include/engine/hashmap.h | 81 |
1 files changed, 40 insertions, 41 deletions
diff --git a/include/engine/hashmap.h b/include/engine/hashmap.h index 4b97b8e..bf1d87c 100644 --- a/include/engine/hashmap.h +++ b/include/engine/hashmap.h @@ -3,53 +3,52 @@ #include "types.h" -#include <stdlib.h> #include "list.h" #include "memory.h" - +#include <stdlib.h> i32 lolhash(const usize s, i32 v); /* Define a linked list before using this */ /* Example: DEFINE_LLIST(i32) */ -#define DEFINE_HASHMAP(type, lsize, cmp, type_to_int) \ -typedef DEFINE_LLIST(type); \ -typedef struct hashmap_##type { \ - usize size; \ - List_##type elems[64]; \ -} hashmap_##type; \ - \ -type* hashmap_##type##_lookup(hashmap_##type* hmap, const type* val) { \ - const i32 idx = lolhash(64, type_to_int(val)); \ - List_##type *head = &hmap->elems[idx]; \ - while (head != NULL) { \ - if (!cmp(&(head->value), val)) return &(head->value); \ - head = head->next; \ - } \ - return NULL; \ -} \ - \ -void hashmap_##type##_insert(memory *m, hashmap_##type *hmap, const type *val) { \ - const i32 idx = lolhash(64, type_to_int(val)); \ - List_##type *head = &(hmap->elems[idx]); \ - \ - /* This is highly dependant on whether the memory is zero-initialized */ \ - if (!type_to_int(&(head->value))) \ - { \ - memcpy(&(head->value), val, sizeof(type)); \ - return; \ - } \ - \ - \ - while (head->next != NULL && cmp(&head->value, val)) { \ - head = head->next; \ - } \ - \ - if (!cmp(&head->value, val)) memcpy(&(head->value), val, sizeof(type)); \ - else { \ - head->next = memory_allocate(m, sizeof(List_##type)); \ - memcpy(&(head->next->value), val, sizeof(type)); \ - } \ -} +#define DEFINE_HASHMAP(type, lsize, cmp, type_to_int) \ + typedef DEFINE_LLIST(type); \ + typedef struct hashmap_##type { \ + usize size; \ + List_##type elems[64]; \ + } hashmap_##type; \ + \ + type* hashmap_##type##_lookup(hashmap_##type* hmap, const type* val) { \ + const i32 idx = lolhash(64, type_to_int(val)); \ + List_##type* head = &hmap->elems[idx]; \ + while (head != NULL) { \ + if (!cmp(&(head->value), val)) return &(head->value); \ + head = head->next; \ + } \ + return NULL; \ + } \ + \ + void hashmap_##type##_insert(memory* m, hashmap_##type* hmap, \ + const type* val) { \ + const i32 idx = lolhash(64, type_to_int(val)); \ + List_##type* head = &(hmap->elems[idx]); \ + \ + /* This is highly dependant on whether the memory is zero-initialized */ \ + if (!type_to_int(&(head->value))) { \ + memcpy(&(head->value), val, sizeof(type)); \ + return; \ + } \ + \ + while (head->next != NULL && cmp(&head->value, val)) { \ + head = head->next; \ + } \ + \ + if (!cmp(&head->value, val)) \ + memcpy(&(head->value), val, sizeof(type)); \ + else { \ + head->next = memory_allocate(m, sizeof(List_##type)); \ + memcpy(&(head->next->value), val, sizeof(type)); \ + } \ + } #endif |
