-
Notifications
You must be signed in to change notification settings - Fork 33
/
genhash.c
334 lines (280 loc) · 6.67 KB
/
genhash.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
/*
* Copyright (c) 2006 Dustin Sallings <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "genhash.h"
#include "genhash_int.h"
/* Table of 32 primes by their distance from the nearest power of two */
static int prime_size_table[]={
3, 7, 13, 23, 47, 97, 193, 383, 769, 1531, 3067, 6143, 12289, 24571, 49157,
98299, 196613, 393209, 786433, 1572869, 3145721, 6291449, 12582917,
25165813, 50331653, 100663291, 201326611, 402653189, 805306357,
1610612741
};
static int
estimate_table_size(int est)
{
int rv=0;
int magn=0;
assert(est > 0);
magn=(int)log((double)est)/log(2);
magn--;
magn = (magn < 0) ? 0 : magn;
assert(magn < (int) (sizeof(prime_size_table) / sizeof(int)));
rv=prime_size_table[magn];
return rv;
}
genhash_t* genhash_init(int est, struct hash_ops ops)
{
genhash_t* rv=NULL;
int size=0;
if (est < 1) {
return NULL;
}
assert(ops.hashfunc != NULL);
assert(ops.hasheq != NULL);
assert(ops.dupKey != NULL);
assert(ops.dupValue != NULL);
assert(ops.freeKey != NULL);
assert(ops.freeValue != NULL);
size=estimate_table_size(est);
rv=calloc(1, sizeof(genhash_t)
+ (size * sizeof(struct genhash_entry_t *)));
assert(rv != NULL);
rv->size=size;
rv->ops=ops;
return rv;
}
static void
free_bucket(genhash_t* h, struct genhash_entry_t* b)
{
if(b != NULL) {
free_bucket(h, b->next);
h->ops.freeKey(b->key);
h->ops.freeValue(b->value);
free(b);
}
}
void
genhash_free(genhash_t* h)
{
if(h != NULL) {
size_t i=0;
for(i=0; i<h->size; i++) {
free_bucket(h, h->buckets[i]);
}
free(h);
}
}
void
genhash_store(genhash_t *h, const void* k, const void* v)
{
int n=0;
struct genhash_entry_t *p;
assert(h != NULL);
n=h->ops.hashfunc(k) % h->size;
assert(n >= 0);
assert(n < (int) h->size);
p=calloc(1, sizeof(struct genhash_entry_t));
assert(p);
p->key=h->ops.dupKey(k);
p->value=h->ops.dupValue(v);
p->next=h->buckets[n];
h->buckets[n]=p;
}
static struct genhash_entry_t *
genhash_find_entry(genhash_t *h, const void* k)
{
int n=0;
struct genhash_entry_t *p;
assert(h != NULL);
n=h->ops.hashfunc(k) % h->size;
assert(n >= 0);
assert(n < (int) h->size);
p=h->buckets[n];
for(p=h->buckets[n]; p && !h->ops.hasheq(k, p->key); p=p->next);
return p;
}
void*
genhash_find(genhash_t *h, const void* k)
{
struct genhash_entry_t *p;
void *rv=NULL;
p=genhash_find_entry(h, k);
if(p) {
rv=p->value;
}
return rv;
}
enum update_type
genhash_update(genhash_t* h, const void* k, const void* v)
{
struct genhash_entry_t *p;
enum update_type rv=0;
p=genhash_find_entry(h, k);
if(p) {
void *k2=h->ops.dupKey(k);
h->ops.freeKey(p->key);
p->key=k2;
void *v2=h->ops.dupValue(v);
h->ops.freeValue(p->value);
p->value=v2;
rv=MODIFICATION;
} else {
genhash_store(h, k, v);
rv=NEW;
}
return rv;
}
enum update_type
genhash_fun_update(genhash_t* h, const void* k,
void *(*upd)(const void *, const void *),
void (*fr)(void*),
const void *def)
{
struct genhash_entry_t *p;
enum update_type rv=0;
p=genhash_find_entry(h, k);
if(p) {
void *newValue=upd(k, p->value);
void *k2=h->ops.dupKey(k);
h->ops.freeKey(p->key);
p->key=k2;
void *v2=h->ops.dupValue(newValue);
h->ops.freeValue(p->value);
p->value=v2;
fr(newValue);
rv=MODIFICATION;
} else {
void *newValue=upd(k, def);
genhash_store(h, k, newValue);
fr(newValue);
rv=NEW;
}
return rv;
}
int
genhash_delete(genhash_t* h, const void* k)
{
struct genhash_entry_t *deleteme=NULL;
int n=0;
int rv=0;
assert(h != NULL);
n=h->ops.hashfunc(k) % h->size;
assert(n >= 0);
assert(n < (int) h->size);
if(h->buckets[n] != NULL) {
/* Special case the first one */
if(h->ops.hasheq(h->buckets[n]->key, k)) {
deleteme=h->buckets[n];
h->buckets[n]=deleteme->next;
} else {
struct genhash_entry_t *p=NULL;
for(p=h->buckets[n]; deleteme==NULL && p->next != NULL; p=p->next) {
if(h->ops.hasheq(p->next->key, k)) {
deleteme=p->next;
p->next=deleteme->next;
}
}
}
}
if(deleteme != NULL) {
h->ops.freeKey(deleteme->key);
h->ops.freeValue(deleteme->value);
free(deleteme);
rv++;
}
return rv;
}
int
genhash_delete_all(genhash_t* h, const void* k)
{
int rv=0;
while(genhash_delete(h, k) == 1) {
rv++;
}
return rv;
}
void
genhash_iter(genhash_t* h,
void (*iterfunc)(const void* key, const void* val, void *arg), void *arg)
{
size_t i=0;
struct genhash_entry_t *p=NULL;
assert(h != NULL);
for(i=0; i<h->size; i++) {
for(p=h->buckets[i]; p!=NULL; p=p->next) {
iterfunc(p->key, p->value, arg);
}
}
}
int
genhash_clear(genhash_t *h)
{
size_t i = 0;
assert(h != NULL);
for(i = 0; i < h->size; i++) {
while(h->buckets[i]) {
struct genhash_entry_t *p = NULL;
p = h->buckets[i];
h->buckets[i] = p->next;
h->ops.freeKey(p->key);
h->ops.freeValue(p->value);
free(p);
}
}
return 0;
}
static void
count_entries(const void *key, const void *val, void *arg)
{
(void)key;
(void)val;
int *count=(int *)arg;
(*count)++;
}
int
genhash_size(genhash_t* h) {
int rv=0;
assert(h != NULL);
genhash_iter(h, count_entries, &rv);
return rv;
}
int
genhash_size_for_key(genhash_t* h, const void* k)
{
int rv=0;
assert(h != NULL);
genhash_iter_key(h, k, count_entries, &rv);
return rv;
}
void
genhash_iter_key(genhash_t* h, const void* key,
void (*iterfunc)(const void* key, const void* val, void *arg), void *arg)
{
int n=0;
struct genhash_entry_t *p=NULL;
assert(h != NULL);
n=h->ops.hashfunc(key) % h->size;
assert(n >= 0);
assert(n < (int) h->size);
for(p=h->buckets[n]; p!=NULL; p=p->next) {
if(h->ops.hasheq(key, p->key)) {
iterfunc(p->key, p->value, arg);
}
}
}
int
genhash_string_hash(const void* p)
{
int rv=5381;
int i=0;
char *str=(char *)p;
for(i=0; str[i] != 0x00; i++) {
rv = ((rv << 5) + rv) ^ str[i];
}
return rv;
}