| 1 | #ifndef HASHMAP_H |
| 2 | #define HASHMAP_H |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <stddef.h> |
| 6 | |
| 7 | |
| 8 | // collision list |
| 9 | struct hashmap_col_list { |
| 10 | struct hashmap_col_list * next; |
| 11 | struct hashmap_col_list * prev; |
| 12 | |
| 13 | unsigned long hash; |
| 14 | char * key; |
| 15 | void * val; |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | struct hashmap_bucket { |
| 20 | struct hashmap_col_list * entries; |
| 21 | struct hashmap_col_list * last; |
| 22 | }; |
| 23 | |
| 24 | |
| 25 | typedef struct { |
| 26 | void (*destroyer)(void*); |
| 27 | struct hashmap_bucket ** buckets; |
| 28 | size_t len; // total entries |
| 29 | size_t max; |
| 30 | } hashmap; |
| 31 | |
| 32 | |
| 33 | typedef struct { |
| 34 | size_t curbuck; |
| 35 | struct hashmap_col_list * col; |
| 36 | |
| 37 | char * key; |
| 38 | void * val; |
| 39 | } hashmap_walk_state; |
| 40 | |
| 41 | |
| 42 | hashmap * hashmap_new(void (*destroyer)(void*)); |
| 43 | void hashmap_insert(hashmap *, char *, void *); |
| 44 | void * hashmap_get(const hashmap *, const char *); |
| 45 | void hashmap_remove(hashmap *, char *); |
| 46 | int hashmap_walk(hashmap *, hashmap_walk_state *); |
| 47 | void hashmap_destroy(hashmap *); |
| 48 | |
| 49 | #endif /* HASHMAP_H */ |
| 50 | |