-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhashmap.c
216 lines (185 loc) · 4.27 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* hashmap.c -- generic hash table
*
* Copyright (c) 2011, Janne Kulmala <[email protected]>.
* All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "hashmap.h"
#include <stdlib.h>
#define MIN_SLOTS 16
static int hashmap_grow(struct hashmap *map)
{
size_t i;
/* first, allocate more room for the table */
struct hash_node **newtable = realloc(map->table, map->len * 2 *
sizeof(struct hash_node *));
if (newtable == NULL)
return -1;
map->table = newtable;
/* then, split all nodes from the lower half of the table
to either lower or upper half of the table */
for (i = 0; i < map->len; ++i) {
struct hash_node *node = map->table[i], *next;
struct hash_node *a = NULL, *b = NULL;
while (node) {
next = node->next;
if (node->hash & map->len) {
/* upper half */
node->next = b;
b = node;
} else {
/* lower half */
node->next = a;
a = node;
}
node = next;
}
map->table[i] = a;
map->table[i + map->len] = b;
}
map->len *= 2;
return 0;
}
static int hashmap_shrink(struct hashmap *map)
{
size_t i;
/* first, fold the upper half of the table to top of the lower half */
map->len /= 2;
for (i = 0; i < map->len; ++i) {
struct hash_node *prev = map->table[i];
struct hash_node *next = map->table[i + map->len];
if (prev == NULL)
map->table[i] = next;
else {
while (prev->next)
prev = prev->next;
prev->next = next;
}
}
/* then, release unneeded memory */
struct hash_node **newtable = realloc(map->table, map->len *
sizeof(struct hash_node *));
if (newtable == NULL)
return -1;
map->table = newtable;
return 0;
}
void hashmap_init(struct hashmap *map, hash_func_t hash, cmp_func_t cmp)
{
map->len = MIN_SLOTS;
map->table = calloc(map->len, sizeof(struct hash_node *));
map->count = 0;
map->hash = hash;
map->cmp = cmp;
}
void hashmap_free(struct hashmap *map)
{
free(map->table);
}
struct hash_node *hashmap_get(struct hashmap *map, void *key)
{
struct hash_node *node = map->table[map->hash(key) & (map->len - 1)];
while (node) {
if (map->cmp(node, key))
return node;
node = node->next;
}
return NULL;
}
int hashmap_insert(struct hashmap *map, struct hash_node *node, void *key)
{
size_t slot;
node->hash = map->hash(key);
slot = node->hash & (map->len - 1);
node->next = map->table[slot];
map->table[slot] = node;
map->count++;
if (map->count > map->len * 3)
hashmap_grow(map);
return 0;
}
struct hash_node *hashmap_remove(struct hashmap *map, void *key)
{
size_t slot = map->hash(key) & (map->len - 1);
struct hash_node *node = map->table[slot], *prev = NULL;
while (node) {
if (map->cmp(node, key)) {
if (prev != NULL)
prev->next = node->next;
else
map->table[slot] = node->next;
map->count--;
if (map->count < map->len / 4 && map->len > MIN_SLOTS)
hashmap_shrink(map);
return node;
}
prev = node;
node = node->next;
}
return NULL;
}
#ifdef TEST
#include <stdio.h>
#include <assert.h>
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
#define container_of(ptr, type, member) \
((type *) ((char *) (ptr) - offsetof(type, member)))
struct test {
struct hash_node node;
int i, j;
};
static size_t hash_test(void *key)
{
int *i = key;
return *i;
}
static int cmp_test(struct hash_node *node, void *key)
{
struct test *t = container_of(node, struct test, node);
int *i = key;
return t->i == *i;
}
#define COUNT 1000000
#define GET_COUNT 10000000
int main()
{
int i;
struct test *t;
struct hashmap map;
hashmap_init(&map, hash_test, cmp_test);
for (i = 0; i < COUNT; ++i) {
t = calloc(1, sizeof *t);
t->i = i;
t->j = i + 123;
hashmap_insert(&map, &t->node, &i);
}
for (i = 0; i < GET_COUNT; ++i) {
int k = rand() % COUNT;
struct test *t;
struct hash_node *node = hashmap_get(&map, &k);
if (node == NULL) {
printf("%d not found\n", k);
assert(0);
}
t = container_of(node, struct test, node);
assert (t->i == k && t->j == k + 123);
}
for (i = 0; i < COUNT; ++i) {
int k = COUNT - 1 - i;
struct hash_node *node = hashmap_remove(&map, &k);
if (node == NULL) {
printf("%d not found\n", k);
assert(0);
}
t = container_of(node, struct test, node);;
assert (t->i == k && t->j == k + 123);
}
assert(map.len == MIN_SLOTS);
return 0;
}
#endif