cedarwood 0.6.0

efficiently-updatable double-array trie in Rust (ported from cedar)
Documentation
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
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
#![cfg_attr(not(feature = "std"), no_std)]

//!  Efficiently-updatable double-array trie in Rust (ported from cedar).
//!
//! Add it to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! cedarwood = "0.6"
//! ```
//!
//! then you are good to go.
//!
//! ## Example
//!
//! ```rust
//! use cedarwood::Cedar;
//!
//! let dict = vec![
//!     "a",
//!     "ab",
//!     "abc",
//!     "アルゴリズム",
//!     "データ",
//!     "構造",
//!     "网",
//!     "网球",
//!     "网球拍",
//!     "中",
//!     "中华",
//!     "中华人民",
//!     "中华人民共和国",
//! ];
//! let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
//! let mut cedar = Cedar::new();
//! cedar.build(&key_values)?;
//!
//! let result: Vec<i32> = cedar.common_prefix_search("abcdefg").iter().map(|x| x.0).collect();
//! assert_eq!(vec![0, 1, 2], result);
//!
//! let result: Vec<i32> = cedar
//!     .common_prefix_search("网球拍卖会")
//!     .iter()
//!     .map(|x| x.0)
//!     .collect();
//! assert_eq!(vec![6, 7, 8], result);
//!
//! let result: Vec<i32> = cedar
//!     .common_prefix_search("中华人民共和国")
//!     .iter()
//!     .map(|x| x.0)
//!     .collect();
//! assert_eq!(vec![9, 10, 11, 12], result);
//!
//! let result: Vec<i32> = cedar
//!     .common_prefix_search("データ構造とアルゴリズム")
//!     .iter()
//!     .map(|x| x.0)
//!     .collect();
//! assert_eq!(vec![4], result);
//! # Ok::<(), cedarwood::CedarError>(())
//! ```

extern crate alloc;
#[cfg(test)]
extern crate std;

use alloc::string::{FromUtf8Error, String};
use alloc::vec;
use alloc::vec::Vec;
use core::{fmt, mem};
use smallvec::SmallVec;

/// The smallest value accepted by [`Cedar::update`].
pub const MIN_VALUE: i32 = 0;

/// The largest value accepted by [`Cedar::update`].
///
/// The two larger `i32` values and all negative values are kept out of the public contract so the
/// default and `reduced-trie` layouts accept exactly the same inputs.
pub const MAX_VALUE: i32 = i32::MAX - 2;

/// Default ceiling for peak memory allocated while loading an untrusted serialized trie (512 MiB).
///
/// Use [`Cedar::load_from_reader_with_limit`] when a different application-specific ceiling is
/// appropriate. The limit applies to vector storage, not merely the number of bytes in the file.
#[cfg(feature = "std")]
pub const DEFAULT_LOAD_MEMORY_LIMIT: usize = 512 * 1024 * 1024;

/// Errors returned when configuring or mutating a trie.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CedarError {
    /// Empty keys cannot be stored because the root is not a value node.
    EmptyKey,
    /// Byte `0` is reserved as cedar's terminal label.
    NulByte { position: usize },
    /// Values must be in [`MIN_VALUE`] through [`MAX_VALUE`], inclusive.
    InvalidValue { value: i32 },
    /// `max_trial` must be greater than zero.
    InvalidMaxTrial { value: i32 },
    /// A sorted constructor received the same key twice.
    DuplicateKey { index: usize },
    /// A sorted constructor received a key smaller than its predecessor.
    KeysNotSorted { index: usize },
}

impl fmt::Display for CedarError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyKey => write!(f, "cedar keys must not be empty"),
            Self::NulByte { position } => write!(
                f,
                "cedar keys must not contain the terminal byte 0x00 (at byte {position})"
            ),
            Self::InvalidValue { value } => write!(
                f,
                "cedar value {value} is outside the supported range {MIN_VALUE}..={MAX_VALUE}"
            ),
            Self::InvalidMaxTrial { value } => {
                write!(f, "max_trial must be greater than zero, got {value}")
            }
            Self::DuplicateKey { index } => {
                write!(f, "duplicate key at sorted input index {index}")
            }
            Self::KeysNotSorted { index } => {
                write!(f, "key at input index {index} is smaller than its predecessor")
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for CedarError {}

/// Errors returned while saving or loading cedarwood's versioned binary format.
#[cfg(feature = "std")]
#[non_exhaustive]
#[derive(Debug)]
pub enum CedarPersistenceError {
    /// The underlying stream or filesystem operation failed.
    Io(std::io::Error),
    /// The input ended before the complete declared representation was read.
    Truncated,
    /// The eight-byte cedarwood format marker did not match.
    InvalidMagic,
    /// The file uses a format version this reader does not understand.
    UnsupportedVersion { major: u16, minor: u16 },
    /// The file is not explicitly marked as little-endian.
    UnsupportedByteOrder { byte_order: u8 },
    /// The file was produced for a different compile-time trie layout.
    LayoutMismatch { expected: u8, found: u8 },
    /// Reserved header flags were set or a header field was otherwise invalid.
    InvalidHeader { field: &'static str, value: u64 },
    /// Loading and structural validation would reserve more memory than the caller allows.
    AllocationLimitExceeded { requested: usize, limit: usize },
    /// The allocator could not reserve the validated amount of memory.
    AllocationFailed { requested: usize },
    /// A structural invariant failed validation.
    CorruptData {
        section: &'static str,
        index: usize,
        reason: &'static str,
    },
    /// Bytes remained after the complete declared representation.
    TrailingData,
}

#[cfg(feature = "std")]
impl fmt::Display for CedarPersistenceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io(error) => write!(f, "persistence I/O error: {error}"),
            Self::Truncated => write!(f, "serialized cedar trie is truncated"),
            Self::InvalidMagic => write!(f, "input is not a cedarwood serialized trie"),
            Self::UnsupportedVersion { major, minor } => {
                write!(f, "unsupported cedarwood format version {major}.{minor}")
            }
            Self::UnsupportedByteOrder { byte_order } => {
                write!(f, "unsupported cedarwood byte-order marker {byte_order}")
            }
            Self::LayoutMismatch { expected, found } => write!(
                f,
                "serialized trie layout {found} does not match this build's layout {expected}"
            ),
            Self::InvalidHeader { field, value } => {
                write!(f, "invalid serialized header field {field}: {value}")
            }
            Self::AllocationLimitExceeded { requested, limit } => write!(
                f,
                "serialized trie requires up to {requested} bytes while loading, limit is {limit}"
            ),
            Self::AllocationFailed { requested } => {
                write!(
                    f,
                    "could not reserve {requested} elements while loading serialized trie"
                )
            }
            Self::CorruptData { section, index, reason } => write!(f, "corrupt {section} at index {index}: {reason}"),
            Self::TrailingData => write!(f, "serialized trie has trailing data"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for CedarPersistenceError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(error) => Some(error),
            _ => None,
        }
    }
}

#[cfg(feature = "std")]
impl From<std::io::Error> for CedarPersistenceError {
    fn from(error: std::io::Error) -> Self {
        Self::Io(error)
    }
}

/// Configuration used to create a [`Cedar`] trie.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CedarBuilder {
    ordered: bool,
    max_trial: i32,
}

impl CedarBuilder {
    /// Creates a builder with ordered sibling chains and `max_trial = 1`.
    pub const fn new() -> Self {
        Self {
            ordered: true,
            max_trial: 1,
        }
    }

    /// Controls whether sibling labels remain sorted. The default is `true`.
    #[must_use]
    pub const fn ordered(mut self, ordered: bool) -> Self {
        self.ordered = ordered;
        self
    }

    /// Sets the number of failed probes allowed before an open block is closed.
    pub fn max_trial(mut self, max_trial: i32) -> Result<Self, CedarError> {
        if max_trial <= 0 {
            return Err(CedarError::InvalidMaxTrial { value: max_trial });
        }
        self.max_trial = max_trial;
        Ok(self)
    }

    /// Builds an empty trie with this configuration.
    #[must_use]
    pub fn build(self) -> Cedar {
        Cedar::with_config(self.ordered, self.max_trial)
    }

    /// Directly constructs a trie from strictly byte-sorted, unique UTF-8 keys using this
    /// builder's configuration.
    pub fn from_sorted(self, key_values: &[(&str, i32)]) -> Result<Cedar, CedarError> {
        Cedar::validate_sorted_entries(key_values)?;
        let mut cedar = Cedar::with_config(self.ordered, self.max_trial);
        cedar.build_sorted_validated(key_values);
        Ok(cedar)
    }

    /// Directly constructs a trie from strictly sorted, unique byte keys using this builder's
    /// configuration.
    pub fn from_sorted_bytes(self, key_values: &[(&[u8], i32)]) -> Result<Cedar, CedarError> {
        Cedar::validate_sorted_entries(key_values)?;
        let mut cedar = Cedar::with_config(self.ordered, self.max_trial);
        cedar.build_sorted_validated(key_values);
        Ok(cedar)
    }
}

impl Default for CedarBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// A snapshot of the trie's owned-memory and occupancy metrics.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MemoryStats {
    /// Number of stored key/value entries. This is identical to [`Cedar::len`].
    pub entries: usize,
    /// Occupied double-array slots, including the root and terminal slots where applicable.
    pub used_node_slots: usize,
    /// Number of `Node` elements for which the node vector has allocated storage.
    pub node_capacity: usize,
    /// Bytes reserved by all four owned vectors, computed from each vector's capacity and element
    /// size. It excludes the inline `Cedar` value and allocator bookkeeping.
    pub allocated_bytes: usize,
    /// `used_node_slots / node_capacity`, or `0.0` if capacity is zero.
    pub load_factor: f64,
}

/// NInfo stores the information about the trie
#[derive(Debug, Default, Clone, Copy)]
struct NInfo {
    sibling: u8, // the index of right sibling, it is 0 if it doesn't have a sibling.
    child: u8,   // the index of the first child
}

/// Node contains the array of `base` and `check` as specified in the paper: "An efficient implementation of trie structures"
/// https://dl.acm.org/citation.cfm?id=146691
#[derive(Debug, Default, Clone, Copy)]
struct Node {
    base_: i32, // if it is a negative value, then it stores the value of previous index that is free.
    check: i32, // if it is a negative value, then it stores the value of next index that is free.
}

impl Node {
    #[inline]
    fn base(&self) -> i32 {
        #[cfg(feature = "reduced-trie")]
        return -(self.base_ + 1);
        #[cfg(not(feature = "reduced-trie"))]
        return self.base_;
    }
}

/// Block stores the linked-list pointers and the stats info for blocks.
#[derive(Debug, Clone)]
struct Block {
    prev: i32,   // previous block's index, 3 bytes width
    next: i32,   // next block's index, 3 bytes width
    num: i16,    // the number of slots that is free, the range is 0-256
    reject: i16, // a heuristic number to make the search for free space faster, it is the minimum number of iteration in each trie node it has to try before we can conclude that we can reject this block. If the number of kids for the block we are looking for is less than this number then this block is worthy of searching.
    trial: i32,  // the number of times this block has been probed by `find_places` for the free block.
    e_head: i32, // the index of the first empty element in this block
}

impl Block {
    pub fn new() -> Self {
        Block {
            prev: 0,
            next: 0,
            num: 256,    // each of block has 256 free slots at the beginning
            reject: 257, // initially every block need to be fully iterated through so that we can reject it to be unusable.
            trial: 0,
            e_head: 0,
        }
    }
}

impl Default for Block {
    fn default() -> Self {
        Self::new()
    }
}

/// Blocks are marked as either of three categories, so that we can quickly decide if we can
/// allocate it for use or not.
enum BlockType {
    Open,   // The block has spaces more than 1.
    Closed, // The block is only left with one free slot
    Full,   // The block's slots are fully used.
}

/// `Cedar` holds all of the information about double array trie.
#[derive(Clone)]
pub struct Cedar {
    array: Vec<Node>, // storing the `base` and `check` info from the original paper.
    n_infos: Vec<NInfo>,
    blocks: Vec<Block>,
    reject: Vec<i16>,
    blocks_head_full: i32,   // the index of the first 'Full' block, 0 means no 'Full' block
    blocks_head_closed: i32, // the index of the first 'Closed' block, 0 means no ' Closed' block
    blocks_head_open: i32,   // the index of the first 'Open' block, 0 means no 'Open' block
    capacity: usize,
    size: usize,
    entries: usize,
    ordered: bool,
    max_trial: i32, // the parameter for cedar, it could be tuned for more, but the default is 1.
}

// Persistence decoding must pass through this private state before a queryable Cedar can escape.
#[cfg(feature = "std")]
struct UnvalidatedCedar(Cedar);

#[cfg(feature = "std")]
impl UnvalidatedCedar {
    fn validate(self) -> Result<Cedar, CedarPersistenceError> {
        self.0.validate_loaded_state()?;
        Ok(self.0)
    }
}

impl fmt::Debug for Cedar {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Cedar(entries={}, node_slots={}, ordered={})",
            self.entries, self.size, self.ordered
        )
    }
}

#[cfg(feature = "reduced-trie")]
const CEDAR_VALUE_LIMIT: i32 = i32::MAX - 1;
const CEDAR_NO_VALUE: i32 = -1;

// The versioned wire DTOs and primitive codec are kept separate from the live trie structs. Phase
// 7 can gate this module and the public persistence methods behind `std` without touching query or
// mutation code.
#[cfg(feature = "std")]
mod persistence {
    pub(super) mod v1 {
        use super::super::CedarPersistenceError;
        use std::io::{self, Read, Write};

        pub const MAGIC: [u8; 8] = *b"CEDARW\0\0";
        pub const MAJOR: u16 = 1;
        pub const MINOR: u16 = 0;
        pub const LITTLE_ENDIAN: u8 = 1;
        pub const FLAG_ORDERED: u16 = 1;
        #[cfg(test)]
        pub const HEADER_LEN: usize = 88;

        #[cfg(feature = "reduced-trie")]
        pub const LAYOUT: u8 = 1;
        #[cfg(not(feature = "reduced-trie"))]
        pub const LAYOUT: u8 = 0;

        #[derive(Clone, Copy)]
        pub struct Header {
            pub flags: u16,
            pub max_trial: i32,
            pub blocks_head_full: i32,
            pub blocks_head_closed: i32,
            pub blocks_head_open: i32,
            pub array_len: usize,
            pub n_infos_len: usize,
            pub blocks_len: usize,
            pub reject_len: usize,
            pub capacity: usize,
            pub size: usize,
            pub entries: usize,
        }

        #[derive(Clone, Copy)]
        pub struct NodeRecord {
            pub base: i32,
            pub check: i32,
        }

        #[derive(Clone, Copy)]
        pub struct NInfoRecord {
            pub sibling: u8,
            pub child: u8,
        }

