-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
714 lines (627 loc) · 19.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
#include <stdarg.h>
#include <string.h>
#include "hash.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Precomputed hash sizes
int powers[POW_SIZE] = {
2, 4, 8, 16, 32,
64, 128, 256, 512, 1024,
2048, 4096, 8192, 16384, 32768,
65536, 131072, 262144, 524288, 1048576,
2097152, 4194304, 8388608, 16777216
};
int log_prime[POW_SIZE] = {
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
21, 22, 23, 24
};
int resize_default[POW_SIZE] = {
1, 3, 6, 12, 24, 48,
96, 192, 384, 768, 1536,
3072, 6144, 12288, 24576, 49152,
98304, 196608, 393216, 786432, 1572864,
6291456, 12582912
};
void set_lf (Hash * H, double new_load)
{
if (new_load) {
H->load_factor = new_load;
H->to_resize = new_load * H->cur_size;
}
}
/*
* Function: createHash()
* ----------------------
* Creates a hash object and returns it
*
* elements: number of arguments (not including elements)
* optional first_arg: starting_size (rounded to power of 2)
* optional second_arg: type (OPEN_ADDR / COLLISION)
* optional third_arg: key val type (defaults to INT_KEY_INT_VAL)
*
* returns: pointer to hash object
*/
Hash * createHash(int elements, ...)
{
va_list arg_list;
int starting_size = 8; // Default size
int type = OPEN_ADDR; // Default type
int type_k_v = INT_KEY_INT_VAL; // Default int key, int val
if (elements > 4) {
fprintf(stderr, "<Hash.h>: supplied too many parameters to <Hash.h>: createHash(): max 2\n");
return NULL;
}
va_start (arg_list, elements);
for (int i = 0; i < elements; i++) {
switch (i) {
case 0:
starting_size = va_arg(arg_list, int);
break;
case 1:
type = va_arg(arg_list, int);
assert(type == COLLISION || type == OPEN_ADDR);
break;
case 2:
type_k_v = va_arg(arg_list, int);
assert(type_k_v > 0 && type_k_v < 5);
break;
}
}
int index = 0;
for (; index < POW_SIZE; index++) {
if (powers[index] >= starting_size) {
starting_size = powers[index];
break;
}
}
Hash * new_hash = (Hash *)malloc(SIZE_hash);
// 4 cases
switch (type_k_v) {
case 1:
new_hash->int_k_int_v = (INT_k_INT_v **)calloc(starting_size, SIZE_int_k_int_v);
break;
case 2:
new_hash->int_k_str_v = (INT_k_STR_v **)calloc(starting_size, SIZE_int_k_str_v);
break;
case 3:
new_hash->str_k_str_v = (STR_k_STR_v **)calloc(starting_size, SIZE_str_k_str_v);
break;
case 4:
new_hash->str_k_int_v = (STR_k_INT_v **)calloc(starting_size, SIZE_str_k_int_v);
break;
}
new_hash->probe_limit = index + 1;
new_hash->type = type; // Collision / open addr
new_hash->cur_size = starting_size;
new_hash->num_elem = 0;
new_hash->load_factor = DEFAULT_LF; // Default load factor (change at 0.75 = N / size)
new_hash->to_resize = resize_default[index];
new_hash->k_v_type = type_k_v;
return new_hash;
}
/*
* Function: put()
* ---------------
* Adds a key, value pair to the hash (calls the specific function)
*
* H: the hash object to add the kv pair to
* cur_key: the address of a key
* cur_value: the address of a value
*
* returns: void
*/
void put(Hash * H, void *cur_key, void *cur_value) {
switch (H->k_v_type) {
case (1): // int key, int val
{
int k = *((intptr_t *) cur_key);
int v = *((intptr_t *) cur_value);
put_INT_k_INT_v(H, k, v);
break;
}
case (2): // int key, str value
{
int k = *((intptr_t *) cur_key);
char *v = *((char **) cur_value);
put_INT_k_STR_v(H, k, v);
break;
}
}
}
/*
* Function: put_INT_k_INT_v()
* ---------------------------
* Adds an int key, int value pair to the hash
*
* H: the hash object to add the kv pair to
* cur_key: the address of the int key
* cur_value: the address of the int value
*
* returns: void
*/
void put_INT_k_INT_v (Hash * H, int cur_key, int cur_value)
{
int gen_key = (cur_key & (H->cur_size - 1));
// Linear probing once around for a spot
INT_k_INT_v * new_node = createINT_k_INT_v(cur_key, cur_value, 0);
while (1) {
// Inserting new key
if (H->int_k_int_v[gen_key] == NULL) {
H->num_elem++;
H->int_k_int_v[gen_key] = new_node;
break;
// Overwriting key
} else if (H->int_k_int_v[gen_key]->k == new_node->k ||
H->int_k_int_v[gen_key]->distance == SHRT_MAX) {
swap_INT_k_INT_v(&H->int_k_int_v[gen_key], &new_node);
free(new_node);
return;
}
// Robin hood hash
// If the distance of the current key has probed less, swap_INT_k_INT_v and insert the curr key
if (H->int_k_int_v[gen_key]->distance < new_node->distance) {
swap_INT_k_INT_v(&H->int_k_int_v[gen_key], &new_node);
gen_key = (cur_key & (H->cur_size - 1));
new_node->distance--;
}
gen_key++;
new_node->distance++;
if (gen_key >= H->cur_size) gen_key = 0;
if (new_node->distance >= H->probe_limit) {
resize_OPEN_INT_k_INT_v(H);
gen_key = (new_node->k & (H->cur_size - 1));
new_node->distance = 0;
}
}
if (H->num_elem >= H->to_resize) {
resize_OPEN_INT_k_INT_v(H);
}
return;
}
/*
* Function: put_INT_k_STR_v()
* ---------------------------
* Adds an int key, str value pair to the hash
*
* H: the hash object to add the kv pair to
* cur_key: the address of the int key
* cur_value: the address of the str value
*
* returns: void
*/
void put_INT_k_STR_v (Hash * H, int cur_key, char * cur_value)
{
int gen_key = (cur_key & (H->cur_size - 1));
INT_k_STR_v * new_node = createINT_k_STR_v(cur_key, cur_value, 0);
while (1) {
// Inserting new key
if (H->int_k_str_v[gen_key] == NULL) {
H->num_elem++;
H->int_k_str_v[gen_key] = new_node;
break;
// Overwriting key
} else if (H->int_k_str_v[gen_key]->k == new_node->k ||
H->int_k_str_v[gen_key]->distance == SHRT_MAX) {
swap_INT_k_STR_v(&H->int_k_str_v[gen_key], &new_node);
free(new_node->v);
free(new_node);
return;
}
// Robin hood hash
if (H->int_k_str_v[gen_key]->distance < new_node->distance) {
swap_INT_k_STR_v(&H->int_k_str_v[gen_key], &new_node);
gen_key = (cur_key & (H->cur_size - 1));
new_node->distance--;
}
gen_key++;
new_node->distance++;
if (gen_key >= H->cur_size) gen_key = 0;
if (new_node->distance >= H->probe_limit) {
resize_OPEN_INT_k_STR_v(H);
gen_key = (new_node->k & (H->cur_size - 1));
new_node->distance = 0;
}
}
if (H->num_elem >= H->to_resize) resize_OPEN_INT_k_STR_v(H);
return;
}
/*
* Function: swap_INT_k_INT_v()
* ----------------------------
* Swaps two pointers to int key, int value nodes
*
* tmp1: first node
* tmp2: second node
*
* returns: void
*/
void swap_INT_k_INT_v(INT_k_INT_v ** tmp1, INT_k_INT_v ** tmp2)
{
INT_k_INT_v * tmp = *tmp1;
*tmp1 = *tmp2;
*tmp2 = tmp;
}
void swap_INT_k_STR_v(INT_k_STR_v ** tmp1, INT_k_STR_v ** tmp2)
{
INT_k_STR_v * tmp = *tmp1;
*tmp1 = *tmp2;
*tmp2 = tmp;
}
/*
* Function: UNUSED
* ----------------
*/
unsigned int overwriteKey(Hash * H, int key, int val, int gen_key)
{
unsigned int count = 0;
for (; count < H->probe_limit; count++, gen_key++) {
if (H->int_k_int_v[gen_key] && H->int_k_int_v[gen_key]->k == key) {
H->int_k_int_v[gen_key]->v = val;
return 1;
} else if (H->int_k_int_v[gen_key] == NULL) {
break;
}
if (gen_key >= H->cur_size - 1) {
gen_key = 0;
}
}
return 0;
}
/*
* Function: printHash()
* ---------------------
* Prints the complete hash including key values and distances
*
* H: hash to print
*
* returns: void
*/
void printHash (Hash * H)
{
printf("Open addressing hash\n");
switch(H->k_v_type) {
case (1):
for (int i = 0; i < H->cur_size; i++) {
printf("[%s%d%s] : ", YELLOW, i, END);
if (H->int_k_int_v[i] && H->int_k_int_v[i]->distance != SHRT_MAX) {
printf("(%d:%d) d=%d\n", H->int_k_int_v[i]->k, H->int_k_int_v[i]->v,
H->int_k_int_v[i]->distance);
} else {
printf("\n");
}
}
break;
case (2):
for (int i = 0; i < H->cur_size; i++) {
printf("[%s%d%s] : ", YELLOW, i, END);
if (H->int_k_str_v[i] && H->int_k_str_v[i]->distance != SHRT_MAX) {
printf("(%d:%s) d=%d\n", H->int_k_str_v[i]->k, H->int_k_str_v[i]->v,
H->int_k_str_v[i]->distance);
} else {
printf("\n");
}
}
break;
}
}
/*
* Function: resize_OPEN_INT_k_INT_v()
* -----------------------------------
* Enlarges the hash by a power of 2, called automatically
*
* old_H: pointer to the old hash
*
* returns: void
*/
void resize_OPEN_INT_k_INT_v(Hash * old_H)
{
Hash * new_hash = (Hash *)malloc(SIZE_hash);
int saved = old_H->num_elem;
new_hash->probe_limit = old_H->probe_limit + 1;
new_hash->type = old_H->type; // Collision / open addr
new_hash->cur_size = old_H->cur_size * 2;
new_hash->load_factor = DEFAULT_LF; // Default load factor (change at 0.75 = N / size)
new_hash->to_resize = old_H->to_resize * 2;
new_hash->k_v_type = old_H->k_v_type;
new_hash->int_k_int_v = (INT_k_INT_v **)calloc(new_hash->cur_size, SIZE_int_k_int_v);
for (int i = 0; i < old_H->cur_size; i++) {
if (old_H->int_k_int_v[i] && old_H->int_k_int_v[i]->k != INT_MIN) {
insert_int_int(new_hash, old_H->int_k_int_v[i]->k, old_H->int_k_int_v[i]->v);
free(old_H->int_k_int_v[i]);
}
}
free(old_H->int_k_int_v);
*old_H = *new_hash;
old_H->num_elem = saved;
free(new_hash);
}
void resize_OPEN_INT_k_STR_v(Hash * old_H)
{
Hash * new_hash = (Hash *)malloc(SIZE_hash);
int saved = old_H->num_elem;
new_hash->probe_limit = old_H->probe_limit + 1;
new_hash->type = old_H->type; // Collision / open addr
new_hash->cur_size = old_H->cur_size * 2;
new_hash->load_factor = DEFAULT_LF; // Default load factor (change at 0.75 = N / size)
new_hash->to_resize = old_H->to_resize * 2;
new_hash->k_v_type = old_H->k_v_type;
new_hash->int_k_str_v = (INT_k_STR_v **)calloc(new_hash->cur_size, SIZE_int_k_str_v);
for (int i = 0; i < old_H->cur_size; i++) {
if (old_H->int_k_str_v[i] && old_H->int_k_str_v[i]->distance != SHRT_MAX) {
insert_int_str(new_hash, old_H->int_k_str_v[i]->k, old_H->int_k_str_v[i]->v);
free(old_H->int_k_str_v[i]->v);
free(old_H->int_k_str_v[i]);
}
}
free(old_H->int_k_str_v);
*old_H = *new_hash;
old_H->num_elem = saved;
free(new_hash);
}
/*
* Function: insert_int_int()
* --------------------------
* Faster version of put (we don't need to check for deleted nodes)
*
* H: hash to insert element
* cur_key: current int key to insert
* cur_value: current int value to insert
*
* returns: void
*/
void insert_int_int(Hash * H, int cur_key, int cur_value)
{
int gen_key = (cur_key & (H->cur_size - 1));
// Linear probing once around for a spot
INT_k_INT_v * new_node = createINT_k_INT_v(cur_key, cur_value, 0);
while (1) {
// Inserting new key
if (H->int_k_int_v[gen_key] == NULL) {
H->num_elem++;
H->int_k_int_v[gen_key] = new_node;
break;
}
// Robin hood hash
if (H->int_k_int_v[gen_key]->distance < new_node->distance) {
swap_INT_k_INT_v(&H->int_k_int_v[gen_key], &new_node);
gen_key = (cur_key & (H->cur_size - 1));
new_node->distance--;
}
gen_key++;
new_node->distance++;
if (gen_key >= H->cur_size) gen_key = 0;
}
}
/*
* Function: insert_int_str()
* --------------------------
* Faster version of put (we don't need to check for deleted nodes)
*
* H: hash to insert element
* cur_key: current int key to insert
* cur_value: current str value to insert
*
* returns: void
*/
void insert_int_str(Hash * H, int cur_key, char * cur_value)
{
int gen_key = (cur_key & (H->cur_size - 1));
// Linear probing once around for a spot
INT_k_STR_v * new_node = createINT_k_STR_v(cur_key, cur_value, 0);
while (1) {
// Inserting new key
if (H->int_k_str_v[gen_key] == NULL) {
H->num_elem++;
H->int_k_str_v[gen_key] = new_node;
break;
}
// Robin hood hash
if (H->int_k_str_v[gen_key]->distance < new_node->distance) {
swap_INT_k_STR_v(&H->int_k_str_v[gen_key], &new_node);
gen_key = (cur_key & (H->cur_size - 1));
new_node->distance--;
}
gen_key++;
new_node->distance++;
if (gen_key >= H->cur_size) gen_key = 0;
}
}
/*
* Function: UNUSED
* ----------------
*/
double load_factor(Hash * H)
{
printf("Nodes / size = %d / %d\n", H->num_elem, H->cur_size);
H->load_factor = (double)H->num_elem / H->cur_size;
return H->load_factor;
}
/*
* Function: free_hash()
* ---------------------
* Frees the hash object (completely)
*
* H: hash to free
*
* returns: void
*/
void free_hash (Hash * H)
{
switch (H->k_v_type) {
case (1):
for (int i = 0; i < H->cur_size; i++) {
if (H->int_k_int_v[i]) {
free(H->int_k_int_v[i]);
}
}
free(H->int_k_int_v);
H->int_k_int_v = NULL;
break;
case(2):
for (int i = 0; i < H->cur_size; i++) {
if (H->int_k_str_v[i]) {
free(H->int_k_str_v[i]->v);
free(H->int_k_str_v[i]);
}
}
free(H->int_k_str_v);
H->int_k_str_v = NULL;
break;
}
free(H);
}
/*
* Function: del()
* ---------------
* Delete a key from a hash (set distance=SHRT_MAX), will free later
*
* H: hash to delete from
* key: key to delete
*
* returns: void
*/
void del(Hash * H, int key)
{
switch (H->k_v_type) {
case 1: // int key, int val
{
del_INT_k_INT_v(H, key);
break;
}
case 2: // int key, str value
{
del_INT_k_STR_v(H, key);
break;
}
}
}
/*
* Function: del_INT_k_INT_v()
* ---------------------------
* Delete an int kv pair from a hash (set distance=SHRT_MAX), will free later
*
* H: hash to delete from
* key: key to delete
*
* returns: void
*/
void del_INT_k_INT_v(Hash * H, int key)
{
int gen_key = (key & (H->cur_size - 1));
int dist_from_key = 0;
// Search for key
while (1) {
// Can't free the node, just set distance to shrt_max
if (H->int_k_int_v[gen_key] && H->int_k_int_v[gen_key]->k == key) {
H->int_k_int_v[gen_key]->distance = SHRT_MAX; // Distance MAX_INT (this is not possible)
break;
}
gen_key++;
dist_from_key++;
// Limit for probing exceeded, non existent
if (H->int_k_int_v[gen_key] == NULL || dist_from_key >= H->probe_limit) break;
if (gen_key >= H->cur_size) gen_key = 0;
}
}
/*
* Function: del_INT_k_STR_v()
* ---------------------------
* Delete an int_k str_v pair from a hash (set distance=SHRT_MAX), will free later
*
* H: hash to delete from
* key: key to delete
*
* returns: void
*/
void del_INT_k_STR_v(Hash * H, int key)
{
int gen_key = (key & (H->cur_size - 1));
int dist_from_key = 0;
// Search for key
while (1) {
// Can't free the node, just set distance to shrt_max
if (H->int_k_str_v[gen_key] && H->int_k_str_v[gen_key]->k == key) {
H->int_k_str_v[gen_key]->distance = SHRT_MAX; // Distance MAX_INT (this is not possible)
break;
}
gen_key++;
dist_from_key++;
// Limit for probing exceeded, non existent
if (H->int_k_str_v[gen_key] == NULL || dist_from_key >= H->probe_limit) break;
if (gen_key >= H->cur_size) gen_key = 0;
}
}
/*
* Function: get_INT_k_INT_v()
* ---------------------------
* Specific return function for int keys int vals
*
* H: hash to retrieve from
* key: key to retrieve
*
* returns: the corresponding value
*/
int get_INT_k_INT_v(Hash * H, int key)
{
int gen_key = (key & (H->cur_size - 1));
int dist_from_key = 0;
while (1) {
if (H->int_k_int_v[gen_key] && H->int_k_int_v[gen_key]->k == key) {
return H->int_k_int_v[gen_key]->v;
}
gen_key++;
dist_from_key++;
if (gen_key >= H->cur_size) gen_key = 0;
if (H->int_k_int_v[gen_key] == NULL || dist_from_key >= H->probe_limit) break; // Non-existent key
}
return INT_MIN;
}
/*
* Function: get_INT_k_STR_v()
* ---------------------------
* Specific return function for int keys str vals
*
* H: hash to retrieve from
* key: key to retrieve
*
* returns: the corresponding value
*/
char * get_INT_k_STR_v(Hash * H, int key)
{
int gen_key = (key & (H->cur_size - 1));
int dist_from_key = 0;
while (1) {
if (H->int_k_str_v[gen_key] && H->int_k_str_v[gen_key]->k == key) {
char *ret = calloc(strlen(H->int_k_str_v[gen_key]->v) + 1, sizeof(char));
strncpy(ret, H->int_k_str_v[gen_key]->v, strlen(H->int_k_str_v[gen_key]->v));
return ret;
}
gen_key++;
dist_from_key++;
if (gen_key >= H->cur_size) gen_key = 0;
if (H->int_k_str_v[gen_key] == NULL || dist_from_key >= H->probe_limit) break; // Non-existent key
}
return NULL;
}
//** Creates a node for open addressing
INT_k_INT_v * createINT_k_INT_v(int cur_key, int cur_value, int dist)
{
INT_k_INT_v * N = (INT_k_INT_v *)malloc(SIZE_int_k_int_v);
N->k = cur_key;
N->v = cur_value;
N->distance = dist;
return N;
}
INT_k_STR_v * createINT_k_STR_v(int cur_key, char *cur_value, int dist)
{
INT_k_STR_v * N = (INT_k_STR_v *)malloc(SIZE_int_k_str_v);
N->k = cur_key;
N->v = (char *)malloc((strlen(cur_value) + 1) * sizeof(char));
strcpy(N->v, cur_value);
N->distance = dist;
return N;
}