zipora 3.1.7

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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
use crate::error::{Result, ZiporaError};
use std::cmp::Ordering;

use super::state::*;

///
/// ```rust
/// use zipora::fsa::double_array::DoubleArrayTrie;
///
/// let mut trie = DoubleArrayTrie::new();
/// trie.insert(b"hello").unwrap();
/// trie.insert(b"help").unwrap();
/// trie.insert(b"world").unwrap();
///
/// assert!(trie.contains(b"hello"));
/// assert!(trie.contains(b"help"));
/// assert!(!trie.contains(b"hel"));
/// assert_eq!(trie.len(), 3);
/// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DoubleArrayTrie {
    pub(crate) states: Vec<DaState>,
    pub(crate) ninfos: Vec<NInfo>,
    num_keys: usize,
    /// Heuristic search position
    search_head: usize,
}

impl DoubleArrayTrie {
    /// Create a new empty trie.
    pub fn new() -> Self {
        Self::with_capacity(256)
    }

    /// Create with pre-allocated capacity.
    ///
    /// Minimum capacity is 256 to guarantee that `base ^ ch` (where ch is
    /// any byte 0-255) is always in-bounds for states with `child0 = 0`.
    pub fn with_capacity(capacity: usize) -> Self {
        let cap = capacity.max(256);
        let mut states = Vec::with_capacity(cap);
        states.push(DaState::new_root());
        states.resize(cap, DaState::new_free());

        let ninfos = vec![NInfo::default(); cap];

        Self {
            states,
            ninfos,
            num_keys: 0,
            search_head: 1,
        }
    }