        #[derive(Clone, Copy)]
        pub struct BlockRecord {
            pub prev: i32,
            pub next: i32,
            pub num: i16,
            pub reject: i16,
            pub trial: i32,
            pub e_head: i32,
        }

        pub struct Decoded {
            pub header: Header,
            pub nodes: Vec<NodeRecord>,
            pub n_infos: Vec<NInfoRecord>,
            pub blocks: Vec<BlockRecord>,
            pub reject: Vec<i16>,
        }

        fn persistence_io(error: io::Error) -> CedarPersistenceError {
            if error.kind() == io::ErrorKind::UnexpectedEof {
                CedarPersistenceError::Truncated
            } else {
                CedarPersistenceError::Io(error)
            }
        }

        fn read_exact(reader: &mut impl Read, bytes: &mut [u8]) -> Result<(), CedarPersistenceError> {
            reader.read_exact(bytes).map_err(persistence_io)
        }

        pub fn read_u8(reader: &mut impl Read) -> Result<u8, CedarPersistenceError> {
            let mut bytes = [0; 1];
            read_exact(reader, &mut bytes)?;
            Ok(bytes[0])
        }

        pub fn read_u16(reader: &mut impl Read) -> Result<u16, CedarPersistenceError> {
            let mut bytes = [0; 2];
            read_exact(reader, &mut bytes)?;
            Ok(u16::from_le_bytes(bytes))
        }

        pub fn read_i16(reader: &mut impl Read) -> Result<i16, CedarPersistenceError> {
            let mut bytes = [0; 2];
            read_exact(reader, &mut bytes)?;
            Ok(i16::from_le_bytes(bytes))
        }

        pub fn read_i32(reader: &mut impl Read) -> Result<i32, CedarPersistenceError> {
            let mut bytes = [0; 4];
            read_exact(reader, &mut bytes)?;
            Ok(i32::from_le_bytes(bytes))
        }

        pub fn read_u64(reader: &mut impl Read) -> Result<u64, CedarPersistenceError> {
            let mut bytes = [0; 8];
            read_exact(reader, &mut bytes)?;
            Ok(u64::from_le_bytes(bytes))
        }

        pub fn write_i16(writer: &mut impl Write, value: i16) -> Result<(), CedarPersistenceError> {
            writer.write_all(&value.to_le_bytes()).map_err(Into::into)
        }

        pub fn write_i32(writer: &mut impl Write, value: i32) -> Result<(), CedarPersistenceError> {
            writer.write_all(&value.to_le_bytes()).map_err(Into::into)
        }

        pub fn write_usize(writer: &mut impl Write, value: usize) -> Result<(), CedarPersistenceError> {
            let value = u64::try_from(value).map_err(|_| CedarPersistenceError::InvalidHeader {
                field: "usize",
                value: u64::MAX,
            })?;
            writer.write_all(&value.to_le_bytes()).map_err(Into::into)
        }

        pub fn read_usize(reader: &mut impl Read, field: &'static str) -> Result<usize, CedarPersistenceError> {
            let value = read_u64(reader)?;
            usize::try_from(value).map_err(|_| CedarPersistenceError::InvalidHeader { field, value })
        }

        pub fn reserve_exact<T>(vector: &mut Vec<T>, len: usize) -> Result<(), CedarPersistenceError> {
            vector
                .try_reserve_exact(len)
                .map_err(|_| CedarPersistenceError::AllocationFailed { requested: len })
        }

        pub fn write_header(writer: &mut impl Write, header: Header) -> Result<(), CedarPersistenceError> {
            writer.write_all(&MAGIC)?;
            writer.write_all(&MAJOR.to_le_bytes())?;
            writer.write_all(&MINOR.to_le_bytes())?;
            writer.write_all(&[LITTLE_ENDIAN, LAYOUT])?;
            writer.write_all(&header.flags.to_le_bytes())?;
            write_i32(writer, header.max_trial)?;
            write_i32(writer, header.blocks_head_full)?;
            write_i32(writer, header.blocks_head_closed)?;
            write_i32(writer, header.blocks_head_open)?;
            write_usize(writer, header.array_len)?;
            write_usize(writer, header.n_infos_len)?;
            write_usize(writer, header.blocks_len)?;
            write_usize(writer, header.reject_len)?;
            write_usize(writer, header.capacity)?;
            write_usize(writer, header.size)?;
            write_usize(writer, header.entries)
        }

        pub fn read_header(reader: &mut impl Read) -> Result<Header, CedarPersistenceError> {
            let mut magic = [0; 8];
            read_exact(reader, &mut magic)?;
            if magic != MAGIC {
                return Err(CedarPersistenceError::InvalidMagic);
            }
            let major = read_u16(reader)?;
            let minor = read_u16(reader)?;
            if major != MAJOR || minor > MINOR {
                return Err(CedarPersistenceError::UnsupportedVersion { major, minor });
            }
            let byte_order = read_u8(reader)?;
            if byte_order != LITTLE_ENDIAN {
                return Err(CedarPersistenceError::UnsupportedByteOrder { byte_order });
            }
            let layout = read_u8(reader)?;
            if layout != LAYOUT {
                return Err(CedarPersistenceError::LayoutMismatch {
                    expected: LAYOUT,
                    found: layout,
                });
            }
            let flags = read_u16(reader)?;
            if flags & !FLAG_ORDERED != 0 {
                return Err(CedarPersistenceError::InvalidHeader {
                    field: "flags",
                    value: u64::from(flags),
                });
            }
            Ok(Header {
                flags,
                max_trial: read_i32(reader)?,
                blocks_head_full: read_i32(reader)?,
                blocks_head_closed: read_i32(reader)?,
                blocks_head_open: read_i32(reader)?,
                array_len: read_usize(reader, "array_len")?,
                n_infos_len: read_usize(reader, "n_infos_len")?,
                blocks_len: read_usize(reader, "blocks_len")?,
                reject_len: read_usize(reader, "reject_len")?,
                capacity: read_usize(reader, "capacity")?,
                size: read_usize(reader, "size")?,
                entries: read_usize(reader, "entries")?,
            })
        }

        pub fn decode(reader: &mut impl Read, memory_limit: usize) -> Result<Decoded, CedarPersistenceError> {
            let header = read_header(reader)?;
            if header.array_len != header.n_infos_len || header.array_len != header.capacity {
                return Err(CedarPersistenceError::InvalidHeader {
                    field: "node_vector_lengths",
                    value: header.array_len as u64,
                });
            }
            if header.capacity < 256
                || !header.capacity.is_power_of_two()
                || header.capacity % 256 != 0
                || header.capacity > i32::MAX as usize
            {
                return Err(CedarPersistenceError::InvalidHeader {
                    field: "capacity",
                    value: header.capacity as u64,
                });
            }
            if header.size < 256 || header.size > header.capacity || header.size % 256 != 0 {
                return Err(CedarPersistenceError::InvalidHeader {
                    field: "size",
                    value: header.size as u64,
                });
            }
            if header.blocks_len != header.capacity / 256 || header.reject_len != 257 || header.entries > header.size {
                return Err(CedarPersistenceError::InvalidHeader {
                    field: "state_lengths",
                    value: header.blocks_len as u64,
                });
            }

            let one_copy_bytes = header
                .array_len
                .checked_mul(std::mem::size_of::<NodeRecord>())
                .and_then(|total| {
                    header
                        .n_infos_len
                        .checked_mul(std::mem::size_of::<NInfoRecord>())
                        .and_then(|bytes| total.checked_add(bytes))
                })
                .and_then(|total| {
                    header
                        .blocks_len
                        .checked_mul(std::mem::size_of::<BlockRecord>())
                        .and_then(|bytes| total.checked_add(bytes))
                })
                .and_then(|total| {
                    header
                        .reject_len
                        .checked_mul(std::mem::size_of::<i16>())
                        .and_then(|bytes| total.checked_add(bytes))
                })
                .ok_or(CedarPersistenceError::InvalidHeader {
                    field: "state_lengths",
                    value: u64::MAX,
                })?;
            let validation_bytes = header
                .size
                .checked_mul(std::mem::size_of::<bool>() * 2 + std::mem::size_of::<(usize, bool)>())
                .and_then(|total| total.checked_add(header.blocks_len * std::mem::size_of::<u8>()))
                .ok_or(CedarPersistenceError::InvalidHeader {
                    field: "validation_memory",
                    value: u64::MAX,
                })?;
            let peak_bytes = one_copy_bytes
                .checked_mul(2)
                .and_then(|total| total.checked_add(validation_bytes))
                .ok_or(CedarPersistenceError::InvalidHeader {
                    field: "peak_allocation",
                    value: u64::MAX,
                })?;
            if peak_bytes > memory_limit {
                return Err(CedarPersistenceError::AllocationLimitExceeded {
                    requested: peak_bytes,
                    limit: memory_limit,
                });
            }

            let mut nodes = Vec::new();
            reserve_exact(&mut nodes, header.array_len)?;
            for _ in 0..header.array_len {
                nodes.push(NodeRecord {
                    base: read_i32(reader)?,
                    check: read_i32(reader)?,
                });
            }
            let mut n_infos = Vec::new();
            reserve_exact(&mut n_infos, header.n_infos_len)?;
            for _ in 0..header.n_infos_len {
                n_infos.push(NInfoRecord {
                    sibling: read_u8(reader)?,
                    child: read_u8(reader)?,
                });
            }
            let mut blocks = Vec::new();
            reserve_exact(&mut blocks, header.blocks_len)?;
            for _ in 0..header.blocks_len {
                blocks.push(BlockRecord {
                    prev: read_i32(reader)?,
                    next: read_i32(reader)?,
                    num: read_i16(reader)?,
                    reject: read_i16(reader)?,
                    trial: read_i32(reader)?,
                    e_head: read_i32(reader)?,
                });
            }
            let mut reject = Vec::new();
            reserve_exact(&mut reject, header.reject_len)?;
            for _ in 0..header.reject_len {
                reject.push(read_i16(reader)?);
            }

            let mut trailing = [0; 1];
            match reader.read(&mut trailing) {
                Ok(0) => {}
                Ok(_) => return Err(CedarPersistenceError::TrailingData),
                Err(error) => return Err(CedarPersistenceError::Io(error)),
            }
            Ok(Decoded {
                header,
                nodes,
                n_infos,
                blocks,
                reject,
            })
        }
    }
}

/// Iterator for `common_prefix_search`
#[derive(Clone)]
pub struct PrefixIter<'a> {
    cedar: &'a Cedar,
    key: &'a [u8],
    from: usize,
    i: usize,
}

impl<'a> Iterator for PrefixIter<'a> {
    type Item = (i32, usize);

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, Some(self.key.len()))
    }

    fn next(&mut self) -> Option<Self::Item> {
        while self.i < self.key.len() {
            // Zero is the structural terminal label, not a traversable key byte. Stop here rather
            // than scanning the complete query up front: tokenizer callers create one suffix per
            // character boundary, so eager validation would turn a linear scan into O(n^2).
            if self.key[self.i] == 0 {
                break;
            }

            // Inline the single-byte traversal instead of calling find() with a 1-byte slice
            let from = self.from;

            #[cfg(feature = "reduced-trie")]
            {
                if self.cedar.array[from].base_ >= 0 {
                    break;
                }
            }

            let base = self.cedar.array[from].base();
            let to = (base ^ i32::from(self.key[self.i])) as usize;
            if self.cedar.array[to].check != (from as i32) {
                break;
            }

            self.from = to;
            self.i += 1;

            // Check for value at this position
            #[cfg(feature = "reduced-trie")]
            {
                if self.cedar.array[to].base_ >= 0 {
                    return Some((self.cedar.array[to].base_, self.i - 1));
                }
            }

            let terminal_base = self.cedar.array[to].base();
            let terminal = &self.cedar.array[terminal_base as usize];
            if terminal.check == (to as i32) && terminal.base_ != CEDAR_NO_VALUE {
                return Some((terminal.base_, self.i - 1));
            }
        }

        None
    }
}

/// Iterator for `common_prefix_predict`
#[derive(Clone)]
pub struct PrefixPredictIter<'a> {
    cedar: &'a Cedar,
    key: &'a [u8],
    started: bool,
    from: usize,
    p: usize,
    root: usize,
    value: Option<i32>,
    valid: bool,
}

impl<'a> PrefixPredictIter<'a> {
    fn next_until_none(&mut self) -> Option<(i32, usize)> {
        if let Some(value) = self.value {
            let result = (value, self.p);

            let (v_, from_, p_) = self.cedar.next(self.from, self.p, self.root);
            self.from = from_;
            self.p = p_;
            self.value = v_;

            Some(result)
        } else {
            None
        }
    }
}

impl<'a> Iterator for PrefixPredictIter<'a> {
    type Item = (i32, usize);

    fn next(&mut self) -> Option<Self::Item> {
        if !self.valid {
            return None;
        }
        if !self.started {
            self.started = true;
            // To locate the prefix's position first, if it doesn't exist then that means we
            // don't have do anything. `from` would serve as the cursor.
            if self.cedar.find(self.key, &mut self.from).is_some() {
                self.root = self.from;

                let (v_, from_, p_) = self.cedar.begin(self.from, self.p);
                self.from = from_;
                self.p = p_;
                self.value = v_;

                self.next_until_none()
            } else {
                None
            }
        } else {
            self.next_until_none()
        }
    }
}

/// Iterator over every stored byte key and value.
pub struct Entries<'a> {
    cedar: &'a Cedar,
    stack: Vec<(usize, Vec<u8>)>,
    remaining: usize,
}

impl Iterator for Entries<'_> {
    type Item = (Vec<u8>, i32);

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.remaining, Some(self.remaining))
    }

    fn next(&mut self) -> Option<Self::Item> {
        while let Some((index, key)) = self.stack.pop() {
            let base = self.cedar.array[index].base();
            let children = self.cedar.child_labels(index);
            for label in children.into_iter().rev() {
                let child = (base ^ i32::from(label)) as usize;
                let mut child_key = key.clone();
                child_key.push(label);
                self.stack.push((child, child_key));
            }

            if let Some(value) = self.cedar.value_at_node(index) {
                self.remaining -= 1;
                return Some((key, value));
            }
        }
        debug_assert_eq!(self.remaining, 0);
        None
    }
}

impl ExactSizeIterator for Entries<'_> {}

/// UTF-8 adapter for [`Entries`]. Invalid byte keys are returned as [`FromUtf8Error`] values.
pub struct Utf8Entries<'a> {
    inner: Entries<'a>,
}

impl Iterator for Utf8Entries<'_> {
    type Item = Result<(String, i32), FromUtf8Error>;

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }

    fn next(&mut self) -> Option<Self::Item> {
        self.inner
            .next()
            .map(|(key, value)| String::from_utf8(key).map(|key| (key, value)))
    }
}

impl Cedar {
    /// Creates an empty trie using [`CedarBuilder`]'s defaults.
    pub fn new() -> Self {
        CedarBuilder::new().build()
    }

