libdictenstein 0.1.0

High-performance dictionary data structures (trie, DAWG, double-array trie, suffix automaton, lock-free durable persistent ART) behind one trait API; pairs with liblevenshtein for fuzzy matching
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
//! Union zipper for multi-dictionary iteration.
//!
//! This module provides a `UnionZipper` that presents multiple dictionaries as a unified
//! view, allowing iteration over the union of terms as if they were merged. Includes
//! configurable value merge strategies for handling duplicate terms.
//!
//! # Module layout (post-C6 split)
//!
//! - [`merge_strategies`] — [`ValueMergeStrategy`], [`FirstWins`], [`LastWins`].
//! - [`lattice`] — re-exports the [`Lattice`] trait (from the `llattice` crate)
//!   plus the [`LatticeJoin`] / [`LatticeMeet`] merge-strategy adapters.
//! - This `mod.rs` — [`UnionZipper`] + [`UnionIterator`] + [`ValuedUnionIterator`] +
//!   [`UnionZipperExt`] / [`ValuedUnionZipperExt`] extension traits + tests.
//!
//! All sub-module items are re-exported here so existing
//! `use libdictenstein::union_zipper::FirstWins` etc. call sites keep
//! resolving.
//!
//! # Use Cases
//!
//! - **Multiple scopes in code completion**: Local + global dictionaries
//! - **Layered dictionaries**: Base + overrides
//! - **Multiple data sources**: Unified view of separate indexes
//!
//! # Examples
//!
//! ## Basic Union of Two Dictionaries
//!
//! ```rust
//! use libdictenstein::prelude::*;
//! use libdictenstein::union_zipper::{UnionZipper, UnionZipperExt};
//! use libdictenstein::double_array_trie_zipper::DoubleArrayTrieZipper;
//!
//! let dict1 = DoubleArrayTrie::from_terms(vec!["cat", "dog"].iter());
//! let dict2 = DoubleArrayTrie::from_terms(vec!["cat", "fish"].iter());
//!
//! let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
//! let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
//!
//! // Create union using extension trait
//! let union = z1.union_with(z2);
//!
//! // Iterate all unique terms (cat appears only once)
//! let mut results: Vec<String> = union.iter()
//!     .map(|(path, _)| String::from_utf8(path).unwrap())
//!     .collect();
//! results.sort();
//! assert_eq!(results, vec!["cat", "dog", "fish"]);
//! ```
//!
//! ## Navigating the Union
//!
//! ```rust
//! use libdictenstein::prelude::*;
//! use libdictenstein::union_zipper::{UnionZipper, UnionZipperExt};
//! use libdictenstein::double_array_trie_zipper::DoubleArrayTrieZipper;
//! use libdictenstein::zipper::DictZipper;
//!
//! let dict1 = DoubleArrayTrie::from_terms(vec!["cat", "car"].iter());
//! let dict2 = DoubleArrayTrie::from_terms(vec!["cab", "can"].iter());
//!
//! let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
//! let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
//!
//! let union = z1.union_with(z2);
//!
//! // Descend to 'c' -> 'a' and see children from both dictionaries
//! let ca = union.descend(b'c').and_then(|z| z.descend(b'a')).unwrap();
//! let children: Vec<u8> = ca.children().map(|(label, _)| label).collect();
//! assert!(children.contains(&b't')); // from dict1: "cat"
//! assert!(children.contains(&b'r')); // from dict1: "car"
//! assert!(children.contains(&b'b')); // from dict2: "cab"
//! assert!(children.contains(&b'n')); // from dict2: "can"
//! ```
//!
//! ## Value Merge Strategies
//!
//! ```rust
//! use libdictenstein::prelude::*;
//! use libdictenstein::union_zipper::{UnionZipper, FirstWins, LastWins};
//! use libdictenstein::double_array_trie_zipper::DoubleArrayTrieZipper;
//! use libdictenstein::zipper::{DictZipper, ValuedDictZipper};
//!
//! let dict1 = DoubleArrayTrie::from_terms_with_values(vec![("cat", 1), ("dog", 2)].into_iter());
//! let dict2 = DoubleArrayTrie::from_terms_with_values(vec![("cat", 10), ("fish", 3)].into_iter());
//!
//! let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
//! let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
//!
//! // FirstWins (default): "cat" -> 1
//! let union = UnionZipper::new(vec![z1.clone(), z2.clone()]);
//! let cat = union.descend(b'c')
//!     .and_then(|z| z.descend(b'a'))
//!     .and_then(|z| z.descend(b't'))
//!     .unwrap();
//! assert_eq!(cat.value(), Some(1));
//!
//! // LastWins: "cat" -> 10
//! let union = UnionZipper::with_strategy(vec![z1, z2], LastWins);
//! let cat = union.descend(b'c')
//!     .and_then(|z| z.descend(b'a'))
//!     .and_then(|z| z.descend(b't'))
//!     .unwrap();
//! assert_eq!(cat.value(), Some(10));
//! ```
//!
//! ## Composable with PrefixZipper
//!
//! ```rust
//! use libdictenstein::prelude::*;
//! use libdictenstein::union_zipper::UnionZipperExt;
//! use libdictenstein::prefix_zipper::PrefixZipper;
//! use libdictenstein::double_array_trie_zipper::DoubleArrayTrieZipper;
//!
//! let dict1 = DoubleArrayTrie::from_terms(vec!["process", "produce"].iter());
//! let dict2 = DoubleArrayTrie::from_terms(vec!["product", "program"].iter());
//!
//! let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
//! let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
//!
//! let union = z1.union_with(z2);
//!
//! // Use PrefixZipper on the union
//! let mut results: Vec<String> = union.with_prefix(b"pro")
//!     .unwrap()
//!     .map(|(path, _)| String::from_utf8(path).unwrap())
//!     .collect();
//! results.sort();
//! assert_eq!(results, vec!["process", "produce", "product", "program"]);
//! ```
//!
//! # Performance
//!
//! - **Navigation**: O(k × n) where k = prefix length, n = number of dictionaries
//! - **Children collection**: O(c × n) where c = max children per node, n = dictionaries
//! - **Iteration**: O(m) where m = total terms in union (with deduplication)
//! - **Memory**: O(n) for zipper storage + O(d) stack depth during iteration
//!
//! # Backend Compatibility
//!
//! Works uniformly across all dictionary backends via the `DictZipper` trait:
//! - `DoubleArrayTrie` (byte and char variants)
//! - `DynamicDawg` (byte and char variants)
//! - `PathMapDictionary` (byte variant)
//! - `SuffixAutomaton` (byte and char variants)