    /// Number of keys in the trie.
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.num_keys
    }

    /// Check if the trie is empty.
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.num_keys == 0
    }

    /// Total number of allocated states.
    #[inline]
    pub fn total_states(&self) -> usize {
        self.states.len()
    }

    /// Memory usage in bytes.
    #[inline]
    pub fn mem_size(&self) -> usize {
        self.states.len() * std::mem::size_of::<DaState>()
            + self.ninfos.len() * std::mem::size_of::<NInfo>()
    }

    /// Check if a state is terminal.
    #[inline(always)]
    pub fn is_term(&self, state: u32) -> bool {
        (state as usize) < self.ninfos.len() && self.ninfos[state as usize].is_term()
    }

    /// Check if a state is free.
    #[inline(always)]
    pub fn is_free(&self, state: u32) -> bool {
        (state as usize) >= self.states.len() || self.states[state as usize].is_free()
    }

    /// Single state transition: `next = base[curr] ^ ch`, valid if `check[next] == curr`.
    /// Returns NIL_STATE if transition doesn't exist.
    #[inline(always)]
    pub fn state_move(&self, curr: u32, ch: u8) -> u32 {
        // SAFETY: `curr` is typically from a previous state_move or 0 (root).
        let base = unsafe { self.states.get_unchecked(curr as usize) }.child0();
        let next = (base ^ ch as u32) as usize;
        // SAFETY: same invariant as contains()
        debug_assert!(next < self.states.len());
        let next_state = unsafe { self.states.get_unchecked(next) };

        if next_state.parent == curr {
            next as u32
        } else {
            NIL_STATE
        }
    }

    /// Insert a key. Returns true if the key was new.
    #[inline]
    pub fn insert(&mut self, key: &[u8]) -> Result<bool> {
        self.insert_with_relocate_cb(key, |_, _| {})
    }

    /// Insert a key, calling `on_relocate(old_pos, new_pos)` whenever a child
    /// state is moved during collision resolution. This lets external value
    /// arrays stay in sync with state IDs.
    ///
    /// Uses consult (relocate-smaller-side) to minimize total relocations.
    pub fn insert_with_relocate_cb(
        &mut self,
        key: &[u8],
        mut on_relocate: impl FnMut(u32, u32),
    ) -> Result<bool> {
        if key.is_empty() {
            let was_new = !self.ninfos[0].is_term();
            self.ninfos[0].set_term();
            if was_new {
                self.num_keys += 1;
            }
            return Ok(was_new);
        }

        let mut curr = 0u32;

        for &ch in key {
            // child0 == 0 means "no children" (find_free_base never returns 0).
            let base = self.states[curr as usize].child0;
            if base == 0 {
                let new_base = self.find_free_base(&[ch])?;
                self.set_base_padded(curr as usize, new_base);
                let next = new_base ^ ch as u32;
                self.ensure_capacity(next as usize + 1);

                self.states[next as usize].child0 = 0;
                self.states[next as usize].set_parent(curr);
                self.add_child_link(curr as usize, ch);
                curr = next;
            } else {
                let next = base ^ ch as u32;
                self.ensure_capacity(next as usize + 1);

                if !self.states[next as usize].is_free()
                    && self.states[next as usize].parent() == curr
                {
                    curr = next;
                } else if self.states[next as usize].is_free() {
                    self.states[next as usize].child0 = 0;
                    self.states[next as usize].set_parent(curr);
                    self.add_child_link(curr as usize, ch);
                    curr = next;
                } else {
                    // Conflict — position occupied by another parent's child.
                    // Consult: relocate the side with fewer children.
                    let conflict_parent = self.states[next as usize].parent();

                    // Guard: conflict_parent must be valid and not an ancestor
                    // of curr (relocating an ancestor would corrupt traversal).
                    let can_consult = (conflict_parent as usize) < self.ninfos.len()
                        && conflict_parent != curr
                        && !self.is_ancestor(conflict_parent, curr);

                    if can_consult {
                        let curr_n = self.count_children(curr) + 1;
                        let conf_n = self.count_children(conflict_parent);
                        if curr_n > conf_n {
                            // Relocate conflict parent (fewer children)
                            let old_base_cf = self.states[conflict_parent as usize].child0();
                            self.relocate_existing(conflict_parent)?;
                            let new_base_cf = self.states[conflict_parent as usize].child0();

                            // Notify callback about conflict parent's moved children
                            Self::notify_relocated(
                                &self.ninfos,
                                conflict_parent as usize,
                                old_base_cf,
                                new_base_cf,
                                &mut on_relocate,
                            );

                            self.states[next as usize].child0 = 0;
                            self.states[next as usize].set_parent(curr);
                            self.add_child_link(curr as usize, ch);
                            curr = next;
                            continue;
                        }
                    }

                    // Default: relocate curr's children
                    let old_base = self.states[curr as usize].child0();
                    let new_base = self.relocate(curr, ch)?;

                    Self::notify_relocated_excluding(
                        &self.ninfos,
                        curr as usize,
                        old_base,
                        new_base,
                        ch,
                        &mut on_relocate,
                    );

                    let next = new_base ^ ch as u32;
                    self.ensure_capacity(next as usize + 1);
                    self.states[next as usize].child0 = 0;
                    self.states[next as usize].set_parent(curr);
                    self.add_child_link(curr as usize, ch);
                    curr = next;
                }
            }
        }

        let was_new = !self.ninfos[curr as usize].is_term();
        self.ninfos[curr as usize].set_term();
        if was_new {
            self.num_keys += 1;
        }
        Ok(was_new)
    }

    /// Notify callback about all moved children of a parent after relocation.
    fn notify_relocated(
        ninfos: &[NInfo],
        parent_pos: usize,
        old_base: u32,
        new_base: u32,
        on_relocate: &mut impl FnMut(u32, u32),
    ) {
        let mut c = ninfos[parent_pos].first_child();
        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            on_relocate(old_base ^ label as u32, new_base ^ label as u32);
            let child_pos = (new_base ^ label as u32) as usize;
            c = if child_pos < ninfos.len() {
                ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }
    }

    /// Notify callback about moved children, excluding `exclude_ch` (new child).
    fn notify_relocated_excluding(
        ninfos: &[NInfo],
        parent_pos: usize,
        old_base: u32,
        new_base: u32,
        exclude_ch: u8,
        on_relocate: &mut impl FnMut(u32, u32),
    ) {
        let mut c = ninfos[parent_pos].first_child();
        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            if label != exclude_ch {
                on_relocate(old_base ^ label as u32, new_base ^ label as u32);
            }
            let child_pos = (new_base ^ label as u32) as usize;
            c = if child_pos < ninfos.len() {
                ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }
    }

    /// Check if a key exists — tight loop, 1 branch per transition.
    ///
    /// # Safety invariant
    /// All allocated states have child0 set by `set_base_padded` (valid base)
    /// or initialized to 0 (leaf). Both guarantee `child0 ^ ch < states.len()`
    /// for any ch in 0..=255. Root parent is NIL_STATE (never matches curr).
    #[inline]
    pub fn contains(&self, key: &[u8]) -> bool {
        let states = self.states.as_slice();

        if key.is_empty() {
            return self.ninfos[0].is_term();
        }

        let ninfos = self.ninfos.as_slice();
        let mut curr = 0usize;
        for &ch in key {
            // SAFETY: curr is always a valid index because we check `next_state.parent == curr`
            // from previously validated `next` indices, and `curr=0` is valid.
            let base = unsafe { states.get_unchecked(curr) }.child0;
            let next = (base ^ ch as u32) as usize;
            // SAFETY: set_base_padded guarantees (base | 0xFF) < len for valid bases.
            // Leaf states have child0=0, so next=ch ∈ [0,255] < 256 ≤ len.
            // Free states have parent with FREE_BIT set, never matching curr.
            debug_assert!(
                next < states.len(),
                "OOB: next={next}, len={}",
                states.len()
            );
            let next_state = unsafe { states.get_unchecked(next) };
            if next_state.parent != curr as u32 {
                return false;
            }
            curr = next;
        }

        // SAFETY: curr has been verified to be < states.len() (and therefore ninfos.len() which is the same size)
        unsafe { ninfos.get_unchecked(curr) }.is_term()
    }

    /// Lookup key and return its terminal state, or None.
    #[inline]
    pub fn lookup_state(&self, key: &[u8]) -> Option<u32> {
        let states = self.states.as_slice();

        if key.is_empty() {
            return if self.ninfos[0].is_term() {
                Some(0)
            } else {
                None
            };
        }

        let ninfos = self.ninfos.as_slice();
        let mut curr = 0usize;
        for &ch in key {
            let base = states[curr].child0;
            let next = (base ^ ch as u32) as usize;
            // SAFETY: same invariant as contains()
            debug_assert!(next < states.len());
            let next_state = unsafe { states.get_unchecked(next) };
            if next_state.parent != curr as u32 {
                return None;
            }
            curr = next;
        }

        if ninfos[curr].is_term() {
            Some(curr as u32)
        } else {
            None
        }
    }

    /// Remove a key. Returns true if the key existed.
    ///
    /// After clearing the terminal flag, prunes dead-end nodes that are
    /// no longer part of any valid key path (leaf states with no children
    /// and no terminal flag).
    pub fn remove(&mut self, key: &[u8]) -> bool {
        if let Some(state) = self.lookup_state(key)
            && self.ninfos[state as usize].is_term()
        {
            self.ninfos[state as usize].clear_term();
            self.num_keys -= 1;
            self.prune_dead_branch(state);
            return true;
        }
        false
    }

    /// Restore the key string from a state by walking the parent chain.
    pub fn restore_key(&self, state: u32) -> Option<Vec<u8>> {
        if state as usize >= self.states.len() {
            return None;
        }
        if self.states[state as usize].is_free() {
            return None;
        }

        let mut symbols = Vec::new();
        let mut curr = state;

        while curr != 0 {
            let parent = self.states[curr as usize].parent();
            let parent_base = self.states[parent as usize].child0();
            let symbol = (curr ^ parent_base) as u8;
            symbols.push(symbol);
            curr = parent;
        }

        symbols.reverse();
        Some(symbols)
    }

    /// Prune dead-end nodes after a key removal.
    /// Walks up the parent chain freeing non-terminal leaf nodes and
    /// removing them from their parent's NInfo child chain.
    fn prune_dead_branch(&mut self, state: u32) {
        let mut curr = state;

        while curr != 0 {
            // Stop if this state is terminal or has children
            if self.ninfos[curr as usize].is_term() {
                break;
            }
            if self.ninfos[curr as usize].first_child() != NINFO_NONE {
                break;
            }

            // This state is a dead leaf — remove it
            let parent = self.states[curr as usize].parent();
            if parent as usize >= self.states.len() {
                break;
            }

            let parent_base = self.states[parent as usize].child0();
            let label = (curr ^ parent_base) as u8;

            // Remove from parent's NInfo child chain
            self.remove_child_link(parent as usize, label);

            // Free the state
            self.ninfos[curr as usize] = NInfo::default();
            self.states[curr as usize].set_free();

            // Continue up to parent
            curr = parent;
        }
    }

    /// Remove a label from the sorted sibling chain of parent_pos.
    fn remove_child_link(&mut self, parent_pos: usize, label: u8) {
        let label_enc = label_to_ninfo(label);
        let base = self.states[parent_pos].child0();

        let first = self.ninfos[parent_pos].first_child();
        if first == NINFO_NONE {
            return;
        }

        if first == label_enc {
            // Removing the first child — update parent's child pointer
            let child_pos = (base ^ label as u32) as usize;
            let next = if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
            self.ninfos[parent_pos].set_first_child(next);
            return;
        }

        // Walk chain to find and unlink the label
        let mut prev_enc = first;
        loop {
            let prev_label = (prev_enc - 1) as u8;
            let prev_pos = (base ^ prev_label as u32) as usize;
            if prev_pos >= self.ninfos.len() {
                break;
            }
            let next_enc = self.ninfos[prev_pos].sibling;
            if next_enc == label_enc {
                // Found it — unlink by pointing prev to label's next
                let label_pos = (base ^ label as u32) as usize;
                let after = if label_pos < self.ninfos.len() {
                    self.ninfos[label_pos].sibling
                } else {
                    NINFO_NONE
                };
                self.ninfos[prev_pos].sibling = after;
                return;
            }
            if next_enc == NINFO_NONE {
                break;
            }
            prev_enc = next_enc;
        }
    }

    /// Get all keys in the trie.
    pub fn keys(&self) -> Vec<Vec<u8>> {
        let mut result = Vec::with_capacity(self.num_keys);
        let mut path = Vec::new();
        self.collect_keys(0, &mut path, &mut result);
        result
    }

    /// Get all keys with a given prefix.
    pub fn keys_with_prefix(&self, prefix: &[u8]) -> Vec<Vec<u8>> {
        // Navigate to prefix state
        let mut curr = 0u32;
        for &ch in prefix {
            let next = self.state_move(curr, ch);
            if next == NIL_STATE {
                return Vec::new();
            }
            curr = next;
        }

        let mut result = Vec::new();
        let mut path = prefix.to_vec();
        self.collect_keys(curr, &mut path, &mut result);
        result
    }

    /// Iterate all children of a state, calling `f(symbol, child_state)`.
    #[inline]
    pub fn for_each_child(&self, state: u32, mut f: impl FnMut(u8, u32)) {
        let mut c = self.ninfos[state as usize].first_child();
        if c == NINFO_NONE {
            return;
        }
        let base = self.states[state as usize].child0();
        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            let child_pos = (base ^ label as u32) as usize;
            if child_pos < self.states.len() && !self.states[child_pos].is_free() {
                f(label, child_pos as u32);
            }
            c = if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }
    }

    /// Get sorted children of a state as (symbol, child_state) pairs.
    /// Matching C++ `get_all_move()`. Used by cursor for binary search.
    #[inline]
    #[allow(dead_code)]
    fn get_children(&self, state: u32) -> Vec<(u8, u32)> {
        let mut c = self.ninfos[state as usize].first_child();
        if c == NINFO_NONE {
            return Vec::new();
        }
        let base = self.states[state as usize].child0();

        let mut children = Vec::new();
        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            let child_pos = (base ^ label as u32) as usize;
            if child_pos < self.states.len() && !self.states[child_pos].is_free() {
                children.push((label, child_pos as u32));
            }
            c = if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }
        children // Already sorted by symbol (NInfo chain is sorted)
    }

    /// Find the child with the given symbol, or the next higher child.
    /// Returns (index, exact_match) where index is into get_children() result.
    #[inline]
    fn lower_bound_child(&self, state: u32, symbol: u8) -> Option<(u8, u32)> {
        let mut c = self.ninfos[state as usize].first_child();
        if c == NINFO_NONE {
            return None;
        }
        let base = self.states[state as usize].child0();
        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            if label < symbol {
                let child_pos = (base ^ label as u32) as usize;
                c = if child_pos < self.ninfos.len() {
                    self.ninfos[child_pos].sibling
                } else {
                    NINFO_NONE
                };
                continue;
            }
            let child_pos = (base ^ label as u32) as usize;
            if child_pos < self.states.len() && !self.states[child_pos].is_free() {
                return Some((label, child_pos as u32));
            }
            c = if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }
        None
    }

    /// Find the highest child with symbol < given symbol.
    #[inline]
    fn prev_child(&self, state: u32, symbol: u32) -> Option<(u8, u32)> {
        let mut c = self.ninfos[state as usize].first_child();
        if c == NINFO_NONE {
            return None;
        }
        let base = self.states[state as usize].child0();
        let mut result = None;
        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            let child_pos = (base ^ label as u32) as usize;
            if (label as u32) < symbol
                && child_pos < self.states.len()
                && !self.states[child_pos].is_free()
            {
                result = Some((label, child_pos as u32));
            }
            c = if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }
        result
    }

    /// Get the first (lowest symbol) child of a state.
    #[inline]
    fn first_child(&self, state: u32) -> Option<(u8, u32)> {
        let c = self.ninfos[state as usize].first_child();
        if c == NINFO_NONE {
            return None;
        }
        let base = self.states[state as usize].child0();

        let label = (c - 1) as u8;
        let child_pos = (base ^ label as u32) as usize;
        if child_pos < self.states.len() && !self.states[child_pos].is_free() {
            Some((label, child_pos as u32))
        } else {
            None
        }
    }

    /// Get the last (highest symbol) child of a state.
    #[inline]
    fn last_child(&self, state: u32) -> Option<(u8, u32)> {
        let mut c = self.ninfos[state as usize].first_child();
        if c == NINFO_NONE {
            return None;
        }
        let base = self.states[state as usize].child0();
        let mut result = None;
        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            let child_pos = (base ^ label as u32) as usize;
            if child_pos < self.states.len() && !self.states[child_pos].is_free() {
                result = Some((label, child_pos as u32));
            }
            c = if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }
        result
    }

    /// Call `f` for each key with the given prefix — zero allocation.
    ///
    /// The callback receives a `&[u8]` reference to the key bytes.
    /// Unlike `keys_with_prefix()`, this does not allocate `Vec<Vec<u8>>`.
    pub fn for_each_key_with_prefix(&self, prefix: &[u8], mut f: impl FnMut(&[u8])) {
        let mut curr = 0u32;
        for &ch in prefix {
            let next = self.state_move(curr, ch);
            if next == NIL_STATE {
                return;
            }
            curr = next;
        }
        let mut path = prefix.to_vec();
        self.walk_keys(curr, &mut path, &mut f);
    }

    /// Internal DFS for callback-based key iteration.
    fn walk_keys(&self, state: u32, path: &mut Vec<u8>, f: &mut impl FnMut(&[u8])) {
        if state as usize >= self.states.len() {
            return;
        }

        if self.ninfos[state as usize].is_term() {
            f(path);
        }

        let mut c = self.ninfos[state as usize].first_child();
        if c == NINFO_NONE {
            return;
        }
        let base = self.states[state as usize].child0();

        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            let child_pos = (base ^ label as u32) as usize;
            if child_pos < self.states.len() && !self.states[child_pos].is_free() {
                path.push(label);
                self.walk_keys(child_pos as u32, path, f);
                path.pop();
            }
            c = if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }
    }

    /// Build from sorted keys (more efficient than incremental insert).
    pub fn build_from_sorted(keys: &[&[u8]]) -> Result<Self> {
        if keys.is_empty() {
            return Ok(Self::new());
        }

        // Estimate total states needed
        let total_bytes: usize = keys.iter().map(|k| k.len()).sum();
        let estimated_states = (total_bytes / 2).max(256);
        let mut trie = Self::with_capacity(estimated_states * 3 / 2);

        for &key in keys {
            trie.insert(key)?;
        }

        trie.shrink_to_fit();
        Ok(trie)
    }

    // =========================================================================
    // Consult — relocate the smaller side on conflict
    // =========================================================================

    /// Count children of a state using sibling chain (O(k)).
    #[inline]
    fn count_children(&self, state: u32) -> usize {
        let mut c = self.ninfos[state as usize].first_child();
        if c == NINFO_NONE {
            return 0;
        }
        let base = self.states[state as usize].child0();
        let mut count = 0;
        while c != NINFO_NONE {
            count += 1;
            let label = (c - 1) as u8;
            let pos = (base ^ label as u32) as usize;
            c = if pos < self.ninfos.len() {
                self.ninfos[pos].sibling
            } else {
                NINFO_NONE
            };
        }
        count
    }

    /// Resolve collision by relocating the smaller side.
    #[allow(dead_code)]
    fn consult_and_relocate(&mut self, curr: u32, ch: u8) -> Result<u32> {
        let base = self.states[curr as usize].child0();
        let conflict_pos = base ^ ch as u32;
        let conflict_parent = self.states[conflict_pos as usize].parent();

        let curr_children = self.count_children(curr);
        let conflict_children = self.count_children(conflict_parent);

        if curr_children < conflict_children {
            // Relocate curr (fewer children)
            self.relocate(curr, ch)
        } else {
            // Relocate the conflicting side
            // We need to relocate conflict_parent's children, then retry
            self.relocate(conflict_parent, ch)?;
            // After relocation, the conflict position should be free now
            // Return curr's existing base (no change needed)
            Ok(self.states[curr as usize].child0())
        }
    }

    /// Shrink internal arrays to fit actual usage.
    /// Preserves XOR padding invariant: (base | 0xFF) + 1 must be in-bounds.
    pub fn shrink_to_fit(&mut self) {
        // Find highest reachable position to preserve padding invariant
        let mut max_reachable = 0usize;
        for (i, s) in self.states.iter().enumerate() {
            if !s.is_free() {
                // Check if this state actually has children (via NInfo)
                if i < self.ninfos.len() && self.ninfos[i].first_child() != NINFO_NONE {
                    let base = s.child0();
                    // With XOR, max child pos = base | 0xFF
                    max_reachable = max_reachable.max((base as usize | 0xFF) + 1);
                }
            }
        }

        let last_used = self.states.iter().rposition(|s| !s.is_free()).unwrap_or(0);
        let new_len = (last_used + 1).max(max_reachable).min(self.states.len());
        self.states.truncate(new_len);
        self.states.shrink_to_fit();
        self.ninfos.truncate(new_len);
        self.ninfos.shrink_to_fit();

        self.search_head = 1;
    }

    // --- Internal methods ---

    /// Insert label into the sorted sibling chain of parent_pos.
    fn add_child_link(&mut self, parent_pos: usize, label: u8) {
        let label_enc = label_to_ninfo(label);
        let base = self.states[parent_pos].child0();

        let first = self.ninfos[parent_pos].first_child();
        if first == NINFO_NONE || label_enc < first {
            // New first child
            let child_pos = (base ^ label as u32) as usize;
            if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling = first;
            }
            self.ninfos[parent_pos].set_first_child(label_enc);
        } else if first == label_enc {
            return; // Already first child
        } else {
            // Walk chain to find insertion point
            let mut prev_enc = first;
            loop {
                let prev_label = (prev_enc - 1) as u8;
                let prev_pos = (base ^ prev_label as u32) as usize;
                if prev_pos >= self.ninfos.len() {
                    break;
                }
                let next_enc = self.ninfos[prev_pos].sibling;
                if next_enc == NINFO_NONE || label_enc < next_enc {
                    let child_pos = (base ^ label as u32) as usize;
                    if child_pos < self.ninfos.len() {
                        self.ninfos[child_pos].sibling = next_enc;
                        self.ninfos[prev_pos].sibling = label_enc;
                    }
                    break;
                }
                if next_enc == label_enc {
                    break;
                } // Already present
                prev_enc = next_enc;
            }
        }
    }

    /// Ensure states array is large enough, using 1.5x amortized growth.
    /// New states are NOT added to the free list — they're detected as free
    /// by `is_free()`. Only explicitly freed states go on the free list.
    #[inline]
    fn ensure_capacity(&mut self, required: usize) {
        if required <= self.states.len() {
            return;
        }
        let new_len = required.max(self.states.len() * 3 / 2).max(256);
        self.states.resize(new_len, DaState::new_free());
        self.ninfos.resize(new_len, NInfo::default());
    }

    /// Set base and ensure all XOR-reachable positions are in-bounds.
    /// With XOR transitions, child positions span [base & !0xFF, base | 0xFF].
    #[inline]
    fn set_base_padded(&mut self, state: usize, base: u32) {
        self.states[state].set_child0(base);
        self.ensure_capacity((base as usize | 0xFF) + 1);
    }

    /// Find a free base value where all given children symbols can be placed.
    ///
    /// Uses search_head to skip past occupied regions at the start, then
    /// probes candidates by incrementing base. For single-child inserts,
    /// jumps directly to the next free position when the candidate is occupied.
    fn find_free_base(&mut self, children: &[u8]) -> Result<u32> {
        debug_assert!(!children.is_empty());

        let ch0 = children[0] as u32;
        let single = children.len() == 1;

        // Advance search_head past any occupied region
        while self.search_head < self.states.len() && !self.states[self.search_head].is_free() {
            self.search_head += 1;
        }

        let mut base = (self.search_head as u32) ^ ch0;
        if base == 0 {
            base = 1;
        }

        let mut attempts = 0u32;

        loop {
            if attempts > 1_000_000 || base > MAX_STATE {
                return Err(ZiporaError::invalid_data(
                    "Double array: cannot find free base",
                ));
            }
            attempts += 1;

            self.ensure_capacity((base as usize | 0xFF) + 1);

            let first_pos = (base ^ ch0) as usize;
            if first_pos == 0 || !self.states[first_pos].is_free() {
                base += 1;
                continue;
            }

            if single {
                self.search_head = first_pos;
                return Ok(base);
            }

            // Multi-child: check remaining positions
            let all_free = children[1..].iter().all(|&ch| {
                let pos = (base ^ ch as u32) as usize;
                pos > 0 && self.states[pos].is_free()
            });

            if all_free {
                self.search_head = first_pos;
                return Ok(base);
            }
            base += 1;
        }
    }

    /// Relocate all children of `state` to a new base that also accommodates `new_ch`.
    fn relocate(&mut self, state: u32, new_ch: u8) -> Result<u32> {
        // Collect existing children via NInfo chain
        let old_base = self.states[state as usize].child0();
        let mut children_symbols = Vec::new();

        {
            let mut c = self.ninfos[state as usize].first_child();
            while c != NINFO_NONE {
                let label = (c - 1) as u8;
                let child_pos = (old_base ^ label as u32) as usize;
                if child_pos < self.states.len()
                    && !self.states[child_pos].is_free()
                    && !children_symbols.contains(&label)
                {
                    children_symbols.push(label);
                }
                c = if child_pos < self.ninfos.len() {
                    self.ninfos[child_pos].sibling
                } else {
                    NINFO_NONE
                };
            }
        }

        if !children_symbols.contains(&new_ch) {
            children_symbols.push(new_ch);
        }
        children_symbols.sort_unstable();

        let new_base = self.find_free_base(&children_symbols)?;

        self.ninfos[state as usize].set_first_child(NINFO_NONE);

        // Move existing children to new positions
        {
            for &ch in &children_symbols {
                if ch == new_ch {
                    continue;
                }
                let old_pos = old_base ^ ch as u32;
                let new_pos = new_base ^ ch as u32;

                if old_pos as usize >= self.states.len() {
                    continue;
                }
                if self.states[old_pos as usize].is_free() {
                    continue;
                }
                if self.states[old_pos as usize].parent() != state {
                    continue;
                }

                self.ensure_capacity(new_pos as usize + 1);

                let old_state = self.states[old_pos as usize];
                self.states[new_pos as usize].child0 = old_state.child0;
                self.states[new_pos as usize].set_parent(state);

                let old_ninfo = self.ninfos[old_pos as usize];
                self.ninfos[new_pos as usize].child = old_ninfo.child;
                self.ninfos[new_pos as usize].sibling = NINFO_NONE;

                // Update grandchildren to point to new parent position
                {
                    let mut gc = old_ninfo.first_child();
                    if gc != NINFO_NONE {
                        let child_base = old_state.child0();
                        while gc != NINFO_NONE {
                            let glabel = (gc - 1) as u8;
                            let gpos = (child_base ^ glabel as u32) as usize;
                            if gpos < self.states.len()
                                && !self.states[gpos].is_free()
                                && self.states[gpos].parent() == old_pos
                            {
                                self.states[gpos].set_parent(new_pos);
                            }
                            gc = if gpos < self.ninfos.len() {
                                self.ninfos[gpos].sibling
                            } else {
                                NINFO_NONE
                            };
                        }
                    }
                }

                self.ninfos[old_pos as usize] = NInfo::default();
                self.states[old_pos as usize].set_free();
            }
        }

        // Update state's base to new location
        self.set_base_padded(state as usize, new_base);

        // Rebuild parent's NInfo chain from scratch (excluding new_ch, caller will add it)
        for &ch in &children_symbols {
            if ch != new_ch {
                self.add_child_link(state as usize, ch);
            }
        }

        Ok(new_base)
    }

    /// Relocate ALL existing children of `state` without adding a new child.
    /// Used by consult when relocating the conflict parent.
    /// Returns the new base value.
    fn relocate_existing(&mut self, state: u32) -> Result<u32> {
        let old_base = self.states[state as usize].child0();

        let mut children_symbols: Vec<u8> = Vec::new();
        let mut c = self.ninfos[state as usize].first_child();
        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            let child_pos = (old_base ^ label as u32) as usize;
            if child_pos < self.states.len() && !self.states[child_pos].is_free() {
                children_symbols.push(label);
            }
            c = if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }

        if children_symbols.is_empty() {
            return Ok(old_base);
        }
        children_symbols.sort_unstable();

        let new_base = self.find_free_base(&children_symbols)?;

        self.ninfos[state as usize].set_first_child(NINFO_NONE);

        for &ch in &children_symbols {
            let old_pos = old_base ^ ch as u32;
            let new_pos = new_base ^ ch as u32;

            if old_pos as usize >= self.states.len() {
                continue;
            }
            if self.states[old_pos as usize].is_free() {
                continue;
            }
            if self.states[old_pos as usize].parent() != state {
                continue;
            }

            self.ensure_capacity(new_pos as usize + 1);

            let old_state = self.states[old_pos as usize];
            self.states[new_pos as usize].child0 = old_state.child0;
            self.states[new_pos as usize].set_parent(state);

            let old_ninfo = self.ninfos[old_pos as usize];
            self.ninfos[new_pos as usize].child = old_ninfo.child;
            self.ninfos[new_pos as usize].sibling = NINFO_NONE;

            {
                let mut gc = old_ninfo.first_child();
                if gc != NINFO_NONE {
                    let child_base = old_state.child0();
                    while gc != NINFO_NONE {
                        let glabel = (gc - 1) as u8;
                        let gpos = (child_base ^ glabel as u32) as usize;
                        if gpos < self.states.len()
                            && !self.states[gpos].is_free()
                            && self.states[gpos].parent() == old_pos
                        {
                            self.states[gpos].set_parent(new_pos);
                        }
                        gc = if gpos < self.ninfos.len() {
                            self.ninfos[gpos].sibling
                        } else {
                            NINFO_NONE
                        };
                    }
                }
            }

            self.ninfos[old_pos as usize] = NInfo::default();
            self.states[old_pos as usize].set_free();
        }

        self.set_base_padded(state as usize, new_base);

        for &ch in &children_symbols {
            self.add_child_link(state as usize, ch);
        }

        Ok(new_base)
    }

    /// Check if `ancestor` is an ancestor of `descendant` in the trie.
    ///
    /// Required for correctness in consult (relocate-smaller-side): relocating
    /// an ancestor of `curr` would invalidate our traversal state. The depth-256
    /// bound prevents infinite loops on malformed parent chains.
    fn is_ancestor(&self, ancestor: u32, descendant: u32) -> bool {
        let mut curr = descendant;
        let mut depth = 0;
        while curr != 0 && depth < 256 {
            let parent = self.states[curr as usize].parent();
            if parent == ancestor {
                return true;
            }
            curr = parent;
            depth += 1;
        }
        false
    }

    /// Recursively collect keys via DFS.
    fn collect_keys(&self, state: u32, path: &mut Vec<u8>, keys: &mut Vec<Vec<u8>>) {
        if state as usize >= self.states.len() {
            return;
        }

        if self.ninfos[state as usize].is_term() {
            keys.push(path.clone());
        }

        let mut c = self.ninfos[state as usize].first_child();
        if c == NINFO_NONE {
            return;
        }
        let base = self.states[state as usize].child0();

        while c != NINFO_NONE {
            let label = (c - 1) as u8;
            let child_pos = (base ^ label as u32) as usize;
            if child_pos < self.states.len() && !self.states[child_pos].is_free() {
                path.push(label);
                self.collect_keys(child_pos as u32, path, keys);
                path.pop();
            }
            c = if child_pos < self.ninfos.len() {
                self.ninfos[child_pos].sibling
            } else {
                NINFO_NONE
            };
        }
    }
}

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

