/
hashmap.c
201 lines (163 loc) · 3.64 KB
/
hashmap.c
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
#ifdef __cplusplus
extern "C" {
#endif
#include "hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
static void error_exit(const char* fmt, ...)
{
fprintf(stderr, "\nError:\n");
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
exit(1);
}
static void* emalloc(size_t size)
{
void* mem = malloc(size);
if(!mem) error_exit("Out of memory!\n");
return mem;
}
static char* my_strdup(const char* str)
{
char* dup = emalloc(strlen(str) + 1);
strcpy(dup, str);
return dup;
}
static unsigned long map_hash_function(const char* str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
static hashmap_node_t* create_node(const char* key, void* value)
{
hashmap_node_t* node = malloc(sizeof(hashmap_node_t));
if(!node) error_exit("failed to create hashmap node (key: '%s'); out of memory", key);
node->key = my_strdup(key);
node->value = value;
node->next = NULL;
return node;
}
void map_init(hashmap_t* map)
{
memset(map->buckets, 0, sizeof(map->buckets));
vec_init(&map->active_buckets, sizeof(unsigned long));
}
void map_set(hashmap_t* map, const char* key, void* value)
{
unsigned long pos = map_hash_function(key) % HASHMAP_BUCKET_AMT;
hashmap_node_t* node = create_node(key, value);
if(!map->buckets[pos])
vec_push_back(&map->active_buckets, &pos);
node->next = map->buckets[pos];
map->buckets[pos] = node;
}
void* map_get(hashmap_t* map, const char* key)
{
unsigned long pos = map_hash_function(key) % HASHMAP_BUCKET_AMT;
if(!map->buckets[pos])
return NULL;
else
{
hashmap_node_t* node = map->buckets[pos];
while(node)
{
if(strcmp(node->key, key) == 0)
return node->value;
node = node->next;
}
}
return NULL;
}
void* map_remove(hashmap_t* map, const char* key)
{
unsigned long pos = map_hash_function(key) % HASHMAP_BUCKET_AMT;
if(!map->buckets[pos])
return NULL;
else
{
hashmap_node_t* node = map->buckets[pos];
hashmap_node_t* prev = NULL;
while(node)
{
if(strcmp(node->key, key) == 0)
{
if(prev)
prev->next = node->next;
else
{
map->buckets[pos] = NULL;
int to_remove = -1;
for(size_t i = 0; i < map->active_buckets.length; ++i)
{
if(*(unsigned long*)vec_get(&map->active_buckets, i) == pos)
{
to_remove = i;
break;
}
}
if(to_remove >= 0) vec_remove(&map->active_buckets, to_remove);
}
void* value = node->value;
free(node->key);
free(node);
return value;
}
prev = node;
node = node->next;
}
}
return NULL;
}
static void free_bucket(hashmap_node_t* bucket)
{
if(!bucket) return;
hashmap_node_t* node = bucket;
hashmap_node_t* next = NULL;
while(node)
{
next = node->next;
free(node->key);
free(node);
node = next;
}
}
void map_traverse(hashmap_t* map, hashmap_traverse_t traverse, void* data)
{
for(size_t i = 0; i < map->active_buckets.length; ++i)
{
unsigned long pos = *(unsigned long*)vec_get(&map->active_buckets, i);
hashmap_node_t* node = map->buckets[pos];
while(node)
{
traverse(node->key, node->value, data);
node = node->next;
}
}
}
void map_clear(hashmap_t* map)
{
for(size_t i = 0; i < map->active_buckets.length; ++i)
{
unsigned long pos = *(unsigned long*)vec_get(&map->active_buckets, i);
hashmap_node_t* node = map->buckets[pos];
free_bucket(node);
map->buckets[pos] = NULL;
}
vec_clear(&map->active_buckets);
}
void map_destroy(hashmap_t* map)
{
map_clear(map);
vec_destroy(&map->active_buckets);
}
#ifdef __cplusplus
}
#endif