arcweight 0.3.0

A high-performance, modular library for weighted finite state transducers with comprehensive examples and benchmarks
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
//! Vector-based mutable FST implementation.
//!
//! This module provides [`VectorFst`], the primary mutable FST implementation optimized
//! for construction and modification operations. The design follows the OpenFst library's
//! `StdVectorFst` implementation while leveraging Rust's ownership and memory safety guarantees.
//!
//! # Architecture
//!
//! `VectorFst` uses a straightforward vector-of-vectors storage model:
//!
//! ```text
//! VectorFst<W>
//! +------------------+
//! | states: Vec      |     VectorState<W>
//! |   [0] ---------> | +------------------+
//! |   [1] ...        | | final_weight     |
//! |   [2] ...        | | arcs: SmallVec   |
//! +------------------+ +------------------+
//! | start: Option    |
//! | properties       |
//! +------------------+
//! ```
//!
//! Arc storage uses `SmallVec` with inline capacity of 8 arcs, avoiding heap allocation
//! for states with few outgoing transitions (approximately 90% of states in typical WFSTs).
//!
//! # References
//!
//! - Allauzen, C., Riley, M., Schalkwyk, J., Skut, W., & Mohri, M. (2007).
//!   OpenFst: A General and Efficient Weighted Finite-State Transducer Library.
//!   In *Proc. CIAA 2007*, LNCS 4783, pp. 11-23. Springer.

use super::traits::*;
use crate::arc::{Arc, ArcIterator};
use crate::properties::{compute_properties, FstProperties};
use crate::semiring::Semiring;
use core::slice;
use smallvec::SmallVec;

/// Inline capacity for arcs per state.
/// 8 arcs covers ~90% of states in typical WFSTs without heap allocation.
const ARC_INLINE_CAPACITY: usize = 8;

/// Type alias for arc storage using SmallVec for inline optimization
type ArcVec<W> = SmallVec<[Arc<W>; ARC_INLINE_CAPACITY]>;

/// State data in vector FST
#[derive(Debug, Clone)]
struct VectorState<W: Semiring> {
    /// Final weight (None if not final)
    final_weight: Option<W>,
    /// Outgoing arcs - uses SmallVec for inline storage of up to 8 arcs
    arcs: ArcVec<W>,
}

impl<W: Semiring> Default for VectorState<W> {
    fn default() -> Self {
        Self {
            final_weight: None,
            arcs: SmallVec::new(),
        }
    }
}

