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
//! BijectiveMap: Generic bijective dictionary for arbitrary hashable values.
//!
//! This module provides [`BijectiveMap`], a bidirectional dictionary that maps
//! terms to arbitrary hashable values while maintaining the bijection invariant.
//!
//! `BijectiveMap` supports any value type that implements `DictionaryValue + Eq + Hash`.
//! For vocabulary use cases with sequential `u64` indices, use
//! [`PersistentVocabARTrie`](crate::persistent_vocab_artrie::PersistentVocabARTrie).
//!
//! # Use Cases
//!
//! - Symbol tables with arbitrary IDs
//! - Bidirectional mappings between strings and enums
//! - Any bijection where values aren't sequential integers

use crate::bijective::BijectiveDictionary;
use crate::dynamic_dawg_char::DynamicDawgChar;
use crate::sync_compat::RwLock;
use crate::value::DictionaryValue;
use crate::{Dictionary, MappedDictionary};
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;

/// A bijective dictionary mapping terms to arbitrary hashable values.
///
/// `BijectiveMap` provides efficient bidirectional lookup between terms (strings)
/// and values of any type that implements `DictionaryValue + Eq + Hash`.
///
/// # Type Parameter
///
/// - `V`: The value type. Must implement [`DictionaryValue`], [`Eq`], and [`Hash`].
///
/// # Performance
///
/// - Forward lookup (`get_value`): O(k) where k = term length (via DAWG traversal)
/// - Reverse lookup (`get_term`): O(1) average (via hash lookup)
/// - Memory: ~1.5x (DAWG + HashMap)
///
/// # Thread Safety
///
/// `BijectiveMap` is fully thread-safe:
/// - Forward lookups use the thread-safe `DynamicDawgChar` backend
/// - Reverse lookups use `Arc<RwLock<HashMap<V, String>>>`
///
/// # Examples
///
/// ```rust
/// use libdictenstein::bijective::BijectiveMap;
///
/// let bimap: BijectiveMap<String> = BijectiveMap::new();
///
/// bimap.insert("key1", "value1".to_string());
/// bimap.insert("key2", "value2".to_string());
///
/// // Forward lookup
/// assert_eq!(bimap.get_value("key1"), Some("value1".to_string()));
///
/// // Reverse lookup
/// assert_eq!(bimap.get_term(&"value1".to_string()), Some("key1".to_string()));
/// ```
#[derive(Debug)]
pub struct BijectiveMap<V: DictionaryValue + Eq + Hash> {
    /// Forward mapping: term → value (using DynamicDawgChar)
    forward: DynamicDawgChar<V>,

    /// Reverse mapping: value → term
    reverse: Arc<RwLock<HashMap<V, String>>>,
}

impl<V: DictionaryValue + Eq + Hash> Clone for BijectiveMap<V> {
    fn clone(&self) -> Self {
        Self {
            forward: self.forward.clone(),
            reverse: Arc::new(RwLock::new(self.reverse.read().clone())),
        }
    }
}

impl<V: DictionaryValue + Eq + Hash> Default for BijectiveMap<V> {
    fn default() -> Self {
        Self::new()
    }
}

impl<V: DictionaryValue + Eq + Hash> BijectiveMap<V> {
    /// Create an empty bijective map.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// assert!(bimap.is_empty());
    /// ```
    pub fn new() -> Self {
        Self {
            forward: DynamicDawgChar::new(),
            reverse: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Create a bijective map with pre-allocated capacity.
    ///
    /// This can improve performance when you know the approximate number
    /// of entries in advance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<String> = BijectiveMap::with_capacity(1000);
    /// ```
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            forward: DynamicDawgChar::new(),
            reverse: Arc::new(RwLock::new(HashMap::with_capacity(capacity))),
        }
    }