impl std::fmt::Debug for DoubleArrayTrie {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DoubleArrayTrie")
            .field("num_keys", &self.num_keys)
            .field("total_states", &self.states.len())
            .field("mem_size", &self.mem_size())
            .finish()
    }
}

/// Bidirectional lexicographic cursor over a `DoubleArrayTrie`.
///
/// Supports `seek_lower_bound`, `next`, `prev`, `seek_begin`, `seek_end`.
/// Matching the C++ reference's `ADFA_LexIterator` interface.
///
/// # Examples
///
/// ```rust
/// use zipora::fsa::double_array::DoubleArrayTrie;
///
/// let mut trie = DoubleArrayTrie::new();
/// for word in &["apple", "banana", "cherry", "date", "elderberry"] {
///     trie.insert(word.as_bytes()).unwrap();
/// }
///
/// let mut cursor = trie.cursor();
///
/// // Seek to first key >= "c"
/// assert!(cursor.seek_lower_bound(b"c"));
/// assert_eq!(cursor.key(), b"cherry");
///
/// // Advance
/// assert!(cursor.next());
/// assert_eq!(cursor.key(), b"date");
///
/// // Go back
/// assert!(cursor.prev());
/// assert_eq!(cursor.key(), b"cherry");
/// ```
pub struct DoubleArrayTrieCursor<'a> {
    pub(crate) trie: &'a DoubleArrayTrie,
    /// Stack of (state, next_symbol_to_try) for DFS position tracking
    pub(crate) stack: Vec<(u32, u16)>,
    /// Current key bytes
    current_key: Vec<u8>,
    /// Whether the cursor is positioned on a valid key
    valid: bool,
}