/// Vector-based mutable FST implementation optimized for construction and modification.
///
/// `VectorFst` is the primary mutable FST implementation in ArcWeight, storing states
/// and arcs in dynamically resizable vectors. This design closely follows the OpenFst
/// library's `StdVectorFst` while leveraging Rust's memory safety guarantees.
///
/// # Type Parameters
///
/// - `W`: The semiring type for arc and final state weights. Must implement [`Semiring`].
///
/// # Design Characteristics
///
/// - **Mutability**: Full support for adding/removing states and arcs
/// - **Memory Layout**: Contiguous vector storage for cache-friendly access
/// - **Random Access**: $`O(1)`$ access to any state or arc by index
/// - **Dynamic Growth**: Automatic resizing with exponential growth strategy
/// - **Small Arc Optimization**: Uses `SmallVec<[Arc<W>; 8]>` for inline storage
///
/// # Complexity
///
/// | Operation | Time Complexity | Notes |
/// |-----------|----------------|-------|
/// | `add_state()` | $`O(1)`$ amortized | Vector reallocation rare |
/// | `add_arc(s, arc)` | $`O(1)`$ amortized | SmallVec inline for ≤8 arcs |
/// | `arcs(s)` | $`O(1)`$ to create | Iterator over arc slice |
/// | `num_arcs(s)` | $`O(1)`$ | Direct length access |
/// | `num_states()` | $`O(1)`$ | Cached vector length |
/// | `compute_properties()` | $`O(V + E)`$ | Full graph traversal |
///
/// # Memory Characteristics
///
/// - **State Overhead**: ~24 bytes per state (excluding arc storage)
/// - **Arc Storage**: 8 arcs inline, heap allocation beyond that
/// - **Arc Size**: ~32 bytes per arc (labels + weight + nextstate)
/// - **Growth Factor**: 2x on reallocation (standard Rust Vec behavior)
///
/// # Use Cases
///
/// ## FST Construction
/// ```rust
/// use arcweight::prelude::*;
///
/// // Build a simple word acceptor for "hello"
/// fn build_word_acceptor(word: &str) -> VectorFst<BooleanWeight> {
///     let mut fst = VectorFst::new();
///     
///     // Create state chain
///     let mut states = Vec::new();
///     for _ in 0..=word.len() {
///         states.push(fst.add_state());
///     }
///     
///     fst.set_start(states[0]);
///     fst.set_final(states[word.len()], BooleanWeight::one());
///     
///     // Add character transitions
///     for (i, ch) in word.chars().enumerate() {
///         fst.add_arc(states[i], Arc::new(
///             ch as u32, ch as u32, BooleanWeight::one(), states[i + 1]
///         ));
///     }
///     
///     fst
/// }
///
/// let hello_fst = build_word_acceptor("hello");
/// assert_eq!(hello_fst.num_states(), 6);
/// ```
///
/// ## Weighted Transduction
/// ```rust
/// use arcweight::prelude::*;
///
/// // Build pronunciation dictionary: orthography -> phonemes
/// fn build_pronunciation_entry(
///     orthography: &str,
///     phonemes: &str,
///     frequency: f32
/// ) -> VectorFst<TropicalWeight> {
///     let mut fst = VectorFst::new();
///     
///     let mut current = fst.add_state();
///     fst.set_start(current);
///     
///     // Input: orthographic characters
///     for ch in orthography.chars() {
///         let next = fst.add_state();
///         fst.add_arc(current, Arc::new(
///             ch as u32, 0, // Input char, epsilon output
///             TropicalWeight::new(-frequency.ln()), // Negative log frequency
///             next
///         ));
///         current = next;
///     }
///     
///     // Output: phonemic sequence
///     for ph in phonemes.chars() {
///         let next = fst.add_state();
///         fst.add_arc(current, Arc::new(
///             0, ph as u32, // Epsilon input, phoneme output
///             TropicalWeight::one(),
///             next
///         ));
///         current = next;
///     }
///     
///     fst.set_final(current, TropicalWeight::one());
///     fst
/// }
/// ```
///
/// ## Dynamic FST Modification
/// ```rust
/// use arcweight::prelude::*;
///
/// // Incrementally build vocabulary FST
/// fn build_vocabulary_incrementally(words: &[&str]) -> VectorFst<BooleanWeight> {
///     let mut fst = VectorFst::new();
///     let root = fst.add_state();
///     fst.set_start(root);
///     
///     for word in words {
///         // Add word to existing trie structure
///         add_word_to_trie(&mut fst, root, word);
///     }
///     
///     fst
/// }
///
/// fn add_word_to_trie(fst: &mut VectorFst<BooleanWeight>, mut current: u32, word: &str) {
///     for ch in word.chars() {
///         // Find existing arc or create new path
///         let label = ch as u32;
///         
///         if let Some(next) = find_arc_target(fst, current, label) {
///             current = next;
///         } else {
///             let next = fst.add_state();
///             fst.add_arc(current, Arc::new(label, label, BooleanWeight::one(), next));
///             current = next;
///         }
///     }
///     fst.set_final(current, BooleanWeight::one());
/// }
///
/// fn find_arc_target(fst: &VectorFst<BooleanWeight>, state: u32, label: u32) -> Option<u32> {
///     fst.arcs(state).find(|arc| arc.ilabel == label).map(|arc| arc.nextstate)
/// }
/// ```
///
/// # Optimization Considerations
///
/// ## Memory Pre-allocation
/// ```rust
/// use arcweight::prelude::*;
///
/// // Pre-allocate for known size to avoid reallocations
/// let mut fst = VectorFst::<TropicalWeight>::new();
/// fst.reserve_states(1000);  // Reserve space for 1000 states
///
/// let state = fst.add_state();
/// fst.reserve_arcs(state, 50);  // Reserve space for 50 arcs from this state
/// ```
///
/// ## Batch Operations
/// ```rust
/// use arcweight::prelude::*;
///
/// // Batch state creation for better performance
/// fn build_linear_chain(length: usize) -> VectorFst<BooleanWeight> {
///     let mut fst = VectorFst::new();
///     fst.reserve_states(length + 1);
///     
///     // Create all states at once
///     let states: Vec<_> = (0..=length).map(|_| fst.add_state()).collect();
///     
///     fst.set_start(states[0]);
///     fst.set_final(states[length], BooleanWeight::one());
///     
///     // Add transitions
///     for i in 0..length {
///         fst.add_arc(states[i], Arc::new(
///             (i + 1) as u32, (i + 1) as u32, BooleanWeight::one(), states[i + 1]
///         ));
///     }
///     
///     fst
/// }
/// ```
///
/// # Thread Safety
///
/// `VectorFst` is `Send + Sync` when the semiring type is `Send + Sync`, enabling:
/// - **Parallel Construction**: Build separate FSTs concurrently
/// - **Read-Only Sharing**: Share immutable references across threads
/// - **Algorithm Parallelization**: Use as input to parallel algorithms
///
/// Mutation requires exclusive access (`&mut self`), preventing data races.
/// For concurrent mutable access, use [`ConcurrentFst`] instead.
///
/// # References
///
/// - Allauzen, C., Riley, M., Schalkwyk, J., Skut, W., & Mohri, M. (2007).
///   OpenFst: A General and Efficient Weighted Finite-State Transducer Library.
///   In *Proc. CIAA 2007*, LNCS 4783, pp. 11-23. Springer.
///
/// - Mohri, M., Pereira, F., & Riley, M. (2000). The Design Principles of a
///   Weighted Finite-State Transducer Library. *Theoretical Computer Science*,
///   231(1), 17-32.
///
/// # See Also
///
/// - [`ConstFst`] for read-only memory-optimized FSTs
/// - [`CacheFst`] for caching wrapper
/// - [`ConcurrentFst`] for thread-safe mutable access
///
/// [`ConstFst`]: crate::fst::ConstFst
/// [`CacheFst`]: crate::fst::CacheFst
/// [`ConcurrentFst`]: crate::fst::ConcurrentFst
#[derive(Debug, Clone)]
pub struct VectorFst<W: Semiring> {
    states: Vec<VectorState<W>>,
    start: Option<StateId>,
    properties: FstProperties,
}

