-
Notifications
You must be signed in to change notification settings - Fork 3
/
has.c
815 lines (697 loc) · 20.8 KB
/
has.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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*
* Copyright 2016 Mathias Brossard <[email protected]>
*/
/**
* @file has.c
*/
#include "has.h"
#include <stdlib.h>
#include <string.h>
#define hash_size(s) ((s) + ((s) >> 1))
#define hash_freed ((void *)1)
#define hash_hash(d, i) (d->value.hash.hash[i])
#define hash_first(d, h) (h % hash_size(d->value.hash.size))
#define hash_next(d, i) (((i + 1) == hash_size(d->value.hash.size)) ? 0 : i + 1)
has_t * has_new(size_t count)
{
has_t *r = calloc(sizeof(has_t), count);
if(r && count == 1) {
r->owner = 1;
}
return r;
}
void has_free(has_t *e)
{
if(e == NULL) {
return;
}
if(e->type == has_hash) {
int i;
for(i = 0; i < e->value.hash.size; i++) {
if(e->value.hash.entries[i].key.pointer) {
if(e->value.hash.entries[i].key.owner) {
free(e->value.hash.entries[i].key.pointer);
}
if(e->value.hash.entries[i].value) {
has_free(e->value.hash.entries[i].value);
}
}
}
free(e->value.hash.entries);
free(e->value.hash.hash);
} else if(e->type == has_array) {
int i;
for(i = 0; i < e->value.array.count; i++) {
has_free(e->value.array.elements[i]);
}
free(e->value.array.elements);
} else if(e->type == has_string &&
e->value.string.owner) {
free(e->value.string.pointer);
}
if(e->owner) {
free(e);
}
}
void has_set_owner(has_t *e, bool owner)
{
if(e) {
e->owner = owner ? 1 : 0;
}
}
/* I don't like to use macros to often */
#define WF(r, call) if((r = (call)) != 0) { return r; }
int has_walk(has_t *e, has_walk_function_t f, void *p)
{
int i, j, r;
if(e == NULL) {
return -1;
}
if(e->type == has_hash) {
WF(r, f(e, has_walk_hash_begin, 0, NULL, 0, NULL, p));
for(i = 0, j = 0; i < e->value.hash.size; i++) {
has_hash_entry_t *l = &(e->value.hash.entries[i]);
if(l->key.pointer) {
WF(r, f(e, has_walk_hash_key, j, l->key.pointer,
l->key.size, NULL, p));
WF(r, f(e, has_walk_hash_value_begin, j, NULL, 0, l->value, p));
WF(r, has_walk(l->value, f, p));
WF(r, f(e, has_walk_hash_value_end, j, NULL, 0, l->value, p));
j++;
}
}
WF(r, f(e, has_walk_hash_end, 0, NULL, 0, NULL, p));
} else if(e->type == has_array) {
WF(r, f(e, has_walk_array_begin, 0, NULL, 0, NULL, p));
for(i = 0; i < e->value.array.count; i++) {
has_t *cur = e->value.array.elements[i];
WF(r, f(e, has_walk_array_entry_begin, i, NULL, 0, cur, p));
WF(r, has_walk(cur, f, p));
WF(r, f(e, has_walk_array_entry_end, i, NULL, 0, cur, p));
}
WF(r, f(e, has_walk_array_end, 0, NULL, 0, NULL, p));
} else if(e->type == has_string) {
WF(r, f(e, has_walk_string, 0, e->value.string.pointer,
e->value.string.size, NULL, p));
} else {
WF(r, f(e, has_walk_other, 0, NULL, 0, NULL, p));
}
return 0;
}
#undef WF
has_t * has_hash_new(size_t size)
{
has_t *r = has_new(1), *s = NULL;
if(r) {
if((s = has_hash_init(r, size)) == NULL) {
has_free(r);
}
}
return s;
}
has_t * has_hash_init(has_t *hash, size_t size)
{
has_hash_entry_t *e, **h;
if(hash == NULL ||
((e = calloc(sizeof(has_hash_entry_t), size)) == NULL) ||
((h = calloc(sizeof(has_hash_entry_t *), hash_size(size))) == NULL)) {
return NULL;
}
hash->type = has_hash;
hash->value.hash.size = size;
hash->value.hash.count = 0;
hash->value.hash.entries = e;
hash->value.hash.hash = h;
return hash;
}
inline bool has_is_hash(has_t *e)
{
return (e && e->type == has_hash) ? true : false;
}
has_t * has_hash_set_o(has_t *hash, char *key, size_t size, has_t *value, bool owner)
{
size_t i, j;
has_hash_entry_t *e = NULL;
int32_t h;
if(hash == NULL || hash->type != has_hash) {
return NULL;
}
h = has_hash_function(key, size);
/* Search for a value with same key */
for(i = hash_first(hash, h); (e = hash_hash(hash, i)) ; i = hash_next(hash, i)) {
if((e != hash_freed) && /* Check freed */
(e->hash == h) && /* Check hash */
(e->key.size == size) && /* Check key size */
(memcmp(e->key.pointer, key, size) == 0)) { /* Full key compare */
has_free(e->value); /* Free the value */
e->value = value;
if(e->key.owner) {
free(e->key.pointer); /* Free the key if we own it */
}
e->key.pointer = key;
e->key.owner = owner;
return hash;
}
}
if(hash->value.hash.size == hash->value.hash.count) {
has_hash_entry_t **t;
i = hash->value.hash.size * sizeof(has_hash_entry_t);
if((e = calloc(2* i, 1)) == NULL) {
return NULL;
}
memcpy(e, hash->value.hash.entries, i);
free(hash->value.hash.entries);
hash->value.hash.entries = e;
i = 2 * hash->value.hash.size;
if((t = calloc(hash_size(i), sizeof(has_hash_entry_t *))) == NULL) {
return NULL;
}
free(hash->value.hash.hash);
hash->value.hash.hash = t;
hash->value.hash.size *= 2;
/* Rebuild hash table */
for(i = 0 ; i < hash->value.hash.count ; i++) {
e = &(hash->value.hash.entries[i]);
j = hash_first(hash, e->hash);
while(hash_hash(hash, j) != NULL && hash_hash(hash, j) != hash_freed) {
j = hash_next(hash, j);
}
hash_hash(hash, j) = e;
}
}
/* Insert key in the first empty slot. Start at
hash->value.hash.count and wrap at
hash->value.hash.size. Because hash->value.hash.count <
hash->value.hash.size this will necessarily terminate. */
for (i = hash->value.hash.count ; hash->value.hash.entries[i].key.pointer ; ) {
i = (i + 1 == hash->value.hash.size) ? 0 : i + 1;
}
/* Insert element */
e = &(hash->value.hash.entries[i]);
e->key.pointer = key;
e->key.size = size;
e->key.owner = owner;
e->hash = h;
e->value = value;
j = hash_first(hash, e->hash);
while(hash_hash(hash, j) != NULL && hash_hash(hash, j) != hash_freed) {
j = hash_next(hash, j);
}
hash_hash(hash, j) = e;
/* Increase counter */
hash->value.hash.count++;
return hash;
}
has_t * has_hash_set(has_t *hash, char *key, size_t size, has_t *value)
{
return has_hash_set_o(hash, key, size, value, false);
}
has_t * has_hash_set_str(has_t *hash, char *string, has_t *value)
{
return has_hash_set_o(hash, string, strlen(string), value, false);
}
has_t * has_hash_set_str_o(has_t *hash, char *string, has_t *value, bool owner)
{
return has_hash_set_o(hash, string, strlen(string), value, owner);
}
has_t *has_hash_add(has_t *h, has_t *k, has_t *v)
{
if(h == NULL || h->type != has_hash ||
k == NULL || k->type != has_string) {
return NULL;
} else {
has_t *r;
char *s = k->value.string.pointer;
size_t l = k->value.string.size;
bool owner = k->value.string.owner;
if(owner) {
k->value.string.owner = false;
}
has_free(k);
if((r = has_hash_set_o(h, s, l, v, owner)) == NULL) {
has_free(v);
if(owner) {
free(s);
}
}
return r;
}
}
bool has_hash_exists(has_t *hash, const char *key, size_t size)
{
size_t i;
uint32_t h;
has_hash_entry_t *e = NULL;
bool r = false;
if(!hash) {
return r;
}
h = has_hash_function(key, size);
for(i = hash_first(hash, h); (e = hash_hash(hash, i)) ; i = hash_next(hash, i)) {
if((e != hash_freed) && /* Check freed */
(e->hash == h) && /* Check hash */
(e->key.size == size) && /* Check key size */
(memcmp(e->key.pointer, key, size) == 0)) { /* Full key compare */
r = true;
break;
}
}
return r;
}
bool has_hash_exists_str(has_t *hash, const char *string)
{
return has_hash_exists(hash, string, strlen(string));
}
has_t * has_hash_get(has_t *hash, const char *key, size_t size)
{
size_t i;
uint32_t h;
has_hash_entry_t *e = NULL;
has_t *r = NULL;
if(!hash) {
return r;
}
h = has_hash_function(key, size);
for(i = hash_first(hash, h); (e = hash_hash(hash, i)) ; i = hash_next(hash, i)) {
if((e != hash_freed) && /* Check freed */
(e->hash == h) && /* Check hash */
(e->key.size == size) && /* Check key size */
(memcmp(e->key.pointer, key, size) == 0)) { /* Full key compare */
r = e->value;
break;
}
}
return r;
}
has_t * has_hash_get_str(has_t *hash, const char *string)
{
return has_hash_get(hash, string, strlen(string));
}
has_t * has_hash_remove(has_t *hash, const char *key, size_t size)
{
size_t i;
has_hash_entry_t *e;
has_t *r = NULL;
uint32_t h;
if(hash == NULL) {
return NULL;
}
h = has_hash_function(key, size);
for(i = hash_first(hash, h); (e = hash_hash(hash, i)) ; i = hash_next(hash, i)) {
if((e != hash_freed) && /* Check freed */
(e->hash == h) && /* Check hash */
(e->key.size == size) && /* Check key size */
(memcmp(e->key.pointer, key, size) == 0)) { /* Full key compare */
if(e->key.owner) {
free(e->key.pointer);
}
r = e->value;
/* Lazy free */
hash->value.hash.hash[i] = hash_freed;
hash->value.hash.count--;
e->hash = 0;
e->key.size = 0;
e->key.pointer = NULL;
break;
}
}
/* Resilver when hash is empty */
if(hash->value.hash.count == 0) {
memset(hash->value.hash.entries, 0,
hash->value.hash.size * sizeof(has_hash_entry_t));
memset(hash->value.hash.hash, 0,
hash_size(hash->value.hash.size) * sizeof(has_hash_entry_t *));
}
return r;
}
has_t * has_hash_remove_str(has_t *hash, const char *string)
{
return has_hash_remove(hash, string, strlen(string));
}
bool has_hash_delete(has_t *hash, const char *key, size_t size)
{
has_t *t = has_hash_remove(hash, key, size);
if(t) {
has_free(t);
return true;
}
return false;
}
bool has_hash_delete_str(has_t *hash, const char *string)
{
return has_hash_delete(hash, string, strlen(string));
}
int has_hash_keys_values(has_t *hash, char ***keys, size_t **lengths,
has_t ***values, int *count)
{
char **k = NULL;
size_t *l = NULL;
has_t **v = NULL;
int i, j;
if(hash == NULL || hash->type != has_hash ||
(keys == NULL && lengths == NULL && values == NULL) ||
((keys == NULL) != (lengths == NULL)) ||
((keys && lengths) &&
((k = calloc(sizeof(char *), (hash->value.hash.count + 1))) == NULL ||
(l = calloc(sizeof(size_t), (hash->value.hash.count + 1))) == NULL)) ||
((values != NULL) &&
(v = calloc(sizeof(has_t *), (hash->value.hash.count + 1))) == NULL)) {
return -1;
}
for(i = 0, j = 0; j < hash->value.hash.count &&
i < hash->value.hash.size; i++) {
if(hash->value.hash.entries[i].key.pointer) {
if(k && l) {
k[j] = hash->value.hash.entries[i].key.pointer;
l[j] = hash->value.hash.entries[i].key.size;
}
if(v) {
v[j] = hash->value.hash.entries[i].value;
}
j++;
}
}
if(count) {
*count = hash->value.hash.count;
}
if(keys && lengths) {
*keys = k;
*lengths = l;
}
if(values) {
*values = v;
}
return 0;
}
int has_hash_keys(has_t *hash, char ***keys, size_t** lengths, int *count)
{
return has_hash_keys_values(hash, keys, lengths, NULL, count);
}
int has_hash_values(has_t *hash, has_t ***values, int *count)
{
return has_hash_keys_values(hash, NULL, NULL, values, count);
}
static char *xstrndup(const char *str, int l)
{
char *r = NULL;
if(l >= 0 && (r = calloc(l + 1, 1)) != NULL) {
memcpy(r, str, l);
}
return r;
}
int has_hash_keys_str(has_t *hash, char ***keys, int *count)
{
char **k;
int i, j;
if(hash == NULL || hash->type != has_hash || keys == NULL ||
(k = calloc(sizeof(char *), (hash->value.hash.count + 1))) == NULL) {
return -1;
}
for(i = 0, j = 0; j < hash->value.hash.count &&
i < hash->value.hash.size; i++) {
if(hash->value.hash.entries[i].key.pointer) {
if((k[j] = xstrndup(hash->value.hash.entries[i].key.pointer,
hash->value.hash.entries[i].key.size)) == NULL) {
break;
}
j++;
}
}
if(j != hash->value.hash.count) {
for(i = 0 ; k[i]; i++) {
free(k[i]);
}
free(k);
return -1;
}
if(count) {
*count = hash->value.hash.count;
}
*keys = k;
return 0;
}
has_t * has_array_new(size_t size)
{
has_t *r = has_new(1), *s = NULL;
if(r && (s = has_array_init(r, size)) == NULL) {
has_free(r);
}
return s;
}
has_t * has_array_init(has_t *array, size_t size)
{
if(array == NULL) {
return NULL;
}
array->value.array.elements = calloc(sizeof(has_t *), size);
if(array->value.array.elements == NULL) {
return NULL;
}
array->type = has_array;
array->value.array.size = size;
array->value.array.count = 0;
return array;
}
has_t * has_array_reallocate(has_t *array, size_t size)
{
size_t n;
has_t **new;
if(array == NULL || array->type != has_array) {
return NULL;
}
if((n = array->value.array.size) > size) {
return array; /* Already big enough */
}
while(n < size) { n *= 2; } /* Double until big enough */
new = realloc(array->value.array.elements, n * sizeof(has_t *));
if(new == NULL) {
return NULL;
}
/* Zero new part */
memset(new + sizeof(has_t *) * array->value.array.size, 0,
sizeof(has_t *) * (n - array->value.array.size));
array->value.array.elements = new;
array->value.array.size = n;
return array;
}
has_t * has_array_push(has_t *array, has_t *value)
{
if(array == NULL) {
return NULL;
}
if(array->value.array.count == array->value.array.size &&
(has_array_reallocate(array, array->value.array.size * 2) == NULL)) {
return NULL;
}
array->value.array.elements[array->value.array.count] = value;
array->value.array.count++;
return array;
}
has_t * has_array_pop(has_t *array)
{
has_t *r = NULL;
if(array && array->type == has_array && array->value.array.count > 0) {
array->value.array.count--;
r = array->value.array.elements[array->value.array.count];
array->value.array.elements[array->value.array.count] = NULL;
}
return r;
}
has_t * has_array_set(has_t *array, size_t index, has_t *value)
{
if(array == NULL || array->type != has_array) {
return NULL;
}
if(index > array->value.array.size &&
(has_array_reallocate(array, index) == NULL)) {
return NULL;
}
array->value.array.elements[index] = value;
if(index >= array->value.array.count) {
array->value.array.count = index + 1;
}
return array;
}
has_t * has_array_get(has_t *array, size_t index)
{
return (array && array->type == has_array &&
array->value.array.count > index) ?
array->value.array.elements[index] : NULL;
}
int has_array_count(has_t *array)
{
return (array && array->type == has_array) ? array->value.array.count : 0;
}
has_t * has_string_init(has_t *string, char *pointer, size_t size, bool owner)
{
if(string == NULL) {
return NULL;
}
string->type = has_string;
string->value.string.pointer = pointer;
string->value.string.owner = owner;
string->value.string.size = size;
return string;
}
has_t * has_string_init_str(has_t *string, char *str, bool owner)
{
return has_string_init(string, str, strlen(str), owner);
}
has_t * has_string_new(char *pointer, size_t size)
{
return has_string_init(has_new(1), pointer, size, false);
}
has_t * has_string_new_o(char *pointer, size_t size, bool owner)
{
return has_string_init(has_new(1), pointer, size, owner);
}
has_t * has_string_new_str(char *str)
{
return has_string_init(has_new(1), str, strlen(str), false);
}
has_t * has_string_new_str_o(char *str, bool owner)
{
return has_string_init(has_new(1), str, strlen(str), owner);
}
inline bool has_is_string(has_t *e)
{
return (e && e->type == has_string) ? true : false;
}
has_t * has_null_new(int32_t value)
{
return has_null_init(has_new(1));
}
has_t * has_null_init(has_t *null)
{
if(null) {
null->type = has_null;
}
return null;
}
inline bool has_is_null(has_t *e)
{
return (e && e->type == has_null) ? true : false;
}
has_t * has_int_new(int32_t value)
{
return has_int_init(has_new(1), value);
}
has_t * has_int_init(has_t *integer, int32_t value)
{
if(integer) {
integer->type = has_integer;
integer->value.integer = value;
}
return integer;
}
inline bool has_is_int(has_t *e)
{
return (e && e->type == has_integer) ? true : false;
}
int32_t has_int_get(has_t *integer)
{
return (integer && integer->type == has_integer) ?
integer->value.integer : 0;
}
has_t * has_bool_new(bool value)
{
return has_bool_init(has_new(1), value);
}
has_t * has_bool_init(has_t *boolean, bool value)
{
if(boolean) {
boolean->type = has_boolean;
boolean->value.boolean = value;
}
return boolean;
}
bool has_bool_get(has_t *boolean)
{
return (boolean && boolean->type == has_boolean) ?
boolean->value.boolean : 0;
}
has_t * has_double_new(double value)
{
return has_double_init(has_new(1), value);
}
has_t * has_double_init(has_t *fp, double value)
{
if(fp) {
fp->type = has_double;
fp->value.fp = value;
}
return fp;
}
double has_double_get(has_t *fp)
{
return (fp && fp->type == has_double) ?
fp->value.fp : 0.0;
}
inline bool has_is_array(has_t *e)
{
return (e && e->type == has_array) ? true : false;
}
inline bool has_is_boolean(has_t *e)
{
return (e && e->type == has_boolean) ? true : false;
}
inline bool has_is_double(has_t *e)
{
return (e && e->type == has_double) ? true : false;
}
inline bool has_is_pointer(has_t *e)
{
return (e && e->type == has_pointer) ? true : false;
}
#define SuperFastHash has_hash_function
/*
* SuperFastHash by Paul Hsieh : http://www.azillionmonkeys.com/qed/hash.html
* Public domain version http://burtleburtle.net/bob/hash/doobs.html
*/
#undef get16bits
#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
|| defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
#define get16bits(d) (*((const uint16_t *) (d)))
#endif
#if !defined (get16bits)
#define get16bits(d) ((((const uint8_t *)(d))[1] << UINT32_C(8))\
+((const uint8_t *)(d))[0])
#endif
uint32_t SuperFastHash (const char * data, int len) {
uint32_t hash = len, tmp;
int rem;
if (len <= 0 || data == NULL) return 0;
rem = len & 3;
len >>= 2;
/* Main loop */
for (;len > 0; len--) {
hash += get16bits (data);
tmp = (get16bits (data+2) << 11) ^ hash;
hash = (hash << 16) ^ tmp;
data += 2*sizeof (uint16_t);
hash += hash >> 11;
}
/* Handle end cases */
switch (rem) {
case 3: hash += get16bits (data);
hash ^= hash << 16;
hash ^= data[sizeof (uint16_t)] << 18;
hash += hash >> 11;
break;
case 2: hash += get16bits (data);
hash ^= hash << 11;
hash += hash >> 17;
break;
case 1: hash += *data;
hash ^= hash << 10;
hash += hash >> 1;
}
/* Force "avalanching" of final 127 bits */
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}