    /// Directly constructs a trie from strictly byte-sorted, unique UTF-8 keys.
    ///
    /// Unlike [`Cedar::build`], this constructor allocates every sibling set together instead of
    /// incrementally resolving conflicts for each key. The complete input is validated before any
    /// trie storage is allocated. Empty input constructs an empty trie.
    pub fn from_sorted(key_values: &[(&str, i32)]) -> Result<Self, CedarError> {
        CedarBuilder::new().from_sorted(key_values)
    }

    /// Directly constructs a trie from strictly sorted, unique byte keys.
    ///
    /// Keys are ordered by their raw bytes. Every key must be nonempty and contain no `0x00`, and
    /// every value must be in [`MIN_VALUE`] through [`MAX_VALUE`]. Invalid input returns a typed
    /// error without constructing a partial trie.
    pub fn from_sorted_bytes(key_values: &[(&[u8], i32)]) -> Result<Self, CedarError> {
        CedarBuilder::new().from_sorted_bytes(key_values)
    }

    /// Returns a configurable builder for an empty trie.
    pub const fn builder() -> CedarBuilder {
        CedarBuilder::new()
    }

    fn with_config(ordered: bool, max_trial: i32) -> Self {
        let mut array: Vec<Node> = Vec::with_capacity(256);
        let n_infos: Vec<NInfo> = vec![NInfo::default(); 256];
        let mut blocks: Vec<Block> = vec![Block::new(); 1];
        let reject: Vec<i16> = (0..=256).map(|i| i + 1).collect();

        #[cfg(feature = "reduced-trie")]
        array.push(Node { base_: -1, check: -1 });
        #[cfg(not(feature = "reduced-trie"))]
        array.push(Node { base_: 0, check: -1 });

        for i in 1..256 {
            // make `base_` point to the previous element, and make `check` point to the next element
            array.push(Node {
                base_: -(i - 1),
                check: -(i + 1),
            })
        }

        // make them link as a cyclic doubly-linked list
        array[1].base_ = -255;
        array[255].check = -1;

        blocks[0].e_head = 1;

        Cedar {
            array,
            n_infos,
            blocks,
            reject,
            blocks_head_full: 0,
            blocks_head_closed: 0,
            blocks_head_open: 0,
            capacity: 256,
            size: 256,
            entries: 0,
            ordered,
            max_trial,
        }
    }