impl<W: Semiring> VectorFst<W> {
    /// Create a new empty FST
    ///
    /// # Examples
    ///
    /// ```
    /// use arcweight::prelude::*;
    ///
    /// let fst = VectorFst::<TropicalWeight>::new();
    /// assert_eq!(fst.num_states(), 0);
    /// assert!(fst.start().is_none());
    /// ```
    pub fn new() -> Self {
        Self {
            states: Vec::new(),
            start: None,
            properties: FstProperties::default(),
        }
    }

    /// Create with capacity
    ///
    /// Pre-allocates space for the specified number of states to avoid
    /// reallocations during FST construction.
    ///
    /// # Examples
    ///
    /// ```
    /// use arcweight::prelude::*;
    ///
    /// let mut fst = VectorFst::<TropicalWeight>::with_capacity(100);
    ///
    /// // Add many states efficiently
    /// for _ in 0..100 {
    ///     fst.add_state();
    /// }
    ///
    /// assert_eq!(fst.num_states(), 100);
    /// ```
    pub fn with_capacity(states: usize) -> Self {
        Self {
            states: Vec::with_capacity(states),
            start: None,
            properties: FstProperties::default(),
        }
    }

    /// Compute and cache properties
    pub fn compute_properties(&mut self) {
        self.properties = compute_properties(self);
    }
}

impl<W: Semiring> Default for VectorFst<W> {
    fn default() -> Self {
        Self::new()
    }
}

/// Arc iterator for VectorFst
#[derive(Debug)]
pub struct VectorArcIterator<'a, W: Semiring> {
    arcs: slice::Iter<'a, Arc<W>>,
}

impl<W: Semiring> Iterator for VectorArcIterator<'_, W> {
    type Item = Arc<W>;

    fn next(&mut self) -> Option<Self::Item> {
        self.arcs.next().cloned()
    }
}

impl<W: Semiring> ArcIterator<W> for VectorArcIterator<'_, W> {}

impl<W: Semiring> Fst<W> for VectorFst<W> {
    type ArcIter<'a>
        = VectorArcIterator<'a, W>
    where
        W: 'a;

    fn start(&self) -> Option<StateId> {
        self.start
    }

    fn final_weight(&self, state: StateId) -> Option<&W> {
        self.states
            .get(state as usize)
            .and_then(|s| s.final_weight.as_ref())
    }

