From 6bb40bf0c5cd8ee8fb87640fd04b2c595f84c1d3 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 16 Dec 2021 09:56:16 +0100 Subject: Add hashmap --- include/hashmap.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 include/hashmap.h (limited to 'include') diff --git a/include/hashmap.h b/include/hashmap.h new file mode 100644 index 0000000..0928081 --- /dev/null +++ b/include/hashmap.h @@ -0,0 +1,38 @@ +#ifndef MGUI_HASHMAP_H +#define MGUI_HASHMAP_H + +#include +#include +#include + +typedef struct mgui_hashmap_entry mgui_hashmap_entry; + +struct mgui_hashmap_entry { + mgui_hashmap_entry *next; + uint64_t hash; + void *value; + size_t key_size; + /* char key[...]; Dynamically size string of size |key_size|. Allocated directly in the struct (sizeof(mgui_hashmap_entry) + key_size) */ +}; + +typedef struct { + mgui_hashmap_entry **entries; + size_t capacity; + size_t size; +} mgui_hashmap; + +void mgui_hashmap_init(mgui_hashmap *self); +void mgui_hashmap_deinit(mgui_hashmap *self); + +/* + Note: stores the |value| itself, and does not copy the content of |value|. + Note: inserting the same value multiple times in the hash map is undefined behavior. + Note: |hash| can be NULL. +*/ +void mgui_hashmap_insert(mgui_hashmap *self, const char *key, size_t key_size, void *value, uint64_t *hash_out); +bool mgui_hashmap_get(mgui_hashmap *self, const char *key, size_t key_size, void **value_out); +/* Note: |hash| has to be the hash for |key|, which you can get by using calling |mgui_hashmap_hash| with |key| or as a result of |mgui_hashmap_insert| */ +bool mgui_hashmap_get_by_hash(mgui_hashmap *self, const char *key, size_t key_size, uint64_t hash, void **value_out); +uint64_t mgui_hashmap_hash(const char *key, size_t key_size); + +#endif /* MGUI_HASHMAP_H */ -- cgit v1.2.3