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
//! Subsumption Relation for Generalized Positions
//!
//! Implements the subsumption relation ≤^χ_s from Definition 11 (thesis pages 18-21).
//!
//! # Theory Background
//!
//! The subsumption relation is a partial order on positions that enables state minimization.
//! Position π₁ subsumes π₂ (written π₁ <^χ_s π₂) if π₂ represents a "better" state
//! (more errors available, close enough in position).
//!
//! ## Definition 11: Subsumption for Standard Levenshtein (χ = ε)
//!
//! ```text
//! i#e ≤^ε_s j#f  ⇔  f > e ∧ |j - i| ≤ f - e
//! ```
//!
//! **Intuition**: Position j#f subsumes i#e if:
//! 1. f > e (more errors available at j)
//! 2. |j - i| ≤ f - e (positions close enough given error difference)
//!
//! # Phase 1 Implementation
//!
//! Current implementation supports standard operations only (match, substitute, insert, delete).
//! This means we use the standard subsumption rule above.
//!
//! Future phases will add runtime OperationSet-specific subsumption rules.

use super::position::GeneralizedPosition;

/// Check if pos1 strictly subsumes pos2 (pos1 <^χ_s pos2)
///
/// # Arguments
///
/// - `pos1`: First position (potential subsumer)
/// - `pos2`: Second position (potentially subsumed)
/// - `max_distance`: Maximum edit distance n
///
/// # Returns
///
/// `true` if pos1 <^χ_s pos2 (pos2 is subsumed by pos1), `false` otherwise
///
/// # Theory
///
/// From Definition 11 (page 18):
/// - Both positions must have same parameter type (I or M)
/// - pos2 must have more errors available (f > e)
/// - Positions must be close enough (|j - i| ≤ f - e)
///
/// # Phase 1: Standard Operations Only
///
/// This implementation uses the standard subsumption rule. Future phases
/// will extend this to support custom operation sets.
///
/// # Example
///
/// ```ignore
/// let pos1 = GeneralizedPosition::new_i(4, 1, 3)?;
/// let pos2 = GeneralizedPosition::new_i(5, 2, 3)?;
/// assert!(subsumes(&pos1, &pos2, 3));  // 4#1 <^ε_s 5#2
/// ```
#[inline(always)]
pub fn subsumes(pos1: &GeneralizedPosition, pos2: &GeneralizedPosition, max_distance: u8) -> bool {
    subsumes_standard(pos1, pos2, max_distance)
}