    fn num_arcs(&self, state: StateId) -> usize {
        self.states
            .get(state as usize)
            .map(|s| s.arcs.len())
            .unwrap_or(0)
    }

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

    fn properties(&self) -> FstProperties {
        // If properties are not computed, compute them
        if self.properties.known.is_empty() {
            compute_properties(self)
        } else {
            self.properties
        }
    }

    fn arcs(&self, state: StateId) -> Self::ArcIter<'_> {
        let arcs = self
            .states
            .get(state as usize)
            .map(|s| s.arcs.iter())
            .unwrap_or_else(|| [].iter());
        VectorArcIterator { arcs }
    }
}

impl<W: Semiring> MutableFst<W> for VectorFst<W> {
    /// # Examples
    ///
    /// ```
    /// use arcweight::prelude::*;
    ///
    /// let mut fst = VectorFst::<TropicalWeight>::new();
    ///
    /// let s0 = fst.add_state();
    /// let s1 = fst.add_state();
    ///
    /// assert_eq!(s0, 0);
    /// assert_eq!(s1, 1);
    /// assert_eq!(fst.num_states(), 2);
    /// ```
    fn add_state(&mut self) -> StateId {
        let id = self.states.len() as StateId;
        self.states.push(VectorState::default());
        self.properties.invalidate_all();
        id
    }

    /// # Examples
    ///
    /// ```
    /// use arcweight::prelude::*;
    ///
    /// let mut fst = VectorFst::<TropicalWeight>::new();
    /// let s0 = fst.add_state();
    /// let s1 = fst.add_state();
    ///
    /// // Add arc: input=1, output=1, weight=0.5, target=s1
    /// fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(0.5), s1));
    ///
    /// assert_eq!(fst.num_arcs(s0), 1);
    /// ```
    fn add_arc(&mut self, state: StateId, arc: Arc<W>) {
        if let Some(s) = self.states.get_mut(state as usize) {
            s.arcs.push(arc);
            self.properties.invalidate_all();
        }
    }

    fn set_start(&mut self, state: StateId) {
        self.start = Some(state);
        self.properties.invalidate_all();
    }

    /// # Examples
    ///
    /// ```
    /// use arcweight::prelude::*;
    ///
    /// let mut fst = VectorFst::<TropicalWeight>::new();
    /// let s0 = fst.add_state();
    /// let s1 = fst.add_state();
    ///
    /// // Make s1 a final state with weight 0.8
    /// fst.set_final(s1, TropicalWeight::new(0.8));
    ///
    /// assert!(fst.is_final(s1));
    /// assert_eq!(fst.final_weight(s1), Some(&TropicalWeight::new(0.8)));
    /// ```
    fn set_final(&mut self, state: StateId, weight: W) {
        if let Some(s) = self.states.get_mut(state as usize) {
            s.final_weight = if <W as num_traits::Zero>::is_zero(&weight) {
                None
            } else {
                Some(weight)
            };
            self.properties.invalidate_all();
        }
    }

    fn delete_arcs(&mut self, state: StateId) {
        if let Some(s) = self.states.get_mut(state as usize) {
            s.arcs.clear();
            self.properties.invalidate_all();
        }
    }

    fn delete_arc(&mut self, state: StateId, arc_idx: usize) {
        if let Some(s) = self.states.get_mut(state as usize) {
            if arc_idx < s.arcs.len() {
                s.arcs.remove(arc_idx);
                self.properties.invalidate_all();
            }
        }
    }

    fn reserve_states(&mut self, n: usize) {
        self.states.reserve(n);
    }

    fn reserve_arcs(&mut self, state: StateId, n: usize) {
        if let Some(s) = self.states.get_mut(state as usize) {
            s.arcs.reserve(n);
        }
    }

    fn clear(&mut self) {
        self.states.clear();
        self.start = None;
        self.properties = FstProperties::default();
    }
}