impl<'a> DoubleArrayTrieCursor<'a> {
    /// Create a new cursor (not positioned).
    pub(crate) fn new(trie: &'a DoubleArrayTrie) -> Self {
        Self {
            trie,
            stack: Vec::with_capacity(64),
            current_key: Vec::with_capacity(64),
            valid: false,
        }
    }

    /// Current key bytes. Only valid when `is_valid()` is true.
    #[inline]
    pub fn key(&self) -> &[u8] {
        &self.current_key
    }

    /// Whether the cursor is positioned on a valid key.
    #[inline]
    pub fn is_valid(&self) -> bool {
        self.valid
    }

    /// Seek to the first key in the trie.
    pub fn seek_begin(&mut self) -> bool {
        self.stack.clear();
        self.current_key.clear();
        self.valid = false;

        // Start at root
        if self.trie.states.is_empty() {
            return false;
        }

        // If root is terminal (empty key), we're done
        if self.trie.ninfos[0].is_term() {
            self.stack.push((0, 0));
            self.valid = true;
            return true;
        }

        // Push root and descend to first terminal
        self.stack.push((0, 0));
        self.descend_to_next_terminal()
    }

    /// Seek to the last key in the trie.
    pub fn seek_end(&mut self) -> bool {
        self.stack.clear();
        self.current_key.clear();
        self.valid = false;

        if self.trie.states.is_empty() {
            return false;
        }

        // Start at root, descend to rightmost (highest symbol) terminal
        self.stack.push((0, 256));
        self.descend_to_rightmost_terminal(0)
    }

