rustfst/algorithms/factor_weight/factor_iterators/
string_factor.rs1use crate::algorithms::factor_weight::FactorIterator;
2use crate::semirings::{
3 StringWeightLeft, StringWeightRestrict, StringWeightRight, StringWeightVariant,
4};
5
6#[derive(Debug, PartialEq, Clone)]
7pub struct StringFactorLeft {
9 weight: StringWeightLeft,
10 done: bool,
11}
12
13#[derive(Debug, PartialEq, Clone)]
14pub struct StringFactorRight {
16 weight: StringWeightRight,
17 done: bool,
18}
19
20#[derive(Debug, PartialEq, Clone)]
21pub struct StringFactorRestrict {
23 weight: StringWeightRestrict,
24 done: bool,
25}
26
27macro_rules! impl_string_factor {
28 ($factor: ident, $semiring: ident) => {
29 impl Iterator for $factor {
30 type Item = ($semiring, $semiring);
31
32 fn next(&mut self) -> Option<Self::Item> {
33 if self.done() {
34 return None;
35 }
36 let l = self.weight.value.unwrap_labels();
37 let l1 = vec![l[0]];
38 let l2: Vec<_> = l.iter().skip(1).cloned().collect();
39 self.done = true;
40 Some((l1.into(), l2.into()))
41 }
42 }
43
44 impl FactorIterator<$semiring> for $factor {
45 fn new(weight: $semiring) -> Self {
46 let done = match &weight.value {
47 StringWeightVariant::Infinity => true,
48 StringWeightVariant::Labels(l) => (l.len() == 0),
49 };
50 Self { weight, done }
51 }
52 fn done(&self) -> bool {
53 self.done
54 }
55 }
56 };
57}
58
59impl_string_factor!(StringFactorLeft, StringWeightLeft);
60impl_string_factor!(StringFactorRight, StringWeightRight);
61impl_string_factor!(StringFactorRestrict, StringWeightRestrict);