impl<W: Semiring> ExpandedFst<W> for VectorFst<W> {
    fn arcs_slice(&self, state: StateId) -> &[Arc<W>] {
        self.states
            .get(state as usize)
            .map(|s| s.arcs.as_slice())
            .unwrap_or(&[])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::semiring::TropicalWeight;
    use num_traits::One;

    #[test]
    fn test_empty_fst() {
        let fst = VectorFst::<TropicalWeight>::new();

        assert_eq!(fst.num_states(), 0);
        assert!(fst.is_empty());
        assert_eq!(fst.start(), None);
        assert_eq!(fst.num_arcs_total(), 0);
    }

    #[test]
    fn test_add_state() {
        let mut fst = VectorFst::<TropicalWeight>::new();

        let s0 = fst.add_state();
        let s1 = fst.add_state();

        assert_eq!(s0, 0);
        assert_eq!(s1, 1);
        assert_eq!(fst.num_states(), 2);

        // FST is considered empty until start state is set
        fst.set_start(s0);
        assert!(!fst.is_empty());
    }

    #[test]
    fn test_start_state() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();

        assert_eq!(fst.start(), None);

        fst.set_start(s0);
        assert_eq!(fst.start(), Some(s0));
    }

    #[test]
    fn test_final_states() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();

        assert!(!fst.is_final(s0));
        assert!(!fst.is_final(s1));

        fst.set_final(s1, TropicalWeight::new(2.5));
        assert!(!fst.is_final(s0));
        assert!(fst.is_final(s1));
        assert_eq!(*fst.final_weight(s1).unwrap().value(), 2.5);

        fst.remove_final(s1);
        assert!(!fst.is_final(s1));
    }

    #[test]
    fn test_add_arcs() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();

        assert_eq!(fst.num_arcs(s0), 0);
        assert_eq!(fst.num_arcs(s1), 0);

        fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(1.5), s1));
        fst.add_arc(s0, Arc::new(3, 4, TropicalWeight::new(2.0), s1));

        assert_eq!(fst.num_arcs(s0), 2);
        assert_eq!(fst.num_arcs(s1), 0);
        assert_eq!(fst.num_arcs_total(), 2);
    }

    #[test]
    fn test_arc_iteration() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();

        fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(1.5), s1));
        fst.add_arc(s0, Arc::new(3, 4, TropicalWeight::new(2.0), s1));

        let arcs: Vec<_> = fst.arcs(s0).collect();
        assert_eq!(arcs.len(), 2);

        assert_eq!(arcs[0].ilabel, 1);
        assert_eq!(arcs[0].olabel, 2);
        assert_eq!(*arcs[0].weight.value(), 1.5);
        assert_eq!(arcs[0].nextstate, s1);

        assert_eq!(arcs[1].ilabel, 3);
        assert_eq!(arcs[1].olabel, 4);
        assert_eq!(*arcs[1].weight.value(), 2.0);
        assert_eq!(arcs[1].nextstate, s1);
    }

    #[test]
    fn test_reserve_states() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        fst.reserve_states(100);

        // Add states and verify they are added efficiently
        for i in 0..100 {
            let state = fst.add_state();
            assert_eq!(state, i);
        }

        assert_eq!(fst.num_states(), 100);
    }

    #[test]
    fn test_reserve_arcs() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();

        fst.reserve_arcs(s0, 50);

        // Add arcs and verify they are added efficiently
        for i in 0..50 {
            fst.add_arc(s0, Arc::new(i, i, TropicalWeight::new(i as f32), s1));
        }

        assert_eq!(fst.num_arcs(s0), 50);
    }

    #[test]
    fn test_clear() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(1.5), s1));

        assert!(!fst.is_empty());
        assert_eq!(fst.num_states(), 2);

        fst.clear();

        assert!(fst.is_empty());
        assert_eq!(fst.num_states(), 0);
        assert_eq!(fst.start(), None);
    }

    #[test]
    fn test_state_iteration() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();

        let states: Vec<_> = fst.states().collect();
        assert_eq!(states, vec![s0, s1, s2]);
    }

    // Property-based tests
    mod proptests {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            #[test]
            fn test_fst_state_consistency_property(num_states in 1..100usize) {
                let mut fst = VectorFst::<TropicalWeight>::new();

                for _ in 0..num_states {
                    fst.add_state();
                }

                assert_eq!(fst.num_states(), num_states);

                // all states should be valid
                for state in fst.states() {
                    assert!(state < num_states as StateId);
                }

                // Arc counts should be consistent
                let total_arcs = fst.num_arcs_total();
                let sum_arcs: usize = fst.states().map(|s| fst.num_arcs(s)).sum();
                assert_eq!(total_arcs, sum_arcs);
            }
        }
    }
}