    /// Seek to the first key >= `target`.
    /// Matching C++ `seek_lower_bound` algorithm: binary search per level.
    pub fn seek_lower_bound(&mut self, target: &[u8]) -> bool {
        self.stack.clear();
        self.current_key.clear();
        self.valid = false;

        if self.trie.states.is_empty() {
            return false;
        }

        let mut curr = 0u32;

        for &ch in target {
            // Find child >= ch (matching C++ lower_bound_ex on children)
            match self.trie.lower_bound_child(curr, ch) {
                Some((found_ch, next_state)) if found_ch == ch => {
                    // Exact match — follow this transition
                    self.stack.push((curr, ch as u16 + 1));
                    self.current_key.push(ch);
                    curr = next_state;
                }
                Some((found_ch, _)) => {
                    // No exact match, but found higher child
                    // Position at that child and descend to first terminal
                    self.stack.push((curr, found_ch as u16));
                    return self.advance_from_stack();
                }
                None => {
                    // No child >= ch — backtrack to find next key
                    self.stack.push((curr, 256)); // Exhausted all children
                    return self.advance_from_stack();
                }
            }
        }

        // Key fully consumed. If current state is terminal, exact match.
        if self.trie.ninfos[curr as usize].is_term() {
            self.stack.push((curr, 0));
            self.valid = true;
            return true;
        }

        // Not terminal — descend to first terminal in subtree
        self.stack.push((curr, 0));
        self.descend_to_next_terminal()
    }

