liblevenshtein 0.9.1

Levenshtein/Universal Automata for approximate string matching using various dictionary backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
//! Universal State Type
//!
//! Implements universal states from Mitankin's thesis (Definition 15, pages 38-39).
//!
//! # Theory Background
//!
//! Universal states are sets of universal positions that maintain the anti-chain property:
//! no position subsumes another. This enables efficient state minimization.
//!
//! ## State Sets (Page 38)
//!
//! **Non-final states**:
//! ```text
//! I^χ_states = {Q | Q ⊆ I^χ_s ∧ ∀q₁,q₂ ∈ Q (q₁ ⊀^χ_s q₂)} \ {∅}
//! ```
//!
//! **Final states**:
//! ```text
//! M^χ_states = {Q | Q ⊆ M^χ_s ∧
//!               ∀q₁,q₂ ∈ Q (q₁ ⊀^χ_s q₂) ∧
//!               ∃q ∈ Q (q ≤^χ_s M#n) ∧
//!               ∃i ∈ [-n, 0] ∀q ∈ Q (M + i#0 ≤^χ_s q)} \ {∅}
//! ```
//!
//! **All states**:
//! ```text
//! Q^∀,χ_n = I^χ_states ∪ M^χ_states
//! ```
//!
//! ## Anti-chain Property
//!
//! For all positions p₁, p₂ in state Q:
//! - p₁ ⊀^χ_s p₂ (p₁ does not subsume p₂)
//! - p₂ ⊀^χ_s p₁ (p₂ does not subsume p₁)
//!
//! This is maintained by the ⊔ (join) operator when adding positions.
//!
//! # Examples
//!
//! ```ignore
//! use liblevenshtein::transducer::universal::{UniversalState, UniversalPosition, Standard};
//!
//! // Create initial state: {I + 0#0}
//! let initial = UniversalState::<Standard>::initial(2);
//! assert!(!initial.is_final());
//!
//! // Create state with multiple positions
//! let mut state = UniversalState::<Standard>::new(2);
//! state.add_position(UniversalPosition::new_i(0, 0, 2)?);
//! state.add_position(UniversalPosition::new_i(1, 1, 2)?);
//! ```

use smallvec::SmallVec;
use std::fmt;

use crate::transducer::universal::position::{PositionVariant, UniversalPosition};
use crate::transducer::universal::subsumption::subsumes;

/// Universal state maintaining anti-chain property
///
/// A state is a set of universal positions where no position subsumes another.
/// This implements Q^∀,χ_n from the thesis (Definition 15, pages 38-39).
///
/// # Type Parameters
///
/// - `V`: Position variant (Standard, Transposition, or MergeAndSplit)
///
/// # Invariant
///
/// For all p₁, p₂ ∈ positions: p₁ ⊀^χ_s p₂ ∧ p₂ ⊀^χ_s p₁
///
/// This invariant is maintained by `add_position()` using the ⊔ operator.
///
/// # SmallVec Optimization
///
/// Uses SmallVec with inline size of 8 to avoid heap allocations for typical states.
/// This optimization is theoretically justified by the **bounded diagonal property**
/// (Theorem 8.2, Mitankin et al., TCS 2011).
///
/// For Standard Levenshtein with error bound n=2:
/// - Diagonal bound c = 2
/// - Band width = 2c + 1 = 5 diagonals
/// - Typical state size ≤ 8 positions (with subsumption)
///
/// This is not empirical tuning — it's a mathematical guarantee. The bounded diagonal
/// property proves that for operations with bounded length difference, positions
/// cluster around the main diagonal in the DP matrix, creating a bounded "band" of
/// active positions independent of word length.
///
/// ## Theoretical Foundation
///
/// **Theorem 8.2** (TCS 2011, Page 2348): The following are equivalent:
/// 1. R[Op,r] has bounded length difference
/// 2. There exists constant c such that every Op instance satisfies c-bounded diagonal property
/// 3. Every zero-weighted type in Υ is length preserving
///
/// For Standard Levenshtein: c = 2, yielding O(n²) state space (independent of word length).
///
/// ## References
///
/// - Mitankin, P., Mihov, S., Schulz, K.U. (2011). "Deciding Word Neighborhood
///   with Universal Neighborhood Automata". *Theoretical Computer Science*,
///   410(37-39):2339-2358.
/// - See: `docs/research/universal-levenshtein/TCS_2011_PAPER_ANALYSIS.md`
///   Section 2 for detailed analysis
///
/// # Implementation Note
///
/// Maintains sorted order manually via binary search + insertion.
/// This provides:
/// - Stack allocation for 90%+ of states (inline capacity 8)
/// - Excellent cache locality (contiguous memory)
/// - Online subsumption during insertion (O(kn) typical, k << n)
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UniversalState<V: PositionVariant> {
    /// Set of positions (anti-chain), maintained in sorted order
    /// SmallVec avoids heap allocation for states with ≤8 positions
    positions: SmallVec<[UniversalPosition<V>; 8]>,

    /// Maximum edit distance n
    max_distance: u8,

    /// Length difference m = |query| - |dict| tracking
    ///
    /// Tracks position relative to diagonal in the DP matrix.
    /// Range: `[-max_distance, +max_distance]` (bounded diagonal property).
    ///
    /// This field enables correct diagonal crossing detection:
    /// - When `|length_diff| > max_distance`, positions must be converted (I → M or M → I)
    /// - See: `docs/research/universal-levenshtein/DIAGONAL_CROSSING_BUG_ANALYSIS.md`
    ///
    /// Updated during `transition_with_consumption()` based on character consumption.
    length_diff: i8,
}

