blob: 52df62521713042dbb70c0e690db8b5ec6efbf88 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#ifndef UNJA_HASHMAP_H
#define UNJA_HASHMAP_H
#include <stdbool.h>
#include <sys/types.h>
#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
|