pub mod lattice;
pub mod merge_strategies;

use std::collections::HashSet;

use crate::zipper::{DictZipper, ValuedDictZipper};

// Re-exports for back-compat (callers `use libdictenstein::union_zipper::FirstWins`
// expect these names at the crate-public path).
pub use lattice::{Lattice, LatticeJoin, LatticeMeet};
pub use merge_strategies::{FirstWins, LastWins, ValueMergeStrategy};

// =============================================================================
// UnionZipper
// =============================================================================

/// A zipper that presents multiple dictionaries as a unified view.
///
/// `UnionZipper` wraps multiple zippers and presents their union as a single
/// navigable structure. Terms that exist in multiple dictionaries appear only
/// once during iteration.
///
/// # Type Parameters
///
/// * `Z` - The underlying zipper type (must implement `DictZipper`)
/// * `S` - The value merge strategy (defaults to `FirstWins`)
///
/// # Navigation
///
/// Navigation through the union considers all underlying dictionaries:
/// - `is_final()` returns true if ANY dictionary marks the position as final
/// - `descend(label)` succeeds if ANY dictionary has the path
/// - `children()` returns the union of all children from all dictionaries
///
/// # Examples
///
/// See module-level documentation for comprehensive examples.
#[derive(Clone, Debug)]
pub struct UnionZipper<Z: DictZipper, S = FirstWins> {
    /// The underlying zippers. `None` entries indicate dictionaries that don't
    /// have the current path.
    zippers: Vec<Option<Z>>,

    /// Path from root to current position.
    path: Vec<Z::Unit>,

    /// Value merge strategy.
    strategy: S,
}

impl<Z: DictZipper> UnionZipper<Z, FirstWins> {
    /// Create a new union zipper with the default `FirstWins` strategy.
    ///
    /// # Arguments
    ///
    /// * `zippers` - Zippers to union, each positioned at their respective roots
    ///
    /// # Returns
    ///
    /// A new `UnionZipper` positioned at the union root.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::prelude::*;
    /// use libdictenstein::union_zipper::UnionZipper;
    /// use libdictenstein::double_array_trie_zipper::DoubleArrayTrieZipper;
    ///
    /// let dict1 = DoubleArrayTrie::from_terms(vec!["cat", "dog"].iter());
    /// let dict2 = DoubleArrayTrie::from_terms(vec!["fish", "bird"].iter());
    ///
    /// let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
    /// let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
    ///
    /// let union = UnionZipper::new(vec![z1, z2]);
    /// ```
    pub fn new(zippers: Vec<Z>) -> Self {
        Self {
            zippers: zippers.into_iter().map(Some).collect(),
            path: Vec::new(),
            strategy: FirstWins,
        }
    }
}

impl<Z: DictZipper, S: Clone + Send + Sync> UnionZipper<Z, S> {
    /// Create a new union zipper with a custom merge strategy.
    ///
    /// # Arguments
    ///
    /// * `zippers` - Zippers to union, each positioned at their respective roots
    /// * `strategy` - The merge strategy for handling duplicate values
    ///
    /// # Returns
    ///
    /// A new `UnionZipper` with the specified strategy.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::prelude::*;
    /// use libdictenstein::union_zipper::{UnionZipper, LastWins};
    /// use libdictenstein::double_array_trie_zipper::DoubleArrayTrieZipper;
    ///
    /// let dict1 = DoubleArrayTrie::from_terms_with_values(vec![("cat", 1)].into_iter());
    /// let dict2 = DoubleArrayTrie::from_terms_with_values(vec![("cat", 10)].into_iter());
    ///
    /// let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
    /// let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
    ///
    /// let union = UnionZipper::with_strategy(vec![z1, z2], LastWins);
    /// ```
    pub fn with_strategy(zippers: Vec<Z>, strategy: S) -> Self {
        Self {
            zippers: zippers.into_iter().map(Some).collect(),
            path: Vec::new(),
            strategy,
        }
    }