    /// Build a bijective map from an iterator of (term, value) pairs.
    ///
    /// # Panics
    ///
    /// Panics if:
    /// - Any term appears more than once
    /// - Any value appears more than once
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap = BijectiveMap::from_pairs([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// assert_eq!(bimap.get_value("a"), Some(1));
    /// assert_eq!(bimap.get_term(&1), Some("a".to_string()));
    /// ```
    pub fn from_pairs<I, S>(pairs: I) -> Self
    where
        I: IntoIterator<Item = (S, V)>,
        S: AsRef<str>,
    {
        let bimap = Self::new();
        for (term, value) in pairs {
            bimap.insert(term.as_ref(), value);
        }
        bimap
    }

    /// Insert a term-value pair.
    ///
    /// # Panics
    ///
    /// Panics if:
    /// - The term already exists in the map (would break key uniqueness)
    /// - The value already exists in the map (would break value uniqueness)
    ///
    /// This strict behavior ensures the bijection invariant is never violated.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// bimap.insert("one", 1);
    /// bimap.insert("two", 2);
    ///
    /// assert_eq!(bimap.get_value("one"), Some(1));
    /// assert_eq!(bimap.get_term(&1), Some("one".to_string()));
    /// ```
    ///
    /// ```rust,should_panic
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// bimap.insert("one", 1);
    /// bimap.insert("one", 2);  // Panics: duplicate term
    /// ```
    ///
    /// ```rust,should_panic
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// bimap.insert("one", 1);
    /// bimap.insert("uno", 1);  // Panics: duplicate value
    /// ```
    pub fn insert(&self, term: &str, value: V) {
        // Check for duplicate term
        if self.forward.get_value(term).is_some() {
            panic!(
                "BijectiveMap::insert: duplicate term '{}' violates bijection invariant",
                term
            );
        }

        // Check for duplicate value
        {
            let reverse = self.reverse.read();
            if reverse.contains_key(&value) {
                panic!("BijectiveMap::insert: duplicate value violates bijection invariant");
            }
        }

        // Insert into both mappings
        self.forward.insert_with_value(term, value.clone());

        {
            let mut reverse = self.reverse.write();
            reverse.insert(value, term.to_string());
        }
    }

    /// Try to insert a term-value pair, returning an error if it would violate
    /// the bijection invariant.
    ///
    /// This is a non-panicking alternative to [`insert`](Self::insert).
    ///
    /// # Returns
    ///
    /// - `Ok(())` if the insertion succeeded
    /// - `Err(InsertError::DuplicateTerm)` if the term already exists
    /// - `Err(InsertError::DuplicateValue)` if the value already exists
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::{BijectiveMap, InsertError};
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    ///
    /// assert!(bimap.try_insert("one", 1).is_ok());
    /// assert_eq!(bimap.try_insert("one", 2), Err(InsertError::DuplicateTerm));
    /// assert_eq!(bimap.try_insert("uno", 1), Err(InsertError::DuplicateValue));
    /// ```
    pub fn try_insert(&self, term: &str, value: V) -> Result<(), InsertError> {
        // Check for duplicate term
        if self.forward.get_value(term).is_some() {
            return Err(InsertError::DuplicateTerm);
        }

        // Check for duplicate value
        {
            let reverse = self.reverse.read();
            if reverse.contains_key(&value) {
                return Err(InsertError::DuplicateValue);
            }
        }

        // Insert into both mappings
        self.forward.insert_with_value(term, value.clone());

        {
            let mut reverse = self.reverse.write();
            reverse.insert(value, term.to_string());
        }

        Ok(())
    }

    /// Get the value associated with a term.
    ///
    /// Returns `None` if the term is not in the map.
    ///
    /// # Performance
    ///
    /// O(k) where k = term length (DAWG traversal).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// bimap.insert("hello", 42);
    ///
    /// assert_eq!(bimap.get_value("hello"), Some(42));
    /// assert_eq!(bimap.get_value("missing"), None);
    /// ```
    #[inline]
    pub fn get_value(&self, term: &str) -> Option<V> {
        self.forward.get_value(term)
    }

    /// Get the term associated with a value.
    ///
    /// Returns `None` if the value is not in the map.
    ///
    /// # Performance
    ///
    /// O(1) average (hash lookup).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// bimap.insert("hello", 42);
    ///
    /// assert_eq!(bimap.get_term(&42), Some("hello".to_string()));
    /// assert_eq!(bimap.get_term(&999), None);
    /// ```
    #[inline]
    pub fn get_term(&self, value: &V) -> Option<String> {
        self.reverse.read().get(value).cloned()
    }

    /// Check if a term exists in the map.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// bimap.insert("hello", 42);
    ///
    /// assert!(bimap.contains_term("hello"));
    /// assert!(!bimap.contains_term("missing"));
    /// ```
    #[inline]
    pub fn contains_term(&self, term: &str) -> bool {
        self.forward.get_value(term).is_some()
    }

    /// Check if a value exists in the map.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// bimap.insert("hello", 42);
    ///
    /// assert!(bimap.contains_value(&42));
    /// assert!(!bimap.contains_value(&999));
    /// ```
    #[inline]
    pub fn contains_value(&self, value: &V) -> bool {
        self.reverse.read().contains_key(value)
    }

    /// Get the number of term-value pairs in the map.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// assert_eq!(bimap.len(), 0);
    ///
    /// bimap.insert("a", 1);
    /// bimap.insert("b", 2);
    /// assert_eq!(bimap.len(), 2);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.reverse.read().len()
    }

    /// Check if the map is empty.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap: BijectiveMap<i32> = BijectiveMap::new();
    /// assert!(bimap.is_empty());
    ///
    /// bimap.insert("a", 1);
    /// assert!(!bimap.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.reverse.read().is_empty()
    }