    /// Advance to the next key in lexicographic order.
    pub fn next(&mut self) -> bool {
        if !self.valid {
            return false;
        }

        // Pop current terminal state, advance from its children or backtrack
        if let Some(&(state, _)) = self.stack.last() {
            // Try to descend into children of current state
            if self.trie.ninfos[state as usize].first_child() != NINFO_NONE {
                let len = self.stack.len();
                self.stack[len - 1] = (state, 0);
                return self.descend_to_next_terminal();
            }
        }

        // No children — backtrack
        self.advance_from_stack()
    }

    /// Move to the previous key in lexicographic order.
    ///
    /// Uses parent-chain walk: O(key_depth) per call.
    /// After prev(), the stack is rebuilt for next() compatibility.
    pub fn prev(&mut self) -> bool {
        if !self.valid {
            return false;
        }
        self.valid = false;

        // Get current state from the actual trie (not stack — avoids stale state)
        let saved_key = self.current_key.clone();
        let mut state = match self.trie.lookup_state(&saved_key) {
            Some(s) => s,
            None => return false,
        };

        // Walk up parent chain looking for a lower path
        let mut depth = saved_key.len();

        loop {
            if state == 0 {
                // At root
                if depth == 0 {
                    // Empty key is current — nothing before it
                    self.current_key.clear();
                    self.stack.clear();
                    return false;
                }
                // Shouldn't reach here
                return false;
            }

            let parent = self.trie.states[state as usize].parent();
            let parent_base = self.trie.states[parent as usize].child0();
            let my_symbol = state ^ parent_base;
            depth -= 1;

            // Find highest sibling with symbol < my_symbol
            if let Some((ch, sibling)) = self.trie.prev_child(parent, my_symbol) {
                // Descend to rightmost terminal in sibling's subtree
                self.current_key.truncate(depth);
                self.current_key.push(ch);
                self.stack.clear();
                self.stack.push((sibling, 256));
                if self.descend_to_rightmost_terminal(sibling) {
                    self.rebuild_stack_from_key();
                    return true;
                }
                self.current_key.truncate(depth);
            }

            // No lower sibling — is parent terminal?
            if self.trie.ninfos[parent as usize].is_term() {
                self.current_key.truncate(depth);
                self.rebuild_stack_from_key();
                self.valid = true;
                return true;
            }

            state = parent;
        }
    }