/// Standard subsumption rule implementation
///
/// From Definition 11 (page 18): i#e ≤^ε_s j#f ⇔ f > e ∧ |j - i| ≤ f - e
///
/// # Phase 2d Extension
///
/// With multi-character operations, positions can be in intermediate states (transposing, splitting).
/// **Key Rule**: Only positions of the same variant can subsume each other.
///
/// Rationale:
/// - Transposing/splitting positions represent intermediate states with different constraints
/// - They have different futures in the automaton
/// - Cross-variant subsumption would be incorrect
///
/// # Arguments
///
/// - `pos1`: Position i#e (potential subsumer)
/// - `pos2`: Position j#f (potentially subsumed)
/// - `_max_distance`: Maximum distance n (unused for standard, kept for consistency)
///
/// # Returns
///
/// `true` if pos1 <^ε_s pos2, `false` otherwise
///
/// # Conditions
///
/// 1. Both must be same variant (INonFinal, MFinal, ITransposing, etc.)
/// 2. pos2 has more errors: f > e
/// 3. Positions close enough: |j - i| ≤ f - e
fn subsumes_standard(
    pos1: &GeneralizedPosition,
    pos2: &GeneralizedPosition,
    _max_distance: u8,
) -> bool {
    use GeneralizedPosition::*;

    // Helper function for the actual subsumption check (same for all variants)
    fn check_subsumption(i: i32, e: u8, j: i32, f: u8) -> bool {
        // f > e (pos2 has more errors available)
        if f <= e {
            return false;
        }

        let error_diff = (f - e) as i32;
        let offset_diff = (j - i).abs();

        // |j - i| ≤ f - e
        offset_diff <= error_diff
    }

    match (pos1, pos2) {
        // I-type subsumes I-type (usual state)
        (
            INonFinal {
                offset: i,
                errors: e,
            },
            INonFinal {
                offset: j,
                errors: f,
            },
        ) => check_subsumption(*i, *e, *j, *f),

        // M-type subsumes M-type (usual state)
        (
            MFinal {
                offset: i,
                errors: e,
            },
            MFinal {
                offset: j,
                errors: f,
            },
        ) => check_subsumption(*i, *e, *j, *f),

        // I-type transposing subsumes I-type transposing
        (
            ITransposing {
                offset: i,
                errors: e,
            },
            ITransposing {
                offset: j,
                errors: f,
            },
        ) => check_subsumption(*i, *e, *j, *f),

        // M-type transposing subsumes M-type transposing
        (
            MTransposing {
                offset: i,
                errors: e,
            },
            MTransposing {
                offset: j,
                errors: f,
            },
        ) => check_subsumption(*i, *e, *j, *f),

        // I-type splitting subsumes I-type splitting
        (
            ISplitting {
                offset: i,
                errors: e,
                ..
            },
            ISplitting {
                offset: j,
                errors: f,
                ..
            },
        ) => check_subsumption(*i, *e, *j, *f),

        // M-type splitting subsumes M-type splitting
        (
            MSplitting {
                offset: i,
                errors: e,
                ..
            },
            MSplitting {
                offset: j,
                errors: f,
                ..
            },
        ) => check_subsumption(*i, *e, *j, *f),

        // Different variants never subsume each other
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_subsumes_i_type_standard() {
        // Test case: 1#2 <^ε_s 2#3
        // Valid positions: |1| ≤ 2 ✓ and |2| ≤ 3 ✓
        // f > e: 3 > 2 ✓
        // |j - i| ≤ f - e: |2 - 1| = 1 ≤ 3 - 2 = 1 ✓
        let pos1 = GeneralizedPosition::new_i(1, 2, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        let pos2 = GeneralizedPosition::new_i(2, 3, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        assert!(subsumes(&pos1, &pos2, 3));
    }

    #[test]
    fn test_not_subsumes_too_far() {
        // 0#2 should not subsume -2#3 (offset difference too large)
        // Valid positions: |0| ≤ 2 ✓ and |-2| ≤ 3 ✓
        // f > e: 3 > 2 ✓
        // |j - i| ≤ f - e: |-2 - 0| = 2 ≤ 3 - 2 = 1? NO
        let pos1 = GeneralizedPosition::new_i(0, 2, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        let pos2 = GeneralizedPosition::new_i(-2, 3, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        assert!(!subsumes(&pos1, &pos2, 3));
    }

    #[test]
    fn test_not_subsumes_same_errors() {
        // Cannot subsume if same error count
        let pos1 = GeneralizedPosition::new_i(0, 1, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        let pos2 = GeneralizedPosition::new_i(1, 1, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        assert!(!subsumes(&pos1, &pos2, 3));
    }

    #[test]
    fn test_subsumes_m_type_standard() {
        // M-type subsumption works the same way
        let pos1 = GeneralizedPosition::new_m(-1, 0, 2)
            .expect("test fixture: GeneralizedPosition::new_m with valid args");
        let pos2 = GeneralizedPosition::new_m(-2, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_m with valid args");
        assert!(subsumes(&pos1, &pos2, 2));
    }

    #[test]
    fn test_not_subsumes_different_types() {
        // I-type cannot subsume M-type and vice versa
        let i_pos = GeneralizedPosition::new_i(0, 0, 2)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        let m_pos = GeneralizedPosition::new_m(0, 0, 2)
            .expect("test fixture: GeneralizedPosition::new_m with valid args");
        assert!(!subsumes(&i_pos, &m_pos, 2));
        assert!(!subsumes(&m_pos, &i_pos, 2));
    }

    #[test]
    fn test_subsumes_reflexive_false() {
        // A position cannot subsume itself (requires f > e)
        let pos = GeneralizedPosition::new_i(0, 1, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        assert!(!subsumes(&pos, &pos, 3));
    }

    #[test]
    fn test_subsumes_boundary_case() {
        // Boundary case: |j - i| = f - e exactly
        let pos1 = GeneralizedPosition::new_i(0, 0, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        let pos2 = GeneralizedPosition::new_i(2, 2, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        // f > e: 2 > 0 ✓
        // |j - i| ≤ f - e: |2 - 0| = 2 ≤ 2 - 0 = 2 ✓
        assert!(subsumes(&pos1, &pos2, 3));
    }

    #[test]
    fn test_subsumes_negative_offsets() {
        // Test with negative offsets
        let pos1 = GeneralizedPosition::new_i(-1, 2, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        let pos2 = GeneralizedPosition::new_i(0, 3, 3)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        // f > e: 3 > 2 ✓
        // |j - i| ≤ f - e: |0 - (-1)| = 1 ≤ 3 - 2 = 1 ✓
        assert!(subsumes(&pos1, &pos2, 3));
    }

    // Tests for Phase 2d.2: Variant subsumption

    #[test]
    fn test_same_variant_subsumption_transposing() {
        // I+(-1)#1_t subsumes I+0#2_t (same variant)
        let pos1 = GeneralizedPosition::new_i_transposing(-1, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_i_transposing with valid args");
        let pos2 = GeneralizedPosition::new_i_transposing(0, 2, 2)
            .expect("test fixture: GeneralizedPosition::new_i_transposing with valid args");
        // f > e: 2 > 1 ✓
        // |j - i| ≤ f - e: |0 - (-1)| = 1 ≤ 2 - 1 = 1 ✓
        assert!(subsumes(&pos1, &pos2, 2));
    }

    #[test]
    fn test_same_variant_subsumption_splitting() {
        // I+(-1)#1_s subsumes I+0#2_s (same variant)
        let pos1 = GeneralizedPosition::new_i_splitting(-1, 1, 2, 'a')
            .expect("test fixture: GeneralizedPosition::new_i_splitting with valid args");
        let pos2 = GeneralizedPosition::new_i_splitting(0, 2, 2, 'a')
            .expect("test fixture: GeneralizedPosition::new_i_splitting with valid args");
        assert!(subsumes(&pos1, &pos2, 2));
    }

    #[test]
    fn test_same_variant_subsumption_m_transposing() {
        // M+(-1)#1_t subsumes M+(-2)#2_t (same variant)
        let pos1 = GeneralizedPosition::new_m_transposing(-1, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_m_transposing with valid args");
        let pos2 = GeneralizedPosition::new_m_transposing(-2, 2, 2)
            .expect("test fixture: GeneralizedPosition::new_m_transposing with valid args");
        assert!(subsumes(&pos1, &pos2, 2));
    }

    #[test]
    fn test_same_variant_subsumption_m_splitting() {
        // M+(-1)#1_s subsumes M+(-2)#2_s (same variant)
        let pos1 = GeneralizedPosition::new_m_splitting(-1, 1, 2, 'a')
            .expect("test fixture: GeneralizedPosition::new_m_splitting with valid args");
        let pos2 = GeneralizedPosition::new_m_splitting(-2, 2, 2, 'a')
            .expect("test fixture: GeneralizedPosition::new_m_splitting with valid args");
        assert!(subsumes(&pos1, &pos2, 2));
    }

    #[test]
    fn test_different_variant_no_subsumption_usual_vs_transposing() {
        // I+0#1 (usual) does NOT subsume I+0#2_t (transposing)
        let pos1 = GeneralizedPosition::new_i(0, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        let pos2 = GeneralizedPosition::new_i_transposing(0, 2, 2)
            .expect("test fixture: GeneralizedPosition::new_i_transposing with valid args");
        assert!(!subsumes(&pos1, &pos2, 2));

        // And vice versa
        assert!(!subsumes(&pos2, &pos1, 2));
    }

    #[test]
    fn test_different_variant_no_subsumption_transposing_vs_splitting() {
        // I+0#1_t (transposing) does NOT subsume I+0#2_s (splitting)
        let pos1 = GeneralizedPosition::new_i_transposing(0, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_i_transposing with valid args");
        let pos2 = GeneralizedPosition::new_i_splitting(0, 2, 2, 'a')
            .expect("test fixture: GeneralizedPosition::new_i_splitting with valid args");
        assert!(!subsumes(&pos1, &pos2, 2));

        // And vice versa
        assert!(!subsumes(&pos2, &pos1, 2));
    }

    #[test]
    fn test_different_variant_no_subsumption_usual_vs_splitting() {
        // I+0#1 (usual) does NOT subsume I+0#2_s (splitting)
        let pos1 = GeneralizedPosition::new_i(0, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        let pos2 = GeneralizedPosition::new_i_splitting(0, 2, 2, 'a')
            .expect("test fixture: GeneralizedPosition::new_i_splitting with valid args");
        assert!(!subsumes(&pos1, &pos2, 2));

        // And vice versa
        assert!(!subsumes(&pos2, &pos1, 2));
    }

    #[test]
    fn test_different_variant_same_offset_errors() {
        // Even with same offset and errors, different variants don't subsume
        let i_usual = GeneralizedPosition::new_i(0, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_i with valid args");
        let i_trans = GeneralizedPosition::new_i_transposing(0, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_i_transposing with valid args");
        let i_split = GeneralizedPosition::new_i_splitting(0, 1, 2, 'a')
            .expect("test fixture: GeneralizedPosition::new_i_splitting with valid args");

        // No cross-variant subsumption
        assert!(!subsumes(&i_usual, &i_trans, 2));
        assert!(!subsumes(&i_usual, &i_split, 2));
        assert!(!subsumes(&i_trans, &i_usual, 2));
        assert!(!subsumes(&i_trans, &i_split, 2));
        assert!(!subsumes(&i_split, &i_usual, 2));
        assert!(!subsumes(&i_split, &i_trans, 2));
    }

    #[test]
    fn test_m_type_variants_no_cross_subsumption() {
        // M-type variants also don't subsume across types
        let m_usual = GeneralizedPosition::new_m(0, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_m with valid args");
        let m_trans = GeneralizedPosition::new_m_transposing(0, 1, 2)
            .expect("test fixture: GeneralizedPosition::new_m_transposing with valid args");
        let m_split = GeneralizedPosition::new_m_splitting(0, 1, 2, 'a')
            .expect("test fixture: GeneralizedPosition::new_m_splitting with valid args");

        assert!(!subsumes(&m_usual, &m_trans, 2));
        assert!(!subsumes(&m_usual, &m_split, 2));
        assert!(!subsumes(&m_trans, &m_usual, 2));
        assert!(!subsumes(&m_trans, &m_split, 2));
        assert!(!subsumes(&m_split, &m_usual, 2));
        assert!(!subsumes(&m_split, &m_trans, 2));
    }
}