-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.c
More file actions
288 lines (251 loc) · 6.08 KB
/
hashmap.c
File metadata and controls
288 lines (251 loc) · 6.08 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "hashmap.h"
#define DEFAULT_CAP 7
struct DictEntry {
int key;
int value;
unsigned long hash;
};
struct HashMap {
struct DictEntry **table;
size_t size;
size_t capacity;
};
static struct DictEntry* get_entry(HashMap *map, int key);
static void destroy_table(struct DictEntry **e, size_t size);
static inline unsigned long inthash (int num);
static inline size_t wrapped_inc(size_t n, size_t capacity);
static inline void set_bit(unsigned int *arr, unsigned int k)
{
size_t bits = 8 * sizeof(unsigned int);
arr[k/bits] |= 1 << (k%bits);
}
static inline void clear_bit(unsigned int *arr, unsigned int k)
{
size_t bits = 8 * sizeof(unsigned int);
arr[k/bits] &= ~(1 << (k%bits));
}
static inline bool get_bit(unsigned int *arr, unsigned int k)
{
size_t bits = 8 * sizeof(unsigned int);
return arr[k/bits] & (1 << (k%bits));
}
/* Sieve of eratosthenes, returns primes < n
* Returns NULL on failure */
static int *sieve(int n)
{
int *primes = malloc(n * sizeof(int));
if (!primes) {
return NULL;
}
/* Create a bitarray with at least n bits, and set
* all the even bits to 0, the odd bits to 1 */
size_t size = (n / (8 * sizeof(unsigned int))) + 1;
unsigned int indices[size];
for (size_t i=0; i<size; i++) {
/* I don't like this
* TODO: Somehow make this work when word size != 4 */
indices[i] = 0xAAAAAAAA;
}
int sqrtn = (int) sqrt(n);
for (int i=3; i<=sqrtn; i+=2) {
if (get_bit(indices, i)) {
for (int j=i*i; j<n; j+=(2*i)) {
clear_bit(indices, j);
}
}
}
primes[0] = 0;
primes[1] = 1;
primes[2] = 2;
for (int i=3; i<n; i+=2) {
primes[i] = get_bit(indices, i) ? i : 0;
}
return primes;
}
static bool is_prime(int n)
{
if (n % 2 == 0)
return n == 2;
if (n % 3 == 0)
return n == 3;
int k = 4;
int limit = sqrt(n) + 1;
for (int i=5; i <= limit; k = 6 - k, i+= k) {
if (n % i == 0)
return false;
}
return true;
}
static int next_prime(int n)
{
while (!is_prime(++n));
return n;
}
/* Create an empty hashmap
* Returns a pointer to the map on success,
* NULL on failure */
HashMap* new_hashmap(void)
{
HashMap *new = malloc(sizeof(HashMap));
if (!new) {
goto ERR;
}
new->size = 0;
new->capacity = DEFAULT_CAP;
new->table = malloc(new->capacity * sizeof(struct DictEntry *));
if (!new->table) {
goto ERR;
}
for (size_t i=0; i<new->capacity; i++) {
new->table[i] = NULL;
}
return new;
ERR:
if (new) {
free(new);
}
return NULL;
}
void destroy_hashmap(HashMap *map)
{
destroy_table(map->table, map->capacity);
free(map);
}
/* TODO: If the hashmap is full (somehow) this will never return */
/* Return the index of the next unoccupied slot in the table for element elem */
static size_t get_index(const HashMap *map, int elem)
{
size_t index = inthash(elem) % map->capacity;
while (map->table[index]) {
index = wrapped_inc(index, map->capacity);
}
return index;
}
/* Increase the size of the table to the next prime number
* that is >= twice the previous size. Rehashes every element
*
* Returns nonzero on failure */
static int hashmap_extend(HashMap *map)
{
size_t old_capacity = map->capacity;
size_t new_capacity = next_prime(2 * map->capacity);
struct DictEntry **old_table = map->table;
struct DictEntry **new_table = malloc(new_capacity * sizeof(struct DictElem *));
if (!new_table) {
return 1;
}
map->table = new_table;
map->capacity = new_capacity;
/* NULL-out the new table */
for (size_t i=0; i<map->capacity; map->table[i] = NULL, i++);
/* Rehash elements into the new table */
for (size_t i=0; i<old_capacity; i++) {
struct DictEntry *elem = old_table[i];
if (!elem) {
continue;
}
size_t index = get_index(map, elem->key);
map->table[index] = elem;
}
free(old_table);
return 0;
}
/* Keep the table working with linear probing after deletion */
void rectify(HashMap *map, size_t index)
{
size_t cursor = wrapped_inc(index, map->capacity);
while (map->table[cursor]) {
struct DictEntry *elem = map->table[cursor];
map->table[cursor] = NULL;
size_t index = get_index(map, elem->key);
map->table[index] = elem;
cursor = wrapped_inc(cursor, map->capacity);
}
}
/* TODO: This could maybe make use of elem->hash */
static struct DictEntry *get_entry(HashMap *map, int key)
{
size_t index = inthash(key) % map->capacity;
while (map->table[index]) {
if (key == map->table[index]->key) {
return map->table[index];
}
index = wrapped_inc(index, map->capacity);
}
return NULL;
}
/* Insert key:value pair into the hashmap
* If key already exists, value is overwritten
*
* Returns non-zero on failure */
int hashmap_insert(HashMap *map, int key, int value)
{
/* Resize if the map is getting too cluttered */
if (map->size > 2*(map->capacity / 3)) {
if (hashmap_extend(map)) {
return 1;
}
}
struct DictEntry *e = get_entry(map, key);
if (e) {
e->value = value;
} else {
struct DictEntry *new = malloc(sizeof(struct DictEntry));
if (!new) {
return 1;
}
new->hash = inthash(key);
new->key = key;
new->value = value;
size_t index = get_index(map, key);
map->table[index] = new;
map->size++;
}
return 0;
}
/* Get the element indexed by key, and place it in result
* Returns non-zero and leaves result unchanged on failure */
int hashmap_get(HashMap *map, int key, int *result)
{
struct DictEntry *entry = get_entry(map, key);
if (entry) {
*result = entry->value;
return 0;
}
return 1;
}
/* Remove key:value from the map
*
* Returns non-zero if key does not exist in map */
int hashmap_remove(HashMap *map, int key)
{
size_t index = inthash(key) % map->capacity;
while (map->table[index]) {
if (key == map->table[index]->key) {
free(map->table[index]);
map->table[index] = NULL;
rectify(map, index);
return 0;
}
index = wrapped_inc(index, map->capacity);
}
return 1;
}
static void destroy_table(struct DictEntry **e, size_t size) {
for (size_t i=0; i<size; i++) {
free(e[i]);
}
free(e);
}
static inline unsigned long inthash (int num)
{
return (unsigned long) num * 2654435761;
}
static inline size_t wrapped_inc(size_t n, size_t capacity)
{
return (n + 1) % capacity;
}