    /// Returns the number of stored key/value entries.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.entries
    }

    /// Returns `true` when no key/value entries are stored.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.entries == 0
    }

    /// Returns the number of occupied double-array slots, including the root.
    #[must_use]
    pub fn used_node_slots(&self) -> usize {
        let free_slots = self
            .blocks
            .iter()
            .take(self.size >> 8)
            .map(|block| block.num as usize)
            .sum::<usize>();
        1 + self.size - free_slots
    }

    /// Returns the allocated element capacity of the double-array node vector.
    #[must_use]
    pub fn node_capacity(&self) -> usize {
        self.array.capacity()
    }

    /// Returns bytes reserved by all owned vectors, excluding `Cedar` itself and allocator
    /// bookkeeping.
    #[must_use]
    pub fn allocated_bytes(&self) -> usize {
        self.array.capacity() * mem::size_of::<Node>()
            + self.n_infos.capacity() * mem::size_of::<NInfo>()
            + self.blocks.capacity() * mem::size_of::<Block>()
            + self.reject.capacity() * mem::size_of::<i16>()
    }

    /// Returns occupied node slots divided by allocated node capacity.
    #[must_use]
    pub fn load_factor(&self) -> f64 {
        if self.node_capacity() == 0 {
            0.0
        } else {
            self.used_node_slots() as f64 / self.node_capacity() as f64
        }
    }

    /// Returns all public memory and occupancy metrics in one snapshot.
    #[must_use]
    pub fn memory_stats(&self) -> MemoryStats {
        MemoryStats {
            entries: self.len(),
            used_node_slots: self.used_node_slots(),
            node_capacity: self.node_capacity(),
            allocated_bytes: self.allocated_bytes(),
            load_factor: self.load_factor(),
        }
    }

    /// Writes this trie using cedarwood's stable, versioned little-endian binary format.
    ///
    /// The representation preserves insertion configuration and allocator state, so a loaded trie
    /// remains mutable. Rust struct memory is never written directly.
    #[cfg(feature = "std")]
    pub fn save_to_writer(&self, mut writer: impl std::io::Write) -> Result<(), CedarPersistenceError> {
        use persistence::v1;

        let flags = if self.ordered { v1::FLAG_ORDERED } else { 0 };
        v1::write_header(
            &mut writer,
            v1::Header {
                flags,
                max_trial: self.max_trial,
                blocks_head_full: self.blocks_head_full,
                blocks_head_closed: self.blocks_head_closed,
                blocks_head_open: self.blocks_head_open,
                array_len: self.array.len(),
                n_infos_len: self.n_infos.len(),
                blocks_len: self.blocks.len(),
                reject_len: self.reject.len(),
                capacity: self.capacity,
                size: self.size,
                entries: self.entries,
            },
        )?;

        for node in &self.array {
            let record = v1::NodeRecord {
                base: node.base_,
                check: node.check,
            };
            v1::write_i32(&mut writer, record.base)?;
            v1::write_i32(&mut writer, record.check)?;
        }
        for info in &self.n_infos {
            let record = v1::NInfoRecord {
                sibling: info.sibling,
                child: info.child,
            };
            writer.write_all(&[record.sibling, record.child])?;
        }
        for block in &self.blocks {
            let record = v1::BlockRecord {
                prev: block.prev,
                next: block.next,
                num: block.num,
                reject: block.reject,
                trial: block.trial,
                e_head: block.e_head,
            };
            v1::write_i32(&mut writer, record.prev)?;
            v1::write_i32(&mut writer, record.next)?;
            v1::write_i16(&mut writer, record.num)?;
            v1::write_i16(&mut writer, record.reject)?;
            v1::write_i32(&mut writer, record.trial)?;
            v1::write_i32(&mut writer, record.e_head)?;
        }
        for value in &self.reject {
            v1::write_i16(&mut writer, *value)?;
        }
        Ok(())
    }

    /// Saves this trie to a file by truncating or creating it.
    ///
    /// This helper is not atomic: an I/O failure can leave a partial file. For atomic replacement,
    /// write with [`Cedar::save_to_writer`] to an application-managed temporary file, flush and
    /// sync it as required, then rename it according to the platform's durability needs.
    #[cfg(feature = "std")]
    pub fn save_to_path(&self, path: impl AsRef<std::path::Path>) -> Result<(), CedarPersistenceError> {
        self.save_to_writer(std::fs::File::create(path)?)
    }

    /// Loads and validates a trie using [`DEFAULT_LOAD_MEMORY_LIMIT`].
    ///
    /// Validation completes before the `Cedar` is returned, so malformed input can never reach
    /// the unchecked query paths through this API.
    #[cfg(feature = "std")]
    pub fn load_from_reader(reader: impl std::io::Read) -> Result<Self, CedarPersistenceError> {
        Self::load_from_reader_with_limit(reader, DEFAULT_LOAD_MEMORY_LIMIT)
    }

    /// Loads and validates a trie while limiting total reserved vector storage.
    #[cfg(feature = "std")]
    pub fn load_from_reader_with_limit(
        mut reader: impl std::io::Read,
        memory_limit: usize,
    ) -> Result<Self, CedarPersistenceError> {
        let decoded = persistence::v1::decode(&mut reader, memory_limit)?;

        Self::from_persistence_v1(decoded)
    }

    /// Loads and validates a trie from a file using [`DEFAULT_LOAD_MEMORY_LIMIT`].
    #[cfg(feature = "std")]
    pub fn load_from_path(path: impl AsRef<std::path::Path>) -> Result<Self, CedarPersistenceError> {
        Self::load_from_reader(std::fs::File::open(path)?)
    }

    #[cfg(feature = "std")]
    fn from_persistence_v1(decoded: persistence::v1::Decoded) -> Result<Self, CedarPersistenceError> {
        use persistence::v1;

        let header = decoded.header;
        let mut array = Vec::new();
        v1::reserve_exact(&mut array, decoded.nodes.len())?;
        array.extend(decoded.nodes.into_iter().map(|record| Node {
            base_: record.base,
            check: record.check,
        }));
        let mut n_infos = Vec::new();
        v1::reserve_exact(&mut n_infos, decoded.n_infos.len())?;
        n_infos.extend(decoded.n_infos.into_iter().map(|record| NInfo {
            sibling: record.sibling,
            child: record.child,
        }));
        let mut blocks = Vec::new();
        v1::reserve_exact(&mut blocks, decoded.blocks.len())?;
        blocks.extend(decoded.blocks.into_iter().map(|record| Block {
            prev: record.prev,
            next: record.next,
            num: record.num,
            reject: record.reject,
            trial: record.trial,
            e_head: record.e_head,
        }));
        let mut reject = Vec::new();
        v1::reserve_exact(&mut reject, decoded.reject.len())?;
        reject.extend(decoded.reject);

        let unvalidated = UnvalidatedCedar(Cedar {
            array,
            n_infos,
            blocks,
            reject,
            blocks_head_full: header.blocks_head_full,
            blocks_head_closed: header.blocks_head_closed,
            blocks_head_open: header.blocks_head_open,
            capacity: header.capacity,
            size: header.size,
            entries: header.entries,
            ordered: header.flags & v1::FLAG_ORDERED != 0,
            max_trial: header.max_trial,
        });
        unvalidated.validate()
    }

    #[cfg(feature = "std")]
    fn corrupt(section: &'static str, index: usize, reason: &'static str) -> CedarPersistenceError {
        CedarPersistenceError::CorruptData { section, index, reason }
    }

    #[cfg(feature = "std")]
    fn validate_loaded_state(&self) -> Result<(), CedarPersistenceError> {
        if self.max_trial <= 0 {
            return Err(CedarPersistenceError::InvalidHeader {
                field: "max_trial",
                value: self.max_trial as u32 as u64,
            });
        }
        if self.capacity != self.array.len()
            || self.n_infos.len() != self.capacity
            || self.capacity < 256
            || !self.capacity.is_power_of_two()
            || self.capacity % 256 != 0
            || self.capacity > i32::MAX as usize
        {
            return Err(Self::corrupt("vectors", 0, "invalid node vector relationship"));
        }
        if self.size < 256 || self.size > self.capacity || self.size % 256 != 0 {
            return Err(Self::corrupt("vectors", 0, "invalid active node length"));
        }
        if self.blocks.len() != self.capacity / 256 {
            return Err(Self::corrupt("vectors", 0, "block vector length does not match nodes"));
        }
        if self.reject.len() != 257 {
            return Err(Self::corrupt("reject", 0, "reject table must contain 257 values"));
        }
        if self.entries > self.size {
            return Err(Self::corrupt("entries", 0, "entry count exceeds active nodes"));
        }
        #[cfg(feature = "reduced-trie")]
        let valid_root_base = self.array[0].base_ == -1;
        #[cfg(not(feature = "reduced-trie"))]
        let valid_root_base = self.array[0].base_ == 0;
        if self.array[0].check != -1 || !valid_root_base {
            return Err(Self::corrupt("nodes", 0, "invalid root sentinel"));
        }

        for index in self.size..self.capacity {
            if self.array[index].base_ != 0
                || self.array[index].check != 0
                || self.n_infos[index].child != 0
                || self.n_infos[index].sibling != 0
            {
                return Err(Self::corrupt("nodes", index, "inactive node is not zero initialized"));
            }
        }
        let active_blocks = self.size / 256;
        for index in active_blocks..self.blocks.len() {
            let block = &self.blocks[index];
            if block.prev != 0
                || block.next != 0
                || block.num != 256
                || block.reject != 257
                || block.trial != 0
                || block.e_head != 0
            {
                return Err(Self::corrupt("blocks", index, "inactive block is not initialized"));
            }
        }

        for (index, value) in self.reject.iter().copied().enumerate() {
            if value < 1 || i32::from(value) > index as i32 + 1 {
                return Err(Self::corrupt("reject", index, "value is outside its heuristic range"));
            }
        }

        let mut free_seen = Vec::new();
        persistence::v1::reserve_exact(&mut free_seen, self.size)?;
        free_seen.resize(self.size, false);
        for block_index in 0..active_blocks {
            let block = &self.blocks[block_index];
            let minimum = if block_index == 0 { 1 } else { 0 };
            if i32::from(block.num) < minimum || block.num > 256 {
                return Err(Self::corrupt("blocks", block_index, "invalid free-slot count"));
            }
            if block.reject < 1 || block.reject > 257 {
                return Err(Self::corrupt("blocks", block_index, "invalid rejection threshold"));
            }
            if block.trial < 0 || block.trial > self.max_trial {
                return Err(Self::corrupt("blocks", block_index, "invalid trial count"));
            }

            let free_count = block.num as usize - usize::from(block_index == 0);
            if free_count == 0 {
                continue;
            }
            let block_start = block_index * 256;
            let block_end = block_start + 256;
            let head = usize::try_from(block.e_head)
                .ok()
                .filter(|head| (*head >= block_start + usize::from(block_index == 0)) && *head < block_end)
                .ok_or_else(|| Self::corrupt("blocks", block_index, "free-list head is outside its block"))?;
            let mut current = head;
            for position in 0..free_count {
                if free_seen[current] {
                    return Err(Self::corrupt("free-list", current, "node appears more than once"));
                }
                let node = self.array[current];
                if node.base_ >= 0 || node.check >= 0 {
                    return Err(Self::corrupt("free-list", current, "free node has nonnegative link"));
                }
                let previous = usize::try_from(-i64::from(node.base_))
                    .ok()
                    .filter(|value| *value >= block_start && *value < block_end)
                    .ok_or_else(|| Self::corrupt("free-list", current, "previous link leaves block"))?;
                let next = usize::try_from(-i64::from(node.check))
                    .ok()
                    .filter(|value| *value >= block_start && *value < block_end)
                    .ok_or_else(|| Self::corrupt("free-list", current, "next link leaves block"))?;
                if self.array[previous].check != -(current as i32) || self.array[next].base_ != -(current as i32) {
                    return Err(Self::corrupt("free-list", current, "links are not reciprocal"));
                }
                free_seen[current] = true;
                current = next;
                if current == head && position + 1 != free_count {
                    return Err(Self::corrupt("free-list", head, "cycle is shorter than block count"));
                }
            }
            if current != head {
                return Err(Self::corrupt("free-list", head, "cycle is longer than block count"));
            }
        }

        for (index, is_free) in free_seen.iter().copied().enumerate().skip(1) {
            if (self.array[index].check < 0) != is_free {
                return Err(Self::corrupt("free-list", index, "free-node membership mismatch"));
            }
            if is_free && (self.n_infos[index].child != 0 || self.n_infos[index].sibling != 0) {
                return Err(Self::corrupt("n_infos", index, "free node retains trie links"));
            }
        }

        self.validate_block_lists(active_blocks)?;
        self.validate_trie_graph(&free_seen)
    }

    #[cfg(feature = "std")]
    fn validate_block_lists(&self, active_blocks: usize) -> Result<(), CedarPersistenceError> {
        if self.blocks[0].trial != 0 {
            return Err(Self::corrupt("blocks", 0, "root block has trial metadata"));
        }
        let mut membership = Vec::new();
        persistence::v1::reserve_exact(&mut membership, active_blocks)?;
        membership.resize(active_blocks, 0_u8);
        let mut full_sentinel_seen = false;
        for (kind, head) in [
            (1_u8, self.blocks_head_open),
            (2_u8, self.blocks_head_closed),
            (3_u8, self.blocks_head_full),
        ] {
            if head == 0 {
                continue;
            }
            let head = usize::try_from(head)
                .ok()
                .filter(|head| *head > 0 && *head < active_blocks)
                .ok_or_else(|| Self::corrupt("block-lists", 0, "head is outside active blocks"))?;
            let mut current = head;
            loop {
                if current == 0 {
                    if kind != 3 || full_sentinel_seen {
                        return Err(Self::corrupt("block-lists", 0, "invalid block-zero sentinel"));
                    }
                    full_sentinel_seen = true;
                } else {
                    if membership[current] != 0 {
                        return Err(Self::corrupt("block-lists", current, "block occurs in multiple lists"));
                    }
                    membership[current] = kind;
                }
                let block = &self.blocks[current];
                let previous = usize::try_from(block.prev)
                    .ok()
                    .filter(|value| *value < active_blocks && (kind == 3 || *value > 0))
                    .ok_or_else(|| Self::corrupt("block-lists", current, "previous block is invalid"))?;
                let next = usize::try_from(block.next)
                    .ok()
                    .filter(|value| *value < active_blocks && (kind == 3 || *value > 0))
                    .ok_or_else(|| Self::corrupt("block-lists", current, "next block is invalid"))?;
                if self.blocks[previous].next != current as i32 || self.blocks[next].prev != current as i32 {
                    return Err(Self::corrupt("block-lists", current, "links are not reciprocal"));
                }
                current = next;
                if current == head {
                    break;
                }
            }
        }

        if self.blocks_head_full == 0 {
            if self.blocks[0].prev != 0 || self.blocks[0].next != 0 || full_sentinel_seen {
                return Err(Self::corrupt("block-lists", 0, "unused full-list sentinel has links"));
            }
        } else if !full_sentinel_seen {
            return Err(Self::corrupt("block-lists", 0, "full list omits block-zero sentinel"));
        }

        for (index, actual) in membership.iter().copied().enumerate().skip(1) {
            let block = &self.blocks[index];
            let expected = if block.num == 0 {
                3
            } else if block.num == 1 || block.trial == self.max_trial {
                2
            } else {
                1
            };
            if actual != expected {
                return Err(Self::corrupt("block-lists", index, "block is in the wrong category"));
            }
        }
        Ok(())
    }

    #[cfg(feature = "std")]
    fn validate_trie_graph(&self, free_seen: &[bool]) -> Result<(), CedarPersistenceError> {
        let mut reached = Vec::new();
        persistence::v1::reserve_exact(&mut reached, self.size)?;
        reached.resize(self.size, false);
        let mut stack = Vec::new();
        persistence::v1::reserve_exact(&mut stack, self.size)?;
        stack.push((0_usize, false));
        let mut entry_count = 0_usize;

        while let Some((index, terminal)) = stack.pop() {
            if reached[index] {
                return Err(Self::corrupt("trie", index, "node is reachable more than once"));
            }
            if free_seen[index] {
                return Err(Self::corrupt("trie", index, "free node is reachable"));
            }
            reached[index] = true;
            let node = self.array[index];

            if terminal {
                if !(MIN_VALUE..=MAX_VALUE).contains(&node.base_) || self.n_infos[index].child != 0 {
                    return Err(Self::corrupt("trie", index, "invalid terminal value node"));
                }
                entry_count += 1;
                continue;
            }

            #[cfg(feature = "reduced-trie")]
            let structural = if node.base_ < 0 {
                true
            } else if (MIN_VALUE..=MAX_VALUE).contains(&node.base_) {
                if self.n_infos[index].child != 0 {
                    return Err(Self::corrupt("trie", index, "inline value node has children"));
                }
                entry_count += 1;
                false
            } else if node.base_ == CEDAR_VALUE_LIMIT {
                if self.n_infos[index].child != 0 {
                    return Err(Self::corrupt("trie", index, "empty leaf has children"));
                }
                false
            } else {
                return Err(Self::corrupt("trie", index, "invalid reduced-layout sentinel"));
            };

            #[cfg(not(feature = "reduced-trie"))]
            let structural = if node.base_ == -1 {
                if self.n_infos[index].child != 0 {
                    return Err(Self::corrupt("trie", index, "leaf has children"));
                }
                false
            } else if node.base_ >= 0 {
                true
            } else {
                return Err(Self::corrupt("trie", index, "invalid default-layout base"));
            };

            if !structural {
                continue;
            }
            let base = usize::try_from(node.base())
                .ok()
                .filter(|base| *base < self.size)
                .ok_or_else(|| Self::corrupt("trie", index, "base is outside active nodes"))?;

            let mut label = if index == 0 {
                if self.n_infos[0].child != 0 {
                    return Err(Self::corrupt("trie", 0, "root child sentinel is not zero"));
                }
                self.n_infos[base].sibling
            } else {
                self.n_infos[index].child
            };
            let mut saw_labels = [false; 256];
            let mut child_count = 0_usize;
            if index != 0 && label == 0 && self.array[base].check == index as i32 {
                saw_labels[0] = true;
                stack.push((base, true));
                child_count += 1;
                label = self.n_infos[base].sibling;
            }

            let mut previous = 0_u8;
            while label != 0 {
                if saw_labels[label as usize] {
                    return Err(Self::corrupt("siblings", index, "sibling chain contains a cycle"));
                }
                if self.ordered && previous != 0 && label <= previous {
                    return Err(Self::corrupt("siblings", index, "ordered sibling chain is not sorted"));
                }
                saw_labels[label as usize] = true;
                let child = base ^ usize::from(label);
                if child >= self.size || free_seen[child] || self.array[child].check != index as i32 {
                    return Err(Self::corrupt("siblings", index, "child ownership link is invalid"));
                }
                stack.push((child, false));
                child_count += 1;
                previous = label;
                label = self.n_infos[child].sibling;
            }
            if child_count == 0 && !(index == 0 && self.entries == 0) {
                return Err(Self::corrupt("trie", index, "structural node has no children"));
            }
        }

        for index in 0..self.size {
            if !free_seen[index] && !reached[index] {
                return Err(Self::corrupt("trie", index, "occupied node is unreachable"));
            }
        }
        if entry_count != self.entries {
            return Err(Self::corrupt("entries", 0, "entry count does not match trie values"));
        }
        Ok(())
    }

    /// SAFETY: `i` must be a valid index into `self.array`. Query callers derive indices from an
    /// occupied node's base and one byte label; allocation reserves complete 256-slot blocks, so
    /// XOR with a byte remains inside the allocated array. Parent `check` links and query cursors
    /// always refer to occupied slots. Debug builds retain the bounds assertion below.
    #[inline(always)]
    unsafe fn node_unchecked(&self, i: usize) -> &Node {
        debug_assert!(
            i < self.array.len(),
            "node_unchecked: index {} out of bounds (len {})",
            i,
            self.array.len()
        );
        self.array.get_unchecked(i)
    }

    /// SAFETY: `i` must be a valid index into `self.n_infos`. `n_infos` is initialized and grown in
    /// lockstep with `array`; query callers use either a validated node index or an occupied node's
    /// base XOR a recorded child label. Debug builds retain the bounds assertion below.
    #[inline(always)]
    unsafe fn ninfo_unchecked(&self, i: usize) -> &NInfo {
        debug_assert!(
            i < self.n_infos.len(),
            "ninfo_unchecked: index {} out of bounds (len {})",
            i,
            self.n_infos.len()
        );
        self.n_infos.get_unchecked(i)
    }

    fn validate_entry(key: &[u8], value: i32) -> Result<(), CedarError> {
        if key.is_empty() {
            return Err(CedarError::EmptyKey);
        }
        if let Some(position) = key.iter().position(|byte| *byte == 0) {
            return Err(CedarError::NulByte { position });
        }
        if !(MIN_VALUE..=MAX_VALUE).contains(&value) {
            return Err(CedarError::InvalidValue { value });
        }
        Ok(())
    }

    fn validate_sorted_entries<K: AsRef<[u8]>>(key_values: &[(K, i32)]) -> Result<(), CedarError> {
        for (index, (key, value)) in key_values.iter().enumerate() {
            let key = key.as_ref();
            Self::validate_entry(key, *value)?;
            if index != 0 {
                match key_values[index - 1].0.as_ref().cmp(key) {
                    core::cmp::Ordering::Less => {}
                    core::cmp::Ordering::Equal => return Err(CedarError::DuplicateKey { index }),
                    core::cmp::Ordering::Greater => return Err(CedarError::KeysNotSorted { index }),
                }
            }
        }
        Ok(())
    }

    // Build an already validated sorted range by allocating each node's complete sibling set in
    // one operation. This intentionally does not use update_/follow/resolve: the sorted ranges
    // expose every child label before allocation, so conflicts never need to be introduced.
    fn build_sorted_validated<K: AsRef<[u8]>>(&mut self, key_values: &[(K, i32)]) {
        if key_values.is_empty() {
            return;
        }

        // (parent node, first key, one-past-last key, shared prefix length)
        let mut pending = vec![(0_usize, 0_usize, key_values.len(), 0_usize)];

        while let Some((from, start, end, depth)) = pending.pop() {
            let terminal = (key_values[start].0.as_ref().len() == depth).then_some(key_values[start].1);

            #[cfg(feature = "reduced-trie")]
            if end == start + 1 {
                if let Some(value) = terminal {
                    self.array[from].base_ = value;
                    continue;
                }
            }

            let mut labels = SmallVec::<[u8; 256]>::new();
            let mut cursor = start;
            if terminal.is_some() {
                labels.push(0);
                cursor += 1;
            }

            while cursor < end {
                let label = key_values[cursor].0.as_ref()[depth];
                labels.push(label);
                cursor += 1;
                while cursor < end
                    && key_values[cursor].0.as_ref().len() > depth
                    && key_values[cursor].0.as_ref()[depth] == label
                {
                    cursor += 1;
                }
            }

            let base = self.allocate_bulk_siblings(from, &labels);
            if let Some(value) = terminal {
                self.array[base as usize].base_ = value;
            }

            cursor = start + usize::from(terminal.is_some());
            while cursor < end {
                let child_start = cursor;
                let label = key_values[cursor].0.as_ref()[depth];
                cursor += 1;
                while cursor < end
                    && key_values[cursor].0.as_ref().len() > depth
                    && key_values[cursor].0.as_ref()[depth] == label
                {
                    cursor += 1;
                }
                pending.push(((base ^ i32::from(label)) as usize, child_start, cursor, depth + 1));
            }
        }

        self.entries = key_values.len();
    }

    fn allocate_bulk_siblings(&mut self, from: usize, labels: &[u8]) -> i32 {
        debug_assert!(!labels.is_empty());
        debug_assert!(labels.windows(2).all(|pair| pair[0] < pair[1]));

        // The root owns block zero at base zero. All other sibling sets use the ordinary block
        // allocator, which updates free-list membership and Block::num exactly as incremental
        // insertion does.
        let base = if from == 0 {
            0
        } else {
            let first = if labels.len() == 1 {
                self.find_place()
            } else {
                self.find_places(labels)
            };
            first ^ i32::from(labels[0])
        };

        #[cfg(feature = "reduced-trie")]
        {
            self.array[from].base_ = -base - 1;
        }
        #[cfg(not(feature = "reduced-trie"))]
        {
            self.array[from].base_ = base;
        }

        if from == 0 {
            // Root label zero is reserved as the traversal anchor. Incremental insertion keeps the
            // first real root label in the root slot's sibling field for erase/predict traversal.
            self.n_infos[from].child = 0;
            self.n_infos[base as usize].sibling = labels[0];
        } else {
            self.n_infos[from].child = labels[0];
        }
        for (index, label) in labels.iter().copied().enumerate() {
            let to = self.pop_e_node(base, label, from as i32);
            self.n_infos[to as usize].sibling = labels.get(index + 1).copied().unwrap_or(0);
        }
        base
    }

    /// Inserts all UTF-8 key/value pairs after validating the complete input.
    ///
    /// If validation fails, the trie is left unchanged. Duplicate keys overwrite earlier values.
    pub fn build(&mut self, key_values: &[(&str, i32)]) -> Result<(), CedarError> {
        for (key, value) in key_values {
            Self::validate_entry(key.as_bytes(), *value)?;
        }
        for (key, value) in key_values {
            self.update_validated(key.as_bytes(), *value);
        }
        Ok(())
    }

    /// Inserts all byte key/value pairs after validating the complete input.
    ///
    /// Keys must be non-empty and must not contain byte `0`, which is cedar's terminal label.
    pub fn build_bytes(&mut self, key_values: &[(&[u8], i32)]) -> Result<(), CedarError> {
        for (key, value) in key_values {
            Self::validate_entry(key, *value)?;
        }
        for (key, value) in key_values {
            self.update_validated(key, *value);
        }
        Ok(())
    }

    /// Inserts or overwrites a UTF-8 key.
    pub fn update(&mut self, key: &str, value: i32) -> Result<(), CedarError> {
        self.update_bytes(key.as_bytes(), value)
    }

    /// Inserts or overwrites a byte key.
    ///
    /// Values must be in [`MIN_VALUE`] through [`MAX_VALUE`]. Keys must be non-empty and cannot
    /// contain byte `0`, which is reserved as the terminal label.
    pub fn update_bytes(&mut self, key: &[u8], value: i32) -> Result<(), CedarError> {
        Self::validate_entry(key, value)?;
        self.update_validated(key, value);
        Ok(())
    }

    fn update_validated(&mut self, key: &[u8], value: i32) {
        let is_new = self.exact_match_search_bytes(key).is_none();
        self.update_(key, value, 0, 0);
        if is_new {
            self.entries += 1;
        }
    }

    // Internal update interface for a validated byte key and cursor.
    fn update_(&mut self, key: &[u8], value: i32, mut from: usize, mut pos: usize) -> i32 {
        while pos < key.len() {
            #[cfg(feature = "reduced-trie")]
            {
                let val_ = self.array[from].base_;
                if val_ >= 0 && val_ != CEDAR_VALUE_LIMIT {
                    let to = self.follow(from, 0);
                    self.array[to as usize].base_ = val_;
                }
            }

            from = self.follow(from, key[pos]) as usize;
            pos += 1;
        }

        #[cfg(feature = "reduced-trie")]
        let to = if self.array[from].base_ >= 0 {
            from as i32
        } else {
            self.follow(from, 0)
        };

        #[cfg(feature = "reduced-trie")]
        {
            if self.array[to as usize].base_ == CEDAR_VALUE_LIMIT {
                self.array[to as usize].base_ = 0;
            }
        }

        #[cfg(not(feature = "reduced-trie"))]
        let to = self.follow(from, 0);

        self.array[to as usize].base_ = value;
        self.array[to as usize].base_
    }

    // To move in the trie by following the `label`, and insert the node if the node is not there,
    // it is used by the `update` to populate the trie.
    #[inline]
    fn follow(&mut self, from: usize, label: u8) -> i32 {
        let base = self.array[from].base();

        let mut to;

        // the node is not there
        if base < 0 || self.array[(base ^ i32::from(label)) as usize].check < 0 {
            // allocate a e node
            to = self.pop_e_node(base, label, from as i32);
            let branch: i32 = to ^ i32::from(label);

            // maintain the info in ninfo
            self.push_sibling(from, branch, label, base >= 0);
        } else {
            // the node is already there and the ownership is not `from`, therefore a conflict.
            to = base ^ i32::from(label);
            if self.array[to as usize].check != (from as i32) {
                // call `resolve` to relocate.
                to = self.resolve(from, base, label);
            }
        }

        to
    }

    // Find key from double array trie, with `from` as the cursor to traverse the nodes.
    //
    // SAFETY: The inner loop uses unchecked indexing for performance (this loop runs once per byte
    // of the key and is the primary query bottleneck). Indices are valid by trie structural
    // invariants: `from` starts at the root or the previously ownership-checked child; an occupied
    // branch's base points into a complete 256-slot block, so XOR with a byte remains allocated.
    // In reduced layout, a nonnegative base is a terminal value and traversal stops before using it
    // as an index. Debug builds retain bounds assertions in the helpers.
    #[inline]
    fn find(&self, key: &[u8], from: &mut usize) -> Option<i32> {
        let mut pos = 0;

        while pos < key.len() {
            #[cfg(feature = "reduced-trie")]
            {
                if unsafe { self.node_unchecked(*from) }.base_ >= 0 {
                    break;
                }
            }

            let to = (unsafe { self.node_unchecked(*from) }.base() ^ i32::from(key[pos])) as usize;
            if unsafe { self.node_unchecked(to) }.check != (*from as i32) {
                return None;
            }

            *from = to;
            pos += 1;
        }

        #[cfg(feature = "reduced-trie")]
        {
            if self.array[*from].base_ >= 0 {
                if pos == key.len() {
                    return Some(self.array[*from].base_);
                } else {
                    return None;
                }
            }
        }

        // return the value of the node if `check` is correctly marked for the ownership, otherwise
        // it means no value is stored.
        let n = &self.array[(self.array[*from].base()) as usize];
        if n.check != (*from as i32) {
            Some(CEDAR_NO_VALUE)
        } else {
            Some(n.base_)
        }
    }

    /// Deletes a UTF-8 key, returning whether an entry was removed.
    pub fn erase(&mut self, key: &str) -> bool {
        self.erase_bytes(key.as_bytes())
    }

    /// Deletes a byte key, returning whether an entry was removed.
    ///
    /// Empty keys and keys containing byte `0` are not representable and return `false`.
    pub fn erase_bytes(&mut self, key: &[u8]) -> bool {
        if key.is_empty() || key.contains(&0) {
            return false;
        }
        let mut from = 0;

        // move the cursor to the right place and use erase__ to delete it.
        if let Some(v) = self.find(key, &mut from) {
            if v != CEDAR_NO_VALUE {
                self.erase__(from);
                self.entries -= 1;
                return true;
            }
        }
        false
    }

    fn erase__(&mut self, mut from: usize) {
        #[cfg(feature = "reduced-trie")]
        let mut e: i32 = if self.array[from].base_ >= 0 {
            from as i32
        } else {
            self.array[from].base()
        };

        #[cfg(feature = "reduced-trie")]
        {
            from = self.array[e as usize].check as usize;
        }

        #[cfg(not(feature = "reduced-trie"))]
        let mut e = self.array[from].base();

        loop {
            let base = self.array[from].base();
            let has_sibling = self.n_infos[(base ^ i32::from(self.n_infos[from].child)) as usize].sibling != 0;

            // if the node has siblings, then remove `e` from the sibling.
            if has_sibling {
                self.pop_sibling(from as i32, base, (base ^ e) as u8);
            }

            // maintain the data structures.
            self.push_e_node(e);
            e = from as i32;

            // traverse to the parent.
            from = self.array[from].check as usize;

            // if it has sibling then this layer has more than one nodes, then we are done.
            if has_sibling {
                break;
            }
        }
    }

    /// Looks up an exact UTF-8 key.
    pub fn exact_match_search(&self, key: &str) -> Option<(i32, usize)> {
        self.exact_match_search_bytes(key.as_bytes())
    }

    /// Looks up an exact byte key.
    ///
    /// Empty keys and keys containing byte `0` are not representable and return `None`.
    pub fn exact_match_search_bytes(&self, key: &[u8]) -> Option<(i32, usize)> {
        if key.is_empty() || key.contains(&0) {
            return None;
        }
        let mut from = 0;

        if let Some(value) = self.find(key, &mut from) {
            if value == CEDAR_NO_VALUE {
                return None;
            }

            Some((value, key.len()))
        } else {
            None
        }
    }

    /// Iterates over stored keys that are prefixes of a UTF-8 query.
    pub fn common_prefix_iter<'a>(&'a self, key: &'a str) -> PrefixIter<'a> {
        self.common_prefix_iter_bytes(key.as_bytes())
    }

    /// Iterates over stored keys that are prefixes of a byte query.
    pub fn common_prefix_iter_bytes<'a>(&'a self, key: &'a [u8]) -> PrefixIter<'a> {
        PrefixIter {
            cedar: self,
            key,
            from: 0,
            i: 0,
        }
    }

    /// Collects stored keys that are prefixes of a UTF-8 query.
    pub fn common_prefix_search(&self, key: &str) -> Vec<(i32, usize)> {
        self.common_prefix_search_bytes(key.as_bytes())
    }

    /// Collects stored keys that are prefixes of a byte query.
    pub fn common_prefix_search_bytes(&self, key: &[u8]) -> Vec<(i32, usize)> {
        self.common_prefix_iter_bytes(key).collect()
    }

    /// Iterates over stored UTF-8 keys that start with `key`.
    pub fn common_prefix_predict_iter<'a>(&'a self, key: &'a str) -> PrefixPredictIter<'a> {
        self.common_prefix_predict_iter_bytes(key.as_bytes())
    }

    /// Iterates over stored byte keys that start with `key`.
    pub fn common_prefix_predict_iter_bytes<'a>(&'a self, key: &'a [u8]) -> PrefixPredictIter<'a> {
        PrefixPredictIter {
            cedar: self,
            key,
            started: false,
            from: 0,
            p: 0,
            root: 0,
            value: None,
            valid: !key.contains(&0),
        }
    }

    /// Collects stored UTF-8 keys that start with `key`.
    pub fn common_prefix_predict(&self, key: &str) -> Vec<(i32, usize)> {
        self.common_prefix_predict_bytes(key.as_bytes())
    }

    /// Collects stored byte keys that start with `key`.
    pub fn common_prefix_predict_bytes(&self, key: &[u8]) -> Vec<(i32, usize)> {
        self.common_prefix_predict_iter_bytes(key).collect()
    }

    /// Iterates over all entries as owned byte keys and values.
    ///
    /// Order follows the current double-array slot layout and is not stable across mutations or
    /// cedarwood versions.
    pub fn entries(&self) -> Entries<'_> {
        Entries {
            cedar: self,
            stack: vec![(0, Vec::new())],
            remaining: self.len(),
        }
    }

    /// Iterates over all entries as UTF-8 strings and values.
    ///
    /// Entries inserted through byte APIs may not be UTF-8. Those items are returned as
    /// [`FromUtf8Error`] rather than being skipped or converted lossily.
    pub fn entries_str(&self) -> Utf8Entries<'_> {
        Utf8Entries { inner: self.entries() }
    }

    fn value_at_node(&self, index: usize) -> Option<i32> {
        let node = self.array[index];
        #[cfg(feature = "reduced-trie")]
        {
            if (MIN_VALUE..=MAX_VALUE).contains(&node.base_) {
                return Some(node.base_);
            }
        }

        let base = node.base();
        if base < 0 {
            return None;
        }
        let terminal = self.array[base as usize];
        (terminal.check == index as i32 && (MIN_VALUE..=MAX_VALUE).contains(&terminal.base_)).then_some(terminal.base_)
    }

    fn child_labels(&self, index: usize) -> SmallVec<[u8; 256]> {
        #[cfg(feature = "reduced-trie")]
        if self.array[index].base_ >= 0 {
            return SmallVec::new();
        }

        let base = self.array[index].base();
        if base < 0 {
            return SmallVec::new();
        }

        let mut labels = SmallVec::new();
        let mut label = self.n_infos[index].child;
        if label == 0 {
            label = self.n_infos[base as usize].sibling;
        }
        while label != 0 {
            labels.push(label);
            label = self.n_infos[(base ^ i32::from(label)) as usize].sibling;
        }
        labels
    }

    // To get the cursor of the first leaf node starting by `from`
    //
    // SAFETY: `from` is the root or a cursor returned by `find`/`next`. Recorded child and sibling
    // labels describe occupied children of that cursor; their parent base points into a complete
    // 256-slot block, so `base ^ child` remains allocated. `array` and `n_infos` have equal length.
    // This function is called per-result in common_prefix_predict and is performance-critical.
    #[inline]
    fn begin(&self, mut from: usize, mut p: usize) -> (Option<i32>, usize, usize) {
        let mut c = unsafe { self.ninfo_unchecked(from) }.child;

        if from == 0 {
            let base = unsafe { self.node_unchecked(0) }.base();
            c = unsafe { self.ninfo_unchecked((base ^ i32::from(c)) as usize) }.sibling;

            if c == 0 {
                return (None, from, p);
            }
        }

        while c != 0 {
            from = (unsafe { self.node_unchecked(from) }.base() ^ i32::from(c)) as usize;
            c = unsafe { self.ninfo_unchecked(from) }.child;
            p += 1;
        }

        let node = unsafe { self.node_unchecked(from) };

        #[cfg(feature = "reduced-trie")]
        if node.base_ >= 0 {
            return (Some(node.base_), from, p);
        }

        let v = unsafe { self.node_unchecked(node.base() as usize) }.base_;
        (Some(v), from, p)
    }

    // To move the cursor from one leaf to the next for the common_prefix_predict.
    //
    // SAFETY: `from` and `root` come from `find`/`begin`. Occupied nodes have `check` links to valid
    // parents, recorded sibling labels identify occupied children, and each parent base points into
    // a complete 256-slot block. Thus every parent and `base ^ sibling` index remains allocated;
    // `array` and `n_infos` are grown in lockstep.
    // This function is called per-result in common_prefix_predict and is performance-critical.
    #[inline]
    fn next(&self, mut from: usize, mut p: usize, root: usize) -> (Option<i32>, usize, usize) {
        #[cfg(feature = "reduced-trie")]
        let mut c: u8 = {
            let node = unsafe { self.node_unchecked(from) };
            if node.base_ < 0 {
                unsafe { self.ninfo_unchecked(node.base() as usize) }.sibling
            } else {
                0
            }
        };

        #[cfg(not(feature = "reduced-trie"))]
        let mut c: u8 = {
            let base = unsafe { self.node_unchecked(from) }.base();
            unsafe { self.ninfo_unchecked(base as usize) }.sibling
        };

        while c == 0 && from != root {
            c = unsafe { self.ninfo_unchecked(from) }.sibling;
            from = unsafe { self.node_unchecked(from) }.check as usize;

            p -= 1;
        }

        if c != 0 {
            from = (unsafe { self.node_unchecked(from) }.base() ^ i32::from(c)) as usize;
            self.begin(from, p + 1)
        } else {
            (None, from, p)
        }
    }

    // pop a block at idx from the linked-list of type `from`, specially handled if it is the last
    // one in the linked-list.
    fn pop_block(&mut self, idx: i32, from: BlockType, last: bool) {
        let head: &mut i32 = match from {
            BlockType::Open => &mut self.blocks_head_open,
            BlockType::Closed => &mut self.blocks_head_closed,
            BlockType::Full => &mut self.blocks_head_full,
        };

        if last {
            *head = 0;
        } else {
            let b_prev = self.blocks[idx as usize].prev;
            let b_next = self.blocks[idx as usize].next;
            self.blocks[b_prev as usize].next = b_next;
            self.blocks[b_next as usize].prev = b_prev;

            if idx == *head {
                *head = b_next;
            }
        }
    }

    // return the block at idx to the linked-list of `to`, specially handled if the linked-list is
    // empty
    fn push_block(&mut self, idx: i32, to: BlockType, empty: bool) {
        let head: &mut i32 = match to {
            BlockType::Open => &mut self.blocks_head_open,
            BlockType::Closed => &mut self.blocks_head_closed,
            BlockType::Full => &mut self.blocks_head_full,
        };

        if empty {
            self.blocks[idx as usize].next = idx;
            self.blocks[idx as usize].prev = idx;
            *head = idx;
        } else {
            self.blocks[idx as usize].prev = self.blocks[*head as usize].prev;
            self.blocks[idx as usize].next = *head;

            let t = self.blocks[*head as usize].prev;
            self.blocks[t as usize].next = idx;
            self.blocks[*head as usize].prev = idx;
            *head = idx;
        }
    }

    /// Reallocate more spaces so that we have more free blocks.
    fn add_block(&mut self) -> i32 {
        if self.size == self.capacity {
            self.capacity += self.capacity;

            self.array.resize(self.capacity, Default::default());
            self.n_infos.resize(self.capacity, Default::default());
            self.blocks.resize(self.capacity >> 8, Block::new());
        }

        self.blocks[self.size >> 8].e_head = self.size as i32;

        // make it a doubly linked list
        self.array[self.size] = Node {
            base_: -((self.size as i32) + 255),
            check: -((self.size as i32) + 1),
        };

        for i in (self.size + 1)..(self.size + 255) {
            self.array[i] = Node {
                base_: -(i as i32 - 1),
                check: -(i as i32 + 1),
            };
        }

        self.array[self.size + 255] = Node {
            base_: -((self.size as i32) + 254),
            check: -(self.size as i32),
        };

        let is_empty = self.blocks_head_open == 0;
        let idx = (self.size >> 8) as i32;
        debug_assert!(self.blocks[idx as usize].num > 1);
        self.push_block(idx, BlockType::Open, is_empty);

        self.size += 256;

        ((self.size >> 8) - 1) as i32
    }

    // transfer the block at idx from the linked-list of `from` to the linked-list of `to`,
    // specially handle the case where the destination linked-list is empty.
    fn transfer_block(&mut self, idx: i32, from: BlockType, to: BlockType, to_block_empty: bool) {
        let is_last = idx == self.blocks[idx as usize].next; //it's the last one if the next points to itself
        let is_empty = to_block_empty && (self.blocks[idx as usize].num != 0);

        self.pop_block(idx, from, is_last);
        self.push_block(idx, to, is_empty);
    }

    /// Mark an edge `e` as used in a trie node.
    fn pop_e_node(&mut self, base: i32, label: u8, from: i32) -> i32 {
        let e: i32 = if base < 0 {
            self.find_place()
        } else {
            base ^ i32::from(label)
        };

        let idx = e >> 8;
        let n_base = self.array[e as usize].base_;
        let n_check = self.array[e as usize].check;

        self.blocks[idx as usize].num -= 1;
        // move the block at idx to the correct linked-list depending the free slots it still have.
        if self.blocks[idx as usize].num == 0 {
            if idx != 0 {
                self.transfer_block(idx, BlockType::Closed, BlockType::Full, self.blocks_head_full == 0);
            }
        } else {
            self.array[(-n_base) as usize].check = n_check;
            self.array[(-n_check) as usize].base_ = n_base;

            if e == self.blocks[idx as usize].e_head {
                self.blocks[idx as usize].e_head = -n_check;
            }

            if idx != 0 && self.blocks[idx as usize].num == 1 && self.blocks[idx as usize].trial != self.max_trial {
                self.transfer_block(idx, BlockType::Open, BlockType::Closed, self.blocks_head_closed == 0);
            }
        }

        #[cfg(feature = "reduced-trie")]
        {
            self.array[e as usize].base_ = CEDAR_VALUE_LIMIT;
            self.array[e as usize].check = from;
            if base < 0 {
                self.array[from as usize].base_ = -(e ^ i32::from(label)) - 1;
            }
        }

        #[cfg(not(feature = "reduced-trie"))]
        {
            if label != 0 {
                self.array[e as usize].base_ = -1;
            } else {
                self.array[e as usize].base_ = 0;
            }
            self.array[e as usize].check = from;
            if base < 0 {
                self.array[from as usize].base_ = e ^ i32::from(label);
            }
        }

        e
    }

    /// Mark an edge `e` as free in a trie node.
    fn push_e_node(&mut self, e: i32) {
        let idx = e >> 8;
        self.blocks[idx as usize].num += 1;

        if self.blocks[idx as usize].num == 1 {
            self.blocks[idx as usize].e_head = e;
            self.array[e as usize] = Node { base_: -e, check: -e };

            if idx != 0 {
                // Move the block from 'Full' to 'Closed' since it has one free slot now.
                self.transfer_block(idx, BlockType::Full, BlockType::Closed, self.blocks_head_closed == 0);
            }
        } else {
            let prev = self.blocks[idx as usize].e_head;

            let next = -self.array[prev as usize].check;

            // Insert to the edge immediately after the e_head
            self.array[e as usize] = Node {
                base_: -prev,
                check: -next,
            };

            self.array[prev as usize].check = -e;
            self.array[next as usize].base_ = -e;

            // Move the block from 'Closed' to 'Open' since it has more than one free slot now.
            if self.blocks[idx as usize].num == 2 || self.blocks[idx as usize].trial == self.max_trial {
                debug_assert!(self.blocks[idx as usize].num > 1);
                if idx != 0 {
                    self.transfer_block(idx, BlockType::Closed, BlockType::Open, self.blocks_head_open == 0);
                }
            }

            // Reset the trial stats
            self.blocks[idx as usize].trial = 0;
        }

        if self.blocks[idx as usize].reject < self.reject[self.blocks[idx as usize].num as usize] {
            self.blocks[idx as usize].reject = self.reject[self.blocks[idx as usize].num as usize];
        }

        self.n_infos[e as usize] = Default::default();
    }

    // push the `label` into the sibling chain
    fn push_sibling(&mut self, from: usize, base: i32, label: u8, has_child: bool) {
        let keep_order: bool = if self.ordered {
            label > self.n_infos[from].child
        } else {
            self.n_infos[from].child == 0
        };

        let sibling: u8;
        {
            let mut c: &mut u8 = &mut self.n_infos[from].child;
            if has_child && keep_order {
                loop {
                    let code = i32::from(*c);
                    c = &mut self.n_infos[(base ^ code) as usize].sibling;

                    if !(self.ordered && (*c != 0) && (*c < label)) {
                        break;
                    }
                }
            }
            sibling = *c;

            *c = label;
        }

        self.n_infos[(base ^ i32::from(label)) as usize].sibling = sibling;
    }

    // remove the `label` from the sibling chain.
    fn pop_sibling(&mut self, from: i32, base: i32, label: u8) {
        let mut idx = from as usize;
        let mut is_child = true;

        loop {
            let next_label = if is_child {
                self.n_infos[idx].child
            } else {
                self.n_infos[idx].sibling
            };

            if next_label == label {
                let sibling_of_target = self.n_infos[(base ^ i32::from(label)) as usize].sibling;
                if is_child {
                    self.n_infos[idx].child = sibling_of_target;
                } else {
                    self.n_infos[idx].sibling = sibling_of_target;
                }
                return;
            }

            idx = (base ^ i32::from(next_label)) as usize;
            is_child = false;
        }
    }

    // Loop through the siblings to see which one reached the end first, which means it is the one
    // with smaller in children size, and we should try to relocate the smaller one.
    fn consult(&self, base_n: i32, base_p: i32, mut c_n: u8, mut c_p: u8) -> bool {
        loop {
            c_n = self.n_infos[(base_n ^ i32::from(c_n)) as usize].sibling;
            c_p = self.n_infos[(base_p ^ i32::from(c_p)) as usize].sibling;

            if !(c_n != 0 && c_p != 0) {
                break;
            }
        }

        c_p != 0
    }

    // Collect the list of the children, and push the label as well if it is not terminal node.
    fn set_child(&self, base: i32, mut c: u8, label: u8, not_terminal: bool) -> SmallVec<[u8; 256]> {
        let mut child: SmallVec<[u8; 256]> = SmallVec::new();

        if c == 0 {
            child.push(c);
            c = self.n_infos[(base ^ i32::from(c)) as usize].sibling;
        }

        if self.ordered {
            while c != 0 && c <= label {
                child.push(c);
                c = self.n_infos[(base ^ i32::from(c)) as usize].sibling;
            }
        }

        if not_terminal {
            child.push(label);
        }

        while c != 0 {
            child.push(c);
            c = self.n_infos[(base ^ i32::from(c)) as usize].sibling;
        }

        child
    }

    // For the case where only one free slot is needed
    fn find_place(&mut self) -> i32 {
        if self.blocks_head_closed != 0 {
            return self.blocks[self.blocks_head_closed as usize].e_head;
        }

        if self.blocks_head_open != 0 {
            return self.blocks[self.blocks_head_open as usize].e_head;
        }

        // the block is not enough, resize it and allocate it.
        self.add_block() << 8
    }

    // For the case where multiple free slots are needed.
    fn find_places(&mut self, child: &[u8]) -> i32 {
        let mut idx = self.blocks_head_open;

        // we still have available 'Open' blocks.
        if idx != 0 {
            debug_assert!(self.blocks[idx as usize].num > 1);
            let bz = self.blocks[self.blocks_head_open as usize].prev;
            let nc = child.len() as i16;

            loop {
                // only proceed if the free slots are more than the number of children. Also, we
                // save the minimal number of attempts to fail in the `reject`, it only worths to
                // try out this block if the number of children is less than that number.
                if self.blocks[idx as usize].num >= nc && nc < self.blocks[idx as usize].reject {
                    let mut e = self.blocks[idx as usize].e_head;
                    loop {
                        let base = e ^ i32::from(child[0]);

                        let mut i = 1;
                        // iterate through the children to see if they are available: (check < 0)
                        while self.array[(base ^ i32::from(child[i])) as usize].check < 0 {
                            if i == child.len() - 1 {
                                // we have found the available block.
                                self.blocks[idx as usize].e_head = e;
                                return e;
                            }
                            i += 1;
                        }

                        // we save the next free block's information in `check`
                        e = -self.array[e as usize].check;
                        if e == self.blocks[idx as usize].e_head {
                            break;
                        }
                    }
                }

                // we broke out of the loop, that means we failed. We save the information in
                // `reject` for future pruning.
                self.blocks[idx as usize].reject = nc;
                if self.blocks[idx as usize].reject < self.reject[self.blocks[idx as usize].num as usize] {
                    // put this stats into the global array of information as well.
                    self.reject[self.blocks[idx as usize].num as usize] = self.blocks[idx as usize].reject;
                }

                let idx_ = self.blocks[idx as usize].next;

                self.blocks[idx as usize].trial += 1;

                // move this block to the 'Closed' block list since it has reached the max_trial
                if self.blocks[idx as usize].trial == self.max_trial {
                    self.transfer_block(idx, BlockType::Open, BlockType::Closed, self.blocks_head_closed == 0);
                }

                // we have finished one round of this cyclic doubly-linked-list.
                if idx == bz {
                    break;
                }

                // going to the next in this linked list group
                idx = idx_;
            }
        }

        self.add_block() << 8
    }

    // resolve the conflict by moving one of the the nodes to a free block.
    fn resolve(&mut self, mut from_n: usize, base_n: i32, label_n: u8) -> i32 {
        let to_pn = base_n ^ i32::from(label_n);

        // the `base` and `from` for the conflicting one.
        let from_p = self.array[to_pn as usize].check;
        let base_p = self.array[from_p as usize].base();

        // whether to replace siblings of newly added
        let flag = self.consult(
            base_n,
            base_p,
            self.n_infos[from_n].child,
            self.n_infos[from_p as usize].child,
        );

        // collect the list of children for the block that we are going to relocate.
        let children = if flag {
            self.set_child(base_n, self.n_infos[from_n].child, label_n, true)
        } else {
            self.set_child(base_p, self.n_infos[from_p as usize].child, 255, false)
        };

        // decide which algorithm to allocate free block depending on the number of children we
        // have.
        let mut base = if children.len() == 1 {
            self.find_place()
        } else {
            self.find_places(&children)
        };

        base ^= i32::from(children[0]);

        let (from, base_) = if flag {
            (from_n as i32, base_n)
        } else {
            (from_p, base_p)
        };

        if flag && children[0] == label_n {
            self.n_infos[from as usize].child = label_n;
        }

        #[cfg(feature = "reduced-trie")]
        {
            self.array[from as usize].base_ = -base - 1;
        }

        #[cfg(not(feature = "reduced-trie"))]
        {
            self.array[from as usize].base_ = base;
        }

        // the actual work for relocating the children
        for i in 0..(children.len()) {
            let to = self.pop_e_node(base, children[i], from);
            let to_ = base_ ^ i32::from(children[i]);

            if i == children.len() - 1 {
                self.n_infos[to as usize].sibling = 0;
            } else {
                self.n_infos[to as usize].sibling = children[i + 1];
            }

            if flag && to_ == to_pn {
                continue;
            }

            self.array[to as usize].base_ = self.array[to_ as usize].base_;

            #[cfg(feature = "reduced-trie")]
            let condition = self.array[to as usize].base_ < 0 && children[i] != 0;
            #[cfg(not(feature = "reduced-trie"))]
            let condition = self.array[to as usize].base_ > 0 && children[i] != 0;

            if condition {
                let mut c = self.n_infos[to_ as usize].child;

                self.n_infos[to as usize].child = c;

                loop {
                    let idx = (self.array[to as usize].base() ^ i32::from(c)) as usize;
                    self.array[idx].check = to;
                    c = self.n_infos[idx].sibling;

                    if c == 0 {
                        break;
                    }
                }
            }

            if !flag && to_ == (from_n as i32) {
                from_n = to as usize;
            }

            // clean up the space that was moved away from.
            if !flag && to_ == to_pn {
                self.push_sibling(from_n, to_pn ^ i32::from(label_n), label_n, true);
                self.n_infos[to_ as usize].child = 0;

                #[cfg(feature = "reduced-trie")]
                {
                    self.array[to_ as usize].base_ = CEDAR_VALUE_LIMIT;
                }

                #[cfg(not(feature = "reduced-trie"))]
                {
                    if label_n != 0 {
                        self.array[to_ as usize].base_ = -1;
                    } else {
                        self.array[to_ as usize].base_ = 0;
                    }
                }

                self.array[to_ as usize].check = from_n as i32;
            } else {
                self.push_e_node(to_);
            }
        }

        // return the position that is free now.
        if flag {
            base ^ i32::from(label_n)
        } else {
            to_pn
        }
    }
}