    /// Iterate over all (term, value) pairs.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap = BijectiveMap::from_pairs([
    ///     ("a", 1),
    ///     ("b", 2),
    /// ]);
    ///
    /// for (term, value) in bimap.iter() {
    ///     println!("{} -> {}", term, value);
    /// }
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = (String, V)> + '_ {
        // Clone the data to avoid lifetime issues with the lock guard
        let reverse = self.reverse.read();
        reverse
            .iter()
            .map(|(v, t)| (t.clone(), v.clone()))
            .collect::<Vec<_>>()
            .into_iter()
    }

    /// Iterate over all terms.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap = BijectiveMap::from_pairs([("a", 1), ("b", 2)]);
    ///
    /// let terms: Vec<_> = bimap.terms().collect();
    /// assert!(terms.contains(&"a".to_string()));
    /// assert!(terms.contains(&"b".to_string()));
    /// ```
    pub fn terms(&self) -> impl Iterator<Item = String> + '_ {
        let reverse = self.reverse.read();
        reverse.values().cloned().collect::<Vec<_>>().into_iter()
    }

    /// Iterate over all values.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use libdictenstein::bijective::BijectiveMap;
    ///
    /// let bimap = BijectiveMap::from_pairs([("a", 1), ("b", 2)]);
    ///
    /// let values: Vec<_> = bimap.values().collect();
    /// assert!(values.contains(&1));
    /// assert!(values.contains(&2));
    /// ```
    pub fn values(&self) -> impl Iterator<Item = V> + '_ {
        let reverse = self.reverse.read();
        reverse.keys().cloned().collect::<Vec<_>>().into_iter()
    }

    /// Get a reference to the underlying forward dictionary.
    ///
    /// This is useful for advanced operations like fuzzy matching with
    /// Levenshtein automata.
    #[inline]
    pub fn forward(&self) -> &DynamicDawgChar<V> {
        &self.forward
    }
}

// =============================================================================
// Error types
// =============================================================================

/// Error returned when an insertion would violate the bijection invariant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InsertError {
    /// The term already exists in the map.
    DuplicateTerm,
    /// The value already exists in the map.
    DuplicateValue,
}

impl std::fmt::Display for InsertError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            InsertError::DuplicateTerm => write!(f, "duplicate term violates bijection invariant"),
            InsertError::DuplicateValue => {
                write!(f, "duplicate value violates bijection invariant")
            }
        }
    }
}

impl std::error::Error for InsertError {}

// =============================================================================
// Dictionary trait implementation
// =============================================================================

impl<V: DictionaryValue + Eq + Hash> Dictionary for BijectiveMap<V> {
    type Node = <DynamicDawgChar<V> as Dictionary>::Node;

    fn root(&self) -> Self::Node {
        self.forward.root()
    }

    fn contains(&self, term: &str) -> bool {
        self.forward.contains(term)
    }

