-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.c
339 lines (287 loc) · 6.93 KB
/
hashtable.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdio.h>
#include <stdlib.h>
#include "hashtable.h"
#define HIGH .7 //maximum acceptable load factor
#define TARGET .5 //target load factor when rehashing
struct hashtable {
void **keys;
void **values;
int *occ;
unsigned long length;
unsigned long size;
int (*equals)(void*, void*);
unsigned long (*hash)(void*);
};
static int physeql(void* x, void* y);
static unsigned long djb2(void* x);
static int is_prime_mr(size_t);
hashtable ht_create(unsigned init_size,
int (*equals)(void*,void*),
unsigned long (*hash)(void*))
{
if (init_size == 0) {
init_size = 17;
}
hashtable h=malloc(sizeof(struct hashtable));
h->keys=malloc(init_size*sizeof(void*));
h->values=malloc(init_size*sizeof(void*));
h->occ=calloc(init_size, sizeof(int));
h->length=0;
h->size=init_size;
if (equals == NULL) {
h -> equals = &physeql;
} else {
h -> equals = equals;
}
if (hash == NULL) {
h -> hash = &djb2;
} else {
h -> hash = hash;
}
return h;
}
static void print_err(char *str)
{
#if PRINT_ERR
fprintf(stderr, str);
#endif
}
// return the prime that comes after x
int next_prime(int x)
{
x++;
while(1) {
if(is_prime_mr((size_t) x)) {
return x;
}
x++;
}
}
void ht_free(hashtable h) {
if (h == NULL) {
print_err("Tried to free NULL\n");
return;
}
free(h->keys);
free(h->values);
free(h->occ);
free(h);
}
/*
if h has an acceptable load factor, do nothing. If not,
find the next prime that brings the load factor below the
target, and rehash to that size.
*/
void rehash(hashtable h) {
if (h == NULL) {
print_err("Tried to rehash NULL\n");
return;
}
//now the real fight begins, therefore try hashing
float lf=(float)(h->length+1)/(float)h->size;
if(lf<HIGH) {
return;
}
int newsize=h->size;
do {
newsize=next_prime(newsize);
} while((float)h->length/(float)newsize>TARGET);
hashtable new=ht_create(newsize, h->equals, h->hash);
//iterate over hashtable, insert stuff into the new hashtable
for(unsigned i=0; i<h->size; i++) {
if(!h->occ[i]) continue;
ht_put(new, h->keys[i], h->values[i]);
}
//gut new hashtable and free the remains
h->size=newsize;
free(h->keys);
h->keys=new->keys;
free(h->values);
h->values=new->values;
free(h->occ);
h->occ=new->occ;
free(new);
}
/*
Find the index where the key lives in the hashtable. If the key does
live in the hashtable, its non-negative index will be returned. If it
does not, a negative number will be returned.
INVARIANT: h must be non-null, and key must be valid
*/
static long long ht_find(hashtable h, void *key)
{
if (h == NULL) {
print_err("Invariant broken in ht_find\n");
return -1;
}
int count=0;
unsigned long hash=h->hash(&key);
unsigned long ind=hash % h->size;
while(count < h->size) {
if(h->occ[ind] && h->equals(h->keys[ind], key)) {
return ind;
} else {
count++;
ind=(hash+count*count)%h->size;
}
}
return -1;
}
void ht_remove(hashtable h, void *key) {
if (h == NULL) {
print_err("Tried to remove element from NULL hashtable\n");
return;
}
long long ind=ht_find(h, key);
if (ind >= 0) {
h->occ[ind]=0;
h->keys[ind]=0;
h->values[ind]=0;
h->length--;
}
}
void *ht_get(hashtable h, void *key) {
if (h == NULL) {
print_err("Tried to get element from NULL hashtable\n");
return NULL;
}
long long ind=ht_find(h, key);
if (ind >= 0) {
return h->values[ind];
} else {
return NULL;
}
}
//logic borrowed from wikipedia
void ht_put(hashtable h, void *key, void *val) {
if (h == NULL) {
print_err("Tried to put element in NULL hashtable\n");
return;
}
int count=0;
unsigned long hash=h->hash(&key);
unsigned long ind=hash % h->size;
rehash(h);
while(count < h->size) {
if(!(h->occ[ind])) {
h->keys[ind]=key;
h->values[ind]=val;
h->occ[ind]=1;
h->length++;
return;
} else if(h->occ[ind] && h->equals(h->keys[ind], key)) {
h->values[ind]=val;
return;
} else{
count++;
ind=(hash+count*count)%h->size;
}
}
}
unsigned ht_len(hashtable h)
{
if (h == NULL) {
return 0;
} else {
return h->length;
}
}
unsigned ht_size(hashtable h)
{
if (h == NULL) {
return 0;
} else {
return h->size;
}
}
unsigned long djb2(void* x) {
unsigned long hash=5381;
unsigned char* str=(unsigned char*)x;
hash=((hash<<5)+hash)+str[0];
hash=((hash<<5)+hash)+str[1];
hash=((hash<<5)+hash)+str[2];
hash=((hash<<5)+hash)+str[3];
return hash;
}
int physeql(void *x, void *y)
{
return (x==y);
}
int ht_containskey(hashtable h, void *key)
{
if(h == NULL) {
print_err("Tried to test containment in NULL\n");
}
return (ht_find(h, key) < 0) ? 0 : 1;
}
/* ---------------------- PRIME NUMBER FUNCTIONS----------------- */
/* These are taken from Rosetta code. I take no responsibility for their style*/
typedef int bool;
#define true 1
#define false 0
//bool is_prime_mr(size_t n);
// calcul a^n%mod
static size_t power(size_t a, size_t n, size_t mod)
{
size_t power = a;
size_t result = 1;
while (n)
{
if (n & 1)
result = (result * power) % mod;
power = (power * power) % mod;
n >>= 1;
}
return result;
}
// n−1 = 2^s * d with d odd by factoring powers of 2 from n−1
static bool witness(size_t n, size_t s, size_t d, size_t a)
{
size_t x = power(a, d, n);
size_t y=0;
while (s) {
y = (x * x) % n;
if (y == 1 && x != 1 && x != n-1)
return false;
x = y;
--s;
}
if (y != 1)
return false;
return true;
}
/*
* if n < 1,373,653, it is enough to test a = 2 and 3;
* if n < 9,080,191, it is enough to test a = 31 and 73;
* if n < 4,759,123,141, it is enough to test a = 2, 7, and 61;
* if n < 1,122,004,669,633, it is enough to test a = 2, 13, 23, and 1662803;
* if n < 2,152,302,898,747, it is enough to test a = 2, 3, 5, 7, and 11;
* if n < 3,474,749,660,383, it is enough to test a = 2, 3, 5, 7, 11, and 13;
* if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17.
*/
static bool is_prime_mr(size_t n) {
if (((!(n & 1)) && n != 2 ) || (n < 2) || (n % 3 == 0 && n != 3))
return false;
if (n <= 3)
return true;
size_t d = n / 2;
size_t s = 1;
while (!(d & 1)) {
d /= 2;
++s;
}
if (n < 1373653)
return witness(n, s, d, 2) && witness(n, s, d, 3);
if (n < 9080191)
return witness(n, s, d, 31) && witness(n, s, d, 73);
if (n < 4759123141)
return witness(n, s, d, 2) && witness(n, s, d, 7) && witness(n, s, d, 61);
if (n < 1122004669633)
return witness(n, s, d, 2) && witness(n, s, d, 13) && witness(n, s, d, 23) && witness(n, s, d, 1662803);
if (n < 2152302898747)
return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11);
if (n < 3474749660383)
return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13);
return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) &&
witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13) && witness(n, s, d, 17);
}