impl Default for Cedar {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::distributions::Alphanumeric;
    use rand::{thread_rng, Rng};
    use std::iter;

    #[test]
    fn test_insert_and_delete() {
        let dict = vec!["a"];
        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        let result = cedar.exact_match_search("ab").map(|x| x.0);
        assert_eq!(None, result);

        cedar.update("ab", 1).unwrap();
        let result = cedar.exact_match_search("ab").map(|x| x.0);
        assert_eq!(Some(1), result);

        cedar.erase("ab");
        let result = cedar.exact_match_search("ab").map(|x| x.0);
        assert_eq!(None, result);

        cedar.update("abc", 2).unwrap();
        let result = cedar.exact_match_search("abc").map(|x| x.0);
        assert_eq!(Some(2), result);

        cedar.erase("abc");
        let result = cedar.exact_match_search("abc").map(|x| x.0);
        assert_eq!(None, result);
    }

    #[test]
    fn test_common_prefix_search() {
        let dict = vec![
            "a",
            "ab",
            "abc",
            "アルゴリズム",
            "データ",
            "構造",
            "",
            "网球",
            "网球拍",
            "",
            "中华",
            "中华人民",
            "中华人民共和国",
        ];
        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        let result: Vec<i32> = cedar.common_prefix_search("abcdefg").iter().map(|x| x.0).collect();
        assert_eq!(vec![0, 1, 2], result);

        let result: Vec<i32> = cedar.common_prefix_search("网球拍卖会").iter().map(|x| x.0).collect();
        assert_eq!(vec![6, 7, 8], result);

        let result: Vec<i32> = cedar
            .common_prefix_search("中华人民共和国")
            .iter()
            .map(|x| x.0)
            .collect();
        assert_eq!(vec![9, 10, 11, 12], result);

        let result: Vec<i32> = cedar
            .common_prefix_search("データ構造とアルゴリズム")
            .iter()
            .map(|x| x.0)
            .collect();
        assert_eq!(vec![4], result);
    }

