aboutsummaryrefslogtreecommitdiff
path: root/src/hashmap.c
blob: 27874350647730bc0bb1f04235e3c7ca710a8a6d (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "hashmap.h"

#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <err.h>

#if SIZE_MAX == 0xFFFFFFFF
/* If size_t is 32bit */
static const size_t fnv_prime = 16777619u;
static const size_t fnv_offsetb = 2166136261u;
#elif SIZE_MAX == 0xFFFFFFFFFFFFFFFF
/* If size_t is 64bit */
static const size_t fnv_prime = 1099511628211u;
static const size_t fnv_offsetb = 14695981039346656037u;
#else
/* If size_t is 128bit. Maybe this is overdoing it? */
static const size_t fnv_prime = 309485009821345068724781371u;
static const size_t fnv_offsetb = 144066263297769815596495629667062367629u;
#endif

struct node {
    const char *key;
    void *value;
    struct node *next;
};

/* FNV1a */
static size_t
hash(const char *str)
{
	size_t hash = fnv_offsetb;
	size_t i = 0;
	while (str[i] != '\0') {
		hash ^= str[i];
		hash *= fnv_prime;
		i++;
	}

	return hash;
}

static void
htable_realloc(struct hashmap *hm, struct node **oldbucks, size_t oldcap)
{
	for (size_t i = 0; i < oldcap; i++) {
		struct node *node = oldbucks[i];
		while (node) {
			size_t pos = hash(node->key) % hm->cur_cap;
			if (hm->buckets[pos]) {
				struct node *newnext = hm->buckets[pos],
							*newprev;
				do {
					newprev = newnext;
					newnext = newprev->next;
				} while (newnext);
				newprev->next = node;
			} else {
				hm->buckets[pos] = node;
			}
			struct node *prev = node;
			node = prev->next;
			prev->next = NULL;
		}
	}
	free(oldbucks);
}

static bool
htable_grow(struct hashmap *hm)
{
	float load = (float)hm->size / (float)hm->cur_cap;
	if (load <= HASHMAP_MAX_LOAD) return true;

	size_t oldcap = hm->cur_cap;
	hm->cur_cap = hm->cur_cap * HASHMAP_GROW_RATE;
	struct node **oldbucks = hm->buckets;
	hm->buckets = calloc(hm->cur_cap, sizeof(**hm->buckets));
	if (!hm->buckets) return false;

	htable_realloc(hm, oldbucks, oldcap);

	return true;
}

static bool
htable_shrink(struct hashmap *hm)
{
	if (hm->cur_cap <= hm->init_cap) return true;

	float load = (float)hm->size / (float)hm->cur_cap;
	if (load >= HASHMAP_MIN_LOAD) return true;

	size_t oldcap = hm->cur_cap;
	hm->cur_cap = hm->cur_cap / HASHMAP_GROW_RATE;
	struct node **oldbucks = hm->buckets;
	hm->buckets = calloc(hm->cur_cap, sizeof(**hm->buckets));
	if (!hm->buckets) return false;
	htable_realloc(hm, oldbucks, oldcap);
	return true;
}

struct hashmap *
hashmap_new_with_cap(size_t cap)
{
	struct hashmap *hm = malloc(sizeof *hm);
	hm->init_cap = cap;
	hm->cur_cap = cap;
	hm->size = 0;
	if (hm == NULL) return NULL;
	hm->buckets = calloc(cap, sizeof hm->buckets);
	if (hm->buckets == NULL) {
		free(hm);
		return NULL;
	}

	return hm;
}

void *
hashmap_insert(struct hashmap *hm, const char *key, void *value)
{
    int pos = hash(key) % hm->cur_cap;
    struct node *head = hm->buckets[pos];
    struct node *node = head;
    void *old_value;

    while (node) {
        if (strcmp(node->key, key) == 0) {
            old_value = node->value;
            node->value = value;
            return old_value;
        }
        node = node->next;
    }
    
    node = malloc(sizeof *node);
    node->key = key;
    node->value = value;
    node->next = head;
    hm->buckets[pos] = node;
    hm->size++;
    htable_grow(hm);
    return NULL;
}

void *
hashmap_get(struct hashmap *hm, const char *key)
{
    size_t pos = hash(key) % hm->cur_cap;
    struct node *node = hm->buckets[pos];
    while (node != NULL) {
        if (strcmp(node->key, key) == 0) {
            return node->value;
        }

        node = node->next;
    }

    return NULL;
}

void *
hashmap_resolve(struct hashmap *hm, const char *key)
{
    char tmp_key[64];
    int i = 0;
    int j = 0;

    while (1) {
        for (j=0; key[i] != '.' && key[i] != '\0'; i++, j++) {
            tmp_key[j] = key[i];
        }
        tmp_key[j] = '\0';
        hm = hashmap_get(hm, tmp_key);
        
        // stop if we read key to end of string
        if (key[i] == '\0') {
            break;
        }

        // otherwise, continue reading keys
        i++;
    }

    return hm;
}

void *
hashmap_remove(struct hashmap *hm, const char *key)
{
    int pos = hash(key) % hm->cur_cap;
    struct node *node = hm->buckets[pos];
    struct node *prev = NULL;
    void *old_value;

    while (node) {
        if (strcmp(node->key, key) == 0) {
            if (prev) {
                prev->next = node->next;
            } else {
                hm->buckets[pos] = node->next;
            }
            old_value = node->value;
            free(node);            
    		hm->size--;
    		htable_shrink(hm);
            return old_value;
        }

        prev = node;
        node = node->next;
    }

    return NULL;
}

void
hashmap_walk(struct hashmap *hm, void (*fn)(const void *key, void *value))
{
    struct node *node;
    struct node *next;

    for (int i=0; i < hm->cur_cap; i++) {
        node = hm->buckets[i];
        while (node) {
            next = node->next;
            fn(node->key, node->value);
            node = next;
        }
    }
}

void
hashmap_free(struct hashmap *hm)
{
    struct node *node;
    struct node *next;

    for (int i=0; i < hm->cur_cap; i++) {
        node = hm->buckets[i];
        while (node) {
            next = node->next;
            free(node);            
            node = next;
        }
    }

	free(hm->buckets);
    free(hm);
}