    /// Rebuild stack from root following current_key (ensures next() works after prev()).
    fn rebuild_stack_from_key(&mut self) {
        let key = self.current_key.clone();
        self.stack.clear();

        let mut curr = 0u32;
        self.stack.push((0, 0));

        for &ch in key.iter() {
            let next = self.trie.state_move(curr, ch);
            if next == NIL_STATE {
                break;
            }
            let len = self.stack.len();
            self.stack[len - 1].1 = ch as u16 + 1;
            self.stack.push((next, 0));
            curr = next;
        }
    }

    /// Descend to the rightmost (lexicographically last) terminal in the subtree.
    /// Matching C++ decr's descent: always go to last child.
    fn descend_to_rightmost_terminal(&mut self, state: u32) -> bool {
        let mut curr = state;
        loop {
            match self.trie.last_child(curr) {
                Some((ch, next_state)) => {
                    let len = self.stack.len();
                    self.stack[len - 1].1 = ch as u16;
                    self.current_key.push(ch);
                    self.stack.push((next_state, 256));
                    curr = next_state;
                }
                None => {
                    // No children — is this state terminal?
                    if self.trie.ninfos[curr as usize].is_term() {
                        self.valid = true;
                        return true;
                    }
                    return false;
                }
            }
        }
    }

    // --- Internal helpers ---