    /// Get the number of underlying dictionaries.
    ///
    /// # Returns
    ///
    /// The total number of dictionaries in this union.
    pub fn dictionary_count(&self) -> usize {
        self.zippers.len()
    }

    /// Get the number of active dictionaries at the current position.
    ///
    /// A dictionary is "active" if it has the current path. This count decreases
    /// as you descend into paths that only exist in some dictionaries.
    ///
    /// # Returns
    ///
    /// The number of dictionaries that have the current path.
    pub fn active_dictionary_count(&self) -> usize {
        self.zippers.iter().filter(|z| z.is_some()).count()
    }

    /// Create an iterator over all terms in the union.
    ///
    /// Terms are yielded exactly once even if they exist in multiple dictionaries.
    /// The iteration order follows a depth-first traversal with sorted labels.
    ///
    /// # Returns
    ///
    /// An iterator yielding `(path, zipper)` pairs for each term.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::prelude::*;
    /// use libdictenstein::union_zipper::UnionZipperExt;
    /// use libdictenstein::double_array_trie_zipper::DoubleArrayTrieZipper;
    ///
    /// let dict1 = DoubleArrayTrie::from_terms(vec!["cat", "dog"].iter());
    /// let dict2 = DoubleArrayTrie::from_terms(vec!["cat", "fish"].iter());
    ///
    /// let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
    /// let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
    ///
    /// let union = z1.union_with(z2);
    /// let count = union.iter().count();
    /// assert_eq!(count, 3); // cat, dog, fish (no duplicates)
    /// ```
    pub fn iter(&self) -> UnionIterator<Z, S> {
        UnionIterator::new(self.clone())
    }
}

impl<Z: DictZipper, S: Clone + Send + Sync> DictZipper for UnionZipper<Z, S> {
    type Unit = Z::Unit;

    fn is_final(&self) -> bool {
        // Final if ANY dictionary marks this position as final
        self.zippers
            .iter()
            .any(|z| z.as_ref().is_some_and(|z| z.is_final()))
    }

    fn descend(&self, label: Self::Unit) -> Option<Self> {
        // Descend in all active zippers
        let new_zippers: Vec<Option<Z>> = self
            .zippers
            .iter()
            .map(|z| z.as_ref().and_then(|z| z.descend(label)))
            .collect();

        // Return new union if at least one zipper has the path
        if new_zippers.iter().any(|z| z.is_some()) {
            let mut new_path = self.path.clone();
            new_path.push(label);

            Some(Self {
                zippers: new_zippers,
                path: new_path,
                strategy: self.strategy.clone(),
            })
        } else {
            None
        }
    }

    fn children(&self) -> impl Iterator<Item = (Self::Unit, Self)> {
        // Collect unique labels from all active zippers
        let mut labels: Vec<Z::Unit> = self
            .zippers
            .iter()
            .filter_map(|z| z.as_ref())
            .flat_map(|z| z.children().map(|(label, _)| label))
            .collect();

        // Remove duplicates and sort for deterministic ordering
        labels.sort_by(|a, b| {
            // Use Debug trait for comparison since CharUnit doesn't require Ord
            // This works for u8, char, and u64 which all have natural ordering
            format!("{:?}", a).cmp(&format!("{:?}", b))
        });
        labels.dedup();

        // Create child zippers for each unique label
        let self_clone = self.clone();
        labels
            .into_iter()
            .filter_map(move |label| self_clone.descend(label).map(|child| (label, child)))
    }

    fn path(&self) -> Vec<Self::Unit> {
        self.path.clone()
    }
}

impl<Z: ValuedDictZipper, S: ValueMergeStrategy<Z::Value> + Clone + Send + Sync> ValuedDictZipper
    for UnionZipper<Z, S>
{
    type Value = Z::Value;

    fn value(&self) -> Option<Self::Value> {
        // Collect values from all active zippers that are final
        let mut result: Option<Z::Value> = None;

        for zipper in self.zippers.iter().filter_map(|z| z.as_ref()) {
            if let Some(v) = zipper.value() {
                result = Some(match result {
                    Some(existing) => self.strategy.merge(existing, v),
                    None => v,
                });
            }
        }

        result
    }
}

// =============================================================================
// UnionIterator
// =============================================================================

