forked from hishamhm/dit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hashtable.c
286 lines (245 loc) · 6.94 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
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include "Prototypes.h"
/*{
#define Hashtable_BORROW_REFS 0
#define Hashtable_OWN_REFS 1
typedef enum {
Hashtable_PTR,
Hashtable_I,
Hashtable_STR
} Hashtable_type;
typedef union {
void* ptr;
int i;
const char* str;
} HashtableKey;
typedef int(*Hashtable_hash_fn)(Hashtable*, HashtableKey);
typedef int(*Hashtable_eq_fn)(Hashtable*, HashtableKey, HashtableKey);
struct HashtableItem_ {
HashtableKey key;
void* value;
HashtableItem* next;
};
struct Hashtable_ {
int size;
HashtableItem** buckets;
int items;
Hashtable_type type;
Hashtable_hash_fn hash;
Hashtable_eq_fn eq;
int owner;
};
struct HashtableIterator_ {
Hashtable* table;
int bucket;
HashtableItem* item;
};
}*/
static HashtableItem* HashtableItem_new(HashtableKey key, void* value) {
HashtableItem* this;
this = (HashtableItem*) calloc(sizeof(HashtableItem), 1);
this->key = key;
this->value = value;
this->next = NULL;
return this;
}
static HashtableItem* HashtableItem_newString(const char* key, void* value) {
HashtableItem* this;
int len = strlen(key);
this = (HashtableItem*) calloc(sizeof(HashtableItem)+len+1, 1);
this->key.str = (const char*) this+sizeof(HashtableItem);
strcpy((char*) this+sizeof(HashtableItem), key);
this->value = value;
this->next = NULL;
return this;
}
static int Hashtable_intEq(Hashtable* this, HashtableKey a, HashtableKey b) {
return a.i == b.i;
}
static int Hashtable_intHash(Hashtable* this, HashtableKey key) {
return (abs(key.i) % this->size);
}
static inline int Hashtable_stringEq(Hashtable* this, HashtableKey a, HashtableKey b) {
int lena = strlen(a.str);
int lenb = strlen(b.str);
if (lena != lenb)
return 0;
return (strncmp(a.str, b.str, lena) == 0);
}
static inline int Hashtable_stringHash(Hashtable* this, HashtableKey key) {
const char* str = key.str;
unsigned long hash = 0;
int c;
/* http://www.cs.yorku.ca/~oz/hash.html */
while ((c = *str++))
hash = c + (hash << 6) + (hash << 16) - hash;
return hash % this->size;
}
Hashtable* Hashtable_new(int size, Hashtable_type type, int owner) {
Hashtable* this;
this = (Hashtable*) calloc(sizeof(Hashtable), 1);
this->size = size;
this->buckets = (HashtableItem**) calloc(sizeof(HashtableItem*), size);
this->type = type;
switch (type) {
case Hashtable_STR:
this->eq = Hashtable_stringEq;
this->hash = Hashtable_stringHash;
break;
case Hashtable_PTR:
case Hashtable_I:
this->eq = Hashtable_intEq;
this->hash = Hashtable_intHash;
}
this->owner = owner;
return this;
}
void Hashtable_delete(Hashtable* this) {
int i;
for (i = 0; i < this->size; i++) {
HashtableItem* walk = this->buckets[i];
while (walk != NULL) {
if (this->owner)
free(walk->value);
HashtableItem* save = walk;
walk = save->next;
free(save);
}
}
free(this->buckets);
free(this);
}
int Hashtable_size(Hashtable* this) {
return this->items;
}
void Hashtable_putInt(Hashtable* this, int key, void* value) {
HashtableKey hk;
hk.i = key;
int index = Hashtable_intHash(this, hk);
HashtableItem** bucket;
for (bucket = &(this->buckets[index]); *bucket; bucket = &((*bucket)->next) ) {
if (Hashtable_intEq(this, (*bucket)->key, hk)) {
if (this->owner)
free((*bucket)->value);
(*bucket)->value = value;
return;
}
}
*bucket = HashtableItem_new(hk, value);
}
void Hashtable_put(Hashtable* this, HashtableKey key, void* value) {
if (this->type == Hashtable_STR) {
Hashtable_putString(this, key.str, value);
return;
}
int index = this->hash(this, key);
HashtableItem** bucket;
for (bucket = &(this->buckets[index]); *bucket; bucket = &((*bucket)->next) ) {
if (this->eq(this, (*bucket)->key, key)) {
if (this->owner)
free((*bucket)->value);
(*bucket)->value = value;
return;
}
}
*bucket = HashtableItem_new(key, value);
this->items++;
return;
}
void Hashtable_putString(Hashtable* this, const char* key, void* value) {
HashtableKey hk;
hk.str = key;
int index = Hashtable_stringHash(this, hk);
HashtableItem** bucket;
for (bucket = &(this->buckets[index]); *bucket; bucket = &((*bucket)->next) ) {
if (Hashtable_stringEq(this, (*bucket)->key, hk)) {
if (this->owner)
free((*bucket)->value);
(*bucket)->value = value;
return;
}
}
*bucket = HashtableItem_newString(key, value);
}
void* Hashtable_take(Hashtable* this, HashtableKey key) {
int index = this->hash(this, key);
HashtableItem** bucket;
for (bucket = &(this->buckets[index]); *bucket; bucket = &((*bucket)->next) ) {
if (this->eq(this, (*bucket)->key, key)) {
void* value = (*bucket)->value;
HashtableItem* next = (*bucket)->next;
free(*bucket);
(*bucket) = next;
this->items--;
return value;
}
}
return NULL;
}
void* Hashtable_remove(Hashtable* this, HashtableKey key) {
void* value = Hashtable_take(this, key);
if (this->owner) {
free(value);
return NULL;
}
return value;
}
void* Hashtable_get(Hashtable* this, HashtableKey key) {
int index = this->hash(this, key);
HashtableItem* bucket;
for(bucket = this->buckets[index]; bucket; bucket = bucket->next)
if (this->eq(this, bucket->key, key))
return bucket->value;
return NULL;
}
void* Hashtable_getInt(Hashtable* this, int key) {
HashtableKey hk;
hk.i = key;
int index = Hashtable_intHash(this, hk);
HashtableItem* bucket;
for(bucket = this->buckets[index]; bucket; bucket = bucket->next)
if (Hashtable_intEq(this, bucket->key, hk))
return bucket->value;
return NULL;
}
void* Hashtable_getString(Hashtable* this, const char* key) {
HashtableKey hk;
hk.str = key;
int index = Hashtable_stringHash(this, hk);
HashtableItem* bucket;
for(bucket = this->buckets[index]; bucket; bucket = bucket->next)
if (Hashtable_stringEq(this, bucket->key, hk))
return bucket->value;
return NULL;
}
void* Hashtable_takeFirst(Hashtable* this) {
int i;
for (i = 0; i < this->size; i++) {
HashtableItem* bucket = this->buckets[i];
if (bucket)
return Hashtable_take(this, bucket->key);
}
return NULL;
}
void Hashtable_start(Hashtable* this, HashtableIterator* iter) {
iter->table = this;
iter->bucket = 0;
iter->item = this->buckets[0];
}
void* Hashtable_iterate(HashtableIterator* iter) {
void* result;
for(;;) {
if (!iter->item) {
iter->bucket++;
if (iter->bucket >= iter->table->size)
return NULL;
iter->item = iter->table->buckets[iter->bucket];
continue;
}
result = iter->item->value;
iter->item = iter->item->next;
return result;
}
}