-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
311 lines (252 loc) · 6.24 KB
/
hash.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
/**
* Author: Humberto Naves ([email protected])
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "hash.h"
#include "alloc.h"
#include "utils.h"
#include "types.h"
#define INDEX_FOR(hash, size) ((hash) & ((size) - 1))
struct _entry {
void *key, *value;
unsigned int hash;
struct _entry *next;
};
typedef struct _entry *entry;
struct _hashtable {
struct _hashpool *const pool;
unsigned int tablelength;
unsigned int entrycount;
unsigned int loadlimit;
struct _entry **table;
hashfn hashfn;
hashequalsfn eqfn;
};
struct _hashpool {
fixedpool tablepool;
fixedpool entrypool;
};
hashpool hashpool_create (size_t numtables, size_t numentries)
{
hashpool pool = (hashpool) xmalloc (sizeof (struct _hashpool));
pool->tablepool = fixedpool_create (sizeof (struct _hashtable), numtables, 0);
pool->entrypool = fixedpool_create (sizeof (struct _entry), numentries, 0);
return pool;
}
static
void destroy_hashtable (void *ptr, void *arg)
{
hashtable ht = ptr;
if (ht->table) {
free (ht->table);
ht->table = NULL;
}
}
void hashpool_destroy (hashpool pool)
{
fixedpool_destroy (pool->tablepool, &destroy_hashtable, NULL);
fixedpool_destroy (pool->entrypool, NULL, NULL);
free (pool);
}
static
entry alloc_entry (hashpool pool)
{
return fixedpool_alloc (pool->entrypool);
}
static
void free_entry (hashpool pool, entry e)
{
fixedpool_free (pool->entrypool, e);
}
hashtable hashtable_alloc (hashpool pool, unsigned int size, hashfn hashfn, hashequalsfn eqfn)
{
hashtable ht;
hashpool *ptr;
ht = fixedpool_alloc (pool->tablepool);
ht->table = (entry *) xmalloc (sizeof (entry) * size);
memset (ht->table, 0, size * sizeof (entry));
ptr = (hashpool *) &ht->pool;
*ptr = pool;
ht->tablelength = size;
ht->entrycount = 0;
ht->hashfn = hashfn;
ht->eqfn = eqfn;
ht->loadlimit = size >> 1;
return ht;
}
void hashtable_free (hashtable ht, hashtraversefn destroyfn, void *arg)
{
entry e;
unsigned int i;
for (i = 0; i < ht->tablelength; i++) {
for (e = ht->table[i]; e; e = e->next) {
if (destroyfn)
destroyfn (e->key, e->value, e->hash, arg);
fixedpool_free (ht->pool->entrypool, e);
}
}
fixedpool_grow (ht->pool->entrypool, ht->table, ht->tablelength);
ht->table = NULL;
ht->tablelength = 0;
ht->entrycount = 0;
fixedpool_free (ht->pool->tablepool, ht);
}
static
void hashtable_grow (hashtable ht)
{
entry *newtable;
entry e, ne;
unsigned int newsize, i, index;
newsize = ht->tablelength << 1;
newtable = (entry *) xmalloc (sizeof (entry) * newsize);
memset (newtable, 0, newsize * sizeof (entry));
for (i = 0; i < ht->tablelength; i++) {
for (e = ht->table[i]; e; e = ne) {
ne = e->next;
index = INDEX_FOR (e->hash, newsize);
e->next = newtable[index];
newtable[index] = e;
}
}
fixedpool_grow (ht->pool->entrypool, ht->table, ht->tablelength);
ht->table = newtable;
ht->tablelength = newsize;
ht->loadlimit = newsize >> 1;
}
unsigned int hashtable_count (hashtable ht)
{
return ht->entrycount;
}
void hashtable_insert (hashtable ht, void *key, void *value)
{
hashtable_inserthash (ht, key, value, ht->hashfn (key));
}
void hashtable_inserthash (hashtable ht, void *key, void *value, unsigned int hash)
{
unsigned int index;
entry e;
if (ht->entrycount >= ht->loadlimit) {
hashtable_grow (ht);
}
e = alloc_entry (ht->pool);
e->hash = hash;
index = INDEX_FOR (e->hash, ht->tablelength);
e->key = key;
e->value = value;
e->next = ht->table[index];
ht->entrycount++;
ht->table[index] = e;
}
static
entry find_entry (hashtable ht, void *key, unsigned int hash, int remove)
{
entry e;
entry *prev;
unsigned int index;
index = INDEX_FOR (hash, ht->tablelength);
for (prev = &(ht->table[index]); (e = *prev) ; prev = &e->next) {
if (hash != e->hash) continue;
if (key != e->key)
if (!ht->eqfn (key, e->key, hash))
continue;
if (remove) {
*prev = e->next;
ht->entrycount--;
free_entry (ht->pool, e);
}
return e;
}
return NULL;
}
void *hashtable_search (hashtable ht, void *key, void **key_found)
{
return hashtable_searchhash (ht, key, key_found, ht->hashfn (key));
}
void *hashtable_searchhash (hashtable ht, void *key, void **key_found, unsigned int hash)
{
entry e;
e = find_entry (ht, key, hash, 0);
if (e) {
if (key_found)
*key_found = e->key;
return e->value;
}
return NULL;
}
int hashtable_haskey (hashtable ht, void *key, void **key_found)
{
return hashtable_haskeyhash (ht, key, key_found, ht->hashfn (key));
}
int hashtable_haskeyhash (hashtable ht, void *key, void **key_found, unsigned int hash)
{
entry e = find_entry (ht, key, hash, 0);
if (e) {
if (key_found)
*key_found = e->key;
return TRUE;
}
return FALSE;
}
void *hashtable_remove (hashtable ht, void *key, void **key_found)
{
return hashtable_removehash (ht, key, key_found, ht->hashfn (key));
}
void *hashtable_removehash (hashtable ht, void *key, void **key_found, unsigned int hash)
{
entry e = find_entry (ht, key, hash, 1);
if (e) {
if (key_found)
*key_found = e->key;
return e->value;
}
return NULL;
}
void hashtable_traverse (hashtable ht, hashtraversefn traversefn, void *arg)
{
entry e;
unsigned int i;
for (i = 0; i < ht->tablelength; i++) {
for (e = ht->table[i]; e; e = e->next) {
traversefn (e->key, e->value, e->hash, arg);
}
}
}
int hashtable_string_compare (void *key1, void *key2, unsigned int hash)
{
return (strcmp (key1, key2) == 0);
}
int hashtable_pointer_compare (void *key1, void *key2, unsigned int hash)
{
return (key1 == key2);
}
unsigned int hashtable_hash_bytes (unsigned char *key, size_t len)
{
unsigned int hash = 0;
size_t i;
for (i = 0; i < len; i++) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
unsigned int hashtable_hash_string (void *key)
{
unsigned int hash = 0;
unsigned char *bytes = (unsigned char *) key;
while (*bytes) {
hash += *bytes++;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}