    /// From current stack position, find the next child >= the symbol at stack top,
    /// then descend to the first terminal in that subtree.
    fn advance_from_stack(&mut self) -> bool {
        self.valid = false;

        while let Some(&mut (state, ref mut next_sym)) = self.stack.last_mut() {
            if *next_sym > 255 {
                self.stack.pop();
                self.current_key.pop();
                continue;
            }

            match self.trie.lower_bound_child(state, *next_sym as u8) {
                Some((ch, next_state)) => {
                    *next_sym = ch as u16 + 1;
                    self.current_key.push(ch);

                    if self.trie.ninfos[next_state as usize].is_term() {
                        self.stack.push((next_state, 0));
                        self.valid = true;
                        return true;
                    }

                    self.stack.push((next_state, 0));
                    // Continue descending in next iteration
                }
                None => {
                    self.stack.pop();
                    self.current_key.pop();
                }
            }
        }

        false
    }

    /// Descend from current stack top to the first (leftmost) terminal in the subtree.
    fn descend_to_next_terminal(&mut self) -> bool {
        loop {
            let &(state, _) = match self.stack.last() {
                Some(s) => s,
                None => return false,
            };

            match self.trie.first_child(state) {
                Some((ch, next_state)) => {
                    let len = self.stack.len();
                    self.stack[len - 1].1 = ch as u16 + 1;
                    self.current_key.push(ch);

                    if self.trie.ninfos[next_state as usize].is_term() {
                        self.stack.push((next_state, 0));
                        self.valid = true;
                        return true;
                    }

                    self.stack.push((next_state, 0));
                }
                None => {
                    return self.advance_from_stack();
                }
            }
        }
    }
}

impl DoubleArrayTrie {
    /// Create a bidirectional lexicographic cursor over this trie.
    #[inline]
    pub fn cursor(&self) -> DoubleArrayTrieCursor<'_> {
        DoubleArrayTrieCursor::new(self)
    }

    /// Iterate all keys in the lexicographic range `[from, to)`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zipora::fsa::double_array::DoubleArrayTrie;
    ///
    /// let mut trie = DoubleArrayTrie::new();
    /// for w in &["apple", "banana", "cherry", "date"] {
    ///     trie.insert(w.as_bytes()).unwrap();
    /// }
    ///
    /// let range: Vec<Vec<u8>> = trie.range(b"b", b"d").collect();
    /// assert_eq!(range.len(), 2); // banana, cherry
    /// ```
    pub fn range<'a>(&'a self, from: &[u8], to: &[u8]) -> RangeIter<'a> {
        let mut cursor = self.cursor();
        let valid = cursor.seek_lower_bound(from);
        RangeIter {
            cursor,
            upper_bound: to.to_vec(),
            started: valid,
        }
    }
}

/// Iterator over a lexicographic range `[from, to)` in a `DoubleArrayTrie`.
pub struct RangeIter<'a> {
    cursor: DoubleArrayTrieCursor<'a>,
    upper_bound: Vec<u8>,
    started: bool,
}

impl<'a> Iterator for RangeIter<'a> {
    type Item = Vec<u8>;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.started {
            return None;
        }

        if !self.cursor.is_valid() {
            return None;
        }

        let key = self.cursor.key();
        // Check upper bound (exclusive)
        if key >= self.upper_bound.as_slice() {
            return None;
        }

        let result = key.to_vec();
        self.started = self.cursor.next();
        Some(result)
    }
}