aboutsummaryrefslogtreecommitdiff
path: root/src/hashmap.h
blob: 5a1e22ddb123b9391ae196833dd93077e40d96cb (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
#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

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)

void *hashmap_insert(struct hashmap *hm, const char *key, void *value);

void *hashmap_get(struct hashmap *hm, const char *key);

void *hashmap_resolve(struct hashmap *hm, const char *key);

void *hashmap_remove(struct hashmap *hm, const char *key);

void hashmap_free(struct hashmap *hm);

void hashmap_walk(struct hashmap *hm, void (*fn)(void *value));

#endif