impl<V: PositionVariant> UniversalState<V> {
    /// Create new empty state
    ///
    /// # Arguments
    ///
    /// - `max_distance`: Maximum edit distance n
    ///
    /// # Example
    ///
    /// ```ignore
    /// let state = UniversalState::<Standard>::new(2);
    /// assert!(state.is_empty());
    /// ```
    pub fn new(max_distance: u8) -> Self {
        Self {
            positions: SmallVec::new(),
            max_distance,
            length_diff: 0,
        }
    }

    /// Create initial state {I + 0#0}
    ///
    /// From thesis page 38: Initial state I^∀,χ = {I + 0#0}
    ///
    /// # Arguments
    ///
    /// - `max_distance`: Maximum edit distance n
    ///
    /// # Example
    ///
    /// ```ignore
    /// let initial = UniversalState::<Standard>::initial(2);
    /// assert_eq!(initial.len(), 1);
    /// assert!(!initial.is_final());
    /// ```
    pub fn initial(max_distance: u8) -> Self {
        let mut state = Self::new(max_distance);
        // I + 0#0 always satisfies invariant, so unwrap is safe
        let initial_pos =
            UniversalPosition::new_i(0, 0, max_distance).expect("I + 0#0 should always be valid");
        state.positions.push(initial_pos);
        state
    }

    /// Add position, maintaining anti-chain property (⊔ operator)
    ///
    /// Implements the subsumption closure from the thesis:
    /// 1. Remove all positions that subsume the new position (worse positions)
    /// 2. Add new position if it doesn't subsume any existing position
    ///
    /// This maintains the invariant ∀p₁,p₂ ∈ Q (p₁ ⊀^χ_s p₂).
    ///
    /// Note: If p1 <^χ_s p2, then p1 subsumes p2, meaning p2 is "better" (more errors).
    /// We keep the better positions and discard the worse ones.
    ///
    /// # Arguments
    ///
    /// - `pos`: Position to add
    ///
    /// # Example
    ///
    /// ```ignore
    /// let mut state = UniversalState::<Standard>::new(2);
    /// state.add_position(UniversalPosition::new_i(0, 0, 2)?);
    /// state.add_position(UniversalPosition::new_i(1, 1, 2)?);
    /// ```
    pub fn add_position(&mut self, pos: UniversalPosition<V>) {
        // Check if this position is subsumed by an existing one
        for existing in &self.positions {
            if subsumes(existing, &pos, self.max_distance) {
                return; // Already covered by existing position
            }
        }

        // Remove any positions that this new position subsumes
        self.positions
            .retain(|p| !subsumes(&pos, p, self.max_distance));

        // Insert in sorted position (binary search)
        let insert_pos = self.positions.binary_search(&pos).unwrap_or_else(|pos| pos);
        self.positions.insert(insert_pos, pos);
    }

    /// Check if state is empty
    pub fn is_empty(&self) -> bool {
        self.positions.is_empty()
    }

    /// Get number of positions in state
    pub fn len(&self) -> usize {
        self.positions.len()
    }

    /// Get iterator over positions
    pub fn positions(&self) -> impl Iterator<Item = &UniversalPosition<V>> {
        self.positions.iter()
    }

    /// Check if state contains a position
    pub fn contains(&self, pos: &UniversalPosition<V>) -> bool {
        self.positions.iter().any(|p| p == pos)
    }

    /// Check if state is final
    ///
    /// A state is final if it contains an M-type position that could accept.
    /// From the thesis, this means the state contains a position subsuming M + 0#k
    /// for some k ≤ n.
    ///
    /// For simplicity in Phase 1, we check if there exists an M-type position with offset ≤ 0.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let mut state = UniversalState::<Standard>::new(2);
    /// state.add_position(UniversalPosition::new_m(0, 0, 2)?);
    /// assert!(state.is_final());
    /// ```
    pub fn is_final(&self) -> bool {
        self.positions.iter().any(|pos| match pos {
            UniversalPosition::MFinal { offset, .. } => *offset <= 0,
            _ => false,
        })
    }

