rustfst/fst_traits/iterators.rs
1use crate::fst_traits::CoreFst;
2use crate::semirings::Semiring;
3use crate::tr::Tr;
4use crate::StateId;
5
6/// Trait to iterate over the states of a wFST.
7pub trait StateIterator<'a> {
8 /// Iterator used to iterate over the `state_id` of the states of an FST.
9 type Iter: Iterator<Item = StateId>;
10
11 /// Creates an iterator over the `state_id` of the states of an FST.
12 ///
13 /// # Example
14 ///
15 /// ```
16 /// # use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst, StateIterator};
17 /// # use rustfst::fst_impls::VectorFst;
18 /// # use rustfst::semirings::{BooleanWeight, Semiring};
19 /// let mut fst = VectorFst::<BooleanWeight>::new();
20 ///
21 /// let s1 = fst.add_state();
22 /// let s2 = fst.add_state();
23 ///
24 /// for state_id in fst.states_iter() {
25 /// println!("State ID : {:?}", state_id);
26 /// }
27 ///
28 /// let states : Vec<_> = fst.states_iter().collect();
29 /// assert_eq!(states, vec![s1, s2]);
30 /// ```
31 fn states_iter(&'a self) -> Self::Iter;
32}
33
34pub struct FstIterData<W, I> {
35 pub state_id: StateId,
36 pub final_weight: Option<W>,
37 pub trs: I,
38 pub num_trs: usize,
39}
40
41pub trait FstIntoIterator<W: Semiring>: CoreFst<W> {
42 type TrsIter: Iterator<Item = Tr<W>>;
43 type FstIter: Iterator<Item = FstIterData<W, Self::TrsIter>>;
44 fn fst_into_iter(self) -> Self::FstIter;
45}
46
47pub trait FstIterator<'a, W: Semiring>: CoreFst<W> {
48 type FstIter: Iterator<Item = FstIterData<W, Self::TRS>>;
49 fn fst_iter(&'a self) -> Self::FstIter;
50}