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
//! FuzzyMultiMap - Value aggregation from fuzzy-matched keys.
//!
//! This module implements the value aggregation layer originally requested by the user.
//! Given a fuzzy query, it finds all matching keys and aggregates their values using
//! collection-specific logic.
//!
//! # Overview
//!
//! FuzzyMultiMap is the primary feature of this refactoring. It:
//!
//! 1. Queries a fuzzy dictionary for matches within a distance threshold
//! 2. Retrieves the value for each matched key
//! 3. Aggregates all values using collection-type-specific logic
//!
//! # Example Use Case
//!
//! ```rust,ignore
//! use std::collections::HashSet;
//! use liblevenshtein::prelude::*;
//! use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;
//! use liblevenshtein::cache::multimap::FuzzyMultiMap;
//!
//! // Map: foo -> {1,2}, bar -> {3}, baz -> {4,5}
//! let dict: DynamicDawgChar<HashSet<i32>> = DynamicDawgChar::new();
//! dict.insert_with_value("foo", HashSet::from([1, 2]));
//! dict.insert_with_value("bar", HashSet::from([3]));
//! dict.insert_with_value("baz", HashSet::from([4, 5]));
//!
//! let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);
//!
//! // Query "bat" with distance 1
//! // Matches: "bar" (distance 1), "baz" (distance 1)
//! // Values: {3}, {4,5}
//! // Result: {3,4,5} (union)
//! let result = fuzzy.query("bat", 1);
//! assert_eq!(result, Some(HashSet::from([3, 4, 5])));
//! ```

use crate::transducer::{Algorithm, Transducer};
use libdictenstein::{DictionaryValue, MappedDictionary, MutableMappedDictionary};
use std::collections::{BTreeSet, HashSet};
use std::hash::Hash;

/// Trait for types that can aggregate multiple values into one.
///
/// This trait defines how to combine multiple values of the same type into a single
/// aggregated result. Different collection types implement this differently:
/// - `HashSet<T>`: Union of all sets
/// - `BTreeSet<T>`: Union of all sets
/// - `Vec<T>`: Concatenation of all vectors
///
/// # Examples
///
/// ```rust,ignore
/// use liblevenshtein::cache::multimap::CollectionAggregate;
/// use std::collections::HashSet;
///
/// let sets = vec![
///     HashSet::from([1, 2]),
///     HashSet::from([2, 3]),
///     HashSet::from([3, 4]),
/// ];
///
/// let result = HashSet::aggregate(sets.into_iter());
/// assert_eq!(result, HashSet::from([1, 2, 3, 4]));
/// ```
pub trait CollectionAggregate: Sized {
    /// Aggregates multiple values into one.
    ///
    /// # Arguments
    ///
    /// - `values`: Iterator of values to aggregate
    ///
    /// # Returns
    ///
    /// Single aggregated value
    fn aggregate<I>(values: I) -> Self
    where
        I: Iterator<Item = Self>;
}

// HashSet: union of all sets
impl<T> CollectionAggregate for HashSet<T>
where
    T: Eq + Hash + Clone,
{
    fn aggregate<I>(values: I) -> Self
    where
        I: Iterator<Item = Self>,
    {
        // Optimization: Pre-allocate with size hint to reduce rehashing
        let mut values = values.peekable();

        // Estimate capacity from first set's size
        let initial_capacity = values
            .peek()
            .map(|first_set| first_set.len() * 2) // Heuristic: 2x first set size
            .unwrap_or(0);

        let mut acc = HashSet::with_capacity(initial_capacity);

        for set in values {
            // Reserve additional capacity if needed
            if acc.len() + set.len() > acc.capacity() {
                acc.reserve(set.len());
            }
            acc.extend(set);
        }

        acc
    }
}

// BTreeSet: union of all sets
impl<T> CollectionAggregate for BTreeSet<T>
where
    T: Ord + Clone,
{
    fn aggregate<I>(values: I) -> Self
    where
        I: Iterator<Item = Self>,
    {
        values.fold(BTreeSet::new(), |mut acc, set| {
            acc.extend(set);
            acc
        })
    }
}

