-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
295 lines (244 loc) · 4.96 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
/*
* hash.c
*
* Copyright (C) 2007 Slawomir Maludzinski
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#define HASH_INIT_SIZE 5
#define HASH_MAGIC 5381
#define HASH_W_MARK 0.8
unsigned long hash_str(const char * str)
{
int c;
unsigned long hash = HASH_MAGIC;
while ((c = *str++) != 0)
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash;
}
int hash_get_watermark(hash * h)
{
return 1.0 * h->size * HASH_W_MARK;
}
hash * hash_new()
{
return hash_new_delete_size(NULL, HASH_INIT_SIZE);
}
hash * hash_new_size(int size)
{
return hash_new_delete_size(NULL, size);
}
hash * hash_new_delete(hash_item_delete * item_delete_func)
{
return hash_new_delete_size(item_delete_func, HASH_INIT_SIZE);
}
hash * hash_new_delete_size(hash_item_delete * item_delete_func, int size)
{
hash * ret = NULL;
ret = malloc(sizeof(hash));
if (ret == NULL)
{
fprintf(stderr, "cannot allocate hash\n");
}
ret->item_delete_func = item_delete_func;
ret->elems = 0;
ret->size = size;
ret->watermark = hash_get_watermark(ret);
ret->items = calloc(ret->size, sizeof(hash_item));
if (ret->items == NULL)
{
fprintf(stderr, "cannot allocate hash items table\n");
}
return ret;
}
void hash_item_null_deallocator(hash_item * item)
{
}
void hash_delete(hash * h)
{
int i;
if (h == NULL)
{
return;
}
if (h->items != NULL && h->item_delete_func != NULL)
{
for (i = 0; i < h->size; i++)
{
h->item_delete_func(h->items[i].key, h->items[i].value);
}
}
if (h->items != NULL)
{
free(h->items);
}
free(h);
}
char hash_is_prime(int number)
{
int d;
for (d = 2; d * d <= number; d++)
{
if ((number % d) == 0)
{
return 0;
}
}
return 1;
}
int hash_next_prime(int start)
{
int s;
for (s = start; ;s++)
{
if (hash_is_prime(s))
{
return s;
}
}
return start;
}
int hash_inc_size(hash * h)
{
return hash_next_prime(h->size * 2);
}
void hash_resize(hash * h)
{
int i;
int h_ns = hash_inc_size(h);
hash_item * h_i = calloc(h_ns, sizeof(hash_item));
for (i = 0; i < h->size; i++)
{
if (h->items[i].key != 0)
{
unsigned long hs = hash_str(h->items[i].key);
while (h_i[hs % h_ns].key != 0)
{
hs++;
}
h_i[hs % h_ns].key = h->items[i].key;
h_i[hs % h_ns].value = h->items[i].value;
}
}
h->size = h_ns;
h->watermark = hash_get_watermark(h);
free(h->items);
h->items = h_i;
}
void hash_insert(hash * h, const char * key, void * value)
{
if (h->elems + 1 > h->watermark)
{
hash_resize(h);
}
unsigned long hs = hash_str(key);
while (h->items[hs % h->size].key != 0)
{
hs++;
}
h->elems++;
h->items[hs % h->size].key = key;
h->items[hs % h->size].value = value;
}
void * hash_search(hash * h, const char * key)
{
if (h == NULL || h->items == NULL)
{
return NULL;
}
unsigned long hs = hash_str(key);
while (h->items[hs % h->size].key != 0)
{
if (strcmp(h->items[hs % h->size].key, key) == 0)
{
return h->items[hs % h->size].value;
}
hs++;
}
return NULL;
}
char hash_contains(hash * h, const char * key)
{
if (h == NULL || h->items == NULL)
{
return 0;
}
unsigned long hs = hash_str(key);
while (h->items[hs % h->size].key != 0)
{
if (strcmp(h->items[hs % h->size].key, key) == 0)
{
return 1;
}
hs++;
}
return 0;
}
void hash_remove(hash * h, const char * key)
{
if (h == NULL || h->items == NULL)
{
return;
}
unsigned long hs = hash_str(key);
while (h->items[hs & h->size].key != 0)
{
if (strcmp(h->items[hs % h->size].key, key) == 0)
{
if (h->item_delete_func != NULL)
{
h->item_delete_func(h->items[hs % h->size].key,
h->items[hs % h->size].value);
}
h->items[hs % h->size].key = 0;
h->items[hs % h->size].value = 0;
h->elems--;
return;
}
}
}
void hash_item_null_printer(void * value)
{
printf("(data)");
}
void hash_print(hash * h)
{
hash_printer(h, hash_item_null_printer);
}
void hash_printer(hash * h, hash_item_printer * printer)
{
int i;
int c = 0;
printf("{ ");
for (i = 0; i < h->size; i++)
{
if (h->items[i].key != 0)
{
if (c)
{
printf(", ");
}
c = 1;
printf("%s", h->items[i].key); printf(" : "); printer(h->items[i].value);
}
}
printf(" }\n");
}