    /// Get maximum distance
    pub fn max_distance(&self) -> u8 {
        self.max_distance
    }

    /// Check if this state contains only I-type positions
    pub fn is_i_state(&self) -> bool {
        !self.is_empty() && self.positions.iter().all(|p| p.is_i_type())
    }

    /// Check if this state contains only M-type positions
    pub fn is_m_state(&self) -> bool {
        !self.is_empty() && self.positions.iter().all(|p| p.is_m_type())
    }

    /// Check if this state contains mixed I and M positions
    pub fn is_mixed_state(&self) -> bool {
        if self.is_empty() {
            return false;
        }
        let has_i = self.positions.iter().any(|p| p.is_i_type());
        let has_m = self.positions.iter().any(|p| p.is_m_type());
        has_i && has_m
    }

    /// Get current length difference m = |query| - |dict|
    ///
    /// Returns the tracked position relative to the diagonal in the DP matrix.
    /// Range: `[-max_distance, +max_distance]`.
    ///
    /// - `m > 0`: Query word is longer (dict consumed faster)
    /// - `m < 0`: Dict word is longer (query consumed faster)
    /// - `m = 0`: Same length consumed from both words
    #[inline]
    pub fn length_diff(&self) -> i8 {
        self.length_diff
    }

    /// Compute transition to successor state (δ^∀,χ_n)
    ///
    /// Implements the universal state transition function from the thesis (Definition 15, page 48):
    ///
    /// ```text
    /// δ^∀,χ_n(Q, x) = {
    ///     Δ               if f_n(rm(Δ), |x|) = false
    ///     m_n(Δ, |x|)     if f_n(rm(Δ), |x|) = true
    /// }
    /// where Δ = ⊔_{π∈Q} δ^∀,χ_e(π, x)
    /// ```
    ///
    /// For each position π in the current state:
    /// 1. Compute successors using δ^∀,χ_e (the `successors()` method)
    /// 2. Union all successor sets
    /// 3. Apply subsumption closure ⊔ (done automatically by `add_position()`)
    /// 4. Check diagonal crossing with f_n(rm(Δ), k)
    /// 5. If crossed, apply m_n conversion to all positions
    ///
    /// # Arguments
    ///
    /// - `bit_vector`: Characteristic vector β(a, w) encoding matches for character a
    /// - `input_length`: Current input position (k) for diagonal crossing detection
    ///
    /// # Returns
    ///
    /// Successor state, or `None` if no successors exist (undefined transition)
    ///
    /// # Example
    ///
    /// ```ignore
    /// let state = UniversalState::<Standard>::initial(2);
    /// let bit_vector = CharacteristicVector::new('a', "abc");
    /// let next_state = state.transition(&bit_vector, 1);
    /// ```
    pub fn transition(
        &self,
        bit_vector: &crate::transducer::universal::CharacteristicVector,
        _input_length: usize,
    ) -> Option<Self> {
        // Special case: empty state has no successors
        if self.is_empty() {
            return None;
        }

        // Create new state for successors (Δ)
        let mut next_state = Self::new(self.max_distance);

        // For each position π in current state Q
        for pos in &self.positions {
            // Compute δ^∀,χ_e(π, x) using the successors() method
            let successors = pos.successors(bit_vector, self.max_distance);

            // Add all successors to next state
            // The add_position() method automatically applies subsumption closure ⊔
            for succ in successors {
                next_state.add_position(succ);
            }
        }

        // Return None if no successors (undefined transition)
        if next_state.is_empty() {
            return None;
        }

        // NOTE: Diagonal crossing is NOT performed in this method.
        //
        // This simplified API doesn't track character consumption, so it cannot
        // correctly update length_diff for diagonal crossing detection.
        //
        // For proper diagonal crossing support, use `transition_with_consumption()`
        // which tracks which word consumed a character and updates length_diff accordingly.
        //
        // This method preserves the parent state's length_diff for backward compatibility,
        // but diagonal crossing conversions are disabled.
        //
        // See: docs/research/universal-levenshtein/DIAGONAL_CROSSING_BUG_ANALYSIS.md
        next_state.length_diff = self.length_diff;

        // Return final state (possibly converted)
        if next_state.is_empty() {
            None
        } else {
            Some(next_state)
        }
    }

