/
judy64g.c
1398 lines (1131 loc) · 31.1 KB
/
judy64g.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
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
// Judy arrays 17 APR 2012
// Author Karl Malbrain, malbrain@yahoo.com
// with assistance from Jan Weiss.
// Simplified judy arrays for strings
// Adapted from the ideas of Douglas Baskins of HP.
// Map a set of strings to corresponding memory cells (uints).
// Each cell must be set to a non-zero value by the caller.
// STANDALONE is defined to compile into a string sorter.
#define STANDALONE
// functions:
// judy_open: open a new judy array returning a judy object.
// judy_close: close an open judy array, freeing all memory.
// judy_clone: clone an open judy array, duplicating the stack.
// judy_data: allocate data memory within judy array for external use.
// judy_cell: insert a string into the judy array, return cell pointer.
// judy_strt: retrieve the cell pointer greater than or equal to given key
// judy_slot: retrieve the cell pointer, or return NULL for a given key.
// judy_key: retrieve the string value for the most recent judy query.
// judy_end: retrieve the cell pointer for the last string in the array.
// judy_nxt: retrieve the cell pointer for the next string in the array.
// judy_prv: retrieve the cell pointer for the prev string in the array.
// judy_del: delete the key and cell for the current stack entry.
#include <stdlib.h>
#include <memory.h>
#ifdef linux
#include <endian.h>
#else
#ifdef __BIG_ENDIAN__
#ifndef BYTE_ORDER
#define BYTE_ORDER 4321
#endif
#else
#ifndef BYTE_ORDER
#define BYTE_ORDER 1234
#endif
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN 4321
#endif
#endif
typedef unsigned char uchar;
typedef unsigned int uint;
#define PRIuint "u"
#if defined(__LP64__) || \
defined(__x86_64__) || \
defined(__amd64__) || \
defined(_WIN64) || \
defined(__sparc64__) || \
defined(__arch64__) || \
defined(__powerpc64__) || \
defined (__s390x__)
// defines for 64 bit
typedef unsigned long long judyvalue;
typedef unsigned long long JudySlot;
#define JUDY_key_mask (0x07)
#define JUDY_key_size 8
#define JUDY_slot_size 8
#define JUDY_span_bytes (3 * JUDY_key_size)
#define JUDY_span_equiv JUDY_2
#define JUDY_radix_equiv JUDY_8
#define PRIjudyvalue "llu"
#else
// defines for 32 bit
typedef uint judyvalue;
typedef uint JudySlot;
#define JUDY_key_mask (0x03)
#define JUDY_key_size 4
#define JUDY_slot_size 4
#define JUDY_span_bytes (7 * JUDY_key_size)
#define JUDY_span_equiv JUDY_4
#define JUDY_radix_equiv JUDY_8
#define PRIjudyvalue "u"
#endif
#define JUDY_mask (~(JudySlot)0x07)
#ifdef STANDALONE
#include <stdio.h>
#include <assert.h>
uint MaxMem = 0;
// void judy_abort (char *msg) __attribute__ ((noreturn)); // Tell static analyser that this function will not return
void judy_abort (char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
#endif
#if !defined(_WIN32)
void vfree (void *what, uint size)
{
free (what);
}
#elif defined(_WIN32)
#include <windows.h>
void *valloc (uint size)
{
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
}
void vfree (void *what, uint size)
{
VirtualFree(what, 0, MEM_RELEASE);
}
#endif
#define JUDY_seg 65536
enum JUDY_types {
JUDY_radix = 0, // inner and outer radix fan-out
JUDY_1 = 1, // linear list nodes of designated count
JUDY_2 = 2,
JUDY_4 = 3,
JUDY_8 = 4,
JUDY_16 = 5,
JUDY_32 = 6,
JUDY_span = 7, // up to 28 tail bytes of key contiguously stored
};
int JudySize[] = {
(JUDY_slot_size * 16), // JUDY_radix node size
(JUDY_slot_size + JUDY_key_size), // JUDY_1 node size
(2 * JUDY_slot_size + 2 * JUDY_key_size),
(4 * JUDY_slot_size + 4 * JUDY_key_size),
(8 * JUDY_slot_size + 8 * JUDY_key_size),
(16 * JUDY_slot_size + 16 * JUDY_key_size),
(32 * JUDY_slot_size + 32 * JUDY_key_size),
(JUDY_span_bytes + JUDY_slot_size)
};
judyvalue JudyMask[9] = {
0, 0xff, 0xffff, 0xffffff, 0xffffffff,
#if JUDY_key_size > 4
0xffffffffffLL, 0xffffffffffffLL, 0xffffffffffffffLL, 0xffffffffffffffffLL
#endif
};
typedef struct {
void *seg; // next used allocator
uint next; // next available offset
} JudySeg;
typedef struct {
JudySlot next; // judy object
uint off; // offset within key
int slot; // slot within object
} JudyStack;
typedef struct {
JudySlot root[1]; // root of judy array
void **reuse[8]; // reuse judy blocks
JudySeg *seg; // current judy allocator
uint level; // current height of stack
uint max; // max height of stack
JudyStack stack[1]; // current cursor
} Judy;
#define JUDY_max JUDY_32
// open judy object
void *judy_open (uint max)
{
JudySeg *seg;
Judy *judy;
uint amt;
if( (seg = valloc(JUDY_seg)) ) {
seg->seg = NULL;
seg->next = JUDY_seg;
} else {
#ifdef STANDALONE
judy_abort ("No virtual memory");
#else
return NULL;
#endif
}
amt = sizeof(Judy) + max * sizeof(JudyStack);
#ifdef STANDALONE
MaxMem += JUDY_seg;
#endif
if( amt & 0x07 )
amt |= 0x07, amt++;
seg->next -= amt;
judy = (Judy *)((uchar *)seg + seg->next);
memset(judy, 0, amt);
judy->seg = seg;
judy->max = max;
return judy;
}
void judy_close (Judy *judy)
{
JudySeg *seg, *nxt = judy->seg;
while( (seg = nxt) )
nxt = seg->seg, vfree (seg, JUDY_seg);
}
// allocate judy node
void *judy_alloc (Judy *judy, int type)
{
uint amt, idx;
JudySeg *seg;
void **block;
if( !judy->seg )
#ifdef STANDALONE
judy_abort("illegal allocation from judy clone");
#else
return NULL;
#endif
if( type == JUDY_radix )
type = JUDY_radix_equiv;
if( type == JUDY_span )
type = JUDY_span_equiv;
amt = JudySize[type];
if( amt & 0x07 )
amt |= 0x07, amt += 1;
// see if free block is already available
if( (block = judy->reuse[type]) ) {
judy->reuse[type] = *block;
memset (block, 0, amt);
return (void *)block;
}
// break down available larger block
// for reuse into smaller blocks
if( type >= JUDY_1 )
for( idx = type; idx++ < JUDY_max; )
if( block = judy->reuse[idx] ) {
judy->reuse[idx] = *block;
while( idx-- > type) {
judy->reuse[idx] = block + JudySize[idx] / sizeof(void *);
block[JudySize[idx] / sizeof(void *)] = 0;
}
memset (block, 0, amt);
return (void *)block;
}
if( judy->seg->next < amt + sizeof(*seg) ) {
if( (seg = valloc (JUDY_seg)) ) {
seg->next = JUDY_seg, seg->seg = judy->seg, judy->seg = seg;
} else {
#ifdef STANDALONE
judy_abort("Out of virtual memory");
#else
return NULL;
#endif
}
#ifdef STANDALONE
MaxMem += JUDY_seg;
#endif
}
judy->seg->next -= amt;
block = (void **)((uchar *)judy->seg + judy->seg->next);
memset (block, 0, amt);
return (void *)block;
}
void *judy_data (Judy *judy, uint amt)
{
JudySeg *seg;
void *block;
if( !judy->seg )
#ifdef STANDALONE
judy_abort("illegal allocation from judy clone");
#else
return NULL;
#endif
if( amt & 0x07 )
amt |= 0x07, amt += 1;
if( judy->seg->next < amt + sizeof(*seg) ) {
if( (seg = valloc (JUDY_seg)) ) {
seg->next = JUDY_seg, seg->seg = judy->seg, judy->seg = seg;
} else {
#ifdef STANDALONE
judy_abort("Out of virtual memory");
#else
return NULL;
#endif
}
#ifdef STANDALONE
MaxMem += JUDY_seg;
#endif
}
judy->seg->next -= amt;
block = (void *)((uchar *)judy->seg + judy->seg->next);
memset (block, 0, amt);
return block;
}
void *judy_clone (Judy *judy)
{
Judy *clone;
uint amt;
amt = sizeof(Judy) + judy->max * sizeof(JudyStack);
clone = judy_data (judy, amt);
memcpy (clone, judy, amt);
clone->seg = NULL; // stop allocations from cloned array
return clone;
}
void judy_free (Judy *judy, void *block, int type)
{
if( type == JUDY_radix )
type = JUDY_radix_equiv;
if( type == JUDY_span )
type = JUDY_span_equiv;
*((void **)(block)) = judy->reuse[type];
judy->reuse[type] = (void **)block;
return;
}
// assemble key from current path
uint judy_key (Judy *judy, uchar *buff, uint max)
{
uint len = 0, idx = 0;
int slot, off, type;
uchar *base;
int keysize;
max--; // leave room for zero terminator
while( len < max && ++idx <= judy->level ) {
slot = judy->stack[idx].slot;
type = judy->stack[idx].next & 0x07;
switch( type ) {
case JUDY_1:
case JUDY_2:
case JUDY_4:
case JUDY_8:
case JUDY_16:
case JUDY_32:
keysize = JUDY_key_size - (judy->stack[idx].off & JUDY_key_mask);
base = (uchar *)(judy->stack[idx].next & JUDY_mask);
off = keysize;
#if BYTE_ORDER != BIG_ENDIAN
while( off-- && len < max )
if( buff[len] = base[slot * keysize + off] )
len++;
else
break;
#else
for( off = 0; off < keysize && len < max; off++ )
if( buff[len] = base[slot * keysize + off] )
len++;
else
break;
#endif
continue;
case JUDY_radix:
if( !slot )
break;
buff[len++] = slot;
continue;
case JUDY_span:
base = (uchar *)(judy->stack[idx].next & JUDY_mask);
for( slot = 0; slot < JUDY_span_bytes && base[slot]; slot++ )
if( len < max )
buff[len++] = base[slot];
continue;
}
}
buff[len] = 0;
return len;
}
// find slot & setup cursor
JudySlot *judy_slot (Judy *judy, uchar *buff, uint max)
{
int slot, size, keysize, tst, cnt;
JudySlot next = *judy->root;
judyvalue value, test = 0;
JudySlot *table;
JudySlot *node;
uint off = 0;
uchar *base;
judy->level = 0;
while( next ) {
if( judy->level < judy->max )
judy->level++;
judy->stack[judy->level].off = off;
judy->stack[judy->level].next = next;
size = JudySize[next & 0x07];
switch( next & 0x07 ) {
case JUDY_1:
case JUDY_2:
case JUDY_4:
case JUDY_8:
case JUDY_16:
case JUDY_32:
base = (uchar *)(next & JUDY_mask);
node = (JudySlot *)((next & JUDY_mask) + size);
keysize = JUDY_key_size - (off & JUDY_key_mask);
cnt = size / (sizeof(JudySlot) + keysize);
slot = cnt;
value = 0;
do {
value <<= 8;
if( off < max )
value |= buff[off];
} while( ++off & JUDY_key_mask );
// find slot > key
while( slot-- ) {
test = *(judyvalue *)(base + slot * keysize);
#if BYTE_ORDER == BIG_ENDIAN
test >>= 8 * (JUDY_key_size - keysize);
#else
test &= JudyMask[keysize];
#endif
if( test <= value )
break;
}
judy->stack[judy->level].slot = slot;
if( test == value ) {
// is this a leaf?
if( !(value & 0xFF) )
return &node[-slot-1];
next = node[-slot-1];
continue;
}
return NULL;
case JUDY_radix:
table = (JudySlot *)(next & JUDY_mask); // outer radix
if( off < max )
slot = buff[off];
else
slot = 0;
// put radix slot on judy stack
judy->stack[judy->level].slot = slot;
if( (next = table[slot >> 4]) )
table = (JudySlot *)(next & JUDY_mask); // inner radix
else
return NULL;
if( !slot ) // leaf?
return &table[slot & 0x0F];
next = table[slot & 0x0F];
off += 1;
break;
case JUDY_span:
node = (JudySlot *)((next & JUDY_mask) + JudySize[JUDY_span]);
base = (uchar *)(next & JUDY_mask);
cnt = tst = JUDY_span_bytes;
if( tst > (int)(max - off) )
tst = max - off;
value = strncmp((const char *)base, (const char *)(buff + off), tst);
if( !value && tst < cnt && !base[tst] ) // leaf?
return &node[-1];
if( !value && tst == cnt ) {
next = node[-1];
off += cnt;
continue;
}
return NULL;
}
}
return NULL;
}
// promote full nodes to next larger size
JudySlot *judy_promote (Judy *judy, JudySlot *next, int idx, judyvalue value, int keysize)
{
uchar *base = (uchar *)(*next & JUDY_mask);
int oldcnt, newcnt, slot;
#if BYTE_ORDER == BIG_ENDIAN
int i;
#endif
JudySlot *newnode, *node;
JudySlot *result;
uchar *newbase;
uint type;
type = (*next & 0x07) + 1;
node = (JudySlot *)((*next & JUDY_mask) + JudySize[type-1]);
oldcnt = JudySize[type-1] / (sizeof(JudySlot) + keysize);
newcnt = JudySize[type] / (sizeof(JudySlot) + keysize);
// promote node to next larger size
newbase = judy_alloc (judy, type);
newnode = (JudySlot *)(newbase + JudySize[type]);
*next = (JudySlot)newbase | type;
// open up slot at idx
memcpy(newbase + (newcnt - oldcnt - 1) * keysize, base, idx * keysize); // copy keys
for( slot = 0; slot < idx; slot++ )
newnode[-(slot + newcnt - oldcnt)] = node[-(slot + 1)]; // copy ptr
// fill in new node
#if BYTE_ORDER != BIG_ENDIAN
memcpy(newbase + (idx + newcnt - oldcnt - 1) * keysize, &value, keysize); // copy key
#else
i = keysize;
while( i-- )
newbase[(idx + newcnt - oldcnt - 1) * keysize + i] = value, value >>= 8;
#endif
result = &newnode[-(idx + newcnt - oldcnt)];
// copy rest of old node
memcpy(newbase + (idx + newcnt - oldcnt) * keysize, base + (idx * keysize), (oldcnt - slot) * keysize); // copy keys
for( ; slot < oldcnt; slot++ )
newnode[-(slot + newcnt - oldcnt + 1)] = node[-(slot + 1)]; // copy ptr
judy->stack[judy->level].next = *next;
judy->stack[judy->level].slot = idx + newcnt - oldcnt - 1;
judy_free (judy, (void **)base, type - 1);
return result;
}
// construct new node for JUDY_radix entry
// make node with slot - start entries
// moving key over one offset
void judy_radix (Judy *judy, JudySlot *radix, uchar *old, int start, int slot, int keysize, uchar key)
{
int size, idx, cnt = slot - start, newcnt;
JudySlot *node, *oldnode;
uint type = JUDY_1 - 1;
JudySlot *table;
uchar *base;
// if necessary, setup inner radix node
if( !(table = (JudySlot *)(radix[key >> 4] & JUDY_mask)) ) {
table = judy_alloc (judy, JUDY_radix);
radix[key >> 4] = (JudySlot)table | JUDY_radix;
}
oldnode = (JudySlot *)(old + JudySize[JUDY_max]);
// is this slot a leaf?
if( !key || !keysize ) {
table[key & 0x0F] = oldnode[-start-1];
return;
}
// calculate new node big enough to contain slots
do {
type++;
size = JudySize[type];
newcnt = size / (sizeof(JudySlot) + keysize);
} while( cnt > newcnt && type < JUDY_max );
// store new node pointer in inner table
base = judy_alloc (judy, type);
node = (JudySlot *)(base + size);
table[key & 0x0F] = (JudySlot)base | type;
// allocate node and copy old contents
// shorten keys by 1 byte during copy
for( idx = 0; idx < cnt; idx++ ) {
#if BYTE_ORDER != BIG_ENDIAN
memcpy (base + (newcnt - idx - 1) * keysize, old + (start + cnt - idx - 1) * (keysize + 1), keysize);
#else
memcpy (base + (newcnt - idx - 1) * keysize, old + (start + cnt - idx - 1) * (keysize + 1) + 1, keysize);
#endif
node[-(newcnt - idx)] = oldnode[-(start + cnt - idx)];
}
}
// decompose full node to radix nodes
void judy_splitnode (Judy *judy, JudySlot *next, uint size, uint keysize)
{
int cnt, slot, start = 0;
uint key = 0x0100, nxt;
JudySlot *newradix;
uchar *base;
base = (uchar *)(*next & JUDY_mask);
cnt = size / (sizeof(JudySlot) + keysize);
// allocate outer judy_radix node
newradix = judy_alloc (judy, JUDY_radix);
*next = (JudySlot)newradix | JUDY_radix;
for( slot = 0; slot < cnt; slot++ ) {
#if BYTE_ORDER != BIG_ENDIAN
nxt = base[slot * keysize + keysize - 1];
#else
nxt = base[slot * keysize];
#endif
if( key > 0xFF )
key = nxt;
if( nxt == key )
continue;
// decompose portion of old node into radix nodes
judy_radix (judy, newradix, base, start, slot, keysize - 1, key);
start = slot;
key = nxt;
}
judy_radix (judy, newradix, base, start, slot, keysize - 1, key);
judy_free (judy, (void **)base, JUDY_max);
}
// return first leaf
JudySlot *judy_first (Judy *judy, JudySlot next, uint off)
{
JudySlot *table, *inner;
uint keysize, size;
JudySlot *node;
int slot, cnt;
uchar *base;
while( next ) {
if( judy->level < judy->max )
judy->level++;
judy->stack[judy->level].off = off;
judy->stack[judy->level].next = next;
size = JudySize[next & 0x07];
switch( next & 0x07 ) {
case JUDY_1:
case JUDY_2:
case JUDY_4:
case JUDY_8:
case JUDY_16:
case JUDY_32:
keysize = JUDY_key_size - (off & JUDY_key_mask);
node = (JudySlot *)((next & JUDY_mask) + size);
base = (uchar *)(next & JUDY_mask);
cnt = size / (sizeof(JudySlot) + keysize);
for( slot = 0; slot < cnt; slot++ )
if( node[-slot-1] )
break;
judy->stack[judy->level].slot = slot;
#if BYTE_ORDER != BIG_ENDIAN
if( !base[slot * keysize] )
return &node[-slot-1];
#else
if( !base[slot * keysize + keysize - 1] )
return &node[-slot-1];
#endif
next = node[-slot - 1];
off = (off | JUDY_key_mask) + 1;
continue;
case JUDY_radix:
table = (JudySlot *)(next & JUDY_mask);
for( slot = 0; slot < 256; slot++ )
if( (inner = (JudySlot *)(table[slot >> 4] & JUDY_mask)) ) {
if( (next = inner[slot & 0x0F]) ) {
judy->stack[judy->level].slot = slot;
if( !slot )
return &inner[slot & 0x0F];
else
break;
}
} else
slot |= 0x0F;
off++;
continue;
case JUDY_span:
node = (JudySlot *)((next & JUDY_mask) + JudySize[JUDY_span]);
base = (uchar *)(next & JUDY_mask);
cnt = JUDY_span_bytes;
if( !base[cnt - 1] ) // leaf node?
return &node[-1];
next = node[-1];
off += cnt;
continue;
}
}
return NULL;
}
// return last leaf cell pointer
JudySlot *judy_last (Judy *judy, JudySlot next, uint off)
{
JudySlot *table, *inner;
uint keysize, size;
JudySlot *node;
int slot, cnt;
uchar *base;
while( next ) {
if( judy->level < judy->max )
judy->level++;
judy->stack[judy->level].off = off;
judy->stack[judy->level].next = next;
size = JudySize[next & 0x07];
switch( next & 0x07 ) {
case JUDY_1:
case JUDY_2:
case JUDY_4:
case JUDY_8:
case JUDY_16:
case JUDY_32:
keysize = JUDY_key_size - (off & JUDY_key_mask);
slot = size / (sizeof(JudySlot) + keysize);
base = (uchar *)(next & JUDY_mask);
node = (JudySlot *)((next & JUDY_mask) + size);
judy->stack[judy->level].slot = --slot;
#if BYTE_ORDER != BIG_ENDIAN
if( !base[slot * keysize] )
#else
if( !base[slot * keysize + keysize - 1] )
#endif
return &node[-slot-1];
next = node[-slot-1];
off += keysize;
continue;
case JUDY_radix:
table = (JudySlot *)(next & JUDY_mask);
for( slot = 256; slot--; ) {
judy->stack[judy->level].slot = slot;
if( (inner = (JudySlot *)(table[slot >> 4] & JUDY_mask)) ) {
if( (next = inner[slot & 0x0F]) )
if( !slot )
return &inner[0];
else
break;
} else
slot &= 0xF0;
}
off++;
continue;
case JUDY_span:
node = (JudySlot *)((next & JUDY_mask) + JudySize[JUDY_span]);
base = (uchar *)(next & JUDY_mask);
cnt = JUDY_span_bytes;
if( !base[cnt - 1] ) // leaf node?
return &node[-1];
next = node[-1];
off += cnt;
continue;
}
}
return NULL;
}
// judy_end: return last entry
JudySlot *judy_end (Judy *judy)
{
judy->level = 0;
return judy_last (judy, *judy->root, 0);
}
// judy_nxt: return next entry
JudySlot *judy_nxt (Judy *judy)
{
JudySlot *table, *inner;
int slot, size, cnt;
JudySlot *node;
JudySlot next;
uint keysize;
uchar *base;
uint off;
if( !judy->level )
return judy_first (judy, *judy->root, 0);
while( judy->level ) {
next = judy->stack[judy->level].next;
slot = judy->stack[judy->level].slot;
off = judy->stack[judy->level].off;
keysize = JUDY_key_size - (off & JUDY_key_mask);
size = JudySize[next & 0x07];
switch( next & 0x07 ) {
case JUDY_1:
case JUDY_2:
case JUDY_4:
case JUDY_8:
case JUDY_16:
case JUDY_32:
cnt = size / (sizeof(JudySlot) + keysize);
node = (JudySlot *)((next & JUDY_mask) + size);
base = (uchar *)(next & JUDY_mask);
if( ++slot < cnt )
#if BYTE_ORDER != BIG_ENDIAN
if( !base[slot * keysize] )
#else
if( !base[slot * keysize + keysize - 1] )
#endif
{
judy->stack[judy->level].slot = slot;
return &node[-slot - 1];
} else {
judy->stack[judy->level].slot = slot;
return judy_first (judy, node[-slot-1], (off | JUDY_key_mask) + 1);
}
judy->level--;
continue;
case JUDY_radix:
table = (JudySlot *)(next & JUDY_mask);
while( ++slot < 256 )
if( (inner = (JudySlot *)(table[slot >> 4] & JUDY_mask)) ) {
if( inner[slot & 0x0F] ) {
judy->stack[judy->level].slot = slot;
return judy_first(judy, inner[slot & 0x0F], off + 1);
}
} else
slot |= 0x0F;
judy->level--;
continue;
case JUDY_span:
judy->level--;
continue;
}
}
return NULL;
}
// judy_prv: return ptr to previous entry
JudySlot *judy_prv (Judy *judy)
{
int slot, size, keysize;
JudySlot *table, *inner;
JudySlot *node;
JudySlot next;
uchar *base;
uint off;
if( !judy->level )
return judy_last (judy, *judy->root, 0);
while( judy->level ) {
next = judy->stack[judy->level].next;
slot = judy->stack[judy->level].slot;
off = judy->stack[judy->level].off;
size = JudySize[next & 0x07];
switch( next & 0x07 ) {
case JUDY_1:
case JUDY_2:
case JUDY_4:
case JUDY_8:
case JUDY_16:
case JUDY_32:
node = (JudySlot *)((next & JUDY_mask) + size);
if( !slot || !node[-slot] ) {
judy->level--;
continue;
}
base = (uchar *)(next & JUDY_mask);
judy->stack[judy->level].slot--;
keysize = JUDY_key_size - (off & JUDY_key_mask);
#if BYTE_ORDER != BIG_ENDIAN
if( base[(slot - 1) * keysize] )
#else
if( base[(slot - 1) * keysize + keysize - 1] )
#endif
return judy_last (judy, node[-slot], (off | JUDY_key_mask) + 1);
return &node[-slot];
case JUDY_radix:
table = (JudySlot *)(next & JUDY_mask);
while( slot-- ) {
judy->stack[judy->level].slot--;
if( (inner = (JudySlot *)(table[slot >> 4] & JUDY_mask)) )
if( inner[slot & 0x0F] )
if( slot )
return judy_last(judy, inner[slot & 0x0F], off + 1);
else
return &inner[0];
}
judy->level--;
continue;
case JUDY_span:
judy->level--;
continue;
}
}
return NULL;
}
// judy_del: delete string from judy array
// returning previous entry.
JudySlot *judy_del (Judy *judy)
{
int slot, off, size, type, high;
JudySlot *table, *inner;
JudySlot next, *node;
int keysize, cnt;
uchar *base;
while( judy->level ) {
next = judy->stack[judy->level].next;
slot = judy->stack[judy->level].slot;
off = judy->stack[judy->level].off;
size = JudySize[next & 0x07];
switch( type = next & 0x07 ) {
case JUDY_1:
case JUDY_2:
case JUDY_4:
case JUDY_8:
case JUDY_16:
case JUDY_32:
keysize = JUDY_key_size - (off & JUDY_key_mask);