forked from fuhsnn/slimcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.c
More file actions
171 lines (141 loc) · 4.69 KB
/
hashmap.c
File metadata and controls
171 lines (141 loc) · 4.69 KB
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
// This is an implementation of the open-addressing hash table.
#include "slimcc.h"
// Initial hash bucket size
#define INIT_SIZE 16
#define SHOULD_REHASH(_used, _cap) ((_used) * 11 / 8 >= _cap)
S_ASSERT(INIT_SIZE > 0 && SHOULD_REHASH(INIT_SIZE - 1, INIT_SIZE))
static uint64_t fnv_hash(const char *s, int len) {
uint64_t hash = 0xcbf29ce484222325;
for (int i = 0; i < len; i++) {
hash *= 0x100000001b3;
hash ^= (unsigned char)s[i];
}
return hash;
}
// Make room for new entries in a given hashmap by removing
// tombstones and possibly extending the bucket size.
static void rehash(HashMap *map) {
// Compute the size of the new hashmap.
int32_t nkeys = 0;
for (int32_t i = 0; i < map->capacity; i++)
if (map->buckets[i].key && map->buckets[i].key != TOMBSTONE)
nkeys++;
int64_t cap = map->capacity;
if ((int64_t)nkeys * 2 > cap) {
cap *= 2;
if (cap * sizeof(HashEntry) > 0x4000000)
internal_error();
}
// Create a new hashmap and copy all key-values.
HashMap map2 = {0};
map2.buckets = calloc(cap, sizeof(HashEntry));
map2.capacity = cap;
for (int i = 0; i < map->capacity; i++) {
HashEntry *ent = &map->buckets[i];
if (ent->key && ent->key != TOMBSTONE)
hashmap_put2(&map2, ent->key, ent->keylen, ent->val);
}
assert(map2.used == nkeys);
free(map->buckets);
*map = map2;
}
static HashEntry *get_entry(HashMap *map, const char *key, int keylen) {
if (!map->buckets)
return NULL;
uint64_t hash = fnv_hash(key, keylen);
uint64_t msk = map->capacity - 1;
HashEntry *buckets = map->buckets;
for (;;) {
HashEntry *ent = &buckets[hash++ & msk];
switch ((uintptr_t)ent->key) {
case 0: {
return NULL;
}
default:
if (ent->keylen == keylen && !memcmp(ent->key, key, keylen))
return ent;
case TOMBSTONE_CASE: {
break;
}
}
}
}
HashEntry *hashmap_get_or_insert(HashMap *map, const char *key, int keylen) {
if (!map->buckets) {
map->buckets = calloc(INIT_SIZE, sizeof(HashEntry));
map->capacity = INIT_SIZE;
} else if (SHOULD_REHASH((int64_t)map->used, map->capacity)) {
rehash(map);
}
uint64_t hash = fnv_hash(key, keylen);
uint64_t msk = map->capacity - 1;
HashEntry *buckets = map->buckets;
for (;;) {
HashEntry *ent = &buckets[hash++ & msk];
switch ((uintptr_t)ent->key) {
case 0:
ent->key = key;
ent->keylen = keylen;
map->used++;
return ent;
default:
if (ent->keylen == keylen && !memcmp(ent->key, key, keylen))
return ent;
case TOMBSTONE_CASE: {
break;
}
}
}
}
void *hashmap_get(HashMap *map, const char *key) {
return hashmap_get2(map, key, strlen(key));
}
void *hashmap_get2(HashMap *map, const char *key, int keylen) {
HashEntry *ent = get_entry(map, key, keylen);
return ent ? ent->val : NULL;
}
void hashmap_put(HashMap *map, const char *key, void *val) {
hashmap_put2(map, key, strlen(key), val);
}
void hashmap_put2(HashMap *map, const char *key, int keylen, void *val) {
HashEntry *ent = hashmap_get_or_insert(map, key, keylen);
ent->val = val;
}
void hashmap_delete(HashMap *map, const char *key) {
hashmap_delete2(map, key, strlen(key));
}
void hashmap_delete2(HashMap *map, const char *key, int keylen) {
HashEntry *ent = get_entry(map, key, keylen);
if (ent)
ent->key = TOMBSTONE;
}
void hashmap_test(void) {
HashMap *map = calloc(1, sizeof(HashMap));
Arena test_arena = {0};
arena_on(&test_arena);
for (int i = 0; i < 5000; i++)
hashmap_put(map, arena_format(&test_arena, "key %d", i), (void *)(size_t)i);
for (int i = 1000; i < 2000; i++)
hashmap_delete(map, arena_format(&test_arena, "key %d", i));
for (int i = 1500; i < 1600; i++)
hashmap_put(map, arena_format(&test_arena, "key %d", i), (void *)(size_t)i);
for (int i = 6000; i < 7000; i++)
hashmap_put(map, arena_format(&test_arena, "key %d", i), (void *)(size_t)i);
for (int i = 0; i < 1000; i++)
assert((size_t)hashmap_get(map, arena_format(&test_arena, "key %d", i)) == i);
for (int i = 1000; i < 1500; i++)
assert(hashmap_get(map, "no such key") == NULL);
for (int i = 1500; i < 1600; i++)
assert((size_t)hashmap_get(map, arena_format(&test_arena, "key %d", i)) == i);
for (int i = 1600; i < 2000; i++)
assert(hashmap_get(map, "no such key") == NULL);
for (int i = 2000; i < 5000; i++)
assert((size_t)hashmap_get(map, arena_format(&test_arena, "key %d", i)) == i);
for (int i = 5000; i < 6000; i++)
assert(hashmap_get(map, "no such key") == NULL);
for (int i = 6000; i < 7000; i++)
hashmap_put(map, arena_format(&test_arena, "key %d", i), (void *)(size_t)i);
assert(hashmap_get(map, "no such key") == NULL);
arena_off(&test_arena);
printf("OK\n");
}