    /// Compute transition with explicit character consumption tracking
    ///
    /// This is the correct API for diagonal crossing integration. It tracks which
    /// word consumed a character, enabling proper `length_diff` updates for
    /// diagonal crossing detection.
    ///
    /// # Arguments
    ///
    /// - `bit_vector`: Characteristic vector β(a, w) encoding matches for character a
    /// - `consumed_query`: Whether a character was consumed from the query word
    /// - `consumed_dict`: Whether a character was consumed from the dictionary word
    ///
    /// # Returns
    ///
    /// Successor state with updated `length_diff`, or `None` if no successors exist
    ///
    /// # Length Difference Updates
    ///
    /// - `(true, true)`: Both consumed → `length_diff` unchanged
    /// - `(true, false)`: Query consumed → `length_diff -= 1` (query advancing)
    /// - `(false, true)`: Dict consumed → `length_diff += 1` (dict advancing)
    /// - `(false, false)`: Neither consumed → `length_diff` unchanged (epsilon)
    ///
    /// # Diagonal Crossing
    ///
    /// When `|length_diff| > max_distance`, positions are converted:
    /// - I-type → M-type (or vice versa)
    ///
    /// This ensures the automaton correctly tracks which positions have crossed
    /// the diagonal in the edit graph.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let state = UniversalState::<Standard>::initial(2);
    /// let bv = CharacteristicVector::new('a', "abc");
    ///
    /// // Both words consume 'a'
    /// let next = state.transition_with_consumption(&bv, true, true);
    /// assert_eq!(next.expect("doc example: transition produces Some").length_diff(), 0);
    ///
    /// // Only query consumes (insertion in dict)
    /// let next = state.transition_with_consumption(&bv, true, false);
    /// assert_eq!(next.expect("doc example: transition produces Some").length_diff(), -1);
    /// ```
    pub fn transition_with_consumption(
        &self,
        bit_vector: &crate::transducer::universal::CharacteristicVector,
        consumed_query: bool,
        consumed_dict: bool,
    ) -> Option<Self> {
        // Special case: empty state has no successors
        if self.is_empty() {
            return None;
        }

        // Compute new length_diff based on consumption
        let new_length_diff = self.length_diff
            + match (consumed_query, consumed_dict) {
                (true, true) => 0,   // Both consumed, diff unchanged
                (true, false) => -1, // Query consumed, query advancing relative to dict
                (false, true) => 1,  // Dict consumed, dict advancing relative to query
                (false, false) => 0, // Neither consumed (epsilon transition)
            };

        // Create new state for successors (Δ)
        let mut next_state = Self::new(self.max_distance);
        next_state.length_diff = new_length_diff;

        // For each position π in current state Q
        for pos in &self.positions {
            // Compute δ^∀,χ_e(π, x) using the successors() method
            let successors = pos.successors(bit_vector, self.max_distance);

            // Add all successors to next state
            // The add_position() method automatically applies subsumption closure ⊔
            for succ in successors {
                next_state.add_position(succ);
            }
        }

        // Return None if no successors (undefined transition)
        if next_state.is_empty() {
            return None;
        }

        // Diagonal crossing check using length_diff
        // When |m| > n, we've crossed the diagonal and need to convert positions
        let c = self.max_distance as i32;
        if new_length_diff.abs() as i32 > c {
            // Apply position conversion (I ↔ M)
            let mut converted_state = Self::new(self.max_distance);
            converted_state.length_diff = new_length_diff;

            for pos in &next_state.positions {
                // Convert using length_diff-aware formula
                if let Some(converted) = convert_position_with_length_diff(
                    pos,
                    new_length_diff as i32,
                    self.max_distance,
                ) {
                    converted_state.add_position(converted);
                }
            }

            // Use converted state if non-empty, otherwise keep original
            if !converted_state.is_empty() {
                next_state = converted_state;
            }
        }

        // Return final state
        if next_state.is_empty() {
            None
        } else {
            Some(next_state)
        }
    }
}

/// Convert position between I-type and M-type using length difference
///
/// Simplified conversion formula that uses `length_diff` (m) instead of `k`.
///
/// For I-type → M-type when m > n:
/// ```text
/// new_offset = offset + (n + 1 - m)
/// ```
///
/// For M-type → I-type when m < -n:
/// ```text
/// new_offset = offset - (n + 1 + m)
/// ```
fn convert_position_with_length_diff<V: PositionVariant>(
    pos: &UniversalPosition<V>,
    length_diff: i32,
    max_distance: u8,
) -> Option<UniversalPosition<V>> {
    let offset = pos.offset();
    let errors = pos.errors();
    let n = max_distance as i32;

    match pos {
        UniversalPosition::INonFinal { .. } => {
            // I-type → M-type: Adjust offset based on how far we've crossed
            // The formula accounts for the crossing amount: (n + 1 - length_diff)
            let crossing_adjustment = n + 1 - length_diff;
            let new_offset = offset + crossing_adjustment;
            UniversalPosition::new_m(new_offset, errors, max_distance).ok()
        }
        UniversalPosition::MFinal { .. } => {
            // M-type → I-type: Reverse adjustment
            let crossing_adjustment = n + 1 + length_diff;
            let new_offset = offset - crossing_adjustment;
            UniversalPosition::new_i(new_offset, errors, max_distance).ok()
        }
    }
}