// Vec: concatenation
impl<T> CollectionAggregate for Vec<T>
where
    T: Clone,
{
    fn aggregate<I>(values: I) -> Self
    where
        I: Iterator<Item = Self>,
    {
        // Optimization: Pre-allocate capacity based on size hints
        let mut values = values.peekable();

        // Estimate capacity from first vec's size
        let initial_capacity = values
            .peek()
            .map(|first_vec| first_vec.len() * 2) // Heuristic: 2x first vec size
            .unwrap_or(0);

        let mut acc = Vec::with_capacity(initial_capacity);

        for vec in values {
            // Reserve additional capacity if needed
            acc.reserve(vec.len());
            acc.extend(vec);
        }

        acc
    }
}

/// FuzzyMultiMap provides value aggregation from fuzzy-matched keys.
///
/// This is the core feature requested by the user - it finds all keys within
/// a given edit distance and aggregates their values using collection-specific logic.
///
/// # Type Parameters
///
/// - `C`: Collection type for values (must implement `CollectionAggregate`)
/// - `D`: Dictionary type (must implement `MappedDictionary<Value = C>`)
///
/// # Examples
///
/// ## HashSet Union
///
/// ```rust,ignore
/// use std::collections::HashSet;
/// use liblevenshtein::prelude::*;
/// use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;
/// use liblevenshtein::cache::multimap::FuzzyMultiMap;
///
/// let dict: DynamicDawgChar<HashSet<i32>> = DynamicDawgChar::new();
/// dict.insert_with_value("hello", HashSet::from([1, 2]));
/// dict.insert_with_value("hallo", HashSet::from([3]));
/// dict.insert_with_value("hullo", HashSet::from([4, 5]));
///
/// let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);
///
/// // Find all values for terms within distance 1 of "hllo"
/// let result = fuzzy.query("hllo", 1).unwrap();
/// // Matches: "hello", "hallo", "hullo" all within distance 1
/// // Result: Union of {1,2}, {3}, {4,5}
/// ```
///
/// ## Vec Concatenation
///
/// ```rust,ignore
/// use liblevenshtein::prelude::*;
/// use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;
/// use liblevenshtein::cache::multimap::FuzzyMultiMap;
///
/// let dict: DynamicDawgChar<Vec<i32>> = DynamicDawgChar::new();
/// dict.insert_with_value("foo", vec![1, 2]);
/// dict.insert_with_value("fob", vec![3]);
/// dict.insert_with_value("fog", vec![4, 5]);
///
/// let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);
///
/// // Concatenates vectors from all matches
/// let result = fuzzy.query("foe", 1).unwrap();
/// ```
pub struct FuzzyMultiMap<C, D>
where
    D: crate::dictionary::Dictionary,
{
    dictionary: D,
    transducer: Transducer<D>,
    _phantom: std::marker::PhantomData<C>,
}

