-
Notifications
You must be signed in to change notification settings - Fork 4
/
hash.c
774 lines (620 loc) · 16.3 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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include "types.h"
#include "cell.h"
#include "global.h"
static struct hash_bucket* hash_add_to_bucket(
Hash *hash,
struct hash_bucket *bucket,
const unsigned int org_index,
const wchar_t *key,
const int key_len,
const struct _toy_type *item,
int bucket_size,
int key_re_alloc
);
static Hash* hash_rehash(Hash *hash);
#define HASH_MAKE_KEY(k) (hash_string_key(k))
/*
int hash_bsize[] = {
7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191,
16381, 32749, 65521, 131071, 262139, 524287, 1048573,
2097151, 4194301, 8388607, 16777213, 33554467, 67108879,
// 134217757, 268435459, 536870923, 1073741827,
0
};
*/
Hash*
new_hash() {
Hash *h;
h = GC_MALLOC(sizeof(Hash));
ALLOC_SAFE(h);
hash_clear(h);
return h;
}
void
hash_clear(Hash* h) {
h->bucket_size = HASH_INIT_BUCKET;
h->bucket = NULL;
h->items = 0;
h->synonyms = 0;
}
Hash*
hash_set(Hash *hash, const wchar_t *key, const struct _toy_type *item) {
unsigned int index;
if (NULL == hash) return NULL;
if (((hash->items)*2) > (hash->bucket_size)) {
if (NULL == hash_rehash(hash)) {
return NULL;
}
}
index = HASH_MAKE_KEY(key);
if (NULL == hash_add_to_bucket(
hash,
hash->bucket,
index,
key,
wcslen(key) + 1,
item,
hash->bucket_size,
1
))
{
return NULL;
}
return hash;
}
Hash*
hash_set_t(Hash *hash, const struct _toy_type *key, const struct _toy_type *item) {
int t;
unsigned int idx;
Cell *caddr;
if (NULL == hash) return NULL;
if (((hash->items)*2) > (hash->bucket_size)) {
if (NULL == hash_rehash(hash)) {
return NULL;
}
}
t = GET_TAG(key);
if ((t == SYMBOL) || (t == REF)) {
if (SYMBOL == t) {
idx = key->u.symbol.hash_index;
caddr = key->u.symbol.cell;
} else {
idx = key->u.ref.hash_index;
caddr = key->u.ref.cell;
}
if (NULL ==
hash_add_to_bucket(
hash,
hash->bucket,
idx,
cell_get_addr(caddr),
cell_get_length(caddr) + 1,
item,
hash->bucket_size,
1
))
{
return NULL;
}
return hash;
}
assert(0);
return NULL;
}
struct hash_bucket*
hash_add_to_bucket(
Hash *hash,
struct hash_bucket *bucket,
const unsigned int org_index,
const wchar_t *key,
const int key_len,
const struct _toy_type *item,
int bucket_size,
int key_re_alloc
) {
wchar_t *dest_key;
struct hash_bucket *b, *bs;
unsigned int index;
index = org_index % bucket_size;
if (hash->bucket == NULL) {
int bsize;
struct hash_bucket *lbucket;
bsize = sizeof(struct hash_bucket) * hash->bucket_size;
lbucket = GC_MALLOC(bsize);
ALLOC_SAFE(lbucket);
memset(lbucket, 0, bsize);
hash->bucket = lbucket;
bucket = lbucket;
}
b = &(bucket[index]);
if (NULL == b->key) {
/* new item into new bucket */
if (key_re_alloc) {
dest_key = (wchar_t*)GC_MALLOC(key_len*sizeof(wchar_t));
ALLOC_SAFE(dest_key);
wcsncpy(dest_key, key, key_len);
b->key = dest_key;
} else {
b->key = (wchar_t*)key;
}
b->index = org_index;
b->item = (struct _toy_type*)item;
b->next = 0;
hash->items++;
return bucket;
}
while (1) {
if (0 == wcscmp(b->key, key)) {
if (GET_TAG(b->item) == ALIAS) {
/* set to alias slot */
if (NULL == hash_set_t(b->item->u.alias.slot, b->item->u.alias.key, item)) {
return NULL;
} else {
return bucket;
}
} else {
/* replace item */
b->item = (struct _toy_type*)item;
return bucket;
}
}
if (! b->next) break;
b = b->next;
}
/* new item add to synonym */
bs = GC_MALLOC(sizeof(struct hash_bucket));
ALLOC_SAFE(bs);
if (key_re_alloc) {
dest_key = GC_MALLOC(key_len*sizeof(wchar_t));
ALLOC_SAFE(dest_key);
wcsncpy(dest_key, key, key_len);
bs->key = dest_key;
} else {
bs->key = (wchar_t*)key;
}
bs->index = org_index;
bs->item = (struct _toy_type*)item;
bs->next = 0;
b->next = bs;
hash->items++;
hash->synonyms++;
return bucket;
}
static int next_size(int now_size);
static int next_size(int now_size) {
int i;
/*
for (i=0; hash_bsize[i]!=0; i++) {
if (now_size == hash_bsize[i]) {
i++;
return hash_bsize[i];
}
}
*/
i = now_size * 2;
if (i < 0) {
return now_size;
}
return i;
}
Hash*
hash_rehash(Hash *hash) {
int new_bucket_size;
struct hash_bucket *b, *newb;
int bsize, i;
new_bucket_size = next_size(hash->bucket_size);
if (new_bucket_size == 0) return hash;
bsize = sizeof(struct hash_bucket) * new_bucket_size;
newb = GC_MALLOC(bsize);
ALLOC_SAFE(newb);
memset(newb, 0, bsize);
hash->items = 0;
hash->synonyms = 0;
for (i=0; i<(hash->bucket_size); i++) {
b = &(hash->bucket[i]);
if (NULL != b->key) {
while (b) {
if (NULL ==
hash_add_to_bucket(
hash,
newb,
b->index,
b->key,
wcslen(b->key) + 1,
b->item,
new_bucket_size,
0
))
{
return NULL;
}
b = b->next;
}
}
}
memset(hash->bucket, 0, sizeof(struct hash_bucket) * hash->bucket_size);
hash->bucket_size = new_bucket_size;
hash->bucket = newb;
return hash;
}
struct _toy_type*
hash_get(Hash *hash, const wchar_t *key) {
unsigned int index;
struct hash_bucket *b;
if (NULL == hash) return NULL;
if (NULL == hash->bucket) return NULL;
index = HASH_MAKE_KEY(key) % hash->bucket_size;
b = &(hash->bucket[index]);
if (NULL == b->key) return NULL;
while (b) {
if (0 == wcscmp(b->key, key)) {
if (GET_TAG(b->item) == ALIAS) {
return hash_get_t(b->item->u.alias.slot, b->item->u.alias.key);
} else {
return b->item;
}
}
b = b->next;
}
return NULL;
}
static struct _toy_type* hash_get_alias(Hash *hash, const wchar_t *key) ;
static struct _toy_type*
hash_get_alias(Hash *hash, const wchar_t *key) {
unsigned int index;
struct hash_bucket *b;
if (NULL == hash) return NULL;
if (NULL == hash->bucket) return NULL;
index = HASH_MAKE_KEY(key) % hash->bucket_size;
b = &(hash->bucket[index]);
if (NULL == b->key) return NULL;
while (b) {
if (0 == wcscmp(b->key, key)) {
return b->item;
}
b = b->next;
}
return NULL;
}
struct _toy_type*
hash_get_t(Hash *hash, const struct _toy_type *key) {
unsigned int index;
struct hash_bucket *b;
int t;
Cell *caddr;
if (NULL == hash) return NULL;
if (NULL == hash->bucket) return NULL;
t = GET_TAG(key);
if ((t == SYMBOL) || (t == REF)) {
if (SYMBOL == t) {
index = key->u.symbol.hash_index % hash->bucket_size;
caddr = key->u.symbol.cell;
} else {
index = key->u.ref.hash_index % hash->bucket_size;
caddr = key->u.ref.cell;
}
b = &(hash->bucket[index]);
if (NULL == b->key) return NULL;
while (b) {
if (0 == wcscmp(b->key, cell_get_addr(caddr))) {
if (GET_TAG(b->item) == ALIAS) {
return hash_get_t(b->item->u.alias.slot, b->item->u.alias.key);
} else {
return b->item;
}
}
b = b->next;
}
return NULL;
}
assert(0);
return NULL;
}
static struct _toy_type* hash_get_and_unset_sub(Hash *hash, unsigned int index, const wchar_t *key);
struct _toy_type*
hash_get_and_unset(Hash *hash, const wchar_t *key) {
unsigned int index;
if (NULL == hash) return NULL;
if (NULL == hash->bucket) return NULL;
index = HASH_MAKE_KEY(key);
return hash_get_and_unset_sub(hash, index, key);
}
struct _toy_type*
hash_get_and_unset_t(Hash *hash, const struct _toy_type *key) {
unsigned int index;
int t;
Cell *caddr;
if (NULL == hash) return NULL;
if (NULL == hash->bucket) return NULL;
t = GET_TAG(key);
if ((t == SYMBOL) || (t == REF)) {
if (SYMBOL == t) {
index = key->u.symbol.hash_index % hash->bucket_size;
caddr = key->u.symbol.cell;
} else {
index = key->u.ref.hash_index % hash->bucket_size;
caddr = key->u.ref.cell;
}
return hash_get_and_unset_sub(hash, index, cell_get_addr(caddr));
}
assert(0);
return NULL;
}
static struct _toy_type*
hash_get_and_unset_sub(Hash *hash, unsigned int org_index, const wchar_t *key) {
struct hash_bucket *b, *bb;
struct _toy_type *item;
unsigned int index;
index = org_index % hash->bucket_size;
b = &(hash->bucket[index]);
if (NULL == b->key) return NULL;
bb = NULL;
while (b) {
if (0 == wcscmp(b->key, key)) {
item = b->item;
if (bb) {
bb->next = b->next;
b->key = NULL;
b->index = 0;
b->item = NULL;
b->next = NULL;
} else {
if (b->next) {
struct hash_bucket *next;
next = b->next;
b->key = b->next->key;
b->index = b->next->index;
b->item = b->next->item;
b->next = b->next->next;
next->key = NULL;
next->index = 0;
next->item = NULL;
next->next = NULL;
} else {
b->key = NULL;
b->index = 0;
b->item = NULL;
b->next = NULL;
}
}
hash->items--;
if (GET_TAG(item) == ALIAS) {
return hash_get_t(item->u.alias.slot, item->u.alias.key);
} else {
return item;
}
}
bb = b;
b = b->next;
}
return NULL;
}
static int
hash_is_exists_sub(Hash *hash, unsigned int index, const wchar_t *key);
int
hash_is_exists(Hash *hash, const wchar_t *key) {
unsigned int index;
if (NULL == hash) return 0;
if (NULL == hash->bucket) return 0;
index = HASH_MAKE_KEY(key);
return hash_is_exists_sub(hash, index, key);
}
int
hash_is_exists_t(Hash *hash, const struct _toy_type *key) {
unsigned int index;
int t;
Cell *caddr;
if (NULL == hash) return 0;
if (NULL == hash->bucket) return 0;
t = GET_TAG(key);
if ((t == SYMBOL) || (t == REF)) {
if (SYMBOL == t) {
index = key->u.symbol.hash_index % hash->bucket_size;
caddr = key->u.symbol.cell;
} else {
index = key->u.ref.hash_index % hash->bucket_size;
caddr = key->u.ref.cell;
}
return hash_is_exists_sub(hash, index, cell_get_addr(caddr));
}
assert(0);
return 0;
}
static int
hash_is_exists_sub(Hash *hash, unsigned int org_index, const wchar_t *key) {
struct hash_bucket *b;
unsigned int index;
index = org_index % hash->bucket_size;
b = &(hash->bucket[index]);
if (NULL == b->key) return 0;
while (b) {
if (0 == wcscmp(b->key, key)) {
return 1;
}
b = b->next;
}
return 0;
}
Toy_Type*
hash_get_keys(Hash *hash) {
Toy_Type *l, *sl;
int i;
struct hash_bucket *b;
if (NULL == hash) return NULL;
l = new_list(NULL);
if (NULL == hash->bucket) return l;
sl = l;
for (i=0; i<(hash->bucket_size); i++) {
b = &(hash->bucket[i]);
if (NULL == b->key) continue;
while (b) {
sl = list_append(sl, new_symbol(b->key));
b = b->next;
}
}
return l;
}
Toy_Type*
hash_get_keys_str(Hash *hash) {
Toy_Type *l, *sl;
int i;
struct hash_bucket *b;
if (NULL == hash) return NULL;
l = new_list(NULL);
if (NULL == hash->bucket) return l;
sl = l;
for (i=0; i<(hash->bucket_size); i++) {
b = &(hash->bucket[i]);
if (NULL == b->key) continue;
while (b) {
sl = list_append(sl, new_string_str(b->key));
b = b->next;
}
}
return l;
}
Toy_Type*
hash_get_pairs(Hash *hash) {
Toy_Type *l, *sl, *p;
int i;
struct hash_bucket *b;
if (NULL == hash) return NULL;
l = new_list(NULL);
if (NULL == hash->bucket) return l;
sl = l;
for (i=0; i<(hash->bucket_size); i++) {
b = &(hash->bucket[i]);
if (NULL == b->key) continue;
while (b) {
if (GET_TAG(b->item) == ALIAS) {
Toy_Type *v;
v = hash_get_t(b->item->u.alias.slot, b->item->u.alias.key);
if (NULL == v) {
p = new_cons(new_symbol(b->key), const_Nil);
} else {
p = new_cons(new_symbol(b->key), v);
}
} else {
p = new_cons(new_symbol(b->key), b->item);
}
sl = list_append(sl, p);
b = b->next;
}
}
return l;
}
Toy_Type*
hash_get_pairs_str(Hash *hash) {
Toy_Type *l, *sl, *p;
int i;
struct hash_bucket *b;
if (NULL == hash) return NULL;
l = new_list(NULL);
if (NULL == hash->bucket) return l;
sl = l;
for (i=0; i<(hash->bucket_size); i++) {
b = &(hash->bucket[i]);
if (NULL == b->key) continue;
while (b) {
if (GET_TAG(b->item) == ALIAS) {
Toy_Type *v;
v = hash_get_t(b->item->u.alias.slot, b->item->u.alias.key);
if (NULL == v) {
p = new_cons(new_string_str(b->key), const_Nil);
} else {
p = new_cons(new_string_str(b->key), v);
}
} else {
p = new_cons(new_string_str(b->key), b->item);
}
sl = list_append(sl, p);
b = b->next;
}
}
return l;
}
int
hash_link(Hash *hash, const wchar_t *key,
Hash *to_hash, const wchar_t *to_key)
{
Toy_Type *o;
Toy_Type *e;
if (NULL == hash) return 0;
if (1 == hash_is_exists(hash, key)) return 0;
e = hash_get_alias(to_hash, to_key);
if ((NULL != e) && (GET_TAG(e) == ALIAS)) return 0;
o = new_alias(to_hash, new_symbol((wchar_t*)to_key));
if (NULL == hash_set(hash, key, o)) {
return 0;
} else {
return 1;
}
}
int
hash_link_t(Hash *hash, const struct _toy_type *key,
Hash *to_hash, const struct _toy_type *to_key)
{
if (GET_TAG(key) != SYMBOL) return 0;
if (GET_TAG(to_key) != SYMBOL) return 0;
return hash_link(hash, cell_get_addr(key->u.symbol.cell),
to_hash, cell_get_addr(to_key->u.symbol.cell));
}
void
hash_debug_dump(Hash *hash) {
int i;
struct hash_bucket *b;
if (NULL == hash) {
printf("hash not created.\n");
return;
}
printf("\n");
printf("hash items: %d\n", hash->items);
printf("hash bucket size: %d\n", hash->bucket_size);
printf("hash synonyms: %d\n", hash->synonyms);
for (i=0; i<(hash->bucket_size); i++) {
if (NULL == hash->bucket[i].key) {
wprintf(L"bucket[%d] is not used\n", i);
} else {
wprintf(L"bucket[%d] dump:\n", i);
b = &(hash->bucket[i]);
while (b) {
wprintf(L" key: \"%ls\"\n", b->key);
b = b->next;
}
}
}
return;
}
/*
* this function is part of the Tcl lang suite
*/
unsigned int
hash_string_key(const wchar_t *key) {
register const wchar_t *string = key;
register unsigned int result;
register int c;
/*
* I tried a zillion different hash functions and asked many other people
* for advice. Many people had their own favorite functions, all
* different, but no-one had much idea why they were good ones. I chose
* the one below (multiply by 9 and add new character) because of the
* following reasons:
*
* 1. Multiplying by 10 is perfect for keys that are decimal strings, and
* multiplying by 9 is just about as good.
* 2. Times-9 is (shift-left-3) plus (old). This means that each
* character's bits hang around in the low-order bits of the hash value
* for ever, plus they spread fairly rapidly up to the high-order bits
* to fill out the hash value. This seems works well both for decimal
* and non-decimal strings, but isn't strong against maliciously-chosen
* keys.
*/
result = 0;
for (c=*string++ ; c ; c=*string++) {
result += (result<<3) + c;
}
return result;
}