-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.c
646 lines (576 loc) · 12.2 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* hashtable.c
**
** pem 2001-05-13, 2009-08-12
**
*/
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "hashtable.h"
#ifndef USE_MACROS
#define USE_MACROS 1
#endif
#define SWAP(A, B, TMP) ((TMP) = (A), (A) = (B), (B) = (TMP))
/*
** A key string type that avoids allocating small chunks
*/
#define HKEY_SHORT 7
typedef struct hkey_s
{
char str[HKEY_SHORT+1];
char *strp;
} hkey_t;
/* Returns strcmp style values, e.g. -1, 0, 1 */
static int
hkey_comp(hkey_t *hkeyp, const char *s)
{
if (hkeyp->strp)
return strcmp(hkeyp->strp, s);
else
return strcmp(hkeyp->str, s);
}
#if USE_MACROS
#define hkey_is_set(HP) ((HP)->strp != NULL || (HP)->str[0] != '\0')
#else
static bool
hkey_is_set(hkey_t *hkeyp)
{
return (hkeyp->strp != NULL || hkeyp->str[0] != '\0');
}
#endif
static bool
hkey_set(hkey_t *hkeyp, const char *s)
{
bool ret = true;
size_t len = strlen(s);
if (len <= HKEY_SHORT)
{
strcpy(hkeyp->str, s);
hkeyp->strp = NULL;
}
else
{
hkeyp->str[0] = '\0';
hkeyp->strp = (char *)malloc(len+1);
if (hkeyp->strp == NULL)
ret = false;
else
strncpy(hkeyp->strp, s, len+1);
}
return ret;
}
#if USE_MACROS
#define hkey_key(HP) ((HP)->strp ? (HP)->strp : (HP)->str)
#else
static char *
hkey_key(hkey_t *hkeyp)
{
return (hkeyp->strp ? hkeyp->strp : hkeyp->str);
}
#endif
static void
hkey_clear(hkey_t *hkeyp)
{
if (hkeyp)
{
if (hkeyp->strp)
free(hkeyp->strp);
hkeyp->strp = NULL;
hkeyp->str[0] = '\0';
}
}
/*
** A hashed datum structure
*/
typedef struct datum_s
{
hkey_t hkey;
void *value;
struct datum_s *next;
} datum_t;
#if USE_MACROS
#define datum_set_value(DP, V) ((DP)->value = (V))
#define datum_set_next(DP1, DP2) ((DP1)->next = (DP2))
#else
static void
datum_set_value(datum_t *dp, void *val)
{
dp->value = val;
}
static void
datum_set_next(datum_t *dp1, datum_t *dp2)
{
dp1->next = dp2;
}
#endif /* !USE_MACROS */
static bool
datum_set(datum_t *dp, const char *hkey, void *val, datum_t *nextp)
{
hkey_clear(&dp->hkey);
if (!hkey_set(&dp->hkey, hkey))
return false;
dp->value = val;
dp->next = nextp;
return true;
}
static datum_t *
datum_copy(datum_t *dp)
{
datum_t *dp2 = malloc(sizeof(datum_t));
if (dp2)
{
memset(&dp2->hkey, 0, sizeof(dp2->hkey));
if (!datum_set(dp2, hkey_key(&dp->hkey), dp->value, dp->next))
{
free(dp2);
dp2 = NULL;
}
}
return dp2;
}
static void
datum_clear(datum_t *dp)
{
if (dp)
{
hkey_clear(&dp->hkey);
dp->value = NULL;
dp->next = NULL;
}
}
static void
datum_free(datum_t *dp)
{
datum_clear(dp);
free(dp);
}
#if USE_MACROS
#define datum_is_set(DP) hkey_is_set(&(DP)->hkey)
#define datum_key(DP) hkey_key(&(DP)->hkey)
#define datum_value(DP) ((DP)->value)
#define datum_comp(DP, S) hkey_comp(&(DP)->hkey, (S))
#define datum_next(DP) ((DP)->next)
#else
static bool
datum_is_set(datum_t *dp)
{
return hkey_is_set(&dp->hkey);
}
static char *
datum_key(datum_t *dp)
{
return hkey_key(&dp->hkey);
}
static void *
datum_value(datum_t *dp)
{
return dp->value;
}
/* Returns strcmp style values, e.g. -1, 0, 1 */
static int
datum_comp(datum_t *dp, const char *s)
{
return hkey_comp(&dp->hkey, s);
}
static datum_t *
datum_next(datum_t *dp)
{
return dp->next;
}
#endif /* !USE_MACROS */
/*
** Two good string hash functions
*/
hashval_t
hash_string_fast(const char *s)
{
hashval_t val = 0;
while (*s)
val += (val << 3) + *s++;
return val;
}
#define SEED_MAX 2147483646
hashval_t
hash_string_good(const char *s)
{
uint32_t i;
hashval_t val;
union
{
hashval_t ul;
char s[sizeof(hashval_t)];
} u;
/* Pack the first word */
i = 0;
while (i < sizeof(u.s) && *s)
u.s[i++] = *s++;
while (i < sizeof(u.s))
u.s[i++] = '\0'; /* Pad if necessary */
if (u.ul > SEED_MAX)
u.ul -= SEED_MAX;
/* Inline coding a the minimal standard pseudo random number generator */
#define RAND_A 16807
#define RAND_M 2147483647
#define RAND_Q 127773 /* m div a */
#define RAND_R 2836 /* m mod a */
{
register int32_t lo, hi, test;
hi = u.ul/RAND_Q;
lo = u.ul - hi*RAND_Q; /* Seed mod RAND_Q */
test = RAND_A*lo - RAND_R*hi;
if (test <= 0)
test += RAND_M;
val = test;
}
#undef RAND_A
#undef RAND_M
#undef RAND_Q
#undef RAND_R
/* The rest is simply xor:ed wordwise */
while (*s)
{
i = 0;
while (i < sizeof(u.s) && *s)
u.s[i++] = *s++;
while (i < sizeof(u.s))
u.s[i++] = '\0';
val ^= u.ul;
}
return val;
}
#undef SEED_MAX
/*
** The hash table
*/
struct hashtable_s
{
size_t size;
size_t count;
float minload;
float maxload;
hashfunc_t *hfun; /* Hash function */
hashdestfunc_t *dfun; /* Destructor */
datum_t *data;
};
hashtable_t
hashtable_create(size_t initsize, float minload, float maxload,
hashfunc_t *hfun,
hashdestfunc_t *dfun)
{
hashtable_t table = malloc(sizeof(struct hashtable_s));
if (table)
{
if (initsize == 0)
initsize = 101;
initsize |= 1; /* Make it odd, it helps some hash functions */
if (maxload < 0.5 || 1.0 <= maxload)
maxload = 0.8;
if (minload < 0.2 || maxload <= minload)
minload = 0.5;
if (minload >= maxload)
minload = maxload / 2;
table->size = initsize;
table->count = 0;
table->minload = minload;
table->maxload = maxload;
if (!hfun)
hfun = hash_string_fast;
table->hfun = hfun;
table->dfun = dfun;
table->data = malloc(initsize * sizeof(datum_t));
if (table->data == NULL)
{
free(table);
return NULL;
}
memset(table->data, 0, initsize * sizeof(datum_t));
}
return table;
}
void
hashtable_clear(hashtable_t h)
{
for (size_t i = 0 ; i < h->size ; i++)
{
datum_t *dp = h->data + i;
if (datum_is_set(dp))
{
void *val = datum_value(dp);
datum_t *nextp = datum_next(dp);
datum_clear(dp);
if (h->dfun)
h->dfun (val);
dp = nextp;
while (dp)
{
nextp = datum_next(dp);
if (h->dfun)
h->dfun (datum_value(dp));
datum_free(dp);
dp = nextp;
}
}
}
h->count = 0;
memset(h->data, 0, h->size * sizeof(datum_t));
}
void
hashtable_destroy(hashtable_t h)
{
hashtable_clear(h);
free(h->data);
free(h);
}
static hashtable_ret_t
hashtable_put_nogrow(hashtable_t h, const char *key, void *val, void **oldvalp);
/* Returns true on sucess
** Returns false on failure
*/
static bool
hashtable_grow(hashtable_t h)
{
size_t newsize = (size_t) (h->count / h->minload);
hashtable_t h2 = hashtable_create(newsize, h->minload, h->maxload,
h->hfun, NULL);
if (h2)
{
size_t i;
datum_t *data;
for (i = 0 ; i < h->size ; i++)
{
datum_t *dp = h->data + i;
if (datum_is_set(dp))
{
datum_t *nextp = datum_next(dp);
if (hashtable_put_nogrow(h2, datum_key(dp), datum_value(dp), NULL) < 0)
{
hashtable_destroy(h2);
return false;
}
dp = nextp;
while (dp)
{
nextp = datum_next(dp);
if (hashtable_put_nogrow(h2, datum_key(dp), datum_value(dp), NULL) < 0)
{
hashtable_destroy(h2);
return false;
}
dp = nextp;
}
}
}
SWAP(h->data, h2->data, data);
SWAP(h->size, h2->size, i);
SWAP(h->count, h2->count, i);
hashtable_destroy(h2);
}
return true;
}
/* Returns true if found, and *dpp pointing to the entry, *prevp pointing to prev.
** Returns false if not found, and *dpp pointing the slot where it goes.
*/
static bool
hashtable_find(hashtable_t h, const char *key, datum_t **dpp, datum_t **prevp)
{
datum_t *dp = h->data + (h->hfun(key) % h->size);
if (datum_is_set(dp))
{
datum_t *p = dp;
datum_t *prev = NULL;
while (p)
{
if (datum_comp(p, key) == 0)
{
*dpp = p;
if (prevp)
*prevp = prev;
return true;
}
prev = p;
p = datum_next(p);
}
}
*dpp = dp;
return false;
}
/* Returns hashtable_ret_error on failure.
** Returns hashtable_ret_ok on success, and if key didn't exist.
** Returns hashtable_ret_replaced on success, and if key was replaced.
*/
static hashtable_ret_t
hashtable_put_nogrow(hashtable_t h, const char *key, void *val, void **oldvalp)
{
datum_t *dp;
if (hashtable_find(h, key, &dp, NULL))
{ /* Found */
if (oldvalp != NULL)
*oldvalp = datum_value(dp); /* Return old one */
else if (h->dfun)
h->dfun (datum_value(dp)); /* Clear old one */
if (!datum_set_value(dp, val))
return hashtable_ret_error;
return hashtable_ret_replaced;
}
else
{ /* Not found */
if (datum_is_set(dp))
{ /* Push new value */
datum_t *newp = datum_copy(dp); /* Copy the old one */
if (!newp)
return hashtable_ret_error;
if (!datum_set(dp, key, val, newp)) /* Set the new one, */
{ /* pointing to the old */
datum_free(newp);
return hashtable_ret_error;
}
}
else
{ /* Just smack it into this slot */
if (!datum_set(dp, key, val, NULL))
return hashtable_ret_error;
}
h->count += 1;
}
return hashtable_ret_ok;
}
/* Returns hashtable_ret_error on failure.
** Returns hashtable_ret_ok on success, and if key didn't exist.
** Returns hashtable_ret_replaced on success, and if key was replaced.
*/
hashtable_ret_t
hashtable_put(hashtable_t h, const char *key, void *val, void **oldvalp)
{
if (key == NULL || key[0] == '\0')
return hashtable_ret_error;
if (((float)h->count+1) / h->size >= h->maxload)
{
if (!hashtable_grow(h))
return hashtable_ret_error;
}
return hashtable_put_nogrow(h, key, val, oldvalp);
}
/* Returns hashtable_ret_not_found if not found
** Returns hashtable_ret_ok if found, and '*valuep' updated to value.
*/
hashtable_ret_t
hashtable_get(hashtable_t h, const char *key, void **valp)
{
datum_t *dp;
if (hashtable_find(h, key, &dp, NULL))
{
if (valp)
*valp = datum_value(dp);
return hashtable_ret_ok;
}
return hashtable_ret_not_found;
}
/* Returns hashtable_ret_not_found if not found
** Returns hashtable_ret_ok if removed
*/
hashtable_ret_t
hashtable_rem(hashtable_t h, const char *key, void **valp)
{
datum_t *dp, *tmp;
if (hashtable_find(h, key, &dp, &tmp))
{
if (valp)
*valp = datum_value(dp); /* Return old value */
else if (h->dfun)
h->dfun (datum_value(dp)); /* Destroy old value */
if (!tmp)
{ /* No previous pointer */
tmp = datum_next(dp);
datum_clear(dp);
if (tmp)
{
datum_set(dp, datum_key(tmp), datum_value(tmp), datum_next(tmp));
datum_free(tmp);
}
}
else
{ /* Has a previous pointer */
datum_set_next(tmp, datum_next(dp));
datum_free(dp);
}
h->count -= 1;
return hashtable_ret_ok;
}
return hashtable_ret_not_found;
}
void
hashtable_info(hashtable_t h,
size_t *sizep, size_t *countp, size_t *slotsp, size_t *cmaxp)
{
if (sizep)
*sizep = h->size;
if (countp)
*countp = h->count;
if (slotsp || cmaxp)
{
size_t i, cmax = 0, scount = 0;
for (i = 0 ; i < h->size ; i++)
{
datum_t *dp = h->data + i;
if (datum_is_set(dp))
{
scount += 1;
if (cmaxp)
{
size_t c = 0;
while (dp)
{
c += 1;
dp = datum_next(dp);
}
if (c > cmax)
cmax = c;
}
}
}
if (slotsp)
*slotsp = scount;
if (cmaxp)
*cmaxp = cmax;
}
}
void
hashtable_iter_init(hashtable_t h, hashtable_iter_t *iterp)
{
(void)h; /* Unused */
iterp->i = 0;
iterp->p = NULL;
}
/* Returns true if a next value was found, with *keyp and *valuep
** updated, when non-NULL.
** Returns false when no more values are found.
*/
bool
hashtable_iter_next(hashtable_t h, hashtable_iter_t *iterp,
const char **keyp, void **valuep)
{
datum_t *dp;
if (iterp->p != NULL)
{
dp = (datum_t *)iterp->p;
if (keyp != NULL)
*keyp = datum_key(dp);
if (valuep != NULL)
*valuep = datum_value(dp);
iterp->p = datum_next(dp);
return true;
}
while (iterp->i < h->size)
{
dp = h->data + (iterp->i)++;
if (! datum_is_set(dp))
continue;
if (keyp != NULL)
*keyp = datum_key(dp);
if (valuep != NULL)
*valuep = datum_value(dp);
iterp->p = datum_next(dp);
return true;
}
return false;
}