    #[test]
    fn test_common_prefix_iter() {
        let dict = vec![
            "a",
            "ab",
            "abc",
            "アルゴリズム",
            "データ",
            "構造",
            "",
            "网球",
            "网球拍",
            "",
            "中华",
            "中华人民",
            "中华人民共和国",
        ];

        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        let result: Vec<i32> = cedar.common_prefix_iter("abcdefg").map(|x| x.0).collect();
        assert_eq!(vec![0, 1, 2], result);

        let result: Vec<i32> = cedar.common_prefix_iter("网球拍卖会").map(|x| x.0).collect();
        assert_eq!(vec![6, 7, 8], result);

        let result: Vec<i32> = cedar.common_prefix_iter("中华人民共和国").map(|x| x.0).collect();
        assert_eq!(vec![9, 10, 11, 12], result);

        let result: Vec<i32> = cedar
            .common_prefix_iter("データ構造とアルゴリズム")
            .map(|x| x.0)
            .collect();
        assert_eq!(vec![4], result);
    }

    #[test]
    fn test_common_prefix_predict() {
        let dict = vec!["a", "ab", "abc"];
        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        let result: Vec<i32> = cedar.common_prefix_predict("a").iter().map(|x| x.0).collect();
        assert_eq!(vec![0, 1, 2], result);
    }

