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
use crate::algorithms::factor_weight::factor_iterators::{
    StringFactorLeft, StringFactorRestrict, StringFactorRight,
};
use crate::algorithms::factor_weight::FactorIterator;
use crate::semirings::{
    GallicWeight, GallicWeightLeft, GallicWeightMin, GallicWeightRestrict, GallicWeightRight,
    Semiring, StringWeightVariant,
};

#[derive(Debug, PartialEq, Clone)]
/// Factor a GallicWeight using StringFactor.
pub struct GallicFactorLeft<W: Semiring> {
    weight: GallicWeightLeft<W>,
    done: bool,
}

#[derive(Debug, PartialEq, Clone)]
/// Factor a GallicWeight using StringFactor.
pub struct GallicFactorRight<W: Semiring> {
    weight: GallicWeightRight<W>,
    done: bool,
}

#[derive(Debug, PartialEq, Clone)]
/// Factor a GallicWeight using StringFactor.
pub struct GallicFactorMin<W: Semiring> {
    weight: GallicWeightMin<W>,
    done: bool,
}

#[derive(Debug, PartialEq, Clone)]
/// Factor a GallicWeight using StringFactor.
pub struct GallicFactorRestrict<W: Semiring> {
    weight: GallicWeightRestrict<W>,
    done: bool,
}

macro_rules! impl_gallic_factor {
    ($gallic: ident, $g_factor: ident, $s_factor: ident) => {
        impl<W: Semiring> Iterator for $g_factor<W> {
            type Item = ($gallic<W>, $gallic<W>);

            fn next(&mut self) -> Option<Self::Item> {
                if self.done() {
                    return None;
                }
                let mut it = $s_factor::new(self.weight.value1().clone());
                let lol = it.next();
                let (p_f, p_s) = lol.unwrap();
                let g1 = (p_f, self.weight.value2().clone()).into();
                let g2 = (p_s, W::one()).into();
                self.done = true;
                Some((g1, g2))
            }
        }

        impl<W: Semiring> FactorIterator<$gallic<W>> for $g_factor<W> {
            fn new(weight: $gallic<W>) -> Self {
                let done = match &weight.value1().value {
                    StringWeightVariant::Infinity => true,
                    StringWeightVariant::Labels(l) => (l.len() <= 1),
                };
                Self { weight, done }
            }

            fn done(&self) -> bool {
                self.done
            }
        }
    };
}

impl_gallic_factor!(GallicWeightLeft, GallicFactorLeft, StringFactorLeft);
impl_gallic_factor!(GallicWeightRight, GallicFactorRight, StringFactorRight);
impl_gallic_factor!(
    GallicWeightRestrict,
    GallicFactorRestrict,
    StringFactorRestrict
);
impl_gallic_factor!(GallicWeightMin, GallicFactorMin, StringFactorRestrict);

#[derive(Debug, PartialEq, Clone)]
/// Factor a GallicWeight using StringFactor.
pub struct GallicFactor<W: Semiring> {
    weight: GallicWeight<W>,
    done: bool,
    idx: usize,
}

impl<W: Semiring> Iterator for GallicFactor<W> {
    type Item = (GallicWeight<W>, GallicWeight<W>);

    fn next(&mut self) -> Option<Self::Item> {
        if self.done() {
            return None;
        }
        let w = &self.weight.0.list[self.idx];
        let mut s_it = StringFactorRestrict::new(w.value1().clone());
        let (p_f, p_s) = s_it.next().unwrap_or_else(|| {
            (
                StringWeightVariant::Labels(vec![]).into(),
                StringWeightVariant::Labels(vec![]).into(),
            )
        });

        let grw1: GallicWeightRestrict<W> = (p_f, w.value2().clone()).into();
        let grw2: GallicWeightRestrict<W> = (p_s, W::one()).into();
        self.idx += 1;
        Some((grw1.into(), grw2.into()))
    }
}

impl<W: Semiring> FactorIterator<GallicWeight<W>> for GallicFactor<W> {
    fn new(weight: GallicWeight<W>) -> Self {
        let done = weight.0.list.is_empty()
            || (weight.0.list.len() == 1 && weight.0.list[0].value1().len_labels() <= 1);
        Self {
            weight,
            done,
            idx: 0,
        }
    }

    fn done(&self) -> bool {
        self.done || (self.idx >= self.weight.0.list.len())
    }
}