impl<V: PositionVariant> fmt::Display for UniversalState<V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        let mut first = true;
        for pos in &self.positions {
            if !first {
                write!(f, ", ")?;
            }
            write!(f, "{}", pos)?;
            first = false;
        }
        write!(f, "}}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::transducer::universal::position::Standard;

    // =========================================================================
    // Basic State Tests
    // =========================================================================

    #[test]
    fn test_empty_state() {
        let state = UniversalState::<Standard>::new(2);
        assert!(state.is_empty());
        assert_eq!(state.len(), 0);
        assert!(!state.is_final());
        assert_eq!(state.max_distance(), 2);
    }

    #[test]
    fn test_initial_state() {
        let state = UniversalState::<Standard>::initial(2);
        assert!(!state.is_empty());
        assert_eq!(state.len(), 1);
        assert!(!state.is_final());

        let pos = UniversalPosition::new_i(0, 0, 2)
            .expect("test fixture: UniversalPosition::new_i with valid args");
        assert!(state.contains(&pos));
    }

    #[test]
    fn test_add_single_position() {
        let mut state = UniversalState::<Standard>::new(2);
        let pos = UniversalPosition::new_i(1, 1, 2)
            .expect("test fixture: UniversalPosition::new_i with valid args");

        state.add_position(pos.clone());
        assert_eq!(state.len(), 1);
        assert!(state.contains(&pos));
    }

    #[test]
    fn test_add_multiple_non_subsuming_positions() {
        let mut state = UniversalState::<Standard>::new(3);
        // Use positions that don't subsume each other:
        // 0#1 and -2#2
        // Check: does 0#1 subsume -2#2? f > e: 2 > 1 ✓, |-2 - 0| = 2 ≤ 2 - 1 = 1? NO
        // Check: does -2#2 subsume 0#1? f > e: 1 > 2? NO
        let pos1 = UniversalPosition::new_i(0, 1, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args");
        let pos2 = UniversalPosition::new_i(-2, 2, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args");

        state.add_position(pos1.clone());
        state.add_position(pos2.clone());

        // Both positions remain (not subsuming each other)
        assert_eq!(state.len(), 2);
        assert!(state.contains(&pos1));
        assert!(state.contains(&pos2));
    }

    // =========================================================================
    // Anti-chain / Subsumption Tests
    // =========================================================================

    #[test]
    fn test_add_position_removes_subsumed() {
        // Add I+2#2, then add I+1#1 which should remove I+2#2
        // I+1#1 subsumes I+2#2 because: errors(2) > errors(1) AND |2-1| ≤ 2-1
        let mut state = UniversalState::<Standard>::new(3);
        let pos1 = UniversalPosition::new_i(2, 2, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args"); // Will be subsumed
        let pos2 = UniversalPosition::new_i(1, 1, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args"); // Better position

        state.add_position(pos1.clone());
        assert_eq!(state.len(), 1);

        state.add_position(pos2.clone());
        // pos1 should be removed because pos2 subsumes pos1
        assert_eq!(state.len(), 1);
        assert!(!state.contains(&pos1));
        assert!(state.contains(&pos2));
    }

    #[test]
    fn test_add_position_rejected_if_subsumed() {
        // Add I+1#1, then try to add I+2#2 which should be rejected
        // I+1#1 subsumes I+2#2, so I+2#2 is rejected
        let mut state = UniversalState::<Standard>::new(3);
        let pos1 = UniversalPosition::new_i(1, 1, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args"); // Better position
        let pos2 = UniversalPosition::new_i(2, 2, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args"); // Will be rejected

        state.add_position(pos1.clone());
        assert_eq!(state.len(), 1);

        state.add_position(pos2.clone());
        // pos2 should not be added because pos1 subsumes pos2
        assert_eq!(state.len(), 1);
        assert!(state.contains(&pos1));
        assert!(!state.contains(&pos2));
    }

    #[test]
    fn test_anti_chain_maintained() {
        // Add multiple positions and verify no position subsumes another
        let mut state = UniversalState::<Standard>::new(3);

        // Use valid positions that don't subsume each other
        let pos1 = UniversalPosition::new_i(0, 1, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args");
        let pos2 = UniversalPosition::new_i(-2, 2, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args"); // Valid: |-2| = 2 ≤ 2
        let pos3 = UniversalPosition::new_i(-1, 1, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args");

        state.add_position(pos1.clone());
        state.add_position(pos2.clone());
        state.add_position(pos3.clone());

        // Verify anti-chain: no position subsumes another
        let positions: Vec<_> = state.positions().collect();
        for (i, p1) in positions.iter().enumerate() {
            for (j, p2) in positions.iter().enumerate() {
                if i != j {
                    assert!(!subsumes(p1, p2, state.max_distance()));
                }
            }
        }
    }

    // =========================================================================
    // Final State Tests
    // =========================================================================

    #[test]
    fn test_final_state_with_m_zero() {
        let mut state = UniversalState::<Standard>::new(2);
        let pos = UniversalPosition::new_m(0, 0, 2)
            .expect("test fixture: UniversalPosition::new_m with valid args");

        state.add_position(pos);
        assert!(state.is_final());
    }

    #[test]
    fn test_final_state_with_m_negative() {
        let mut state = UniversalState::<Standard>::new(2);
        let pos = UniversalPosition::new_m(-1, 1, 2)
            .expect("test fixture: UniversalPosition::new_m with valid args");

        state.add_position(pos);
        assert!(state.is_final());
    }

    #[test]
    fn test_not_final_with_only_i_positions() {
        let mut state = UniversalState::<Standard>::new(2);
        let pos = UniversalPosition::new_i(0, 0, 2)
            .expect("test fixture: UniversalPosition::new_i with valid args");

        state.add_position(pos);
        assert!(!state.is_final());
    }

    // =========================================================================
    // State Type Tests
    // =========================================================================

    #[test]
    fn test_is_i_state() {
        let mut state = UniversalState::<Standard>::new(2);
        let pos1 = UniversalPosition::new_i(0, 0, 2)
            .expect("test fixture: UniversalPosition::new_i with valid args");
        let pos2 = UniversalPosition::new_i(1, 1, 2)
            .expect("test fixture: UniversalPosition::new_i with valid args");

        state.add_position(pos1);
        state.add_position(pos2);

        assert!(state.is_i_state());
        assert!(!state.is_m_state());
        assert!(!state.is_mixed_state());
    }

    #[test]
    fn test_is_m_state() {
        let mut state = UniversalState::<Standard>::new(2);
        let pos1 = UniversalPosition::new_m(0, 0, 2)
            .expect("test fixture: UniversalPosition::new_m with valid args");
        let pos2 = UniversalPosition::new_m(-1, 1, 2)
            .expect("test fixture: UniversalPosition::new_m with valid args");

        state.add_position(pos1);
        state.add_position(pos2);

        assert!(!state.is_i_state());
        assert!(state.is_m_state());
        assert!(!state.is_mixed_state());
    }

    #[test]
    fn test_is_mixed_state() {
        let mut state = UniversalState::<Standard>::new(2);
        let i_pos = UniversalPosition::new_i(0, 0, 2)
            .expect("test fixture: UniversalPosition::new_i with valid args");
        let m_pos = UniversalPosition::new_m(0, 0, 2)
            .expect("test fixture: UniversalPosition::new_m with valid args");

        state.add_position(i_pos);
        state.add_position(m_pos);

        assert!(!state.is_i_state());
        assert!(!state.is_m_state());
        assert!(state.is_mixed_state());
    }

    // =========================================================================
    // Iterator Tests
    // =========================================================================

    #[test]
    fn test_positions_iterator() {
        let mut state = UniversalState::<Standard>::new(3);
        // Use positions that don't subsume each other
        let pos1 = UniversalPosition::new_i(0, 1, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args");
        let pos2 = UniversalPosition::new_i(-2, 2, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args");

        state.add_position(pos1.clone());
        state.add_position(pos2.clone());

        let positions: Vec<_> = state.positions().cloned().collect();
        assert_eq!(positions.len(), 2);
        assert!(positions.contains(&pos1));
        assert!(positions.contains(&pos2));
    }

    // =========================================================================
    // Display Tests
    // =========================================================================

    #[test]
    fn test_display_empty_state() {
        let state = UniversalState::<Standard>::new(2);
        assert_eq!(format!("{}", state), "{}");
    }

    #[test]
    fn test_display_single_position() {
        let mut state = UniversalState::<Standard>::new(2);
        let pos = UniversalPosition::new_i(0, 0, 2)
            .expect("test fixture: UniversalPosition::new_i with valid args");
        state.add_position(pos);

        let display = format!("{}", state);
        assert!(display.contains("I + 0#0"));
    }

    #[test]
    fn test_display_multiple_positions() {
        let mut state = UniversalState::<Standard>::new(3);
        // Use positions that don't subsume each other
        let pos1 = UniversalPosition::new_i(0, 1, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args");
        let pos2 = UniversalPosition::new_i(-2, 2, 3)
            .expect("test fixture: UniversalPosition::new_i with valid args");

        state.add_position(pos1);
        state.add_position(pos2);

        let display = format!("{}", state);
        assert!(display.contains("I + 0#1"));
        assert!(display.contains("I + -2#2"));
    }

    // =========================================================================
    // Equality Tests
    // =========================================================================

    #[test]
    fn test_state_equality() {
        let mut state1 = UniversalState::<Standard>::new(2);
        let mut state2 = UniversalState::<Standard>::new(2);

        let pos = UniversalPosition::new_i(1, 1, 2)
            .expect("test fixture: UniversalPosition::new_i with valid args");
        state1.add_position(pos.clone());
        state2.add_position(pos);

        assert_eq!(state1, state2);
    }

    #[test]
    fn test_state_inequality_different_positions() {
        let mut state1 = UniversalState::<Standard>::new(2);
        let mut state2 = UniversalState::<Standard>::new(2);

        state1.add_position(
            UniversalPosition::new_i(0, 0, 2)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );
        state2.add_position(
            UniversalPosition::new_i(1, 1, 2)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );

        assert_ne!(state1, state2);
    }

    #[test]
    fn test_state_clone() {
        let mut state1 = UniversalState::<Standard>::new(2);
        state1.add_position(
            UniversalPosition::new_i(1, 1, 2)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );

        let state2 = state1.clone();
        assert_eq!(state1, state2);
    }

    // =========================================================================
    // State Transition Tests (Phase 2 Week 5)
    // =========================================================================

    #[test]
    fn test_transition_from_initial_match() {
        // Test transition from {I + 0#0} on bit vector "100" (match at position 0)
        use crate::transducer::universal::CharacteristicVector;

        let state = UniversalState::<Standard>::initial(2);
        let bv = CharacteristicVector::new('a', "abc");

        let next = state.transition(&bv, 1).expect("Should have successor");

        // From I + 0#0 with "100", we expect I + 0#0 (match keeps offset same)
        // After diagonal check, might convert to M-type
        assert!(!next.is_empty());
    }

    #[test]
    fn test_transition_from_initial_no_match() {
        // Test transition from {I + 0#0} on bit vector "000" (no matches)
        use crate::transducer::universal::CharacteristicVector;

        let state = UniversalState::<Standard>::initial(2);
        let bv = CharacteristicVector::new('x', "abc"); // No 'x' in "abc"

        let next = state.transition(&bv, 1).expect("Should have successor");

        // From I + 0#0 with "000", we expect successors
        // (may be converted to M-type by diagonal crossing)
        assert!(!next.is_empty());
    }

    #[test]
    fn test_transition_applies_subsumption() {
        // Test that subsumption closure is applied during transition
        use crate::transducer::universal::CharacteristicVector;

        let mut state = UniversalState::<Standard>::new(3);
        // Start with a position that will produce subsuming successors
        state.add_position(
            UniversalPosition::new_i(0, 0, 3)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );

        let bv = CharacteristicVector::new('x', "abcd"); // "0000"

        let next = state.transition(&bv, 1).expect("Should have successor");

        // Verify no position subsumes another in result
        let positions: Vec<_> = next.positions().collect();
        for (i, p1) in positions.iter().enumerate() {
            for (j, p2) in positions.iter().enumerate() {
                if i != j {
                    assert!(!subsumes(p1, p2, next.max_distance()));
                }
            }
        }
    }

    #[test]
    fn test_transition_empty_state() {
        // Test that empty state has no successors
        use crate::transducer::universal::CharacteristicVector;

        let state = UniversalState::<Standard>::new(2);
        let bv = CharacteristicVector::new('a', "abc");

        assert!(state.transition(&bv, 1).is_none());
    }

    #[test]
    fn test_transition_multiple_positions() {
        // Test transition from state with multiple positions
        use crate::transducer::universal::CharacteristicVector;

        let mut state = UniversalState::<Standard>::new(2);
        state.add_position(
            UniversalPosition::new_i(0, 0, 2)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );
        state.add_position(
            UniversalPosition::new_i(1, 1, 2)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );

        let bv = CharacteristicVector::new('a', "abc");

        let next = state.transition(&bv, 1).expect("Should have successor");

        // Should have successors from both positions (union of successor sets)
        assert!(!next.is_empty());
    }

    #[test]
    fn test_transition_match_later() {
        // Test transition with match at position 2
        use crate::transducer::universal::CharacteristicVector;

        let state = UniversalState::<Standard>::initial(2);
        let bv = CharacteristicVector::new('c', "abc"); // "001"

        let next = state.transition(&bv, 1).expect("Should have successor");

        // From I + 0#0 with "001", we expect successors
        // (may be converted by diagonal crossing)
        assert!(!next.is_empty());
    }

    #[test]
    fn test_transition_all_errors_consumed() {
        // Test that positions at max errors can still match
        // Position I+0#2 at input position 1 with windowed bit vector
        use crate::transducer::universal::CharacteristicVector;

        let mut state = UniversalState::<Standard>::new(2);
        state.add_position(
            UniversalPosition::new_i(0, 2, 2)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );

        // Windowed bit vector for 'a' at input position 1, word "abc", max_distance 2
        // Window: "$$abc"
        let bv = CharacteristicVector::new('a', "$$abc");

        let next = state.transition(&bv, 1).expect("Should have successor");

        // All positions should have errors ≤ 2
        for pos in next.positions() {
            assert!(pos.errors() <= 2, "Position {:?} exceeds max errors", pos);
        }
    }

    #[test]
    fn test_transition_preserves_max_distance() {
        // Test that transition preserves max_distance
        use crate::transducer::universal::CharacteristicVector;

        let state = UniversalState::<Standard>::initial(3);
        let bv = CharacteristicVector::new('a', "abc");

        let next = state.transition(&bv, 1).expect("Should have successor");

        assert_eq!(next.max_distance(), 3);
    }

    #[test]
    fn test_transition_sequence() {
        // Test a sequence of transitions (simulating processing a word)
        use crate::transducer::universal::CharacteristicVector;

        let mut state = UniversalState::<Standard>::initial(2);

        // Process "aaa" against word "abc"
        let bv1 = CharacteristicVector::new('a', "abc"); // "100"
        state = state.transition(&bv1, 1).expect("Should have successor");
        assert!(!state.is_empty());

        let bv2 = CharacteristicVector::new('a', "abc"); // "100"
        state = state.transition(&bv2, 2).expect("Should have successor");
        assert!(!state.is_empty());

        let bv3 = CharacteristicVector::new('a', "abc"); // "100"
        state = state.transition(&bv3, 3).expect("Should have successor");
        assert!(!state.is_empty());
    }

    #[test]
    fn test_transition_no_valid_successors() {
        // Test case where all successors violate invariants
        use crate::transducer::universal::CharacteristicVector;

        let mut state = UniversalState::<Standard>::new(0); // max_distance = 0
        state.add_position(
            UniversalPosition::new_i(0, 0, 0)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );

        // With max_distance=0, only matches are allowed
        let bv = CharacteristicVector::new('x', "abc"); // No match

        // Should return None (no valid successors)
        assert!(state.transition(&bv, 1).is_none());
    }

    #[test]
    fn test_transition_from_m_type_state() {
        // Test transition from M-type state
        use crate::transducer::universal::CharacteristicVector;

        let mut state = UniversalState::<Standard>::new(2);
        state.add_position(
            UniversalPosition::new_m(-1, 0, 2)
                .expect("test fixture: UniversalPosition::new_m with valid args"),
        );

        let bv = CharacteristicVector::new('a', "abc");

        let next = state.transition(&bv, 1).expect("Should have successor");

        // Should produce M-type successors
        assert!(!next.is_empty());
        // All positions should be M-type
        for pos in next.positions() {
            assert!(pos.is_m_type());
        }
    }

    #[test]
    fn test_transition_union_of_successors() {
        // Test that transition correctly unions successors from multiple positions
        use crate::transducer::universal::CharacteristicVector;

        let mut state = UniversalState::<Standard>::new(2);
        // Add two positions that will produce different successors
        state.add_position(
            UniversalPosition::new_i(0, 0, 2)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );
        state.add_position(
            UniversalPosition::new_i(-1, 1, 2)
                .expect("test fixture: UniversalPosition::new_i with valid args"),
        );

        let bv = CharacteristicVector::new('a', "abc");

        let next = state.transition(&bv, 1).expect("Should have successor");

        // The result should be the union of successors from both positions
        // (with subsumption closure applied)
        assert!(!next.is_empty());

        // Verify anti-chain property
        let positions: Vec<_> = next.positions().collect();
        for (i, p1) in positions.iter().enumerate() {
            for (j, p2) in positions.iter().enumerate() {
                if i != j {
                    assert!(!subsumes(p1, p2, next.max_distance()));
                }
            }
        }
    }

    #[test]
    fn test_transition_multiple_matches() {
        // Test with bit vector containing multiple matches
        use crate::transducer::universal::CharacteristicVector;

        let state = UniversalState::<Standard>::initial(2);
        let bv = CharacteristicVector::new('a', "aaa"); // "111"

        let next = state.transition(&bv, 1).expect("Should have successor");

        // Should match at first position
        assert!(!next.is_empty());
    }
}