    #[test]
    fn test_exact_match_search() {
        let dict = vec!["a", "ab", "abc"];
        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        let result = cedar.exact_match_search("abc").map(|x| x.0);
        assert_eq!(Some(2), result);
    }

    #[test]
    fn test_unicode_han_sip() {
        let dict = vec!["讥䶯䶰", "讥䶯䶰䶱䶲", "讥䶯䶰䶱䶲䶳䶴䶵𦡦"];

        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        let result: Vec<i32> = cedar.common_prefix_iter("讥䶯䶰䶱䶲䶳䶴䶵𦡦").map(|x| x.0).collect();
        assert_eq!(vec![0, 1, 2], result);
    }

    #[test]
    fn test_unicode_grapheme_cluster() {
        let dict = vec!["a", "abc", "abcde\u{0301}"];

        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        let result: Vec<i32> = cedar
            .common_prefix_iter("abcde\u{0301}\u{1100}\u{1161}\u{AC00}")
            .map(|x| x.0)
            .collect();
        assert_eq!(vec![0, 1, 2], result);
    }

    #[test]
    fn test_erase() {
        let dict = vec!["a", "ab", "abc"];
        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        cedar.erase("abc");
        assert!(cedar.exact_match_search("abc").is_none());
        assert!(cedar.exact_match_search("ab").is_some());
        assert!(cedar.exact_match_search("a").is_some());

        cedar.erase("ab");
        assert!(cedar.exact_match_search("ab").is_none());
        assert!(cedar.exact_match_search("a").is_some());

        cedar.erase("a");
        assert!(cedar.exact_match_search("a").is_none());
    }

    #[test]
    fn test_erase_on_internal_key() {
        let mut cedar = Cedar::new();

        cedar.update("aa", 0).unwrap();
        assert!(cedar.exact_match_search("aa").is_some());
        cedar.update("ab", 1).unwrap();
        assert!(cedar.exact_match_search("ab").is_some());

        cedar.erase("a");
        assert!(cedar.exact_match_search("a").is_none());
        cedar.erase("aa");
        assert!(cedar.exact_match_search("aa").is_none());
        cedar.erase("ab");
        assert!(cedar.exact_match_search("ab").is_none());
    }

    #[test]
    fn test_update() {
        let dict = vec!["a", "ab", "abc"];
        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        cedar.update("abcd", 3).unwrap();

        assert!(cedar.exact_match_search("a").is_some());
        assert!(cedar.exact_match_search("ab").is_some());
        assert!(cedar.exact_match_search("abc").is_some());
        assert!(cedar.exact_match_search("abcd").is_some());
        assert!(cedar.exact_match_search("abcde").is_none());

        let dict = vec!["a", "ab", "abc"];
        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();
        cedar.update("bachelor", 1).unwrap();
        cedar.update("jar", 2).unwrap();
        cedar.update("badge", 3).unwrap();
        cedar.update("baby", 4).unwrap();

        assert!(cedar.exact_match_search("bachelor").is_some());
        assert!(cedar.exact_match_search("jar").is_some());
        assert!(cedar.exact_match_search("badge").is_some());
        assert!(cedar.exact_match_search("baby").is_some());
        assert!(cedar.exact_match_search("abcde").is_none());

        let dict = vec!["a", "ab", "abc"];
        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();
        cedar.update("", 1).unwrap();
        cedar.update("中华", 2).unwrap();
        cedar.update("中华人民", 3).unwrap();
        cedar.update("中华人民共和国", 4).unwrap();

        assert!(cedar.exact_match_search("").is_some());
        assert!(cedar.exact_match_search("中华").is_some());
        assert!(cedar.exact_match_search("中华人民").is_some());
        assert!(cedar.exact_match_search("中华人民共和国").is_some());
    }

