#ifndef UNJA_HASHMAP_H #define UNJA_HASHMAP_H #include #include #ifndef HASHMAP_CAP #define HASHMAP_CAP 32 #endif #ifndef HASHMAP_MIN_LOAD #define HASHMAP_MIN_LOAD 0.2 #endif #ifndef HASHMAP_MAX_LOAD #define HASHMAP_MAX_LOAD 0.75 #endif #ifndef HASHMAP_GROW_RATE #define HASHMAP_GROW_RATE 2 #endif typedef void (hashmap_cb)(const void *key, void *value); struct hashmap { struct node **buckets; size_t init_cap; size_t cur_cap; size_t size; }; /* allocate a new hashmap */ struct hashmap *hashmap_new_with_cap(size_t cap); #define hashmap_new() hashmap_new_with_cap(HASHMAP_CAP) /* * Inserts a key-value pair into the map. Returns NULL if map did not have key, * old value if it did. */ void *hashmap_insert(struct hashmap *hm, const char *key, void *value); /* Returns a pointer to the value corresponding to the key. */ void *hashmap_get(struct hashmap *hm, const char *key); /* Retrieve pointer to value by key, handles dot notation for nested hashmaps */ void *hashmap_resolve(struct hashmap *hm, const char *key); /* * Removes a key from the map, returning the value at the key if the key was * previously in the map. */ void *hashmap_remove(struct hashmap *hm, const char *key); /* Iterate over keys in the hashmap */ void hashmap_walk(struct hashmap *hm, hashmap_cb); /* free hashmap related memory calling a function before freeing each node */ void hashmap_destroy(struct hashmap *hm, hashmap_cb cb); /* free hashmap related memory */ void hashmap_free(struct hashmap *hm); #endif