/// Iterator over all terms in a union of dictionaries.
///
/// This iterator performs depth-first traversal and yields each unique term
/// exactly once, even if it exists in multiple underlying dictionaries.
///
/// # Type Parameters
///
/// * `Z` - The underlying zipper type
/// * `S` - The value merge strategy
///
/// # Iterator Item
///
/// Returns `(Vec<Z::Unit>, UnionZipper<Z, S>)`:
/// - `Vec<Z::Unit>` - Complete path (term) as sequence of units
/// - `UnionZipper<Z, S>` - Zipper positioned at the final node
///
/// # Deduplication
///
/// Deduplication is path-based: when a term is yielded, its path is recorded
/// in a HashSet to prevent duplicate yields.
pub struct UnionIterator<Z: DictZipper, S = FirstWins> {
    /// DFS traversal stack
    stack: Vec<UnionZipper<Z, S>>,

    /// Paths already yielded (for deduplication)
    seen: HashSet<Vec<Z::Unit>>,
}

impl<Z: DictZipper, S: Clone + Send + Sync> UnionIterator<Z, S> {
    /// Create a new iterator starting from the given union zipper.
    fn new(zipper: UnionZipper<Z, S>) -> Self {
        let mut stack = Vec::with_capacity(16);
        stack.push(zipper);
        Self {
            stack,
            seen: HashSet::new(),
        }
    }
}

impl<Z: DictZipper, S: Clone + Send + Sync> Iterator for UnionIterator<Z, S> {
    type Item = (Vec<Z::Unit>, UnionZipper<Z, S>);

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(zipper) = self.stack.pop() {
            // Push all children onto stack for continued DFS traversal
            for (_label, child) in zipper.children() {
                self.stack.push(child);
            }

            // If this is a complete term and we haven't seen it, yield it
            if zipper.is_final() {
                let path = zipper.path();
                if self.seen.insert(path.clone()) {
                    return Some((path, zipper));
                }
            }
        }

        None
    }
}

// =============================================================================
// ValuedUnionIterator
// =============================================================================

/// Iterator over (term, value) pairs in a union of valued dictionaries.
///
/// This iterator wraps `UnionIterator` and extracts merged values from final
/// nodes using the configured merge strategy.
///
/// # Type Parameters
///
/// * `Z` - The underlying valued zipper type
/// * `S` - The value merge strategy
///
/// # Iterator Item
///
/// Returns `(Vec<Z::Unit>, Z::Value)`:
/// - `Vec<Z::Unit>` - Complete path (term) as sequence of units
/// - `Z::Value` - Merged value for this term
pub struct ValuedUnionIterator<Z: ValuedDictZipper, S> {
    inner: UnionIterator<Z, S>,
}

impl<Z: ValuedDictZipper, S: ValueMergeStrategy<Z::Value> + Clone + Send + Sync>
    ValuedUnionIterator<Z, S>
{
    /// Create a new valued iterator from a union zipper.
    pub fn new(zipper: UnionZipper<Z, S>) -> Self {
        Self {
            inner: UnionIterator::new(zipper),
        }
    }
}

impl<Z: ValuedDictZipper, S: ValueMergeStrategy<Z::Value> + Clone + Send + Sync> Iterator
    for ValuedUnionIterator<Z, S>
{
    type Item = (Vec<Z::Unit>, Z::Value);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (path, zipper) = self.inner.next()?;
            if let Some(value) = zipper.value() {
                return Some((path, value));
            }
            // Continue if no value (shouldn't happen for valid final nodes, but be safe)
        }
    }
}

// =============================================================================
// UnionZipperExt Extension Trait
// =============================================================================

/// Extension trait for ergonomic union zipper creation.
///
/// This trait is automatically implemented for all `DictZipper` types, providing
/// convenient methods to create union zippers.
///
/// # Examples
///
/// ```rust
/// use libdictenstein::prelude::*;
/// use libdictenstein::union_zipper::UnionZipperExt;
/// use libdictenstein::double_array_trie_zipper::DoubleArrayTrieZipper;
///
/// let dict1 = DoubleArrayTrie::from_terms(vec!["cat", "dog"].iter());
/// let dict2 = DoubleArrayTrie::from_terms(vec!["fish", "bird"].iter());
///
/// let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
/// let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
///
/// // Simple two-zipper union
/// let union = z1.clone().union_with(z2.clone());
///
/// // Multi-zipper union
/// let dict3 = DoubleArrayTrie::from_terms(vec!["elephant"].iter());
/// let z3 = DoubleArrayTrieZipper::new_from_dict(&dict3);
/// let multi_union = z1.union_all(vec![z2, z3]);
/// ```
pub trait UnionZipperExt: DictZipper + Sized {
    /// Create a union of this zipper with another.
    ///
    /// # Arguments
    ///
    /// * `other` - Another zipper of the same type
    ///
    /// # Returns
    ///
    /// A `UnionZipper` combining both dictionaries with `FirstWins` strategy.
    fn union_with(self, other: Self) -> UnionZipper<Self> {
        UnionZipper::new(vec![self, other])
    }

    /// Create a union of this zipper with multiple others.
    ///
    /// # Arguments
    ///
    /// * `others` - Additional zippers to include in the union
    ///
    /// # Returns
    ///
    /// A `UnionZipper` combining all dictionaries with `FirstWins` strategy.
    fn union_all(self, others: impl IntoIterator<Item = Self>) -> UnionZipper<Self> {
        let mut zippers = vec![self];
        zippers.extend(others);
        UnionZipper::new(zippers)
    }
}