impl<C, D> FuzzyMultiMap<C, D>
where
    C: CollectionAggregate + DictionaryValue,
    D: MappedDictionary<Value = C> + crate::dictionary::Dictionary + Clone,
{
    /// Creates a new FuzzyMultiMap.
    ///
    /// # Arguments
    ///
    /// - `dictionary`: The fuzzy dictionary to query
    /// - `algorithm`: Levenshtein algorithm variant (Standard, Transposition, or MergeAndSplit)
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use std::collections::HashSet;
    /// use liblevenshtein::prelude::*;
    /// use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;
    /// use liblevenshtein::cache::multimap::FuzzyMultiMap;
    ///
    /// let dict: DynamicDawgChar<HashSet<i32>> = DynamicDawgChar::new();
    /// dict.insert_with_value("foo", HashSet::from([1]));
    /// dict.insert_with_value("bar", HashSet::from([2]));
    ///
    /// let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);
    /// ```
    pub fn new(dictionary: D, algorithm: Algorithm) -> Self {
        let transducer = Transducer::new(dictionary.clone(), algorithm);
        Self {
            dictionary,
            transducer,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Queries for fuzzy matches and aggregates their values.
    ///
    /// This is the core operation: find all keys within `max_distance` of `query_term`
    /// and aggregate their values using the collection's aggregation logic.
    ///
    /// # Arguments
    ///
    /// - `query_term`: The term to search for
    /// - `max_distance`: Maximum Levenshtein distance for matches
    ///
    /// # Returns
    ///
    /// - `Some(C)`: Aggregated values from all matches
    /// - `None`: No matches found
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use std::collections::HashSet;
    /// use liblevenshtein::prelude::*;
    /// use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;
    /// use liblevenshtein::cache::multimap::FuzzyMultiMap;
    ///
    /// let dict: DynamicDawgChar<HashSet<i32>> = DynamicDawgChar::new();
    /// dict.insert_with_value("foo", HashSet::from([1, 2]));
    /// dict.insert_with_value("bar", HashSet::from([3]));
    /// dict.insert_with_value("baz", HashSet::from([4, 5]));
    ///
    /// let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);
    ///
    /// // Query "bat" - matches "bar" and "baz" at distance 1
    /// let result = fuzzy.query("bat", 1).unwrap();
    /// assert_eq!(result, HashSet::from([3, 4, 5]));
    /// ```
    pub fn query(&self, query_term: &str, max_distance: usize) -> Option<C> {
        // Optimization: Single-pass collection - avoid double Vec allocation
        // Instead of collecting candidates then mapping to values, we fuse the operations
        let mut values = self
            .transducer
            .query(query_term, max_distance)
            .filter_map(|term| self.dictionary.get_value(&term))
            .peekable();

        // Check if we have any results before aggregating
        values.peek()?;

        // Aggregate using collection-specific logic
        Some(C::aggregate(values))
    }

    /// Gets the inner dictionary reference.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use std::collections::HashSet;
    /// use liblevenshtein::prelude::*;
    /// use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;
    /// use liblevenshtein::cache::multimap::FuzzyMultiMap;
    ///
    /// let dict: DynamicDawgChar<HashSet<i32>> = DynamicDawgChar::new();
    /// dict.insert_with_value("foo", HashSet::from([1]));
    ///
    /// let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);
    /// assert_eq!(fuzzy.dictionary().len(), Some(1));
    /// ```
    pub fn dictionary(&self) -> &D {
        &self.dictionary
    }

    /// Gets the algorithm being used.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use std::collections::HashSet;
    /// use liblevenshtein::prelude::*;
    /// use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;
    /// use liblevenshtein::cache::multimap::FuzzyMultiMap;
    ///
    /// let dict: DynamicDawgChar<HashSet<i32>> = DynamicDawgChar::new();
    /// dict.insert_with_value("foo", HashSet::from([1]));
    ///
    /// let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Transposition);
    /// assert_eq!(fuzzy.algorithm(), Algorithm::Transposition);
    /// ```
    pub fn algorithm(&self) -> Algorithm {
        self.transducer.algorithm()
    }

    /// Queries for fuzzy matches and returns (matched_key, distance, values) tuples.
    ///
    /// Unlike [`query`], this method preserves the matched key and its edit distance,
    /// enabling applications that need to distinguish between different matches
    /// (e.g., PhoneticNormalizedDictionary for mapping back to original terms).
    ///
    /// # Arguments
    ///
    /// - `query_term`: The term to search for
    /// - `max_distance`: Maximum Levenshtein distance for matches
    ///
    /// # Returns
    ///
    /// A vector of `(matched_key, distance, values)` tuples, where:
    /// - `matched_key`: The dictionary key that matched
    /// - `distance`: The edit distance between query and matched key
    /// - `values`: The values associated with the matched key
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use std::collections::HashSet;
    /// use liblevenshtein::prelude::*;
    /// use liblevenshtein::dictionary::dynamic_dawg_char::DynamicDawgChar;
    /// use liblevenshtein::cache::multimap::FuzzyMultiMap;
    ///
    /// let dict: DynamicDawgChar<Vec<String>> = DynamicDawgChar::new();
    /// dict.insert_with_value("foo", vec!["original_foo".to_string()]);
    /// dict.insert_with_value("bar", vec!["original_bar".to_string()]);
    /// dict.insert_with_value("baz", vec!["original_baz".to_string()]);
    ///
    /// let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);
    ///
    /// // Query "bat" - matches "bar" and "baz" at distance 1
    /// let results = fuzzy.query_with_distance("bat", 1);
    /// for (key, distance, values) in results {
    ///     println!("Matched '{}' (distance {}): {:?}", key, distance, values);
    /// }
    /// ```
    pub fn query_with_distance(
        &self,
        query_term: &str,
        max_distance: usize,
    ) -> Vec<(String, usize, C)> {
        self.transducer
            .query_with_distance(query_term, max_distance)
            .filter_map(|candidate| {
                self.dictionary
                    .get_value(&candidate.term)
                    .map(|value| (candidate.term, candidate.distance, value))
            })
            .collect()
    }
}

// ============================================================================
// Mutation Methods (for MutableMappedDictionary types)
// ============================================================================

impl<C, D> FuzzyMultiMap<C, D>
where
    C: CollectionAggregate + DictionaryValue,
    D: MutableMappedDictionary<Value = C> + crate::dictionary::Dictionary + Clone,
{
    /// Insert a term with the given value, or replace if it exists.
    ///
    /// Returns `true` if the term was newly inserted, `false` if it already existed.
    ///
    /// # Arguments
    ///
    /// - `term`: The term to insert
    /// - `value`: The value to associate with the term
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use std::collections::HashSet;
    /// use liblevenshtein::prelude::*;
    /// use liblevenshtein::cache::multimap::FuzzyMultiMap;
    ///
    /// let dict = DynamicDawgChar::<HashSet<u32>>::new();
    /// let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);
    ///
    /// let was_new = fuzzy.insert("foo", HashSet::from([1, 2]));
    /// assert!(was_new);
    ///
    /// let was_new = fuzzy.insert("foo", HashSet::from([3]));
    /// assert!(!was_new); // Already existed
    /// ```
    pub fn insert(&self, term: &str, value: C) -> bool {
        self.dictionary.insert_with_value(term, value)
    }

    /// Update an existing term's value or insert with a default value.
    ///
    /// This is useful for accumulation patterns where you want to add to an existing
    /// collection (e.g., add an element to a `HashSet`) rather than replace it.
    ///
    /// Returns `true` if the term was newly inserted, `false` if it already existed.
    ///
    /// # Arguments
    ///
    /// - `term`: The term to update or insert
    /// - `default_value`: The value to use if the term doesn't exist
    /// - `update_fn`: Function to apply to the existing value if the term exists
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use std::collections::HashSet;
    /// use liblevenshtein::prelude::*;
    /// use liblevenshtein::cache::multimap::FuzzyMultiMap;
    ///
    /// let dict = DynamicDawgChar::<HashSet<String>>::new();
    /// let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);
    ///
    /// // First insert: creates new entry
    /// let was_new = fuzzy.update_or_insert(
    ///     "normalized_form",
    ///     HashSet::from(["original1".to_string()]),
    ///     |set| { set.insert("original1".to_string()); }
    /// );
    /// assert!(was_new);
    ///
    /// // Second insert: adds to existing set
    /// let was_new = fuzzy.update_or_insert(
    ///     "normalized_form",
    ///     HashSet::new(),
    ///     |set| { set.insert("original2".to_string()); }
    /// );
    /// assert!(!was_new);
    ///
    /// // Now "normalized_form" maps to {"original1", "original2"}
    /// ```
    pub fn update_or_insert<F>(&self, term: &str, default_value: C, update_fn: F) -> bool
    where
        F: Fn(&mut C),
    {
        self.dictionary
            .update_or_insert(term, default_value, update_fn)
    }
}

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

    #[cfg(feature = "pathmap-backend")]
    use libdictenstein::pathmap::PathMapDictionary;

    #[test]
    #[cfg(feature = "pathmap-backend")]
    fn test_fuzzy_multimap_hashset_union() {
        // User's original example
        let dict = PathMapDictionary::from_terms_with_values([
            ("foo", HashSet::from([1, 2])),
            ("bar", HashSet::from([3])),
            ("baz", HashSet::from([4, 5])),
        ]);

        let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);

        // Query "bat" with distance 1
        // Should match "bar" and "baz"
        let result = fuzzy.query("bat", 1).expect("expected Some result in test");
        assert_eq!(result, HashSet::from([3, 4, 5]));
    }

    #[test]
    #[cfg(feature = "pathmap-backend")]
    fn test_fuzzy_multimap_vec_concatenation() {
        let dict = PathMapDictionary::from_terms_with_values([
            ("foo", vec![1, 2]),
            ("fob", vec![3]),
            ("fog", vec![4, 5]),
        ]);

        let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);

        // Query "foo" with distance 0 - exact match only
        let result = fuzzy.query("foo", 0).expect("expected Some result in test");
        assert_eq!(result, vec![1, 2]);

        // Query "fox" with distance 1
        // Should match "fob" and "fog"
        let result = fuzzy.query("fox", 1).expect("expected Some result in test");
        // Vec concatenation maintains order of matches
        assert!(result.contains(&3));
        assert!(result.contains(&4));
        assert!(result.contains(&5));
    }

    #[test]
    #[cfg(feature = "pathmap-backend")]
    fn test_fuzzy_multimap_no_matches() {
        let dict = PathMapDictionary::from_terms_with_values([
            ("foo", HashSet::from([1])),
            ("bar", HashSet::from([2])),
        ]);

        let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);

        // Query with no matches
        let result = fuzzy.query("xyz", 1);
        assert!(result.is_none());
    }

    #[test]
    #[cfg(feature = "pathmap-backend")]
    fn test_fuzzy_multimap_exact_match() {
        let dict = PathMapDictionary::from_terms_with_values([("hello", HashSet::from([1, 2, 3]))]);

        let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);

        let result = fuzzy
            .query("hello", 0)
            .expect("expected Some result in test");
        assert_eq!(result, HashSet::from([1, 2, 3]));
    }

    #[test]
    #[cfg(feature = "pathmap-backend")]
    fn test_fuzzy_multimap_overlapping_values() {
        // Test that HashSet properly handles duplicate values
        let dict = PathMapDictionary::from_terms_with_values([
            ("foo", HashSet::from([1, 2])),
            ("foe", HashSet::from([2, 3])),
            ("fog", HashSet::from([3, 4])),
        ]);

        let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);

        // All three match "fox" at distance 1
        let result = fuzzy.query("fox", 1).expect("expected Some result in test");
        // Should be union: {1, 2, 3, 4}
        assert_eq!(result, HashSet::from([1, 2, 3, 4]));
    }

    #[test]
    #[cfg(feature = "pathmap-backend")]
    fn test_fuzzy_multimap_with_transposition() {
        let dict = PathMapDictionary::from_terms_with_values([
            ("hello", HashSet::from([1])),
            ("ehllo", HashSet::from([2])), // Adjacent transposition of "hello"
        ]);

        let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Transposition);

        // Query with transposition distance
        let result = fuzzy
            .query("hello", 2)
            .expect("expected Some result in test");
        // Should at least match "hello" (exact)
        assert!(result.contains(&1));
    }

    #[test]
    #[cfg(feature = "pathmap-backend")]
    fn test_fuzzy_multimap_query_with_distance() {
        let dict = PathMapDictionary::from_terms_with_values([
            ("foo", vec!["original_foo".to_string()]),
            ("bar", vec!["original_bar".to_string()]),
            ("baz", vec!["original_baz".to_string()]),
        ]);

        let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);

        // Query "bat" with distance 1
        // Should match "bar" and "baz"
        let results = fuzzy.query_with_distance("bat", 1);

        // Should have 2 results
        assert_eq!(results.len(), 2);

        // Check that we get both matches with their distances
        let bar_result = results.iter().find(|(key, _, _)| key == "bar");
        assert!(bar_result.is_some());
        let (_, distance, values) = bar_result.expect("expected Some bar_result in test");
        assert_eq!(*distance, 1);
        assert_eq!(values, &vec!["original_bar".to_string()]);

        let baz_result = results.iter().find(|(key, _, _)| key == "baz");
        assert!(baz_result.is_some());
        let (_, distance, values) = baz_result.expect("expected Some baz_result in test");
        assert_eq!(*distance, 1);
        assert_eq!(values, &vec!["original_baz".to_string()]);
    }

    #[test]
    #[cfg(feature = "pathmap-backend")]
    fn test_fuzzy_multimap_query_with_distance_exact() {
        let dict = PathMapDictionary::from_terms_with_values([
            ("test", vec!["exact_match".to_string()]),
            ("tost", vec!["near_match".to_string()]),
        ]);

        let fuzzy = FuzzyMultiMap::new(dict, Algorithm::Standard);

        // Exact match should have distance 0
        let results = fuzzy.query_with_distance("test", 1);

        let exact = results.iter().find(|(key, _, _)| key == "test");
        assert!(exact.is_some());
        let (_, distance, _) = exact.expect("expected Some exact in test");
        assert_eq!(*distance, 0);

        let near = results.iter().find(|(key, _, _)| key == "tost");
        assert!(near.is_some());
        let (_, distance, _) = near.expect("expected Some near in test");
        assert_eq!(*distance, 1);
    }
}