    fn len(&self) -> Option<usize> {
        Some(self.reverse.read().len())
    }
}

// =============================================================================
// MappedDictionary trait implementation
// =============================================================================

impl<V: DictionaryValue + Eq + Hash> MappedDictionary for BijectiveMap<V> {
    type Value = V;

    fn get_value(&self, term: &str) -> Option<Self::Value> {
        Self::get_value(self, term)
    }

    fn contains_with_value<F>(&self, term: &str, predicate: F) -> bool
    where
        F: Fn(&Self::Value) -> bool,
    {
        self.forward.get_value(term).is_some_and(|v| predicate(&v))
    }
}

// =============================================================================
// BijectiveDictionary trait implementation
// =============================================================================

impl<V: DictionaryValue + Eq + Hash> BijectiveDictionary for BijectiveMap<V> {
    fn get_term(&self, value: &Self::Value) -> Option<std::borrow::Cow<'_, str>> {
        // Acquire the read guard, look up, clone the String into a Cow::Owned.
        // The clone is necessary because the read guard cannot escape this
        // function's stack frame and `Cow::Borrowed(&str)` would require a
        // borrow tied to `self`, not to the guard. The previous unsafe
        // pointer-dereference shortcut was unsound under concurrent inserts
        // (HashMap rehashing invalidates element pointers).
        let reverse = self.reverse.read();
        reverse
            .get(value)
            .map(|s| std::borrow::Cow::Owned(s.clone()))
    }

    fn contains_value(&self, value: &Self::Value) -> bool {
        Self::contains_value(self, value)
    }

    fn bijection_len(&self) -> usize {
        self.len()
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_empty_map() {
        let bimap: BijectiveMap<i32> = BijectiveMap::new();
        assert!(bimap.is_empty());
        assert_eq!(bimap.len(), 0);
        assert_eq!(bimap.get_value("test"), None);
        assert_eq!(bimap.get_term(&0), None);
    }

    #[test]
    fn test_single_pair() {
        let bimap: BijectiveMap<i32> = BijectiveMap::new();
        bimap.insert("hello", 42);

        assert_eq!(bimap.len(), 1);
        assert_eq!(bimap.get_value("hello"), Some(42));
        assert_eq!(bimap.get_term(&42), Some("hello".to_string()));
    }

    #[test]
    fn test_multiple_pairs() {
        let bimap = BijectiveMap::from_pairs([("a", 1), ("b", 2), ("c", 3)]);

        assert_eq!(bimap.len(), 3);
        assert_eq!(bimap.get_value("a"), Some(1));
        assert_eq!(bimap.get_value("b"), Some(2));
        assert_eq!(bimap.get_value("c"), Some(3));
        assert_eq!(bimap.get_term(&1), Some("a".to_string()));
        assert_eq!(bimap.get_term(&2), Some("b".to_string()));
        assert_eq!(bimap.get_term(&3), Some("c".to_string()));
    }

    #[test]
    #[should_panic(expected = "duplicate term")]
    fn test_duplicate_term_panics() {
        let bimap: BijectiveMap<i32> = BijectiveMap::new();
        bimap.insert("hello", 1);
        bimap.insert("hello", 2); // Should panic
    }

    #[test]
    #[should_panic(expected = "duplicate value")]
    fn test_duplicate_value_panics() {
        let bimap: BijectiveMap<i32> = BijectiveMap::new();
        bimap.insert("one", 1);
        bimap.insert("uno", 1); // Should panic
    }

    #[test]
    fn test_try_insert() {
        let bimap: BijectiveMap<i32> = BijectiveMap::new();

        assert!(bimap.try_insert("one", 1).is_ok());
        assert_eq!(bimap.try_insert("one", 2), Err(InsertError::DuplicateTerm));
        assert_eq!(bimap.try_insert("uno", 1), Err(InsertError::DuplicateValue));

        // Map should still be valid
        assert_eq!(bimap.len(), 1);
        assert_eq!(bimap.get_value("one"), Some(1));
    }

    #[test]
    fn test_contains() {
        let bimap = BijectiveMap::from_pairs([("hello", 42)]);

        assert!(bimap.contains_term("hello"));
        assert!(!bimap.contains_term("world"));
        assert!(bimap.contains_value(&42));
        assert!(!bimap.contains_value(&999));
    }

    #[test]
    fn test_unicode_terms() {
        let bimap = BijectiveMap::from_pairs([("café", 1), ("日本語", 2), ("🎉", 3), ("naïve", 4)]);

        assert_eq!(bimap.get_value("café"), Some(1));
        assert_eq!(bimap.get_value("日本語"), Some(2));
        assert_eq!(bimap.get_value("🎉"), Some(3));
        assert_eq!(bimap.get_value("naïve"), Some(4));

        assert_eq!(bimap.get_term(&1), Some("café".to_string()));
        assert_eq!(bimap.get_term(&2), Some("日本語".to_string()));
        assert_eq!(bimap.get_term(&3), Some("🎉".to_string()));
        assert_eq!(bimap.get_term(&4), Some("naïve".to_string()));
    }

    #[test]
    fn test_string_values() {
        let bimap: BijectiveMap<String> = BijectiveMap::new();
        bimap.insert("key1", "value1".to_string());
        bimap.insert("key2", "value2".to_string());

        assert_eq!(bimap.get_value("key1"), Some("value1".to_string()));
        assert_eq!(
            bimap.get_term(&"value1".to_string()),
            Some("key1".to_string())
        );
    }

    #[test]
    fn test_bijection_invariant() {
        let bimap = BijectiveMap::from_pairs([("a", 1), ("b", 2), ("c", 3)]);

        // For every (term, value) pair, the bijection should hold
        for (term, value) in bimap.iter() {
            assert_eq!(bimap.get_value(&term), Some(value.clone()));
            assert_eq!(bimap.get_term(&value), Some(term));
        }
    }

    #[test]
    fn test_iter() {
        let bimap = BijectiveMap::from_pairs([("x", 1), ("y", 2), ("z", 3)]);

        let pairs: Vec<_> = bimap.iter().collect();

        assert_eq!(pairs.len(), 3);
        assert!(pairs.contains(&("x".to_string(), 1)));
        assert!(pairs.contains(&("y".to_string(), 2)));
        assert!(pairs.contains(&("z".to_string(), 3)));
    }

    #[test]
    fn test_terms_and_values() {
        let bimap = BijectiveMap::from_pairs([("a", 1), ("b", 2)]);

        let terms: Vec<_> = bimap.terms().collect();
        let values: Vec<_> = bimap.values().collect();

        assert_eq!(terms.len(), 2);
        assert!(terms.contains(&"a".to_string()));
        assert!(terms.contains(&"b".to_string()));

        assert_eq!(values.len(), 2);
        assert!(values.contains(&1));
        assert!(values.contains(&2));
    }

    #[test]
    fn test_mapped_dictionary_trait() {
        use crate::MappedDictionary;

        let bimap = BijectiveMap::from_pairs([("test", 42)]);

        // Test via trait
        assert_eq!(MappedDictionary::get_value(&bimap, "test"), Some(42));
        assert_eq!(MappedDictionary::get_value(&bimap, "missing"), None);
    }

    #[test]
    fn test_bijective_dictionary_trait() {
        use crate::bijective::BijectiveDictionary;

        let bimap = BijectiveMap::from_pairs([("test", 42)]);

        // Test via trait (returns Option<Cow<'_, str>> now).
        let term = BijectiveDictionary::get_term(&bimap, &42);
        assert_eq!(term.as_deref(), Some("test"));
        assert!(BijectiveDictionary::get_term(&bimap, &99).is_none());
        assert!(BijectiveDictionary::contains_value(&bimap, &42));
        assert!(!BijectiveDictionary::contains_value(&bimap, &99));
        assert_eq!(BijectiveDictionary::bijection_len(&bimap), 1);
    }

    #[test]
    fn test_with_capacity() {
        let bimap: BijectiveMap<i32> = BijectiveMap::with_capacity(1000);
        assert!(bimap.is_empty());

        // Should still work normally
        bimap.insert("test", 1);
        assert_eq!(bimap.get_value("test"), Some(1));
    }
}