/// Blanket implementation: all DictZippers automatically get UnionZipperExt support.
impl<Z: DictZipper> UnionZipperExt for Z {}

// =============================================================================
// ValuedUnionZipperExt Extension Trait
// =============================================================================

/// Extension trait for valued union zipper iteration.
///
/// This trait is automatically implemented for all `ValuedDictZipper` types,
/// providing methods to iterate with values using merge strategies.
pub trait ValuedUnionZipperExt: ValuedDictZipper + Sized {
    /// Create a union of this zipper with another, using a custom strategy.
    ///
    /// # Arguments
    ///
    /// * `other` - Another zipper of the same type
    /// * `strategy` - The merge strategy for duplicate values
    ///
    /// # Returns
    ///
    /// A `UnionZipper` with the specified strategy.
    fn union_with_strategy<S: ValueMergeStrategy<Self::Value> + Clone + Send + Sync>(
        self,
        other: Self,
        strategy: S,
    ) -> UnionZipper<Self, S> {
        UnionZipper::with_strategy(vec![self, other], strategy)
    }
}

/// Blanket implementation: all ValuedDictZippers get ValuedUnionZipperExt support.
impl<Z: ValuedDictZipper> ValuedUnionZipperExt for Z {}

// =============================================================================
// Unit Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::double_array_trie::DoubleArrayTrie;
    use crate::double_array_trie_zipper::DoubleArrayTrieZipper;

    fn sorted_strings(mut v: Vec<String>) -> Vec<String> {
        v.sort();
        v
    }

    #[test]
    fn test_union_basic() {
        let dict1 = DoubleArrayTrie::from_terms(vec!["cat", "dog"].iter());
        let dict2 = DoubleArrayTrie::from_terms(vec!["fish", "bird"].iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::new(vec![z1, z2]);

        let results: Vec<String> = sorted_strings(
            union
                .iter()
                .map(|(path, _)| String::from_utf8(path).unwrap())
                .collect(),
        );

        assert_eq!(results, vec!["bird", "cat", "dog", "fish"]);
    }

    #[test]
    fn test_union_with_overlap() {
        let dict1 = DoubleArrayTrie::from_terms(vec!["cat", "dog"].iter());
        let dict2 = DoubleArrayTrie::from_terms(vec!["cat", "fish"].iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = z1.union_with(z2);

        let results: Vec<String> = sorted_strings(
            union
                .iter()
                .map(|(path, _)| String::from_utf8(path).unwrap())
                .collect(),
        );

        // "cat" should appear only once
        assert_eq!(results, vec!["cat", "dog", "fish"]);
    }

    #[test]
    fn test_union_descend() {
        let dict1 = DoubleArrayTrie::from_terms(vec!["cat", "car"].iter());
        let dict2 = DoubleArrayTrie::from_terms(vec!["cab", "can"].iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = z1.union_with(z2);

        // Navigate to 'c' -> 'a'
        let ca = union
            .descend(b'c')
            .and_then(|z| z.descend(b'a'))
            .expect("Should be able to descend to 'ca'");

        // Should have children from both dictionaries
        let mut children: Vec<u8> = ca.children().map(|(label, _)| label).collect();
        children.sort();

        assert_eq!(children, vec![b'b', b'n', b'r', b't']);
    }

    #[test]
    fn test_union_is_final() {
        let dict1 = DoubleArrayTrie::from_terms(vec!["cat"].iter());
        let dict2 = DoubleArrayTrie::from_terms(vec!["dog"].iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = z1.union_with(z2);

        // Navigate to "cat"
        let cat = union
            .descend(b'c')
            .and_then(|z| z.descend(b'a'))
            .and_then(|z| z.descend(b't'))
            .expect("Should find 'cat'");

        assert!(cat.is_final());
        assert_eq!(cat.path(), b"cat".to_vec());
    }

    #[test]
    fn test_union_nonexistent_path() {
        let dict1 = DoubleArrayTrie::from_terms(vec!["cat"].iter());
        let dict2 = DoubleArrayTrie::from_terms(vec!["dog"].iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = z1.union_with(z2);

        // Try to navigate to 'x' - doesn't exist in either
        assert!(union.descend(b'x').is_none());
    }

    #[test]
    fn test_union_empty_dictionaries() {
        let dict1: DoubleArrayTrie = DoubleArrayTrie::new();
        let dict2: DoubleArrayTrie = DoubleArrayTrie::new();

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = z1.union_with(z2);

        let count = union.iter().count();
        assert_eq!(count, 0);
    }

    #[test]
    fn test_union_one_empty() {
        let dict1 = DoubleArrayTrie::from_terms(vec!["cat", "dog"].iter());
        let dict2: DoubleArrayTrie = DoubleArrayTrie::new();

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = z1.union_with(z2);

        let results: Vec<String> = sorted_strings(
            union
                .iter()
                .map(|(path, _)| String::from_utf8(path).unwrap())
                .collect(),
        );

        assert_eq!(results, vec!["cat", "dog"]);
    }

    #[test]
    fn test_valued_union_first_wins() {
        let dict1 =
            DoubleArrayTrie::from_terms_with_values(vec![("cat", 1usize), ("dog", 2)].into_iter());
        let dict2 = DoubleArrayTrie::from_terms_with_values(
            vec![("cat", 10usize), ("fish", 3)].into_iter(),
        );

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::new(vec![z1, z2]);

        // Navigate to "cat"
        let cat = union
            .descend(b'c')
            .and_then(|z| z.descend(b'a'))
            .and_then(|z| z.descend(b't'))
            .expect("Should find 'cat'");

        // FirstWins: should get value 1 from dict1
        assert_eq!(cat.value(), Some(1));
    }

    #[test]
    fn test_valued_union_last_wins() {
        let dict1 =
            DoubleArrayTrie::from_terms_with_values(vec![("cat", 1usize), ("dog", 2)].into_iter());
        let dict2 = DoubleArrayTrie::from_terms_with_values(
            vec![("cat", 10usize), ("fish", 3)].into_iter(),
        );

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::with_strategy(vec![z1, z2], LastWins);

        // Navigate to "cat"
        let cat = union
            .descend(b'c')
            .and_then(|z| z.descend(b'a'))
            .and_then(|z| z.descend(b't'))
            .expect("Should find 'cat'");

        // LastWins: should get value 10 from dict2
        assert_eq!(cat.value(), Some(10));
    }

    #[test]
    fn test_valued_union_iterator() {
        let dict1 =
            DoubleArrayTrie::from_terms_with_values(vec![("cat", 1usize), ("dog", 2)].into_iter());
        let dict2 = DoubleArrayTrie::from_terms_with_values(
            vec![("cat", 10usize), ("fish", 3)].into_iter(),
        );

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::new(vec![z1, z2]);
        let valued_iter = ValuedUnionIterator::new(union);

        let mut results: Vec<(String, usize)> = valued_iter
            .map(|(path, val)| (String::from_utf8(path).unwrap(), val))
            .collect();

        results.sort_by(|a, b| a.0.cmp(&b.0));

        assert_eq!(
            results,
            vec![
                ("cat".to_string(), 1), // FirstWins
                ("dog".to_string(), 2),
                ("fish".to_string(), 3),
            ]
        );
    }

    #[test]
    fn test_union_all() {
        let dict1 = DoubleArrayTrie::from_terms(vec!["cat"].iter());
        let dict2 = DoubleArrayTrie::from_terms(vec!["dog"].iter());
        let dict3 = DoubleArrayTrie::from_terms(vec!["fish"].iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
        let z3 = DoubleArrayTrieZipper::new_from_dict(&dict3);

        let union = z1.union_all(vec![z2, z3]);

        let results: Vec<String> = sorted_strings(
            union
                .iter()
                .map(|(path, _)| String::from_utf8(path).unwrap())
                .collect(),
        );

        assert_eq!(results, vec!["cat", "dog", "fish"]);
    }

    #[test]
    fn test_dictionary_count() {
        let dict1 = DoubleArrayTrie::from_terms(vec!["cat"].iter());
        let dict2 = DoubleArrayTrie::from_terms(vec!["dog"].iter());
        let dict3 = DoubleArrayTrie::from_terms(vec!["fish"].iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
        let z3 = DoubleArrayTrieZipper::new_from_dict(&dict3);

        let union = z1.union_all(vec![z2, z3]);

        assert_eq!(union.dictionary_count(), 3);
        assert_eq!(union.active_dictionary_count(), 3);

        // After descending to 'c', only dict1 is active
        let c = union.descend(b'c').unwrap();
        assert_eq!(c.dictionary_count(), 3);
        assert_eq!(c.active_dictionary_count(), 1);
    }

    #[test]
    fn test_custom_merge_strategy() {
        #[derive(Clone)]
        struct Sum;

        impl ValueMergeStrategy<usize> for Sum {
            fn merge(&self, existing: usize, new: usize) -> usize {
                existing + new
            }
        }

        let dict1 = DoubleArrayTrie::from_terms_with_values(vec![("cat", 1usize)].into_iter());
        let dict2 = DoubleArrayTrie::from_terms_with_values(vec![("cat", 10usize)].into_iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::with_strategy(vec![z1, z2], Sum);

        let cat = union
            .descend(b'c')
            .and_then(|z| z.descend(b'a'))
            .and_then(|z| z.descend(b't'))
            .expect("Should find 'cat'");

        // Sum: should get 1 + 10 = 11
        assert_eq!(cat.value(), Some(11));
    }

    // =========================================================================
    // Lattice Trait Tests
    // =========================================================================

    #[test]
    fn test_lattice_numeric_u32() {
        // join = max, meet = min
        assert_eq!(5u32.join(&3), 5);
        assert_eq!(3u32.join(&5), 5);
        assert_eq!(5u32.meet(&3), 3);
        assert_eq!(3u32.meet(&5), 3);

        // Idempotency
        assert_eq!(5u32.join(&5), 5);
        assert_eq!(5u32.meet(&5), 5);
    }

    #[test]
    fn test_lattice_numeric_i32() {
        // Negative numbers
        assert_eq!((-5i32).join(&3), 3);
        assert_eq!((-5i32).meet(&3), -5);
    }

    #[test]
    fn test_lattice_numeric_f64() {
        assert_eq!(5.0f64.join(&3.0), 5.0);
        assert_eq!(5.0f64.meet(&3.0), 3.0);
    }

    #[test]
    fn test_lattice_bool() {
        // join = OR
        assert!(true.join(&false));
        assert!(false.join(&true));
        assert!(true.join(&true));
        assert!(!false.join(&false));

        // meet = AND
        assert!(true.meet(&true));
        assert!(!true.meet(&false));
        assert!(!false.meet(&true));
        assert!(!false.meet(&false));
    }

    #[test]
    fn test_lattice_option() {
        let some_5 = Some(5u32);
        let some_3 = Some(3u32);
        let none: Option<u32> = None;

        // join: Some if either Some
        assert_eq!(some_5.join(&some_3), Some(5)); // max
        assert_eq!(some_5.join(&none), Some(5));
        assert_eq!(none.join(&some_3), Some(3));
        assert_eq!(none.join(&none), None);

        // meet: Some only if both Some
        assert_eq!(some_5.meet(&some_3), Some(3)); // min
        assert_eq!(some_5.meet(&none), None);
        assert_eq!(none.meet(&some_3), None);
        assert_eq!(none.meet(&none), None);
    }

    #[test]
    fn test_lattice_hashset() {
        let set1: HashSet<i32> = [1, 2, 3].into_iter().collect();
        let set2: HashSet<i32> = [2, 3, 4].into_iter().collect();

        // join = union
        let joined = set1.join(&set2);
        assert_eq!(joined, [1, 2, 3, 4].into_iter().collect());

        // meet = intersection
        let met = set1.meet(&set2);
        assert_eq!(met, [2, 3].into_iter().collect());
    }

    #[test]
    fn test_lattice_hashset_disjoint() {
        let set1: HashSet<i32> = [1, 2].into_iter().collect();
        let set2: HashSet<i32> = [3, 4].into_iter().collect();

        let joined = set1.join(&set2);
        assert_eq!(joined, [1, 2, 3, 4].into_iter().collect());

        let met = set1.meet(&set2);
        assert!(met.is_empty());
    }

    #[test]
    fn test_lattice_vec() {
        let vec1 = vec![1, 2, 3];
        let vec2 = vec![2, 3, 4];

        // join = concat + dedup
        let joined = vec1.join(&vec2);
        assert_eq!(joined, vec![1, 2, 3, 4]);

        // meet = intersection preserving order
        let met = vec1.meet(&vec2);
        assert_eq!(met, vec![2, 3]);
    }

    #[test]
    fn test_lattice_vec_preserves_order() {
        let vec1 = vec![3, 1, 2];
        let vec2 = vec![4, 2, 1];

        // join preserves order of first, then appends new elements
        let joined = vec1.join(&vec2);
        assert_eq!(joined, vec![3, 1, 2, 4]);

        // meet preserves order of first
        let met = vec1.meet(&vec2);
        assert_eq!(met, vec![1, 2]);
    }

    // =========================================================================
    // LatticeJoin / LatticeMeet Strategy Tests
    // =========================================================================

    #[test]
    fn test_lattice_join_strategy_numeric() {
        let dict1 = DoubleArrayTrie::from_terms_with_values(vec![("score", 85u32)].into_iter());
        let dict2 = DoubleArrayTrie::from_terms_with_values(vec![("score", 92u32)].into_iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::with_strategy(vec![z1, z2], LatticeJoin);

        let score = union
            .descend(b's')
            .and_then(|z| z.descend(b'c'))
            .and_then(|z| z.descend(b'o'))
            .and_then(|z| z.descend(b'r'))
            .and_then(|z| z.descend(b'e'))
            .expect("Should find 'score'");

        // LatticeJoin: max(85, 92) = 92
        assert_eq!(score.value(), Some(92));
    }

    #[test]
    fn test_lattice_meet_strategy_numeric() {
        let dict1 = DoubleArrayTrie::from_terms_with_values(vec![("score", 85u32)].into_iter());
        let dict2 = DoubleArrayTrie::from_terms_with_values(vec![("score", 92u32)].into_iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::with_strategy(vec![z1, z2], LatticeMeet);

        let score = union
            .descend(b's')
            .and_then(|z| z.descend(b'c'))
            .and_then(|z| z.descend(b'o'))
            .and_then(|z| z.descend(b'r'))
            .and_then(|z| z.descend(b'e'))
            .expect("Should find 'score'");

        // LatticeMeet: min(85, 92) = 85
        assert_eq!(score.value(), Some(85));
    }

    #[test]
    fn test_lattice_join_strategy_hashset() {
        let dict1 = DoubleArrayTrie::from_terms_with_values(
            vec![("key", HashSet::from([1, 2]))].into_iter(),
        );
        let dict2 = DoubleArrayTrie::from_terms_with_values(
            vec![("key", HashSet::from([2, 3]))].into_iter(),
        );

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::with_strategy(vec![z1, z2], LatticeJoin);

        let key = union
            .descend(b'k')
            .and_then(|z| z.descend(b'e'))
            .and_then(|z| z.descend(b'y'))
            .expect("Should find 'key'");

        // LatticeJoin: {1, 2} ∪ {2, 3} = {1, 2, 3}
        assert_eq!(key.value(), Some(HashSet::from([1, 2, 3])));
    }

    #[test]
    fn test_lattice_meet_strategy_hashset() {
        let dict1 = DoubleArrayTrie::from_terms_with_values(
            vec![("key", HashSet::from([1, 2, 3]))].into_iter(),
        );
        let dict2 = DoubleArrayTrie::from_terms_with_values(
            vec![("key", HashSet::from([2, 3, 4]))].into_iter(),
        );

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::with_strategy(vec![z1, z2], LatticeMeet);

        let key = union
            .descend(b'k')
            .and_then(|z| z.descend(b'e'))
            .and_then(|z| z.descend(b'y'))
            .expect("Should find 'key'");

        // LatticeMeet: {1, 2, 3} ∩ {2, 3, 4} = {2, 3}
        assert_eq!(key.value(), Some(HashSet::from([2, 3])));
    }

    #[test]
    fn test_lattice_join_three_dicts() {
        let dict1 =
            DoubleArrayTrie::from_terms_with_values(vec![("ctx", HashSet::from([1]))].into_iter());
        let dict2 =
            DoubleArrayTrie::from_terms_with_values(vec![("ctx", HashSet::from([2]))].into_iter());
        let dict3 =
            DoubleArrayTrie::from_terms_with_values(vec![("ctx", HashSet::from([3]))].into_iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
        let z3 = DoubleArrayTrieZipper::new_from_dict(&dict3);

        let union = UnionZipper::with_strategy(vec![z1, z2, z3], LatticeJoin);

        let ctx = union
            .descend(b'c')
            .and_then(|z| z.descend(b't'))
            .and_then(|z| z.descend(b'x'))
            .expect("Should find 'ctx'");

        // LatticeJoin: {1} ∪ {2} ∪ {3} = {1, 2, 3}
        assert_eq!(ctx.value(), Some(HashSet::from([1, 2, 3])));
    }

    #[test]
    fn test_lattice_meet_three_dicts() {
        let dict1 = DoubleArrayTrie::from_terms_with_values(
            vec![("ctx", HashSet::from([1, 2, 3, 4]))].into_iter(),
        );
        let dict2 = DoubleArrayTrie::from_terms_with_values(
            vec![("ctx", HashSet::from([2, 3, 4, 5]))].into_iter(),
        );
        let dict3 = DoubleArrayTrie::from_terms_with_values(
            vec![("ctx", HashSet::from([3, 4, 5, 6]))].into_iter(),
        );

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);
        let z3 = DoubleArrayTrieZipper::new_from_dict(&dict3);

        let union = UnionZipper::with_strategy(vec![z1, z2, z3], LatticeMeet);

        let ctx = union
            .descend(b'c')
            .and_then(|z| z.descend(b't'))
            .and_then(|z| z.descend(b'x'))
            .expect("Should find 'ctx'");

        // LatticeMeet: {1,2,3,4} ∩ {2,3,4,5} ∩ {3,4,5,6} = {3, 4}
        assert_eq!(ctx.value(), Some(HashSet::from([3, 4])));
    }

    #[test]
    fn test_lattice_join_with_bool() {
        let dict1 = DoubleArrayTrie::from_terms_with_values(vec![("flag", false)].into_iter());
        let dict2 = DoubleArrayTrie::from_terms_with_values(vec![("flag", true)].into_iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::with_strategy(vec![z1, z2], LatticeJoin);

        let flag = union
            .descend(b'f')
            .and_then(|z| z.descend(b'l'))
            .and_then(|z| z.descend(b'a'))
            .and_then(|z| z.descend(b'g'))
            .expect("Should find 'flag'");

        // LatticeJoin (bool): false OR true = true
        assert_eq!(flag.value(), Some(true));
    }

    #[test]
    fn test_lattice_meet_with_bool() {
        let dict1 = DoubleArrayTrie::from_terms_with_values(vec![("flag", false)].into_iter());
        let dict2 = DoubleArrayTrie::from_terms_with_values(vec![("flag", true)].into_iter());

        let z1 = DoubleArrayTrieZipper::new_from_dict(&dict1);
        let z2 = DoubleArrayTrieZipper::new_from_dict(&dict2);

        let union = UnionZipper::with_strategy(vec![z1, z2], LatticeMeet);

        let flag = union
            .descend(b'f')
            .and_then(|z| z.descend(b'l'))
            .and_then(|z| z.descend(b'a'))
            .and_then(|z| z.descend(b'g'))
            .expect("Should find 'flag'");

        // LatticeMeet (bool): false AND true = false
        assert_eq!(flag.value(), Some(false));
    }
}