-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_trie.h
1045 lines (947 loc) · 33 KB
/
component_trie.h
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _COMPONENT_TRIE_H_
#define _COMPONENT_TRIE_H_
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <pthread.h>
#ifdef __cplusplus
extern "C"{
#endif
// --------------- P1 exported interface --------------
// // structure defination of component trie
// typedef struct ct_trie {
// ct_node_t *root;
// char delimiter[MAX_DELIMITER_LEN];
// int delimiter_len;
// uint32_t version;
// pthread_rwlock_t lock;
// } ct_trie_t;
// // some macro defination
// #define MAX_COMPONENT_LEN 16
// #define MAX_DELIMITER_LEN 4
// // new a trie with a specific delimiter. such as: '/', ': ', ', ', '::'
// ct_trie_t* ct_trie_new(const char *delimiter, int delimiter_len);
// // free a trie. not thread safe.
// void ct_trie_free(ct_trie_t *t);
// // insert a kv pair item (prefix, data)
// int ct_trie_insert(ct_trie_t* t, const char *prefix, int prefix_len, void *udata);
// // longest prefix match a prefix and get the corresponding data with udata.
// int ct_trie_lpm(ct_trie_t* t, const char *name, int name_len, void **udata);
// // exact prefix match a prefix and get the corresponding data with udata.
// int ct_trie_em(ct_trie_t* t, const char *name, int name_len, void **udata);
// // remove a kv pair and the the removed data with udata.
// int ct_trie_remove(ct_trie_t* t, const char *prefix, int prefix_len, void **udata);
// --------------- P2 tidwall/hashmap.c --------------
// Copyright 2020 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
#define GROW_AT 0.60
#define SHRINK_AT 0.10
static void *(*__malloc)(size_t) = NULL;
static void *(*__realloc)(void *, size_t) = NULL;
static void (*__free)(void *) = NULL;
// hashmap_set_allocator allows for configuring a custom allocator for
// all hashmap library operations. This function, if needed, should be called
// only once at startup and a prior to calling hashmap_new().
static inline void hashmap_set_allocator(void *(*malloc)(size_t), void (*free)(void*)) {
__malloc = malloc;
__free = free;
}
struct bucket {
uint64_t hash:48;
uint64_t dib:16;
};
// hashmap is an open addressed hash map using robinhood hashing.
struct hashmap {
void *(*malloc)(size_t);
void *(*realloc)(void *, size_t);
void (*free)(void *);
size_t elsize;
size_t cap;
uint64_t seed0;
uint64_t seed1;
uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1);
int (*compare)(const void *a, const void *b, void *udata);
void (*elfree)(void *item);
void *udata;
size_t bucketsz;
size_t nbuckets;
size_t count;
size_t mask;
size_t growat;
size_t shrinkat;
uint8_t growpower;
bool oom;
void *buckets;
void *spare;
void *edata;
};
static inline void hashmap_set_grow_by_power(struct hashmap *map, size_t power) {
map->growpower = power < 1 ? 1 : power > 16 ? 16 : power;
}
static inline struct bucket *bucket_at0(void *buckets, size_t bucketsz, size_t i) {
return (struct bucket*)(((char*)buckets)+(bucketsz*i));
}
static inline struct bucket *bucket_at(struct hashmap *map, size_t index) {
return bucket_at0(map->buckets, map->bucketsz, index);
}
static inline void *bucket_item(struct bucket *entry) {
return ((char*)entry)+sizeof(struct bucket);
}
static inline uint64_t clip_hash(uint64_t hash) {
return hash & 0xFFFFFFFFFFFF;
}
static inline uint64_t get_hash(struct hashmap *map, const void *key) {
return clip_hash(map->hash(key, map->seed0, map->seed1));
}
// hashmap_new_with_allocator returns a new hash map using a custom allocator.
// See hashmap_new for more information information
static inline struct hashmap *hashmap_new_with_allocator(void *(*_malloc)(size_t),
void *(*_realloc)(void*, size_t), void (*_free)(void*),
size_t elsize, size_t cap, uint64_t seed0, uint64_t seed1,
uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b, void *udata),
void (*elfree)(void *item),
void *udata)
{
_malloc = _malloc ? _malloc : __malloc ? __malloc : malloc;
_realloc = _realloc ? _realloc : __realloc ? __realloc : realloc;
_free = _free ? _free : __free ? __free : free;
size_t ncap = 16;
if (cap < ncap) {
cap = ncap;
} else {
while (ncap < cap) {
ncap *= 2;
}
cap = ncap;
}
// printf("%d\n", (int)cap);
size_t bucketsz = sizeof(struct bucket) + elsize;
while (bucketsz & (sizeof(uintptr_t)-1)) {
bucketsz++;
}
// hashmap + spare + edata
size_t size = sizeof(struct hashmap)+bucketsz*2;
struct hashmap *map = (struct hashmap*)_malloc(size);
if (!map) {
return NULL;
}
memset(map, 0, sizeof(struct hashmap));
map->elsize = elsize;
map->bucketsz = bucketsz;
map->seed0 = seed0;
map->seed1 = seed1;
map->hash = hash;
map->compare = compare;
map->elfree = elfree;
map->udata = udata;
map->spare = ((char*)map)+sizeof(struct hashmap);
map->edata = (char*)map->spare+bucketsz;
map->cap = cap;
map->nbuckets = cap;
map->mask = map->nbuckets-1;
map->buckets = _malloc(map->bucketsz*map->nbuckets);
if (!map->buckets) {
_free(map);
return NULL;
}
memset(map->buckets, 0, map->bucketsz*map->nbuckets);
map->growpower = 1;
map->growat = map->nbuckets*GROW_AT;
map->shrinkat = map->nbuckets*SHRINK_AT;
map->malloc = _malloc;
map->realloc = _realloc;
map->free = _free;
return map;
}
// hashmap_new returns a new hash map.
// Param `elsize` is the size of each element in the tree. Every element that
// is inserted, deleted, or retrieved will be this size.
// Param `cap` is the default lower capacity of the hashmap. Setting this to
// zero will default to 16.
// Params `seed0` and `seed1` are optional seed values that are passed to the
// following `hash` function. These can be any value you wish but it's often
// best to use randomly generated values.
// Param `hash` is a function that generates a hash value for an item. It's
// important that you provide a good hash function, otherwise it will perform
// poorly or be vulnerable to Denial-of-service attacks. This implementation
// comes with two helper functions `hashmap_sip()` and `hashmap_murmur()`.
// Param `compare` is a function that compares items in the tree. See the
// qsort stdlib function for an example of how this function works.
// The hashmap must be freed with hashmap_free().
// Param `elfree` is a function that frees a specific item. This should be NULL
// unless you're storing some kind of reference data in the hash.
static inline struct hashmap *hashmap_new(size_t elsize, size_t cap, uint64_t seed0,
uint64_t seed1,
uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1),
int (*compare)(const void *a, const void *b, void *udata),
void (*elfree)(void *item),
void *udata)
{
return hashmap_new_with_allocator(NULL, NULL, NULL, elsize, cap, seed0,
seed1, hash, compare, elfree, udata);
}
static inline void free_elements(struct hashmap *map) {
if (map->elfree) {
for (size_t i = 0; i < map->nbuckets; i++) {
struct bucket *bucket = bucket_at(map, i);
if (bucket->dib) map->elfree(bucket_item(bucket));
}
}
}
// hashmap_clear quickly clears the map.
// Every item is called with the element-freeing function given in hashmap_new,
// if present, to free any data referenced in the elements of the hashmap.
// When the update_cap is provided, the map's capacity will be updated to match
// the currently number of allocated buckets. This is an optimization to ensure
// that this operation does not perform any allocations.
static inline void hashmap_clear(struct hashmap *map, bool update_cap) {
map->count = 0;
free_elements(map);
if (update_cap) {
map->cap = map->nbuckets;
} else if (map->nbuckets != map->cap) {
void *new_buckets = map->malloc(map->bucketsz*map->cap);
if (new_buckets) {
map->free(map->buckets);
map->buckets = new_buckets;
}
map->nbuckets = map->cap;
}
memset(map->buckets, 0, map->bucketsz*map->nbuckets);
map->mask = map->nbuckets-1;
map->growat = map->nbuckets*0.75;
map->shrinkat = map->nbuckets*0.10;
}
static inline bool resize0(struct hashmap *map, size_t new_cap) {
struct hashmap *map2 = hashmap_new_with_allocator(map->malloc, map->realloc,
map->free, map->elsize, new_cap, map->seed0, map->seed1, map->hash,
map->compare, map->elfree, map->udata);
if (!map2) return false;
for (size_t i = 0; i < map->nbuckets; i++) {
struct bucket *entry = bucket_at(map, i);
if (!entry->dib) {
continue;
}
entry->dib = 1;
size_t j = entry->hash & map2->mask;
while(1) {
struct bucket *bucket = bucket_at(map2, j);
if (bucket->dib == 0) {
memcpy(bucket, entry, map->bucketsz);
break;
}
if (bucket->dib < entry->dib) {
memcpy(map2->spare, bucket, map->bucketsz);
memcpy(bucket, entry, map->bucketsz);
memcpy(entry, map2->spare, map->bucketsz);
}
j = (j + 1) & map2->mask;
entry->dib += 1;
}
}
map->free(map->buckets);
map->buckets = map2->buckets;
map->nbuckets = map2->nbuckets;
map->mask = map2->mask;
map->growat = map2->growat;
map->shrinkat = map2->shrinkat;
map->free(map2);
return true;
}
static inline bool resize(struct hashmap *map, size_t new_cap) {
return resize0(map, new_cap);
}
// hashmap_set_with_hash works like hashmap_set but you provide your
// own hash. The 'hash' callback provided to the hashmap_new function
// will not be called
static inline const void *hashmap_set_with_hash(struct hashmap *map, const void *item,
uint64_t hash)
{
hash = clip_hash(hash);
map->oom = false;
if (map->count == map->growat) {
if (!resize(map, map->nbuckets*(1<<map->growpower))) {
map->oom = true;
return NULL;
}
}
struct bucket *entry = (struct bucket*)map->edata;
entry->hash = hash;
entry->dib = 1;
void *eitem = bucket_item(entry);
memcpy(eitem, item, map->elsize);
void *bitem;
size_t i = entry->hash & map->mask;
while(1) {
struct bucket *bucket = bucket_at(map, i);
if (bucket->dib == 0) {
memcpy(bucket, entry, map->bucketsz);
map->count++;
return NULL;
}
bitem = bucket_item(bucket);
if (entry->hash == bucket->hash && (!map->compare ||
map->compare(eitem, bitem, map->udata) == 0))
{
memcpy(map->spare, bitem, map->elsize);
memcpy(bitem, eitem, map->elsize);
return map->spare;
}
if (bucket->dib < entry->dib) {
memcpy(map->spare, bucket, map->bucketsz);
memcpy(bucket, entry, map->bucketsz);
memcpy(entry, map->spare, map->bucketsz);
eitem = bucket_item(entry);
}
i = (i + 1) & map->mask;
entry->dib += 1;
}
}
// hashmap_set inserts or replaces an item in the hash map. If an item is
// replaced then it is returned otherwise NULL is returned. This operation
// may allocate memory. If the system is unable to allocate additional
// memory then NULL is returned and hashmap_oom() returns true.
static inline const void *hashmap_set(struct hashmap *map, const void *item) {
return hashmap_set_with_hash(map, item, get_hash(map, item));
}
// hashmap_get_with_hash works like hashmap_get but you provide your
// own hash. The 'hash' callback provided to the hashmap_new function
// will not be called
static inline const void *hashmap_get_with_hash(struct hashmap *map, const void *key,
uint64_t hash)
{
hash = clip_hash(hash);
size_t i = hash & map->mask;
while(1) {
struct bucket *bucket = bucket_at(map, i);
if (!bucket->dib) return NULL;
if (bucket->hash == hash) {
void *bitem = bucket_item(bucket);
if (!map->compare || map->compare(key, bitem, map->udata) == 0) {
return bitem;
}
}
i = (i + 1) & map->mask;
}
}
// hashmap_get returns the item based on the provided key. If the item is not
// found then NULL is returned.
static inline const void *hashmap_get(struct hashmap *map, const void *key) {
return hashmap_get_with_hash(map, key, get_hash(map, key));
}
// hashmap_probe returns the item in the bucket at position or NULL if an item
// is not set for that bucket. The position is 'moduloed' by the number of
// buckets in the hashmap.
static inline const void *hashmap_probe(struct hashmap *map, uint64_t position) {
size_t i = position & map->mask;
struct bucket *bucket = bucket_at(map, i);
if (!bucket->dib) {
return NULL;
}
return bucket_item(bucket);
}
// hashmap_delete_with_hash works like hashmap_delete but you provide your
// own hash. The 'hash' callback provided to the hashmap_new function
// will not be called
static inline const void *hashmap_delete_with_hash(struct hashmap *map, const void *key,
uint64_t hash)
{
hash = clip_hash(hash);
map->oom = false;
size_t i = hash & map->mask;
while(1) {
struct bucket *bucket = bucket_at(map, i);
if (!bucket->dib) {
return NULL;
}
void *bitem = bucket_item(bucket);
if (bucket->hash == hash && (!map->compare ||
map->compare(key, bitem, map->udata) == 0))
{
memcpy(map->spare, bitem, map->elsize);
bucket->dib = 0;
while(1) {
struct bucket *prev = bucket;
i = (i + 1) & map->mask;
bucket = bucket_at(map, i);
if (bucket->dib <= 1) {
prev->dib = 0;
break;
}
memcpy(prev, bucket, map->bucketsz);
prev->dib--;
}
map->count--;
if (map->nbuckets > map->cap && map->count <= map->shrinkat) {
// Ignore the return value. It's ok for the resize operation to
// fail to allocate enough memory because a shrink operation
// does not change the integrity of the data.
resize(map, map->nbuckets/2);
}
return map->spare;
}
i = (i + 1) & map->mask;
}
}
// hashmap_delete removes an item from the hash map and returns it. If the
// item is not found then NULL is returned.
static inline const void *hashmap_delete(struct hashmap *map, const void *key) {
return hashmap_delete_with_hash(map, key, get_hash(map, key));
}
// hashmap_count returns the number of items in the hash map.
static inline size_t hashmap_count(struct hashmap *map) {
return map->count;
}
// hashmap_free frees the hash map
// Every item is called with the element-freeing function given in hashmap_new,
// if present, to free any data referenced in the elements of the hashmap.
static inline void hashmap_free(struct hashmap *map) {
if (!map) return;
free_elements(map);
map->free(map->buckets);
map->free(map);
}
// hashmap_oom returns true if the last hashmap_set() call failed due to the
// system being out of memory.
static inline bool hashmap_oom(struct hashmap *map) {
return map->oom;
}
// hashmap_scan iterates over all items in the hash map
// Param `iter` can return false to stop iteration early.
// Returns false if the iteration has been stopped early.
static inline bool hashmap_scan(struct hashmap *map,
bool (*iter)(const void *item, void *udata), void *udata)
{
for (size_t i = 0; i < map->nbuckets; i++) {
struct bucket *bucket = bucket_at(map, i);
if (bucket->dib && !iter(bucket_item(bucket), udata)) {
return false;
}
}
return true;
}
// hashmap_iter iterates one key at a time yielding a reference to an
// entry at each iteration. Useful to write simple loops and avoid writing
// dedicated callbacks and udata structures, as in hashmap_scan.
//
// map is a hash map handle. i is a pointer to a size_t cursor that
// should be initialized to 0 at the beginning of the loop. item is a void
// pointer pointer that is populated with the retrieved item. Note that this
// is NOT a copy of the item stored in the hash map and can be directly
// modified.
//
// Note that if hashmap_delete() is called on the hashmap being iterated,
// the buckets are rearranged and the iterator must be reset to 0, otherwise
// unexpected results may be returned after deletion.
//
// This function has not been tested for thread safety.
//
// The function returns true if an item was retrieved; false if the end of the
// iteration has been reached.
static inline bool hashmap_iter(struct hashmap *map, size_t *i, void **item) {
struct bucket *bucket;
do {
if (*i >= map->nbuckets) return false;
bucket = bucket_at(map, *i);
(*i)++;
} while (!bucket->dib);
*item = bucket_item(bucket);
return true;
}
//-----------------------------------------------------------------------------
// SipHash reference C implementation
//
// Copyright (c) 2012-2016 Jean-Philippe Aumasson
// <jeanphilippe.aumasson@gmail.com>
// Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <http://creativecommons.org/publicdomain/zero/1.0/>.
//
// default: SipHash-2-4
//-----------------------------------------------------------------------------
static inline uint64_t SIP64(const uint8_t *in, const size_t inlen, uint64_t seed0,
uint64_t seed1)
{
#define U8TO64_LE(p) \
{ (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) }
#define U64TO8_LE(p, v) \
{ U32TO8_LE((p), (uint32_t)((v))); \
U32TO8_LE((p) + 4, (uint32_t)((v) >> 32)); }
#define U32TO8_LE(p, v) \
{ (p)[0] = (uint8_t)((v)); \
(p)[1] = (uint8_t)((v) >> 8); \
(p)[2] = (uint8_t)((v) >> 16); \
(p)[3] = (uint8_t)((v) >> 24); }
#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
#define SIPROUND \
{ v0 += v1; v1 = ROTL(v1, 13); \
v1 ^= v0; v0 = ROTL(v0, 32); \
v2 += v3; v3 = ROTL(v3, 16); \
v3 ^= v2; \
v0 += v3; v3 = ROTL(v3, 21); \
v3 ^= v0; \
v2 += v1; v1 = ROTL(v1, 17); \
v1 ^= v2; v2 = ROTL(v2, 32); }
uint64_t k0 = U8TO64_LE((uint8_t*)&seed0);
uint64_t k1 = U8TO64_LE((uint8_t*)&seed1);
uint64_t v3 = UINT64_C(0x7465646279746573) ^ k1;
uint64_t v2 = UINT64_C(0x6c7967656e657261) ^ k0;
uint64_t v1 = UINT64_C(0x646f72616e646f6d) ^ k1;
uint64_t v0 = UINT64_C(0x736f6d6570736575) ^ k0;
const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
for (; in != end; in += 8) {
uint64_t m = U8TO64_LE(in);
v3 ^= m;
SIPROUND; SIPROUND;
v0 ^= m;
}
const int left = inlen & 7;
uint64_t b = ((uint64_t)inlen) << 56;
switch (left) {
case 7: b |= ((uint64_t)in[6]) << 48; /* fall through */
case 6: b |= ((uint64_t)in[5]) << 40; /* fall through */
case 5: b |= ((uint64_t)in[4]) << 32; /* fall through */
case 4: b |= ((uint64_t)in[3]) << 24; /* fall through */
case 3: b |= ((uint64_t)in[2]) << 16; /* fall through */
case 2: b |= ((uint64_t)in[1]) << 8; /* fall through */
case 1: b |= ((uint64_t)in[0]); break;
case 0: break;
}
v3 ^= b;
SIPROUND; SIPROUND;
v0 ^= b;
v2 ^= 0xff;
SIPROUND; SIPROUND; SIPROUND; SIPROUND;
b = v0 ^ v1 ^ v2 ^ v3;
uint64_t out = 0;
U64TO8_LE((uint8_t*)&out, b);
return out;
}
//-----------------------------------------------------------------------------
// MurmurHash3 was written by Austin Appleby, and is placed in the public
// domain. The author hereby disclaims copyright to this source code.
//
// Murmur3_86_128
//-----------------------------------------------------------------------------
static inline uint64_t MM86128(const void *key, const int len, uint32_t seed) {
#define ROTL32(x, r) ((x << r) | (x >> (32 - r)))
#define FMIX32(h) h^=h>>16; h*=0x85ebca6b; h^=h>>13; h*=0xc2b2ae35; h^=h>>16;
const uint8_t * data = (const uint8_t*)key;
const int nblocks = len / 16;
uint32_t h1 = seed;
uint32_t h2 = seed;
uint32_t h3 = seed;
uint32_t h4 = seed;
uint32_t c1 = 0x239b961b;
uint32_t c2 = 0xab0e9789;
uint32_t c3 = 0x38b34ae5;
uint32_t c4 = 0xa1e38b93;
const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
for (int i = -nblocks; i; i++) {
uint32_t k1 = blocks[i*4+0];
uint32_t k2 = blocks[i*4+1];
uint32_t k3 = blocks[i*4+2];
uint32_t k4 = blocks[i*4+3];
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
}
const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
uint32_t k1 = 0;
uint32_t k2 = 0;
uint32_t k3 = 0;
uint32_t k4 = 0;
switch(len & 15) {
case 15: k4 ^= tail[14] << 16; /* fall through */
case 14: k4 ^= tail[13] << 8; /* fall through */
case 13: k4 ^= tail[12] << 0;
k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
/* fall through */
case 12: k3 ^= tail[11] << 24; /* fall through */
case 11: k3 ^= tail[10] << 16; /* fall through */
case 10: k3 ^= tail[ 9] << 8; /* fall through */
case 9: k3 ^= tail[ 8] << 0;
k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
/* fall through */
case 8: k2 ^= tail[ 7] << 24; /* fall through */
case 7: k2 ^= tail[ 6] << 16; /* fall through */
case 6: k2 ^= tail[ 5] << 8; /* fall through */
case 5: k2 ^= tail[ 4] << 0;
k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
/* fall through */
case 4: k1 ^= tail[ 3] << 24; /* fall through */
case 3: k1 ^= tail[ 2] << 16; /* fall through */
case 2: k1 ^= tail[ 1] << 8; /* fall through */
case 1: k1 ^= tail[ 0] << 0;
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
/* fall through */
};
h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
h1 += h2; h1 += h3; h1 += h4;
h2 += h1; h3 += h1; h4 += h1;
FMIX32(h1); FMIX32(h2); FMIX32(h3); FMIX32(h4);
h1 += h2; h1 += h3; h1 += h4;
h2 += h1; h3 += h1; h4 += h1;
return (((uint64_t)h2)<<32)|h1;
}
//-----------------------------------------------------------------------------
// xxHash Library
// Copyright (c) 2012-2021 Yann Collet
// All rights reserved.
//
// BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
//
// xxHash3
//-----------------------------------------------------------------------------
#define XXH_PRIME_1 11400714785074694791ULL
#define XXH_PRIME_2 14029467366897019727ULL
#define XXH_PRIME_3 1609587929392839161ULL
#define XXH_PRIME_4 9650029242287828579ULL
#define XXH_PRIME_5 2870177450012600261ULL
static inline uint64_t XXH_read64(const void* memptr) {
uint64_t val;
memcpy(&val, memptr, sizeof(val));
return val;
}
static inline uint32_t XXH_read32(const void* memptr) {
uint32_t val;
memcpy(&val, memptr, sizeof(val));
return val;
}
static inline uint64_t XXH_rotl64(uint64_t x, int r) {
return (x << r) | (x >> (64 - r));
}
static inline uint64_t xxh3(const void* data, size_t len, uint64_t seed) {
const uint8_t* p = (const uint8_t*)data;
const uint8_t* const end = p + len;
uint64_t h64;
if (len >= 32) {
const uint8_t* const limit = end - 32;
uint64_t v1 = seed + XXH_PRIME_1 + XXH_PRIME_2;
uint64_t v2 = seed + XXH_PRIME_2;
uint64_t v3 = seed + 0;
uint64_t v4 = seed - XXH_PRIME_1;
do {
v1 += XXH_read64(p) * XXH_PRIME_2;
v1 = XXH_rotl64(v1, 31);
v1 *= XXH_PRIME_1;
v2 += XXH_read64(p + 8) * XXH_PRIME_2;
v2 = XXH_rotl64(v2, 31);
v2 *= XXH_PRIME_1;
v3 += XXH_read64(p + 16) * XXH_PRIME_2;
v3 = XXH_rotl64(v3, 31);
v3 *= XXH_PRIME_1;
v4 += XXH_read64(p + 24) * XXH_PRIME_2;
v4 = XXH_rotl64(v4, 31);
v4 *= XXH_PRIME_1;
p += 32;
} while (p <= limit);
h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) +
XXH_rotl64(v4, 18);
v1 *= XXH_PRIME_2;
v1 = XXH_rotl64(v1, 31);
v1 *= XXH_PRIME_1;
h64 ^= v1;
h64 = h64 * XXH_PRIME_1 + XXH_PRIME_4;
v2 *= XXH_PRIME_2;
v2 = XXH_rotl64(v2, 31);
v2 *= XXH_PRIME_1;
h64 ^= v2;
h64 = h64 * XXH_PRIME_1 + XXH_PRIME_4;
v3 *= XXH_PRIME_2;
v3 = XXH_rotl64(v3, 31);
v3 *= XXH_PRIME_1;
h64 ^= v3;
h64 = h64 * XXH_PRIME_1 + XXH_PRIME_4;
v4 *= XXH_PRIME_2;
v4 = XXH_rotl64(v4, 31);
v4 *= XXH_PRIME_1;
h64 ^= v4;
h64 = h64 * XXH_PRIME_1 + XXH_PRIME_4;
}
else {
h64 = seed + XXH_PRIME_5;
}
h64 += (uint64_t)len;
while (p + 8 <= end) {
uint64_t k1 = XXH_read64(p);
k1 *= XXH_PRIME_2;
k1 = XXH_rotl64(k1, 31);
k1 *= XXH_PRIME_1;
h64 ^= k1;
h64 = XXH_rotl64(h64, 27) * XXH_PRIME_1 + XXH_PRIME_4;
p += 8;
}
if (p + 4 <= end) {
h64 ^= (uint64_t)(XXH_read32(p)) * XXH_PRIME_1;
h64 = XXH_rotl64(h64, 23) * XXH_PRIME_2 + XXH_PRIME_3;
p += 4;
}
while (p < end) {
h64 ^= (*p) * XXH_PRIME_5;
h64 = XXH_rotl64(h64, 11) * XXH_PRIME_1;
p++;
}
h64 ^= h64 >> 33;
h64 *= XXH_PRIME_2;
h64 ^= h64 >> 29;
h64 *= XXH_PRIME_3;
h64 ^= h64 >> 32;
return h64;
}
// hashmap_sip returns a hash value for `data` using SipHash-2-4.
static inline uint64_t hashmap_sip(const void *data, size_t len, uint64_t seed0,
uint64_t seed1)
{
return SIP64((uint8_t*)data, len, seed0, seed1);
}
// hashmap_murmur returns a hash value for `data` using Murmur3_86_128.
static inline uint64_t hashmap_murmur(const void *data, size_t len, uint64_t seed0,
uint64_t seed1)
{
(void)seed1;
return MM86128(data, len, seed0);
}
static inline uint64_t hashmap_xxhash3(const void *data, size_t len, uint64_t seed0,
uint64_t seed1)
{
(void)seed1;
return xxh3(data, len ,seed0);
}
#define MAX_COMPONENT_LEN 16
#define MAX_DELIMITER_LEN 4
typedef struct ct_node {
char component[MAX_COMPONENT_LEN];
int component_len;
struct hashmap *children;
bool flag;
void *udata;
} ct_node_t;
static inline ct_node_t* ct_node_new(char *component, int component_len) {
if (component == NULL || component_len > MAX_COMPONENT_LEN) {
return NULL;
}
ct_node_t *n = (ct_node_t*)malloc(sizeof(ct_node_t));
memcpy(n->component, component, component_len);
n->component_len = component_len;
n->children = NULL;
n->flag = false;
n->udata = NULL;
return n;
}
static inline int ct_node_init(ct_node_t* n, const char *component, int component_len) {
if (component == NULL || component_len > MAX_COMPONENT_LEN) {
return -1;
}
memcpy(n->component, component, component_len);
n->component_len = component_len;
n->children = NULL;
n->flag = false;
n->udata = NULL;
return 0;
}
static inline void ct_node_free(ct_node_t *n) {
if (n->children != NULL) {
hashmap_free(n->children);
}
free(n);
}
typedef struct ct_trie {
ct_node_t *root;
char delimiter[MAX_DELIMITER_LEN];
int delimiter_len;
uint32_t version;
pthread_rwlock_t lock;
} ct_trie_t;
static inline ct_trie_t* ct_trie_new(const char *delimiter, int delimiter_len) {
if (delimiter == NULL || delimiter_len > MAX_DELIMITER_LEN) {
return NULL;
}
ct_trie_t *t = (struct ct_trie*)malloc(sizeof(ct_trie_t));
memcpy(t->delimiter, delimiter, delimiter_len);
t->delimiter_len = delimiter_len;
t->version = 0;
t->root = ct_node_new((char*)"ROOT", strlen("ROOT"));
if (t->root == NULL) {
printf("error\n");
exit(0);
}
pthread_rwlock_init(&t->lock, NULL);
return t;
}
static inline void ct_trie_free(ct_trie_t *t) {
pthread_rwlock_destroy(&t->lock);
ct_node_free(t->root);
free(t);
}
static inline int component_compare(const void *a, const void *b, void *udata) {
const ct_node_t *na = (const ct_node_t *)a;
const ct_node_t *nb = (const ct_node_t *)b;
if (na->component_len != na->component_len) {
return -1;
}
return memcmp(na->component, nb->component, na->component_len);
}
static inline uint64_t component_hash(const void *item, uint64_t seed0, uint64_t seed1) {
const ct_node_t *n = (const ct_node_t *)item;
return hashmap_xxhash3(n->component,n->component_len, seed0, seed1);
}
static inline int ct_trie_insert(ct_trie_t* t, const char *prefix, int prefix_len, void *udata) {
if (prefix_len <= t->delimiter_len * 2
|| memcmp(t->delimiter, prefix, t->delimiter_len)
|| memcmp(t->delimiter, prefix + prefix_len - t->delimiter_len, t->delimiter_len)) {
return -1;
}
int ret = 0;
int idx = t->delimiter_len;
int start = idx;
ct_node_t *node = t->root;
pthread_rwlock_wrlock(&t->lock);
while (idx < prefix_len) {
while (memcmp(t->delimiter, prefix + idx, t->delimiter_len)) idx++;
ct_node_t tmp;
ct_node_init(&tmp, prefix + start, idx - start);
if (node->children == NULL) {
node->children = hashmap_new(sizeof(ct_node_t), 0, 0, 0,
component_hash, component_compare, NULL, NULL);
hashmap_set(node->children, &tmp);
} else if (hashmap_get(node->children, &tmp) == NULL) {
hashmap_set(node->children, &tmp);
if (hashmap_oom(node->children)) {
ret = -2;
goto error_out;
}
}
node = (ct_node_t*)hashmap_get(node->children, &tmp);
idx += t->delimiter_len;
start = idx;
}
if (node->flag) {
ret = 0;
} else {
ret = 1;
}
node->flag = true;
node->udata = udata;
t->version++;
error_out:
pthread_rwlock_unlock(&t->lock);
return ret;
}
static inline int ct_trie_match(ct_trie_t* t, const char *name, int name_len,
void **udata, bool exact) {
if (name_len <= t->delimiter_len * 2
|| memcmp(t->delimiter, name, t->delimiter_len)) {
return -1;
}
int end = name_len - t->delimiter_len;
while (memcmp(t->delimiter, name + end, t->delimiter_len)) end--;
if (end == 0) {
return -1;
}
int idx = t->delimiter_len;
int start = idx;
ct_node_t *node = t->root;
int walk_cn = 0;
int match_cn = -1;
void *match_data = NULL;
pthread_rwlock_rdlock(&t->lock);
while (idx < end) {
if (node->children == NULL) {
goto final;
}
while (memcmp(t->delimiter, name + idx, t->delimiter_len)) idx++;
ct_node_t tmp;
ct_node_init(&tmp, name + start, idx - start);
node = (ct_node_t*)hashmap_get(node->children, &tmp);
if (node == NULL) {
goto final;
}
walk_cn++;
if (node->flag) {
match_cn = walk_cn;
match_data = node->udata;
}
idx += t->delimiter_len;
start = idx;
}
final:
pthread_rwlock_unlock(&t->lock);
if (exact == false && node == NULL) {
if (udata != NULL)
*udata = NULL;
return -1;
}
if (udata != NULL)
*udata = match_data;
return match_cn;
}
static inline int ct_trie_lpm(ct_trie_t* t, const char *name, int name_len, void **udata) {
return ct_trie_match(t, name, name_len, udata, false);
}
static inline int ct_trie_em(ct_trie_t* t, const char *name, int name_len, void **udata) {
return ct_trie_match(t, name, name_len, udata, true);
}