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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
|
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <roaring/art/art.h>
#include <roaring/memory.h>
#include <roaring/portability.h>
#define CROARING_ART_NODE4_TYPE 0
#define CROARING_ART_NODE16_TYPE 1
#define CROARING_ART_NODE48_TYPE 2
#define CROARING_ART_NODE256_TYPE 3
#define CROARING_ART_NUM_TYPES 4
// Node48 placeholder value to indicate no child is present at this key index.
#define CROARING_ART_NODE48_EMPTY_VAL 48
// We use the least significant bit of node pointers to indicate whether a node
// is a leaf or an inner node. This is never surfaced to the user.
//
// Using pointer tagging to indicate leaves not only saves a bit of memory by
// sparing the typecode, but also allows us to use an intrusive leaf struct.
// Using an intrusive leaf struct leaves leaf allocation up to the user. Upon
// deallocation of the ART, we know not to free the leaves without having to
// dereference the leaf pointers.
//
// All internal operations on leaves should use CROARING_CAST_LEAF before using
// the leaf. The only places that use CROARING_SET_LEAF are locations where a
// field is directly assigned to a leaf pointer. After using CROARING_SET_LEAF,
// the leaf should be treated as a node of unknown type.
#define CROARING_IS_LEAF(p) (((uintptr_t)(p) & 1))
#define CROARING_SET_LEAF(p) ((art_node_t *)((uintptr_t)(p) | 1))
#define CROARING_CAST_LEAF(p) ((art_leaf_t *)((void *)((uintptr_t)(p) & ~1)))
#define CROARING_NODE48_AVAILABLE_CHILDREN_MASK ((UINT64_C(1) << 48) - 1)
#ifdef __cplusplus
extern "C" {
namespace roaring {
namespace internal {
#endif
typedef uint8_t art_typecode_t;
// Aliasing with a "leaf" naming so that its purpose is clearer in the context
// of the trie internals.
typedef art_val_t art_leaf_t;
typedef struct art_internal_validate_s {
const char **reason;
art_validate_cb_t validate_cb;
int depth;
art_key_chunk_t current_key[ART_KEY_BYTES];
} art_internal_validate_t;
// Set the reason message, and return false for convenience.
static inline bool art_validate_fail(const art_internal_validate_t *validate,
const char *msg) {
*validate->reason = msg;
return false;
}
// Inner node, with prefix.
//
// We use a fixed-length array as a pointer would be larger than the array.
typedef struct art_inner_node_s {
art_typecode_t typecode;
uint8_t prefix_size;
uint8_t prefix[ART_KEY_BYTES - 1];
} art_inner_node_t;
// Inner node types.
// Node4: key[i] corresponds with children[i]. Keys are sorted.
typedef struct art_node4_s {
art_inner_node_t base;
uint8_t count;
uint8_t keys[4];
art_node_t *children[4];
} art_node4_t;
// Node16: key[i] corresponds with children[i]. Keys are sorted.
typedef struct art_node16_s {
art_inner_node_t base;
uint8_t count;
uint8_t keys[16];
art_node_t *children[16];
} art_node16_t;
// Node48: key[i] corresponds with children[key[i]] if key[i] !=
// CROARING_ART_NODE48_EMPTY_VAL. Keys are naturally sorted due to direct
// indexing.
typedef struct art_node48_s {
art_inner_node_t base;
uint8_t count;
// Bitset where the ith bit is set if children[i] is available
// Because there are at most 48 children, only the bottom 48 bits are used.
uint64_t available_children;
uint8_t keys[256];
art_node_t *children[48];
} art_node48_t;
// Node256: children[i] is directly indexed by key chunk. A child is present if
// children[i] != NULL.
typedef struct art_node256_s {
art_inner_node_t base;
uint16_t count;
art_node_t *children[256];
} art_node256_t;
// Helper struct to refer to a child within a node at a specific index.
typedef struct art_indexed_child_s {
art_node_t *child;
uint8_t index;
art_key_chunk_t key_chunk;
} art_indexed_child_t;
static inline bool art_is_leaf(const art_node_t *node) {
return CROARING_IS_LEAF(node);
}
static void art_leaf_populate(art_leaf_t *leaf, const art_key_chunk_t key[]) {
memcpy(leaf->key, key, ART_KEY_BYTES);
}
static inline uint8_t art_get_type(const art_inner_node_t *node) {
return node->typecode;
}
static inline void art_init_inner_node(art_inner_node_t *node,
art_typecode_t typecode,
const art_key_chunk_t prefix[],
uint8_t prefix_size) {
node->typecode = typecode;
node->prefix_size = prefix_size;
memcpy(node->prefix, prefix, prefix_size * sizeof(art_key_chunk_t));
}
static void art_free_node(art_node_t *node);
// ===================== Start of node-specific functions ======================
static art_node4_t *art_node4_create(const art_key_chunk_t prefix[],
uint8_t prefix_size);
static art_node16_t *art_node16_create(const art_key_chunk_t prefix[],
uint8_t prefix_size);
static art_node48_t *art_node48_create(const art_key_chunk_t prefix[],
uint8_t prefix_size);
static art_node256_t *art_node256_create(const art_key_chunk_t prefix[],
uint8_t prefix_size);
static art_node_t *art_node4_insert(art_node4_t *node, art_node_t *child,
uint8_t key);
static art_node_t *art_node16_insert(art_node16_t *node, art_node_t *child,
uint8_t key);
static art_node_t *art_node48_insert(art_node48_t *node, art_node_t *child,
uint8_t key);
static art_node_t *art_node256_insert(art_node256_t *node, art_node_t *child,
uint8_t key);
static art_node4_t *art_node4_create(const art_key_chunk_t prefix[],
uint8_t prefix_size) {
art_node4_t *node = (art_node4_t *)roaring_malloc(sizeof(art_node4_t));
art_init_inner_node(&node->base, CROARING_ART_NODE4_TYPE, prefix,
prefix_size);
node->count = 0;
return node;
}
static void art_free_node4(art_node4_t *node) {
for (size_t i = 0; i < node->count; ++i) {
art_free_node(node->children[i]);
}
roaring_free(node);
}
static inline art_node_t *art_node4_find_child(const art_node4_t *node,
art_key_chunk_t key) {
for (size_t i = 0; i < node->count; ++i) {
if (node->keys[i] == key) {
return node->children[i];
}
}
return NULL;
}
static art_node_t *art_node4_insert(art_node4_t *node, art_node_t *child,
uint8_t key) {
if (node->count < 4) {
size_t idx = 0;
for (; idx < node->count; ++idx) {
if (node->keys[idx] > key) {
break;
}
}
size_t after = node->count - idx;
// Shift other keys to maintain sorted order.
memmove(node->keys + idx + 1, node->keys + idx,
after * sizeof(art_key_chunk_t));
memmove(node->children + idx + 1, node->children + idx,
after * sizeof(art_node_t *));
node->children[idx] = child;
node->keys[idx] = key;
node->count++;
return (art_node_t *)node;
}
art_node16_t *new_node =
art_node16_create(node->base.prefix, node->base.prefix_size);
// Instead of calling insert, this could be specialized to 2x memcpy and
// setting the count.
for (size_t i = 0; i < 4; ++i) {
art_node16_insert(new_node, node->children[i], node->keys[i]);
}
roaring_free(node);
return art_node16_insert(new_node, child, key);
}
static inline art_node_t *art_node4_erase(art_node4_t *node,
art_key_chunk_t key_chunk) {
int idx = -1;
for (size_t i = 0; i < node->count; ++i) {
if (node->keys[i] == key_chunk) {
idx = i;
}
}
if (idx == -1) {
return (art_node_t *)node;
}
if (node->count == 2) {
// Only one child remains after erasing, so compress the path by
// removing this node.
uint8_t other_idx = idx ^ 1;
art_node_t *remaining_child = node->children[other_idx];
art_key_chunk_t remaining_child_key = node->keys[other_idx];
if (!art_is_leaf(remaining_child)) {
// Correct the prefix of the child node.
art_inner_node_t *inner_node = (art_inner_node_t *)remaining_child;
memmove(inner_node->prefix + node->base.prefix_size + 1,
inner_node->prefix, inner_node->prefix_size);
memcpy(inner_node->prefix, node->base.prefix,
node->base.prefix_size);
inner_node->prefix[node->base.prefix_size] = remaining_child_key;
inner_node->prefix_size += node->base.prefix_size + 1;
}
roaring_free(node);
return remaining_child;
}
// Shift other keys to maintain sorted order.
size_t after_next = node->count - idx - 1;
memmove(node->keys + idx, node->keys + idx + 1,
after_next * sizeof(art_key_chunk_t));
memmove(node->children + idx, node->children + idx + 1,
after_next * sizeof(art_node_t *));
node->count--;
return (art_node_t *)node;
}
static inline void art_node4_replace(art_node4_t *node,
art_key_chunk_t key_chunk,
art_node_t *new_child) {
for (size_t i = 0; i < node->count; ++i) {
if (node->keys[i] == key_chunk) {
node->children[i] = new_child;
return;
}
}
}
static inline art_indexed_child_t art_node4_next_child(const art_node4_t *node,
int index) {
art_indexed_child_t indexed_child;
index++;
if (index >= node->count) {
indexed_child.child = NULL;
return indexed_child;
}
indexed_child.index = index;
indexed_child.child = node->children[index];
indexed_child.key_chunk = node->keys[index];
return indexed_child;
}
static inline art_indexed_child_t art_node4_prev_child(const art_node4_t *node,
int index) {
if (index > node->count) {
index = node->count;
}
index--;
art_indexed_child_t indexed_child;
if (index < 0) {
indexed_child.child = NULL;
return indexed_child;
}
indexed_child.index = index;
indexed_child.child = node->children[index];
indexed_child.key_chunk = node->keys[index];
return indexed_child;
}
static inline art_indexed_child_t art_node4_child_at(const art_node4_t *node,
int index) {
art_indexed_child_t indexed_child;
if (index < 0 || index >= node->count) {
indexed_child.child = NULL;
return indexed_child;
}
indexed_child.index = index;
indexed_child.child = node->children[index];
indexed_child.key_chunk = node->keys[index];
return indexed_child;
}
static inline art_indexed_child_t art_node4_lower_bound(
art_node4_t *node, art_key_chunk_t key_chunk) {
art_indexed_child_t indexed_child;
for (size_t i = 0; i < node->count; ++i) {
if (node->keys[i] >= key_chunk) {
indexed_child.index = i;
indexed_child.child = node->children[i];
indexed_child.key_chunk = node->keys[i];
return indexed_child;
}
}
indexed_child.child = NULL;
return indexed_child;
}
static bool art_internal_validate_at(const art_node_t *node,
art_internal_validate_t validator);
static bool art_node4_internal_validate(const art_node4_t *node,
art_internal_validate_t validator) {
if (node->count == 0) {
return art_validate_fail(&validator, "Node4 has no children");
}
if (node->count > 4) {
return art_validate_fail(&validator, "Node4 has too many children");
}
if (node->count == 1) {
return art_validate_fail(
&validator, "Node4 and child node should have been combined");
}
validator.depth++;
for (int i = 0; i < node->count; ++i) {
if (i > 0) {
if (node->keys[i - 1] >= node->keys[i]) {
return art_validate_fail(
&validator, "Node4 keys are not strictly increasing");
}
}
for (int j = i + 1; j < node->count; ++j) {
if (node->children[i] == node->children[j]) {
return art_validate_fail(&validator,
"Node4 has duplicate children");
}
}
validator.current_key[validator.depth - 1] = node->keys[i];
if (!art_internal_validate_at(node->children[i], validator)) {
return false;
}
}
return true;
}
static art_node16_t *art_node16_create(const art_key_chunk_t prefix[],
uint8_t prefix_size) {
art_node16_t *node = (art_node16_t *)roaring_malloc(sizeof(art_node16_t));
art_init_inner_node(&node->base, CROARING_ART_NODE16_TYPE, prefix,
prefix_size);
node->count = 0;
return node;
}
static void art_free_node16(art_node16_t *node) {
for (size_t i = 0; i < node->count; ++i) {
art_free_node(node->children[i]);
}
roaring_free(node);
}
static inline art_node_t *art_node16_find_child(const art_node16_t *node,
art_key_chunk_t key) {
for (size_t i = 0; i < node->count; ++i) {
if (node->keys[i] == key) {
return node->children[i];
}
}
return NULL;
}
static art_node_t *art_node16_insert(art_node16_t *node, art_node_t *child,
uint8_t key) {
if (node->count < 16) {
size_t idx = 0;
for (; idx < node->count; ++idx) {
if (node->keys[idx] > key) {
break;
}
}
size_t after = node->count - idx;
// Shift other keys to maintain sorted order.
memmove(node->keys + idx + 1, node->keys + idx,
after * sizeof(art_key_chunk_t));
memmove(node->children + idx + 1, node->children + idx,
after * sizeof(art_node_t *));
node->children[idx] = child;
node->keys[idx] = key;
node->count++;
return (art_node_t *)node;
}
art_node48_t *new_node =
art_node48_create(node->base.prefix, node->base.prefix_size);
for (size_t i = 0; i < 16; ++i) {
art_node48_insert(new_node, node->children[i], node->keys[i]);
}
roaring_free(node);
return art_node48_insert(new_node, child, key);
}
static inline art_node_t *art_node16_erase(art_node16_t *node,
uint8_t key_chunk) {
for (size_t i = 0; i < node->count; ++i) {
if (node->keys[i] == key_chunk) {
// Shift other keys to maintain sorted order.
size_t after_next = node->count - i - 1;
memmove(node->keys + i, node->keys + i + 1,
after_next * sizeof(key_chunk));
memmove(node->children + i, node->children + i + 1,
after_next * sizeof(art_node_t *));
node->count--;
break;
}
}
if (node->count > 4) {
return (art_node_t *)node;
}
art_node4_t *new_node =
art_node4_create(node->base.prefix, node->base.prefix_size);
// Instead of calling insert, this could be specialized to 2x memcpy and
// setting the count.
for (size_t i = 0; i < 4; ++i) {
art_node4_insert(new_node, node->children[i], node->keys[i]);
}
roaring_free(node);
return (art_node_t *)new_node;
}
static inline void art_node16_replace(art_node16_t *node,
art_key_chunk_t key_chunk,
art_node_t *new_child) {
for (uint8_t i = 0; i < node->count; ++i) {
if (node->keys[i] == key_chunk) {
node->children[i] = new_child;
return;
}
}
}
static inline art_indexed_child_t art_node16_next_child(
const art_node16_t *node, int index) {
art_indexed_child_t indexed_child;
index++;
if (index >= node->count) {
indexed_child.child = NULL;
return indexed_child;
}
indexed_child.index = index;
indexed_child.child = node->children[index];
indexed_child.key_chunk = node->keys[index];
return indexed_child;
}
static inline art_indexed_child_t art_node16_prev_child(
const art_node16_t *node, int index) {
if (index > node->count) {
index = node->count;
}
index--;
art_indexed_child_t indexed_child;
if (index < 0) {
indexed_child.child = NULL;
return indexed_child;
}
indexed_child.index = index;
indexed_child.child = node->children[index];
indexed_child.key_chunk = node->keys[index];
return indexed_child;
}
static inline art_indexed_child_t art_node16_child_at(const art_node16_t *node,
int index) {
art_indexed_child_t indexed_child;
if (index < 0 || index >= node->count) {
indexed_child.child = NULL;
return indexed_child;
}
indexed_child.index = index;
indexed_child.child = node->children[index];
indexed_child.key_chunk = node->keys[index];
return indexed_child;
}
static inline art_indexed_child_t art_node16_lower_bound(
art_node16_t *node, art_key_chunk_t key_chunk) {
art_indexed_child_t indexed_child;
for (size_t i = 0; i < node->count; ++i) {
if (node->keys[i] >= key_chunk) {
indexed_child.index = i;
indexed_child.child = node->children[i];
indexed_child.key_chunk = node->keys[i];
return indexed_child;
}
}
indexed_child.child = NULL;
return indexed_child;
}
static bool art_node16_internal_validate(const art_node16_t *node,
art_internal_validate_t validator) {
if (node->count <= 4) {
return art_validate_fail(&validator, "Node16 has too few children");
}
if (node->count > 16) {
return art_validate_fail(&validator, "Node16 has too many children");
}
validator.depth++;
for (int i = 0; i < node->count; ++i) {
if (i > 0) {
if (node->keys[i - 1] >= node->keys[i]) {
return art_validate_fail(
&validator, "Node16 keys are not strictly increasing");
}
}
for (int j = i + 1; j < node->count; ++j) {
if (node->children[i] == node->children[j]) {
return art_validate_fail(&validator,
"Node16 has duplicate children");
}
}
validator.current_key[validator.depth - 1] = node->keys[i];
if (!art_internal_validate_at(node->children[i], validator)) {
return false;
}
}
return true;
}
static art_node48_t *art_node48_create(const art_key_chunk_t prefix[],
uint8_t prefix_size) {
art_node48_t *node = (art_node48_t *)roaring_malloc(sizeof(art_node48_t));
art_init_inner_node(&node->base, CROARING_ART_NODE48_TYPE, prefix,
prefix_size);
node->count = 0;
node->available_children = CROARING_NODE48_AVAILABLE_CHILDREN_MASK;
for (size_t i = 0; i < 256; ++i) {
node->keys[i] = CROARING_ART_NODE48_EMPTY_VAL;
}
return node;
}
static void art_free_node48(art_node48_t *node) {
uint64_t used_children =
(node->available_children) ^ CROARING_NODE48_AVAILABLE_CHILDREN_MASK;
while (used_children != 0) {
// We checked above that used_children is not zero
uint8_t child_idx = roaring_trailing_zeroes(used_children);
art_free_node(node->children[child_idx]);
used_children &= ~(UINT64_C(1) << child_idx);
}
roaring_free(node);
}
static inline art_node_t *art_node48_find_child(const art_node48_t *node,
art_key_chunk_t key) {
uint8_t val_idx = node->keys[key];
if (val_idx != CROARING_ART_NODE48_EMPTY_VAL) {
return node->children[val_idx];
}
return NULL;
}
static art_node_t *art_node48_insert(art_node48_t *node, art_node_t *child,
uint8_t key) {
if (node->count < 48) {
// node->available_children is only zero when the node is full (count ==
// 48), we just checked count < 48
uint8_t val_idx = roaring_trailing_zeroes(node->available_children);
node->keys[key] = val_idx;
node->children[val_idx] = child;
node->count++;
node->available_children &= ~(UINT64_C(1) << val_idx);
return (art_node_t *)node;
}
art_node256_t *new_node =
art_node256_create(node->base.prefix, node->base.prefix_size);
for (size_t i = 0; i < 256; ++i) {
uint8_t val_idx = node->keys[i];
if (val_idx != CROARING_ART_NODE48_EMPTY_VAL) {
art_node256_insert(new_node, node->children[val_idx], i);
}
}
roaring_free(node);
return art_node256_insert(new_node, child, key);
}
static inline art_node_t *art_node48_erase(art_node48_t *node,
uint8_t key_chunk) {
uint8_t val_idx = node->keys[key_chunk];
if (val_idx == CROARING_ART_NODE48_EMPTY_VAL) {
return (art_node_t *)node;
}
node->keys[key_chunk] = CROARING_ART_NODE48_EMPTY_VAL;
node->available_children |= UINT64_C(1) << val_idx;
node->count--;
if (node->count > 16) {
return (art_node_t *)node;
}
art_node16_t *new_node =
art_node16_create(node->base.prefix, node->base.prefix_size);
for (size_t i = 0; i < 256; ++i) {
val_idx = node->keys[i];
if (val_idx != CROARING_ART_NODE48_EMPTY_VAL) {
art_node16_insert(new_node, node->children[val_idx], i);
}
}
roaring_free(node);
return (art_node_t *)new_node;
}
static inline void art_node48_replace(art_node48_t *node,
art_key_chunk_t key_chunk,
art_node_t *new_child) {
uint8_t val_idx = node->keys[key_chunk];
assert(val_idx != CROARING_ART_NODE48_EMPTY_VAL);
node->children[val_idx] = new_child;
}
static inline art_indexed_child_t art_node48_next_child(
const art_node48_t *node, int index) {
art_indexed_child_t indexed_child;
index++;
for (size_t i = index; i < 256; ++i) {
if (node->keys[i] != CROARING_ART_NODE48_EMPTY_VAL) {
indexed_child.index = i;
indexed_child.child = node->children[node->keys[i]];
indexed_child.key_chunk = i;
return indexed_child;
}
}
indexed_child.child = NULL;
return indexed_child;
}
static inline art_indexed_child_t art_node48_prev_child(
const art_node48_t *node, int index) {
if (index > 256) {
index = 256;
}
index--;
art_indexed_child_t indexed_child;
for (int i = index; i >= 0; --i) {
if (node->keys[i] != CROARING_ART_NODE48_EMPTY_VAL) {
indexed_child.index = i;
indexed_child.child = node->children[node->keys[i]];
indexed_child.key_chunk = i;
return indexed_child;
}
}
indexed_child.child = NULL;
return indexed_child;
}
static inline art_indexed_child_t art_node48_child_at(const art_node48_t *node,
int index) {
art_indexed_child_t indexed_child;
if (index < 0 || index >= 256) {
indexed_child.child = NULL;
return indexed_child;
}
indexed_child.index = index;
indexed_child.child = node->children[node->keys[index]];
indexed_child.key_chunk = index;
return indexed_child;
}
static inline art_indexed_child_t art_node48_lower_bound(
art_node48_t *node, art_key_chunk_t key_chunk) {
art_indexed_child_t indexed_child;
for (size_t i = key_chunk; i < 256; ++i) {
if (node->keys[i] != CROARING_ART_NODE48_EMPTY_VAL) {
indexed_child.index = i;
indexed_child.child = node->children[node->keys[i]];
indexed_child.key_chunk = i;
return indexed_child;
}
}
indexed_child.child = NULL;
return indexed_child;
}
static bool art_node48_internal_validate(const art_node48_t *node,
art_internal_validate_t validator) {
if (node->count <= 16) {
return art_validate_fail(&validator, "Node48 has too few children");
}
if (node->count > 48) {
return art_validate_fail(&validator, "Node48 has too many children");
}
uint64_t used_children = 0;
for (int i = 0; i < 256; ++i) {
uint8_t child_idx = node->keys[i];
if (child_idx != CROARING_ART_NODE48_EMPTY_VAL) {
if (used_children & (UINT64_C(1) << child_idx)) {
return art_validate_fail(
&validator, "Node48 keys point to the same child index");
}
art_node_t *child = node->children[child_idx];
if (child == NULL) {
return art_validate_fail(&validator, "Node48 has a NULL child");
}
used_children |= UINT64_C(1) << child_idx;
}
}
uint64_t expected_used_children =
(node->available_children) ^ CROARING_NODE48_AVAILABLE_CHILDREN_MASK;
if (used_children != expected_used_children) {
return art_validate_fail(
&validator,
"Node48 available_children does not match actual children");
}
while (used_children != 0) {
uint8_t child_idx = roaring_trailing_zeroes(used_children);
used_children &= used_children - 1;
uint64_t other_children = used_children;
while (other_children != 0) {
uint8_t other_child_idx = roaring_trailing_zeroes(other_children);
if (node->children[child_idx] == node->children[other_child_idx]) {
return art_validate_fail(&validator,
"Node48 has duplicate children");
}
other_children &= other_children - 1;
}
}
validator.depth++;
for (int i = 0; i < 256; ++i) {
if (node->keys[i] != CROARING_ART_NODE48_EMPTY_VAL) {
validator.current_key[validator.depth - 1] = i;
if (!art_internal_validate_at(node->children[node->keys[i]],
validator)) {
return false;
}
}
}
return true;
}
static art_node256_t *art_node256_create(const art_key_chunk_t prefix[],
uint8_t prefix_size) {
art_node256_t *node =
(art_node256_t *)roaring_malloc(sizeof(art_node256_t));
art_init_inner_node(&node->base, CROARING_ART_NODE256_TYPE, prefix,
prefix_size);
node->count = 0;
for (size_t i = 0; i < 256; ++i) {
node->children[i] = NULL;
}
return node;
}
static void art_free_node256(art_node256_t *node) {
for (size_t i = 0; i < 256; ++i) {
if (node->children[i] != NULL) {
art_free_node(node->children[i]);
}
}
roaring_free(node);
}
static inline art_node_t *art_node256_find_child(const art_node256_t *node,
art_key_chunk_t key) {
return node->children[key];
}
static art_node_t *art_node256_insert(art_node256_t *node, art_node_t *child,
uint8_t key) {
node->children[key] = child;
node->count++;
return (art_node_t *)node;
}
static inline art_node_t *art_node256_erase(art_node256_t *node,
uint8_t key_chunk) {
node->children[key_chunk] = NULL;
node->count--;
if (node->count > 48) {
return (art_node_t *)node;
}
art_node48_t *new_node =
art_node48_create(node->base.prefix, node->base.prefix_size);
for (size_t i = 0; i < 256; ++i) {
if (node->children[i] != NULL) {
art_node48_insert(new_node, node->children[i], i);
}
}
roaring_free(node);
return (art_node_t *)new_node;
}
static inline void art_node256_replace(art_node256_t *node,
art_key_chunk_t key_chunk,
art_node_t *new_child) {
node->children[key_chunk] = new_child;
}
static inline art_indexed_child_t art_node256_next_child(
const art_node256_t *node, int index) {
art_indexed_child_t indexed_child;
index++;
for (size_t i = index; i < 256; ++i) {
if (node->children[i] != NULL) {
indexed_child.index = i;
indexed_child.child = node->children[i];
indexed_child.key_chunk = i;
return indexed_child;
}
}
indexed_child.child = NULL;
return indexed_child;
}
static inline art_indexed_child_t art_node256_prev_child(
const art_node256_t *node, int index) {
if (index > 256) {
index = 256;
}
index--;
art_indexed_child_t indexed_child;
for (int i = index; i >= 0; --i) {
if (node->children[i] != NULL) {
indexed_child.index = i;
indexed_child.child = node->children[i];
indexed_child.key_chunk = i;
return indexed_child;
}
}
indexed_child.child = NULL;
return indexed_child;
}
static inline art_indexed_child_t art_node256_child_at(
const art_node256_t *node, int index) {
art_indexed_child_t indexed_child;
if (index < 0 || index >= 256) {
indexed_child.child = NULL;
return indexed_child;
}
indexed_child.index = index;
indexed_child.child = node->children[index];
indexed_child.key_chunk = index;
return indexed_child;
}
static inline art_indexed_child_t art_node256_lower_bound(
art_node256_t *node, art_key_chunk_t key_chunk) {
art_indexed_child_t indexed_child;
for (size_t i = key_chunk; i < 256; ++i) {
if (node->children[i] != NULL) {
indexed_child.index = i;
indexed_child.child = node->children[i];
indexed_child.key_chunk = i;
return indexed_child;
}
}
indexed_child.child = NULL;
return indexed_child;
}
static bool art_node256_internal_validate(const art_node256_t *node,
art_internal_validate_t validator) {
if (node->count <= 48) {
return art_validate_fail(&validator, "Node256 has too few children");
}
if (node->count > 256) {
return art_validate_fail(&validator, "Node256 has too many children");
}
validator.depth++;
int actual_count = 0;
for (int i = 0; i < 256; ++i) {
if (node->children[i] != NULL) {
actual_count++;
for (int j = i + 1; j < 256; ++j) {
if (node->children[i] == node->children[j]) {
return art_validate_fail(&validator,
"Node256 has duplicate children");
}
}
validator.current_key[validator.depth - 1] = i;
if (!art_internal_validate_at(node->children[i], validator)) {
return false;
}
}
}
if (actual_count != node->count) {
return art_validate_fail(
&validator, "Node256 count does not match actual children");
}
return true;
}
// Finds the child with the given key chunk in the inner node, returns NULL if
// no such child is found.
static art_node_t *art_find_child(const art_inner_node_t *node,
art_key_chunk_t key_chunk) {
switch (art_get_type(node)) {
case CROARING_ART_NODE4_TYPE:
return art_node4_find_child((art_node4_t *)node, key_chunk);
case CROARING_ART_NODE16_TYPE:
return art_node16_find_child((art_node16_t *)node, key_chunk);
case CROARING_ART_NODE48_TYPE:
return art_node48_find_child((art_node48_t *)node, key_chunk);
case CROARING_ART_NODE256_TYPE:
return art_node256_find_child((art_node256_t *)node, key_chunk);
default:
assert(false);
return NULL;
}
}
// Replaces the child with the given key chunk in the inner node.
static void art_replace(art_inner_node_t *node, art_key_chunk_t key_chunk,
art_node_t *new_child) {
switch (art_get_type(node)) {
case CROARING_ART_NODE4_TYPE:
art_node4_replace((art_node4_t *)node, key_chunk, new_child);
break;
case CROARING_ART_NODE16_TYPE:
art_node16_replace((art_node16_t *)node, key_chunk, new_child);
break;
case CROARING_ART_NODE48_TYPE:
art_node48_replace((art_node48_t *)node, key_chunk, new_child);
break;
case CROARING_ART_NODE256_TYPE:
art_node256_replace((art_node256_t *)node, key_chunk, new_child);
break;
default:
assert(false);
}
}
// Erases the child with the given key chunk from the inner node, returns the
// updated node (the same as the initial node if it was not shrunk).
static art_node_t *art_node_erase(art_inner_node_t *node,
art_key_chunk_t key_chunk) {
switch (art_get_type(node)) {
case CROARING_ART_NODE4_TYPE:
return art_node4_erase((art_node4_t *)node, key_chunk);
case CROARING_ART_NODE16_TYPE:
return art_node16_erase((art_node16_t *)node, key_chunk);
case CROARING_ART_NODE48_TYPE:
return art_node48_erase((art_node48_t *)node, key_chunk);
case CROARING_ART_NODE256_TYPE:
return art_node256_erase((art_node256_t *)node, key_chunk);
default:
assert(false);
return NULL;
}
}
// Inserts the leaf with the given key chunk in the inner node, returns a
// pointer to the (possibly expanded) node.
static art_node_t *art_node_insert_leaf(art_inner_node_t *node,
art_key_chunk_t key_chunk,
art_leaf_t *leaf) {
art_node_t *child = (art_node_t *)(CROARING_SET_LEAF(leaf));
switch (art_get_type(node)) {
case CROARING_ART_NODE4_TYPE:
return art_node4_insert((art_node4_t *)node, child, key_chunk);
case CROARING_ART_NODE16_TYPE:
return art_node16_insert((art_node16_t *)node, child, key_chunk);
case CROARING_ART_NODE48_TYPE:
return art_node48_insert((art_node48_t *)node, child, key_chunk);
case CROARING_ART_NODE256_TYPE:
return art_node256_insert((art_node256_t *)node, child, key_chunk);
default:
assert(false);
return NULL;
}
}
// Frees the node and its children. Leaves are freed by the user.
static void art_free_node(art_node_t *node) {
if (art_is_leaf(node)) {
// We leave it up to the user to free leaves.
return;
}
switch (art_get_type((art_inner_node_t *)node)) {
case CROARING_ART_NODE4_TYPE:
art_free_node4((art_node4_t *)node);
break;
case CROARING_ART_NODE16_TYPE:
art_free_node16((art_node16_t *)node);
break;
case CROARING_ART_NODE48_TYPE:
art_free_node48((art_node48_t *)node);
break;
case CROARING_ART_NODE256_TYPE:
art_free_node256((art_node256_t *)node);
break;
default:
assert(false);
}
}
// Returns the next child in key order, or NULL if called on a leaf.
// Provided index may be in the range [-1, 255].
static art_indexed_child_t art_node_next_child(const art_node_t *node,
int index) {
if (art_is_leaf(node)) {
art_indexed_child_t indexed_child;
indexed_child.child = NULL;
return indexed_child;
}
switch (art_get_type((art_inner_node_t *)node)) {
case CROARING_ART_NODE4_TYPE:
return art_node4_next_child((art_node4_t *)node, index);
case CROARING_ART_NODE16_TYPE:
return art_node16_next_child((art_node16_t *)node, index);
case CROARING_ART_NODE48_TYPE:
return art_node48_next_child((art_node48_t *)node, index);
case CROARING_ART_NODE256_TYPE:
return art_node256_next_child((art_node256_t *)node, index);
default:
assert(false);
return (art_indexed_child_t){0, 0, 0};
}
}
// Returns the previous child in key order, or NULL if called on a leaf.
// Provided index may be in the range [0, 256].
static art_indexed_child_t art_node_prev_child(const art_node_t *node,
int index) {
if (art_is_leaf(node)) {
art_indexed_child_t indexed_child;
indexed_child.child = NULL;
return indexed_child;
}
switch (art_get_type((art_inner_node_t *)node)) {
case CROARING_ART_NODE4_TYPE:
return art_node4_prev_child((art_node4_t *)node, index);
case CROARING_ART_NODE16_TYPE:
return art_node16_prev_child((art_node16_t *)node, index);
case CROARING_ART_NODE48_TYPE:
return art_node48_prev_child((art_node48_t *)node, index);
case CROARING_ART_NODE256_TYPE:
return art_node256_prev_child((art_node256_t *)node, index);
default:
assert(false);
return (art_indexed_child_t){0, 0, 0};
}
}
// Returns the child found at the provided index, or NULL if called on a leaf.
// Provided index is only valid if returned by art_node_(next|prev)_child.
static art_indexed_child_t art_node_child_at(const art_node_t *node,
int index) {
if (art_is_leaf(node)) {
art_indexed_child_t indexed_child;
indexed_child.child = NULL;
return indexed_child;
}
switch (art_get_type((art_inner_node_t *)node)) {
case CROARING_ART_NODE4_TYPE:
return art_node4_child_at((art_node4_t *)node, index);
case CROARING_ART_NODE16_TYPE:
return art_node16_child_at((art_node16_t *)node, index);
case CROARING_ART_NODE48_TYPE:
return art_node48_child_at((art_node48_t *)node, index);
case CROARING_ART_NODE256_TYPE:
return art_node256_child_at((art_node256_t *)node, index);
default:
assert(false);
return (art_indexed_child_t){0, 0, 0};
}
}
// Returns the child with the smallest key equal to or greater than the given
// key chunk, NULL if called on a leaf or no such child was found.
static art_indexed_child_t art_node_lower_bound(const art_node_t *node,
art_key_chunk_t key_chunk) {
if (art_is_leaf(node)) {
art_indexed_child_t indexed_child;
indexed_child.child = NULL;
return indexed_child;
}
switch (art_get_type((art_inner_node_t *)node)) {
case CROARING_ART_NODE4_TYPE:
return art_node4_lower_bound((art_node4_t *)node, key_chunk);
case CROARING_ART_NODE16_TYPE:
return art_node16_lower_bound((art_node16_t *)node, key_chunk);
case CROARING_ART_NODE48_TYPE:
return art_node48_lower_bound((art_node48_t *)node, key_chunk);
case CROARING_ART_NODE256_TYPE:
return art_node256_lower_bound((art_node256_t *)node, key_chunk);
default:
assert(false);
return (art_indexed_child_t){0, 0, 0};
}
}
// ====================== End of node-specific functions =======================
// Compares the given ranges of two keys, returns their relative order:
// * Key range 1 < key range 2: a negative value
// * Key range 1 == key range 2: 0
// * Key range 1 > key range 2: a positive value
static inline int art_compare_prefix(const art_key_chunk_t key1[],
uint8_t key1_from,
const art_key_chunk_t key2[],
uint8_t key2_from, uint8_t length) {
return memcmp(key1 + key1_from, key2 + key2_from, length);
}
// Compares two keys in full, see art_compare_prefix.
int art_compare_keys(const art_key_chunk_t key1[],
const art_key_chunk_t key2[]) {
return art_compare_prefix(key1, 0, key2, 0, ART_KEY_BYTES);
}
// Returns the length of the common prefix between two key ranges.
static uint8_t art_common_prefix(const art_key_chunk_t key1[],
uint8_t key1_from, uint8_t key1_to,
const art_key_chunk_t key2[],
uint8_t key2_from, uint8_t key2_to) {
uint8_t min_len = key1_to - key1_from;
uint8_t key2_len = key2_to - key2_from;
if (key2_len < min_len) {
min_len = key2_len;
}
uint8_t offset = 0;
for (; offset < min_len; ++offset) {
if (key1[key1_from + offset] != key2[key2_from + offset]) {
return offset;
}
}
return offset;
}
// Returns a pointer to the rootmost node where the value was inserted, may not
// be equal to `node`.
static art_node_t *art_insert_at(art_node_t *node, const art_key_chunk_t key[],
uint8_t depth, art_leaf_t *new_leaf) {
if (art_is_leaf(node)) {
art_leaf_t *leaf = CROARING_CAST_LEAF(node);
uint8_t common_prefix = art_common_prefix(
leaf->key, depth, ART_KEY_BYTES, key, depth, ART_KEY_BYTES);
// Previously this was a leaf, create an inner node instead and add both
// the existing and new leaf to it.
art_node_t *new_node =
(art_node_t *)art_node4_create(key + depth, common_prefix);
new_node = art_node_insert_leaf((art_inner_node_t *)new_node,
leaf->key[depth + common_prefix], leaf);
new_node = art_node_insert_leaf((art_inner_node_t *)new_node,
key[depth + common_prefix], new_leaf);
// The new inner node is now the rootmost node.
return new_node;
}
art_inner_node_t *inner_node = (art_inner_node_t *)node;
// Not a leaf: inner node
uint8_t common_prefix =
art_common_prefix(inner_node->prefix, 0, inner_node->prefix_size, key,
depth, ART_KEY_BYTES);
if (common_prefix != inner_node->prefix_size) {
// Partial prefix match. Create a new internal node to hold the common
// prefix.
art_node4_t *node4 =
art_node4_create(inner_node->prefix, common_prefix);
// Make the existing internal node a child of the new internal node.
node4 = (art_node4_t *)art_node4_insert(
node4, node, inner_node->prefix[common_prefix]);
// Correct the prefix of the moved internal node, trimming off the chunk
// inserted into the new internal node.
inner_node->prefix_size = inner_node->prefix_size - common_prefix - 1;
if (inner_node->prefix_size > 0) {
// Move the remaining prefix to the correct position.
memmove(inner_node->prefix, inner_node->prefix + common_prefix + 1,
inner_node->prefix_size);
}
// Insert the value in the new internal node.
return art_node_insert_leaf(&node4->base, key[common_prefix + depth],
new_leaf);
}
// Prefix matches entirely or node has no prefix. Look for an existing
// child.
art_key_chunk_t key_chunk = key[depth + common_prefix];
art_node_t *child = art_find_child(inner_node, key_chunk);
if (child != NULL) {
art_node_t *new_child =
art_insert_at(child, key, depth + common_prefix + 1, new_leaf);
if (new_child != child) {
// Node type changed.
art_replace(inner_node, key_chunk, new_child);
}
return node;
}
return art_node_insert_leaf(inner_node, key_chunk, new_leaf);
}
// Erase helper struct.
typedef struct art_erase_result_s {
// The rootmost node where the value was erased, may not be equal to `node`.
// If no value was removed, this is null.
art_node_t *rootmost_node;
// Value removed, null if not removed.
art_val_t *value_erased;
} art_erase_result_t;
// Searches for the given key starting at `node`, erases it if found.
static art_erase_result_t art_erase_at(art_node_t *node,
const art_key_chunk_t *key,
uint8_t depth) {
art_erase_result_t result;
result.rootmost_node = NULL;
result.value_erased = NULL;
if (art_is_leaf(node)) {
art_leaf_t *leaf = CROARING_CAST_LEAF(node);
uint8_t common_prefix = art_common_prefix(leaf->key, 0, ART_KEY_BYTES,
key, 0, ART_KEY_BYTES);
if (common_prefix != ART_KEY_BYTES) {
// Leaf key mismatch.
return result;
}
result.value_erased = (art_val_t *)leaf;
return result;
}
art_inner_node_t *inner_node = (art_inner_node_t *)node;
uint8_t common_prefix =
art_common_prefix(inner_node->prefix, 0, inner_node->prefix_size, key,
depth, ART_KEY_BYTES);
if (common_prefix != inner_node->prefix_size) {
// Prefix mismatch.
return result;
}
art_key_chunk_t key_chunk = key[depth + common_prefix];
art_node_t *child = art_find_child(inner_node, key_chunk);
if (child == NULL) {
// No child with key chunk.
return result;
}
// Try to erase the key further down. Skip the key chunk associated with the
// child in the node.
art_erase_result_t child_result =
art_erase_at(child, key, depth + common_prefix + 1);
if (child_result.value_erased == NULL) {
return result;
}
result.value_erased = child_result.value_erased;
result.rootmost_node = node;
if (child_result.rootmost_node == NULL) {
// Child node was fully erased, erase it from this node's children.
result.rootmost_node = art_node_erase(inner_node, key_chunk);
} else if (child_result.rootmost_node != child) {
// Child node was not fully erased, update the pointer to it in this
// node.
art_replace(inner_node, key_chunk, child_result.rootmost_node);
}
return result;
}
// Searches for the given key starting at `node`, returns NULL if the key was
// not found.
static art_val_t *art_find_at(const art_node_t *node,
const art_key_chunk_t *key, uint8_t depth) {
while (!art_is_leaf(node)) {
art_inner_node_t *inner_node = (art_inner_node_t *)node;
uint8_t common_prefix =
art_common_prefix(inner_node->prefix, 0, inner_node->prefix_size,
key, depth, ART_KEY_BYTES);
if (common_prefix != inner_node->prefix_size) {
return NULL;
}
art_node_t *child =
art_find_child(inner_node, key[depth + inner_node->prefix_size]);
if (child == NULL) {
return NULL;
}
node = child;
// Include both the prefix and the child key chunk in the depth.
depth += inner_node->prefix_size + 1;
}
art_leaf_t *leaf = CROARING_CAST_LEAF(node);
if (depth >= ART_KEY_BYTES) {
return (art_val_t *)leaf;
}
uint8_t common_prefix =
art_common_prefix(leaf->key, 0, ART_KEY_BYTES, key, 0, ART_KEY_BYTES);
if (common_prefix == ART_KEY_BYTES) {
return (art_val_t *)leaf;
}
return NULL;
}
// Returns the size in bytes of the subtrie.
size_t art_size_in_bytes_at(const art_node_t *node) {
if (art_is_leaf(node)) {
return 0;
}
size_t size = 0;
switch (art_get_type((art_inner_node_t *)node)) {
case CROARING_ART_NODE4_TYPE: {
size += sizeof(art_node4_t);
} break;
case CROARING_ART_NODE16_TYPE: {
size += sizeof(art_node16_t);
} break;
case CROARING_ART_NODE48_TYPE: {
size += sizeof(art_node48_t);
} break;
case CROARING_ART_NODE256_TYPE: {
size += sizeof(art_node256_t);
} break;
default:
assert(false);
break;
}
art_indexed_child_t indexed_child = art_node_next_child(node, -1);
while (indexed_child.child != NULL) {
size += art_size_in_bytes_at(indexed_child.child);
indexed_child = art_node_next_child(node, indexed_child.index);
}
return size;
}
static void art_node_print_type(const art_node_t *node) {
if (art_is_leaf(node)) {
printf("Leaf");
return;
}
switch (art_get_type((art_inner_node_t *)node)) {
case CROARING_ART_NODE4_TYPE:
printf("Node4");
return;
case CROARING_ART_NODE16_TYPE:
printf("Node16");
return;
case CROARING_ART_NODE48_TYPE:
printf("Node48");
return;
case CROARING_ART_NODE256_TYPE:
printf("Node256");
return;
default:
assert(false);
return;
}
}
void art_node_printf(const art_node_t *node, uint8_t depth) {
if (art_is_leaf(node)) {
printf("{ type: Leaf, key: ");
art_leaf_t *leaf = CROARING_CAST_LEAF(node);
for (size_t i = 0; i < ART_KEY_BYTES; ++i) {
printf("%02x", leaf->key[i]);
}
printf(" }\n");
return;
}
printf("{\n");
depth++;
printf("%*s", depth, "");
printf("type: ");
art_node_print_type(node);
printf("\n");
art_inner_node_t *inner_node = (art_inner_node_t *)node;
printf("%*s", depth, "");
printf("prefix_size: %d\n", inner_node->prefix_size);
printf("%*s", depth, "");
printf("prefix: ");
for (uint8_t i = 0; i < inner_node->prefix_size; ++i) {
printf("%02x", inner_node->prefix[i]);
}
printf("\n");
switch (art_get_type(inner_node)) {
case CROARING_ART_NODE4_TYPE: {
art_node4_t *node4 = (art_node4_t *)node;
for (uint8_t i = 0; i < node4->count; ++i) {
printf("%*s", depth, "");
printf("key: %02x ", node4->keys[i]);
art_node_printf(node4->children[i], depth);
}
} break;
case CROARING_ART_NODE16_TYPE: {
art_node16_t *node16 = (art_node16_t *)node;
for (uint8_t i = 0; i < node16->count; ++i) {
printf("%*s", depth, "");
printf("key: %02x ", node16->keys[i]);
art_node_printf(node16->children[i], depth);
}
} break;
case CROARING_ART_NODE48_TYPE: {
art_node48_t *node48 = (art_node48_t *)node;
for (int i = 0; i < 256; ++i) {
if (node48->keys[i] != CROARING_ART_NODE48_EMPTY_VAL) {
printf("%*s", depth, "");
printf("key: %02x ", i);
printf("child: %02x ", node48->keys[i]);
art_node_printf(node48->children[node48->keys[i]], depth);
}
}
} break;
case CROARING_ART_NODE256_TYPE: {
art_node256_t *node256 = (art_node256_t *)node;
for (int i = 0; i < 256; ++i) {
if (node256->children[i] != NULL) {
printf("%*s", depth, "");
printf("key: %02x ", i);
art_node_printf(node256->children[i], depth);
}
}
} break;
default:
assert(false);
break;
}
depth--;
printf("%*s", depth, "");
printf("}\n");
}
void art_insert(art_t *art, const art_key_chunk_t *key, art_val_t *val) {
art_leaf_t *leaf = (art_leaf_t *)val;
art_leaf_populate(leaf, key);
if (art->root == NULL) {
art->root = (art_node_t *)CROARING_SET_LEAF(leaf);
return;
}
art->root = art_insert_at(art->root, key, 0, leaf);
}
art_val_t *art_erase(art_t *art, const art_key_chunk_t *key) {
if (art->root == NULL) {
return NULL;
}
art_erase_result_t result = art_erase_at(art->root, key, 0);
if (result.value_erased == NULL) {
return NULL;
}
art->root = result.rootmost_node;
return result.value_erased;
}
art_val_t *art_find(const art_t *art, const art_key_chunk_t *key) {
if (art->root == NULL) {
return NULL;
}
return art_find_at(art->root, key, 0);
}
bool art_is_empty(const art_t *art) { return art->root == NULL; }
void art_free(art_t *art) {
if (art->root == NULL) {
return;
}
art_free_node(art->root);
}
size_t art_size_in_bytes(const art_t *art) {
size_t size = sizeof(art_t);
if (art->root != NULL) {
size += art_size_in_bytes_at(art->root);
}
return size;
}
void art_printf(const art_t *art) {
if (art->root == NULL) {
return;
}
art_node_printf(art->root, 0);
}
// Returns the current node that the iterator is positioned at.
static inline art_node_t *art_iterator_node(art_iterator_t *iterator) {
return iterator->frames[iterator->frame].node;
}
// Sets the iterator key and value to the leaf's key and value. Always returns
// true for convenience.
static inline bool art_iterator_valid_loc(art_iterator_t *iterator,
art_leaf_t *leaf) {
iterator->frames[iterator->frame].node = CROARING_SET_LEAF(leaf);
iterator->frames[iterator->frame].index_in_node = 0;
memcpy(iterator->key, leaf->key, ART_KEY_BYTES);
iterator->value = (art_val_t *)leaf;
return true;
}
// Invalidates the iterator key and value. Always returns false for convenience.
static inline bool art_iterator_invalid_loc(art_iterator_t *iterator) {
memset(iterator->key, 0, ART_KEY_BYTES);
iterator->value = NULL;
return false;
}
// Moves the iterator one level down in the tree, given a node at the current
// level and the index of the child that we're going down to.
//
// Note: does not set the index at the new level.
static void art_iterator_down(art_iterator_t *iterator,
const art_inner_node_t *node,
uint8_t index_in_node) {
iterator->frames[iterator->frame].node = (art_node_t *)node;
iterator->frames[iterator->frame].index_in_node = index_in_node;
iterator->frame++;
art_indexed_child_t indexed_child =
art_node_child_at((art_node_t *)node, index_in_node);
assert(indexed_child.child != NULL);
iterator->frames[iterator->frame].node = indexed_child.child;
iterator->depth += node->prefix_size + 1;
}
// Moves the iterator to the next/previous child of the current node. Returns
// the child moved to, or NULL if there is no neighboring child.
static art_node_t *art_iterator_neighbor_child(
art_iterator_t *iterator, const art_inner_node_t *inner_node,
bool forward) {
art_iterator_frame_t frame = iterator->frames[iterator->frame];
art_indexed_child_t indexed_child;
if (forward) {
indexed_child = art_node_next_child(frame.node, frame.index_in_node);
} else {
indexed_child = art_node_prev_child(frame.node, frame.index_in_node);
}
if (indexed_child.child != NULL) {
art_iterator_down(iterator, inner_node, indexed_child.index);
}
return indexed_child.child;
}
// Moves the iterator one level up in the tree, returns false if not possible.
static bool art_iterator_up(art_iterator_t *iterator) {
if (iterator->frame == 0) {
return false;
}
iterator->frame--;
// We went up, so we are at an inner node.
iterator->depth -=
((art_inner_node_t *)art_iterator_node(iterator))->prefix_size + 1;
return true;
}
// Moves the iterator one level, followed by a move to the next / previous leaf.
// Sets the status of the iterator.
static bool art_iterator_up_and_move(art_iterator_t *iterator, bool forward) {
if (!art_iterator_up(iterator)) {
// We're at the root.
return art_iterator_invalid_loc(iterator);
}
return art_iterator_move(iterator, forward);
}
// Initializes the iterator at the first / last leaf of the given node.
// Returns true for convenience.
static bool art_node_init_iterator(const art_node_t *node,
art_iterator_t *iterator, bool first) {
while (!art_is_leaf(node)) {
art_indexed_child_t indexed_child;
if (first) {
indexed_child = art_node_next_child(node, -1);
} else {
indexed_child = art_node_prev_child(node, 256);
}
art_iterator_down(iterator, (art_inner_node_t *)node,
indexed_child.index);
node = indexed_child.child;
}
// We're at a leaf.
iterator->frames[iterator->frame].node = (art_node_t *)node;
iterator->frames[iterator->frame].index_in_node = 0; // Should not matter.
return art_iterator_valid_loc(iterator, CROARING_CAST_LEAF(node));
}
bool art_iterator_move(art_iterator_t *iterator, bool forward) {
if (art_is_leaf(art_iterator_node(iterator))) {
bool went_up = art_iterator_up(iterator);
if (!went_up) {
// This leaf is the root, we're done.
return art_iterator_invalid_loc(iterator);
}
}
// Advance within inner node.
art_node_t *neighbor_child = art_iterator_neighbor_child(
iterator, (art_inner_node_t *)art_iterator_node(iterator), forward);
if (neighbor_child != NULL) {
// There is another child at this level, go down to the first or last
// leaf.
return art_node_init_iterator(neighbor_child, iterator, forward);
}
// No more children at this level, go up.
return art_iterator_up_and_move(iterator, forward);
}
// Assumes the iterator is positioned at a node with an equal prefix path up to
// the depth of the iterator.
static bool art_node_iterator_lower_bound(const art_node_t *node,
art_iterator_t *iterator,
const art_key_chunk_t key[]) {
while (!art_is_leaf(node)) {
art_inner_node_t *inner_node = (art_inner_node_t *)node;
int prefix_comparison =
art_compare_prefix(inner_node->prefix, 0, key, iterator->depth,
inner_node->prefix_size);
if (prefix_comparison < 0) {
// Prefix so far has been equal, but we've found a smaller key.
// Since we take the lower bound within each node, we can return the
// next leaf.
return art_iterator_up_and_move(iterator, true);
} else if (prefix_comparison > 0) {
// No key equal to the key we're looking for, return the first leaf.
return art_node_init_iterator(node, iterator, true);
}
// Prefix is equal, move to lower bound child.
art_key_chunk_t key_chunk =
key[iterator->depth + inner_node->prefix_size];
art_indexed_child_t indexed_child =
art_node_lower_bound(node, key_chunk);
if (indexed_child.child == NULL) {
// Only smaller keys among children.
return art_iterator_up_and_move(iterator, true);
}
if (indexed_child.key_chunk > key_chunk) {
// Only larger children, return the first larger child.
art_iterator_down(iterator, inner_node, indexed_child.index);
return art_node_init_iterator(indexed_child.child, iterator, true);
}
// We found a child with an equal prefix.
art_iterator_down(iterator, inner_node, indexed_child.index);
node = indexed_child.child;
}
art_leaf_t *leaf = CROARING_CAST_LEAF(node);
if (art_compare_keys(leaf->key, key) >= 0) {
// Leaf has an equal or larger key.
return art_iterator_valid_loc(iterator, leaf);
}
// Leaf has an equal prefix, but the full key is smaller. Move to the next
// leaf.
return art_iterator_up_and_move(iterator, true);
}
art_iterator_t art_init_iterator(const art_t *art, bool first) {
art_iterator_t iterator = CROARING_ZERO_INITIALIZER;
if (art->root == NULL) {
return iterator;
}
art_node_init_iterator(art->root, &iterator, first);
return iterator;
}
bool art_iterator_next(art_iterator_t *iterator) {
return art_iterator_move(iterator, true);
}
bool art_iterator_prev(art_iterator_t *iterator) {
return art_iterator_move(iterator, false);
}
bool art_iterator_lower_bound(art_iterator_t *iterator,
const art_key_chunk_t *key) {
if (iterator->value == NULL) {
// We're beyond the end / start of the ART so the iterator does not have
// a valid key. Start from the root.
iterator->frame = 0;
iterator->depth = 0;
art_node_t *root = art_iterator_node(iterator);
if (root == NULL) {
return false;
}
return art_node_iterator_lower_bound(root, iterator, key);
}
int compare_result =
art_compare_prefix(iterator->key, 0, key, 0, ART_KEY_BYTES);
// Move up until we have an equal prefix, after which we can do a normal
// lower bound search.
while (compare_result != 0) {
if (!art_iterator_up(iterator)) {
if (compare_result < 0) {
// Only smaller keys found.
return art_iterator_invalid_loc(iterator);
} else {
return art_node_init_iterator(art_iterator_node(iterator),
iterator, true);
}
}
// Since we're only moving up, we can keep comparing against the
// iterator key.
art_inner_node_t *inner_node =
(art_inner_node_t *)art_iterator_node(iterator);
compare_result =
art_compare_prefix(iterator->key, 0, key, 0,
iterator->depth + inner_node->prefix_size);
}
if (compare_result > 0) {
return art_node_init_iterator(art_iterator_node(iterator), iterator,
true);
}
return art_node_iterator_lower_bound(art_iterator_node(iterator), iterator,
key);
}
art_iterator_t art_lower_bound(const art_t *art, const art_key_chunk_t *key) {
art_iterator_t iterator = CROARING_ZERO_INITIALIZER;
if (art->root != NULL) {
art_node_iterator_lower_bound(art->root, &iterator, key);
}
return iterator;
}
art_iterator_t art_upper_bound(const art_t *art, const art_key_chunk_t *key) {
art_iterator_t iterator = CROARING_ZERO_INITIALIZER;
if (art->root != NULL) {
if (art_node_iterator_lower_bound(art->root, &iterator, key) &&
art_compare_keys(iterator.key, key) == 0) {
art_iterator_next(&iterator);
}
}
return iterator;
}
void art_iterator_insert(art_t *art, art_iterator_t *iterator,
const art_key_chunk_t *key, art_val_t *val) {
// TODO: This can likely be faster.
art_insert(art, key, val);
assert(art->root != NULL);
iterator->frame = 0;
iterator->depth = 0;
art_node_iterator_lower_bound(art->root, iterator, key);
}
// TODO: consider keeping `art_t *art` in the iterator.
art_val_t *art_iterator_erase(art_t *art, art_iterator_t *iterator) {
if (iterator->value == NULL) {
return NULL;
}
art_key_chunk_t initial_key[ART_KEY_BYTES];
memcpy(initial_key, iterator->key, ART_KEY_BYTES);
art_val_t *value_erased = iterator->value;
bool went_up = art_iterator_up(iterator);
if (!went_up) {
// We're erasing the root.
art->root = NULL;
art_iterator_invalid_loc(iterator);
return value_erased;
}
// Erase the leaf.
art_inner_node_t *parent_node =
(art_inner_node_t *)art_iterator_node(iterator);
art_key_chunk_t key_chunk_in_parent =
iterator->key[iterator->depth + parent_node->prefix_size];
art_node_t *new_parent_node =
art_node_erase(parent_node, key_chunk_in_parent);
if (new_parent_node != ((art_node_t *)parent_node)) {
// Replace the pointer to the inner node we erased from in its
// parent (it may be a leaf now).
iterator->frames[iterator->frame].node = new_parent_node;
went_up = art_iterator_up(iterator);
if (went_up) {
art_inner_node_t *grandparent_node =
(art_inner_node_t *)art_iterator_node(iterator);
art_key_chunk_t key_chunk_in_grandparent =
iterator->key[iterator->depth + grandparent_node->prefix_size];
art_replace(grandparent_node, key_chunk_in_grandparent,
new_parent_node);
} else {
// We were already at the rootmost node.
art->root = new_parent_node;
}
}
iterator->frame = 0;
iterator->depth = 0;
// Do a lower bound search for the initial key, which will find the first
// greater key if it exists. This can likely be mildly faster if we instead
// start from the current position.
art_node_iterator_lower_bound(art->root, iterator, initial_key);
return value_erased;
}
static bool art_internal_validate_at(const art_node_t *node,
art_internal_validate_t validator) {
if (node == NULL) {
return art_validate_fail(&validator, "node is null");
}
if (art_is_leaf(node)) {
art_leaf_t *leaf = CROARING_CAST_LEAF(node);
if (art_compare_prefix(leaf->key, 0, validator.current_key, 0,
validator.depth) != 0) {
return art_validate_fail(
&validator,
"leaf key does not match its position's prefix in the tree");
}
if (validator.validate_cb != NULL &&
!validator.validate_cb(leaf, validator.reason)) {
if (*validator.reason == NULL) {
*validator.reason = "leaf validation failed";
}
return false;
}
} else {
art_inner_node_t *inner_node = (art_inner_node_t *)node;
if (validator.depth + inner_node->prefix_size + 1 > ART_KEY_BYTES) {
return art_validate_fail(&validator,
"node has too much prefix at given depth");
}
memcpy(validator.current_key + validator.depth, inner_node->prefix,
inner_node->prefix_size);
validator.depth += inner_node->prefix_size;
switch (inner_node->typecode) {
case CROARING_ART_NODE4_TYPE:
if (!art_node4_internal_validate((art_node4_t *)inner_node,
validator)) {
return false;
}
break;
case CROARING_ART_NODE16_TYPE:
if (!art_node16_internal_validate((art_node16_t *)inner_node,
validator)) {
return false;
}
break;
case CROARING_ART_NODE48_TYPE:
if (!art_node48_internal_validate((art_node48_t *)inner_node,
validator)) {
return false;
}
break;
case CROARING_ART_NODE256_TYPE:
if (!art_node256_internal_validate((art_node256_t *)inner_node,
validator)) {
return false;
}
break;
default:
return art_validate_fail(&validator, "invalid node type");
}
}
return true;
}
bool art_internal_validate(const art_t *art, const char **reason,
art_validate_cb_t validate_cb) {
const char *reason_local;
if (reason == NULL) {
// Always allow assigning through *reason
reason = &reason_local;
}
*reason = NULL;
if (art->root == NULL) {
return true;
}
art_internal_validate_t validator = {
.reason = reason,
.validate_cb = validate_cb,
.depth = 0,
.current_key = {0},
};
return art_internal_validate_at(art->root, validator);
}
#ifdef __cplusplus
} // extern "C"
} // namespace roaring
} // namespace internal
#endif
|