    #[test]
    fn test_quickcheck_like() {
        let mut rng = thread_rng();
        let case_count = if cfg!(miri) { 32 } else { 1000 };
        let mut dict: Vec<String> = Vec::with_capacity(case_count);
        for _ in 0..case_count {
            let chars: Vec<u8> = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(30).collect();
            let s = String::from_utf8(chars).unwrap();
            dict.push(s);
        }

        let key_values: Vec<(&str, i32)> = dict.iter().enumerate().map(|(k, s)| (s.as_ref(), k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        for (k, s) in dict.iter().enumerate() {
            assert_eq!(cedar.exact_match_search(s).map(|x| x.0), Some(k as i32));
        }
    }

    #[test]
    fn test_quickcheck_like_with_deep_trie() {
        let mut rng = thread_rng();
        let case_count = if cfg!(miri) { 32 } else { 1000 };
        let mut dict: Vec<String> = Vec::with_capacity(case_count);
        let mut s = String::new();
        for _ in 0..case_count {
            let c: char = rng.sample(Alphanumeric) as char;
            s.push(c);
            dict.push(s.clone());
        }

        let key_values: Vec<(&str, i32)> = dict.iter().enumerate().map(|(k, s)| (s.as_ref(), k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        for (k, s) in dict.iter().enumerate() {
            assert_eq!(cedar.exact_match_search(s).map(|x| x.0), Some(k as i32));
        }
    }

    #[test]
    fn test_mass_erase() {
        let mut rng = thread_rng();
        let case_count = if cfg!(miri) { 32 } else { 1000 };
        let mut dict: Vec<String> = Vec::with_capacity(case_count);
        for _ in 0..case_count {
            let chars: Vec<u8> = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(30).collect();
            let s = String::from_utf8(chars).unwrap();

            dict.push(s);
        }

        let key_values: Vec<(&str, i32)> = dict.iter().enumerate().map(|(k, s)| (s.as_ref(), k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        for s in dict.iter() {
            cedar.erase(s);
            assert!(cedar.exact_match_search(s).is_none());
        }
    }

    #[test]
    fn test_common_prefix_search_returns_empty_vec_on_miss() {
        let mut cedar = Cedar::new();
        cedar.update("abc", 0).unwrap();
        assert!(cedar.common_prefix_search("xyz").is_empty());
    }

    #[test]
    fn test_common_prefix_predict_returns_empty_vec_on_miss() {
        let mut cedar = Cedar::new();
        cedar.update("abc", 0).unwrap();
        assert!(cedar.common_prefix_predict("xyz").is_empty());
    }

    #[test]
    fn checked_mutations_reject_invalid_inputs_without_changes() {
        let mut cedar = Cedar::new();

        assert_eq!(cedar.update("", 0), Err(CedarError::EmptyKey));
        assert_eq!(cedar.update_bytes(b"a\0b", 0), Err(CedarError::NulByte { position: 1 }));
        for value in [-2, -1, i32::MAX - 1, i32::MAX] {
            assert_eq!(cedar.update("invalid", value), Err(CedarError::InvalidValue { value }));
        }
        assert!(cedar.is_empty());

        let entries = [("valid", 1), ("", 2)];
        assert_eq!(cedar.build(&entries), Err(CedarError::EmptyKey));
        assert!(cedar.exact_match_search("valid").is_none());
        assert!(cedar.is_empty());
    }

    #[test]
    fn sorted_constructors_validate_order_duplicates_keys_and_values() {
        assert!(Cedar::from_sorted(&[]).unwrap().is_empty());
        assert_eq!(
            Cedar::from_sorted(&[("a", 1), ("a", 2)]).unwrap_err(),
            CedarError::DuplicateKey { index: 1 }
        );
        assert_eq!(
            Cedar::from_sorted(&[("b", 1), ("a", 2)]).unwrap_err(),
            CedarError::KeysNotSorted { index: 1 }
        );
        assert_eq!(Cedar::from_sorted(&[("", 1)]).unwrap_err(), CedarError::EmptyKey);
        assert_eq!(
            Cedar::from_sorted_bytes(&[(b"a\0b", 1)]).unwrap_err(),
            CedarError::NulByte { position: 1 }
        );
        assert_eq!(
            Cedar::from_sorted(&[("a", -1)]).unwrap_err(),
            CedarError::InvalidValue { value: -1 }
        );
    }

    #[test]
    fn sorted_bulk_construction_matches_incremental_and_remains_mutable() {
        let entries = [
            (b"a".as_slice(), 1),
            (b"ab".as_slice(), 2),
            (b"abc".as_slice(), 3),
            (b"b".as_slice(), 4),
            (b"ba".as_slice(), 5),
            (b"\x80".as_slice(), 6),
            ("".as_bytes(), 7),
            ("网球".as_bytes(), 8),
        ];
        assert!(entries.windows(2).all(|pair| pair[0].0 < pair[1].0));

        let mut incremental = Cedar::new();
        incremental.build_bytes(&entries).unwrap();
        let mut bulk = Cedar::from_sorted_bytes(&entries).unwrap();

        let assert_equivalent = |left: &Cedar, right: &Cedar| {
            assert_eq!(left.len(), right.len());
            for query in [
                b"".as_slice(),
                b"a".as_slice(),
                b"abc".as_slice(),
                b"abcd".as_slice(),
                b"ba".as_slice(),
                b"missing".as_slice(),
                b"\x80".as_slice(),
                "网球拍".as_bytes(),
            ] {
                assert_eq!(
                    left.exact_match_search_bytes(query),
                    right.exact_match_search_bytes(query)
                );
                assert_eq!(
                    left.common_prefix_search_bytes(query),
                    right.common_prefix_search_bytes(query)
                );
                assert_eq!(
                    left.common_prefix_predict_bytes(query),
                    right.common_prefix_predict_bytes(query)
                );
            }

            let mut left_entries: Vec<_> = left.entries().collect();
            let mut right_entries: Vec<_> = right.entries().collect();
            left_entries.sort_unstable();
            right_entries.sort_unstable();
            assert_eq!(left_entries, right_entries);
        };

        assert_equivalent(&incremental, &bulk);

        for cedar in [&mut incremental, &mut bulk] {
            cedar.update_bytes(b"abd", 30).unwrap();
            cedar.update_bytes(b"ab", 20).unwrap();
            assert!(cedar.erase_bytes(b"a"));
            assert!(cedar.erase_bytes("网球".as_bytes()));
            cedar.update_bytes(b"a", 10).unwrap();
            assert!(!cedar.erase_bytes(b"not-present"));
        }
        assert_equivalent(&incremental, &bulk);

        let utf8 = Cedar::from_sorted(&[("a", 1), ("ab", 2), ("", 3)]).unwrap();
        assert_eq!(utf8.exact_match_search(""), Some((3, "".len())));

        let mut configured = Cedar::builder()
            .ordered(false)
            .max_trial(3)
            .unwrap()
            .from_sorted(&[("a", 1), ("ab", 2)])
            .unwrap();
        assert!(!configured.ordered);
        assert_eq!(configured.max_trial, 3);
        configured.update("b", 3).unwrap();
        assert_eq!(configured.exact_match_search("b"), Some((3, 1)));
    }

    #[test]
    fn sorted_bulk_construction_handles_a_full_sibling_block() {
        let mut keys = vec![vec![b'a']];
        keys.extend((1_u8..=u8::MAX).map(|label| vec![b'a', label]));
        let entries: Vec<_> = keys
            .iter()
            .enumerate()
            .map(|(value, key)| (key.as_slice(), value as i32))
            .collect();
        let mut cedar = Cedar::from_sorted_bytes(&entries).unwrap();

        assert_eq!(cedar.len(), 256);
        for (key, value) in &entries {
            assert_eq!(cedar.exact_match_search_bytes(key).map(|item| item.0), Some(*value));
        }

        for (key, _) in entries.iter().rev() {
            assert!(cedar.erase_bytes(key));
        }
        assert!(cedar.is_empty());
        cedar.update_bytes(b"after", 7).unwrap();
        assert_eq!(cedar.exact_match_search_bytes(b"after"), Some((7, 5)));
    }

    #[test]
    fn value_boundaries_are_layout_independent() {
        let mut cedar = Cedar::new();
        cedar.update("minimum", MIN_VALUE).unwrap();
        cedar.update("maximum", MAX_VALUE).unwrap();

        assert_eq!(cedar.exact_match_search("minimum").map(|item| item.0), Some(MIN_VALUE));
        assert_eq!(cedar.exact_match_search("maximum").map(|item| item.0), Some(MAX_VALUE));
    }

    #[test]
    fn byte_and_string_apis_agree_and_nul_queries_stop_safely() {
        let mut cedar = Cedar::new();
        cedar
            .build_bytes(&[(b"a".as_slice(), 1), (b"ab".as_slice(), 2), (b"abc".as_slice(), 3)])
            .unwrap();

        assert_eq!(cedar.exact_match_search("abc"), cedar.exact_match_search_bytes(b"abc"));
        assert_eq!(
            cedar.common_prefix_search("abcdef"),
            cedar.common_prefix_search_bytes(b"abcdef")
        );
        assert_eq!(
            cedar.common_prefix_predict("a"),
            cedar.common_prefix_predict_bytes(b"a")
        );
        assert_eq!(
            cedar.common_prefix_iter("abcdef").collect::<Vec<_>>(),
            cedar.common_prefix_iter_bytes(b"abcdef").collect::<Vec<_>>()
        );
        assert_eq!(
            cedar.common_prefix_predict_iter("a").collect::<Vec<_>>(),
            cedar.common_prefix_predict_iter_bytes(b"a").collect::<Vec<_>>()
        );

        assert!(cedar.exact_match_search_bytes(b"a\0").is_none());
        assert_eq!(cedar.common_prefix_search_bytes(b"a\0"), vec![(1, 0)]);
        assert!(cedar.common_prefix_predict_bytes(b"a\0").is_empty());
        assert!(!cedar.erase_bytes(b"a\0"));
    }

    #[test]
    fn len_and_entry_iteration_track_mixed_mutations() {
        use std::collections::BTreeMap;

        let mut cedar = Cedar::new();
        assert_eq!(cedar.len(), 0);
        assert!(cedar.is_empty());

        cedar.update_bytes(b"alpha", 1).unwrap();
        cedar.update_bytes(b"beta", 2).unwrap();
        cedar.update_bytes(&[0xff, b'x'], 3).unwrap();
        cedar.update_bytes(b"alpha", 4).unwrap();
        assert!(!cedar.erase_bytes(b"missing"));
        assert_eq!(cedar.len(), 3);

        assert!(cedar.erase_bytes(b"beta"));
        cedar.update_bytes(b"gamma", 5).unwrap();
        assert_eq!(cedar.len(), 3);

        let actual: BTreeMap<Vec<u8>, i32> = cedar.entries().collect();
        let expected = BTreeMap::from([(b"alpha".to_vec(), 4), (b"gamma".to_vec(), 5), (vec![0xff, b'x'], 3)]);
        assert_eq!(actual, expected);

        let utf8_entries: Vec<_> = cedar.entries_str().collect();
        assert_eq!(utf8_entries.iter().filter(|entry| entry.is_err()).count(), 1);
        assert_eq!(utf8_entries.len(), cedar.len());
    }

    #[test]
    fn builder_and_memory_metrics_have_documented_defaults() {
        assert_eq!(Cedar::builder(), CedarBuilder::default());
        assert_eq!(
            Cedar::builder().max_trial(0),
            Err(CedarError::InvalidMaxTrial { value: 0 })
        );

        let mut cedar = Cedar::builder().ordered(false).max_trial(3).unwrap().build();
        cedar.update("abc", 1).unwrap();
        let stats = cedar.memory_stats();
        assert_eq!(stats.entries, 1);
        assert_eq!(stats.entries, cedar.len());
        assert_eq!(stats.used_node_slots, cedar.used_node_slots());
        assert_eq!(stats.node_capacity, cedar.node_capacity());
        assert_eq!(stats.allocated_bytes, cedar.allocated_bytes());
        assert_eq!(stats.load_factor, cedar.load_factor());
        assert!(stats.used_node_slots <= stats.node_capacity);
        assert!(stats.allocated_bytes > 0);
        assert!((0.0..=1.0).contains(&stats.load_factor));
    }

    #[test]
    fn structural_entry_iteration_handles_unordered_siblings() {
        use std::collections::BTreeMap;

        let mut cedar = Cedar::builder().ordered(false).build();
        for (key, value) in [("z", 1), ("a", 2), ("ab", 3), ("", 4), ("aa", 5)] {
            cedar.update(key, value).unwrap();
        }
        assert!(cedar.erase("ab"));
        cedar.update("ab", 6).unwrap();

        let mut entries = cedar.entries();
        assert_eq!(entries.len(), cedar.len());
        let actual: BTreeMap<_, _> = entries.by_ref().collect();
        assert_eq!(entries.len(), 0);
        assert_eq!(
            actual,
            BTreeMap::from([
                (b"a".to_vec(), 2),
                (b"aa".to_vec(), 5),
                (b"ab".to_vec(), 6),
                (b"z".to_vec(), 1),
                ("".as_bytes().to_vec(), 4),
            ])
        );
    }

    #[cfg(feature = "std")]
    fn serialized_fixture() -> (Cedar, Vec<u8>) {
        let mut cedar = Cedar::builder().ordered(false).max_trial(3).unwrap().build();
        for (value, key) in [
            b"a".as_slice(),
            b"ab".as_slice(),
            b"alphabet".as_slice(),
            b"beta".as_slice(),
            b"gamma".as_slice(),
            "网球".as_bytes(),
            &[0xff, b'x'],
        ]
        .into_iter()
        .enumerate()
        {
            cedar.update_bytes(key, value as i32).unwrap();
        }
        assert!(cedar.erase_bytes(b"beta"));
        cedar.update_bytes(b"delta", 40).unwrap();
        cedar.update_bytes(b"ab", 20).unwrap();

        let mut bytes = Vec::new();
        cedar.save_to_writer(&mut bytes).unwrap();
        (cedar, bytes)
    }

    #[test]
    #[cfg(feature = "std")]
    fn serialization_round_trip_is_deterministic_and_remains_mutable() {
        let (cedar, bytes) = serialized_fixture();
        let mut second = Vec::new();
        cedar.save_to_writer(&mut second).unwrap();
        assert_eq!(bytes, second);
        assert_eq!(&bytes[..8], &persistence::v1::MAGIC);

        let mut loaded = Cedar::load_from_reader(bytes.as_slice()).unwrap();
        assert_eq!(loaded.ordered, cedar.ordered);
        assert_eq!(loaded.max_trial, cedar.max_trial);
        assert_eq!(loaded.len(), cedar.len());
        assert_eq!(loaded.used_node_slots(), cedar.used_node_slots());
        assert_eq!(loaded.node_capacity(), cedar.node_capacity());
        assert_eq!(loaded.load_factor(), cedar.load_factor());

        let mut expected: Vec<_> = cedar.entries().collect();
        let mut actual: Vec<_> = loaded.entries().collect();
        expected.sort_unstable();
        actual.sort_unstable();
        assert_eq!(actual, expected);
        for query in [b"a".as_slice(), b"alphabet", b"missing", "网球拍".as_bytes()] {
            assert_eq!(
                loaded.exact_match_search_bytes(query),
                cedar.exact_match_search_bytes(query)
            );
            assert_eq!(
                loaded.common_prefix_search_bytes(query),
                cedar.common_prefix_search_bytes(query)
            );
            assert_eq!(
                loaded.common_prefix_predict_bytes(query),
                cedar.common_prefix_predict_bytes(query)
            );
        }

        loaded.update_bytes(b"after-load", 99).unwrap();
        assert_eq!(loaded.exact_match_search_bytes(b"after-load"), Some((99, 10)));
        assert!(loaded.erase_bytes(b"alphabet"));
        assert!(loaded.exact_match_search_bytes(b"alphabet").is_none());

        if !cfg!(miri) {
            let path = std::env::temp_dir().join(format!(
                "cedarwood-serialization-{}-{}.bin",
                std::process::id(),
                persistence::v1::LAYOUT
            ));
            cedar.save_to_path(&path).unwrap();
            let from_file = Cedar::load_from_path(&path).unwrap();
            std::fs::remove_file(path).unwrap();
            assert_eq!(from_file.used_node_slots(), cedar.used_node_slots());
            assert_eq!(from_file.node_capacity(), cedar.node_capacity());
        }
    }

    #[test]
    #[cfg(feature = "std")]
    fn serialization_round_trips_empty_full_and_inactive_blocks() {
        let assert_round_trip = |cedar: &Cedar| {
            let mut bytes = Vec::new();
            cedar.save_to_writer(&mut bytes).unwrap();
            let loaded = Cedar::load_from_reader(bytes.as_slice()).unwrap();
            let mut expected: Vec<_> = cedar.entries().collect();
            let mut actual: Vec<_> = loaded.entries().collect();
            expected.sort_unstable();
            actual.sort_unstable();
            assert_eq!(actual, expected);
            assert_eq!(loaded.len(), cedar.len());
            assert_eq!(loaded.used_node_slots(), cedar.used_node_slots());
            assert_eq!(loaded.node_capacity(), cedar.node_capacity());
        };

        assert_round_trip(&Cedar::new());

        let mut keys = Vec::new();
        for prefix in *b"ab" {
            keys.push(vec![prefix]);
            keys.extend((1_u8..=u8::MAX).map(|label| vec![prefix, label]));
        }
        let entries: Vec<_> = keys
            .iter()
            .enumerate()
            .map(|(value, key)| (key.as_slice(), value as i32))
            .collect();
        let mut cedar = Cedar::from_sorted_bytes(&entries).unwrap();
        assert!(cedar.size < cedar.capacity, "fixture must retain an inactive block");
        assert!(cedar.blocks.iter().take(cedar.size / 256).any(|block| block.num == 0));
        assert_round_trip(&cedar);

        for (key, _) in entries.iter().step_by(3) {
            assert!(cedar.erase_bytes(key));
        }
        assert_round_trip(&cedar);
    }

    #[test]
    #[cfg(feature = "std")]
    fn serialization_rejects_headers_limits_truncation_and_trailing_data() {
        let (_, bytes) = serialized_fixture();

        let mut invalid_magic = bytes.clone();
        invalid_magic[0] ^= 0xff;
        assert!(matches!(
            Cedar::load_from_reader(invalid_magic.as_slice()),
            Err(CedarPersistenceError::InvalidMagic)
        ));

        let mut unknown_version = bytes.clone();
        unknown_version[8..10].copy_from_slice(&2_u16.to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(unknown_version.as_slice()),
            Err(CedarPersistenceError::UnsupportedVersion { major: 2, minor: 0 })
        ));

        let mut wrong_byte_order = bytes.clone();
        wrong_byte_order[12] = 2;
        assert!(matches!(
            Cedar::load_from_reader(wrong_byte_order.as_slice()),
            Err(CedarPersistenceError::UnsupportedByteOrder { byte_order: 2 })
        ));

        let mut wrong_layout = bytes.clone();
        wrong_layout[13] ^= 1;
        assert!(matches!(
            Cedar::load_from_reader(wrong_layout.as_slice()),
            Err(CedarPersistenceError::LayoutMismatch { .. })
        ));

        let mut unknown_flags = bytes.clone();
        unknown_flags[14..16].copy_from_slice(&2_u16.to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(unknown_flags.as_slice()),
            Err(CedarPersistenceError::InvalidHeader { field: "flags", .. })
        ));

        let mut invalid_config = bytes.clone();
        invalid_config[16..20].copy_from_slice(&0_i32.to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(invalid_config.as_slice()),
            Err(CedarPersistenceError::InvalidHeader { field: "max_trial", .. })
        ));

        let mut overflowing_length = bytes.clone();
        overflowing_length[32..40].copy_from_slice(&u64::MAX.to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(overflowing_length.as_slice()),
            Err(CedarPersistenceError::InvalidHeader { .. })
        ));

        assert!(matches!(
            Cedar::load_from_reader_with_limit(bytes.as_slice(), 1),
            Err(CedarPersistenceError::AllocationLimitExceeded { .. })
        ));
        assert!(matches!(
            Cedar::load_from_reader(&bytes[..bytes.len() - 1]),
            Err(CedarPersistenceError::Truncated)
        ));

        let mut trailing = bytes;
        trailing.push(0);
        assert!(matches!(
            Cedar::load_from_reader(trailing.as_slice()),
            Err(CedarPersistenceError::TrailingData)
        ));
    }

    #[test]
    #[cfg(feature = "std")]
    fn serialization_rejects_corrupt_allocator_and_trie_state() {
        let (_, bytes) = serialized_fixture();

        let mut bad_root = bytes.clone();
        bad_root[persistence::v1::HEADER_LEN + 4..persistence::v1::HEADER_LEN + 8]
            .copy_from_slice(&0_i32.to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(bad_root.as_slice()),
            Err(CedarPersistenceError::CorruptData {
                section: "nodes",
                index: 0,
                ..
            })
        ));

        let mut bad_free_link = bytes.clone();
        let node_one_check = persistence::v1::HEADER_LEN + mem::size_of::<Node>() + 4;
        bad_free_link[node_one_check..node_one_check + 4].copy_from_slice(&0_i32.to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(bad_free_link.as_slice()),
            Err(CedarPersistenceError::CorruptData { .. })
        ));

        let array_len = u64::from_le_bytes(bytes[32..40].try_into().unwrap()) as usize;
        let n_infos_len = u64::from_le_bytes(bytes[40..48].try_into().unwrap()) as usize;
        let block_zero_num = persistence::v1::HEADER_LEN + array_len * 8 + n_infos_len * 2 + 8;
        let mut bad_block_count = bytes.clone();
        bad_block_count[block_zero_num..block_zero_num + 2].copy_from_slice(&0_i16.to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(bad_block_count.as_slice()),
            Err(CedarPersistenceError::CorruptData {
                section: "blocks",
                index: 0,
                ..
            })
        ));

        let mut bad_entries = bytes.clone();
        let entries = u64::from_le_bytes(bytes[80..88].try_into().unwrap());
        bad_entries[80..88].copy_from_slice(&(entries + 1).to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(bad_entries.as_slice()),
            Err(CedarPersistenceError::CorruptData { section: "entries", .. })
        ));

        let mut bad_block_head = bytes.clone();
        bad_block_head[20..24].copy_from_slice(&i32::MAX.to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(bad_block_head.as_slice()),
            Err(CedarPersistenceError::CorruptData {
                section: "block-lists",
                ..
            })
        ));

        let n_infos_start = persistence::v1::HEADER_LEN + array_len * 8;
        let first_root_label = bytes[n_infos_start];
        assert_ne!(first_root_label, 0);
        let mut sibling_cycle = bytes;
        let child_sibling = n_infos_start + usize::from(first_root_label) * 2;
        sibling_cycle[child_sibling] = first_root_label;
        assert!(matches!(
            Cedar::load_from_reader(sibling_cycle.as_slice()),
            Err(CedarPersistenceError::CorruptData {
                section: "siblings",
                ..
            })
        ));

        let mut single = Cedar::new();
        single.update("a", 7).unwrap();
        let mut empty_structural = Vec::new();
        single.save_to_writer(&mut empty_structural).unwrap();
        let child_base = persistence::v1::HEADER_LEN + usize::from(b'a') * 8;
        #[cfg(feature = "reduced-trie")]
        let structural_without_children = -1_i32;
        #[cfg(not(feature = "reduced-trie"))]
        let structural_without_children = 0_i32;
        empty_structural[child_base..child_base + 4].copy_from_slice(&structural_without_children.to_le_bytes());
        assert!(matches!(
            Cedar::load_from_reader(empty_structural.as_slice()),
            Err(CedarPersistenceError::CorruptData {
                section: "trie",
                index,
                reason: "structural node has no children",
            }) if index == usize::from(b'a')
        ));
    }

    #[test]
    fn test_duplication() {
        let dict = vec!["些许端", "些須", "些须", "", "", "", "", "亞丁", "亞丁港"];
        let key_values: Vec<(&str, i32)> = dict.into_iter().enumerate().map(|(k, s)| (s, k as i32)).collect();
        let mut cedar = Cedar::new();
        cedar.build(&key_values).unwrap();

        assert_eq!(cedar.exact_match_search("").map(|t| t.0), Some(6));
        assert_eq!(cedar.exact_match_search("亞丁港").map(|t| t.0), Some(8));
        assert_eq!(cedar.exact_match_search("").map(|t| t.0), Some(4));
        assert_eq!(cedar.exact_match_search("些須").map(|t| t.0), Some(1));
    }
}