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
use std::borrow::Borrow;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::sync::Arc;

use anyhow::Result;

use crate::algorithms::compose::lookahead_matchers::{LabelLookAheadRelabeler, LookaheadMatcher};
use crate::algorithms::compose::matchers::MatchType;
use crate::algorithms::compose::FstAddOn;
use crate::algorithms::compose::LabelReachableData;
use crate::fst_properties::FstProperties;
use crate::fst_traits::{
    CoreFst, ExpandedFst, Fst, FstIntoIterator, FstIterator, MutableFst, StateIterator,
};
use crate::semirings::Semiring;
use crate::{StateId, SymbolTable};

type InnerFstAddOn<F, T> = FstAddOn<F, (Option<Arc<T>>, Option<Arc<T>>)>;

#[derive(Clone, PartialEq, Debug)]
pub struct MatcherFst<W, F, B, M, T> {
    fst_add_on: InnerFstAddOn<F, T>,
    matcher: PhantomData<M>,
    w: PhantomData<(W, B)>,
}

impl<W, F, B, M, T> MatcherFst<W, F, B, M, T> {
    pub fn fst(&self) -> &F {
        self.fst_add_on.fst()
    }

    pub fn addon(&self) -> &(Option<Arc<T>>, Option<Arc<T>>) {
        self.fst_add_on.add_on()
    }

    pub fn data(&self, match_type: MatchType) -> Option<&Arc<T>> {
        let data = self.fst_add_on.add_on();
        if match_type == MatchType::MatchInput {
            data.0.as_ref()
        } else {
            data.1.as_ref()
        }
    }
}

// TODO: To be generalized
impl<W, F, B, M> MatcherFst<W, F, B, M, M::MatcherData>
where
    W: Semiring,
    F: MutableFst<W>,
    B: Borrow<F>,
    M: LookaheadMatcher<W, F, B, MatcherData = LabelReachableData>,
{
    pub fn new(mut fst: F) -> Result<Self> {
        let imatcher_data = M::create_data::<F, _>(&fst, MatchType::MatchInput)?;
        let omatcher_data = M::create_data::<F, _>(&fst, MatchType::MatchOutput)?;

        let mut add_on = (imatcher_data, omatcher_data);
        LabelLookAheadRelabeler::init(&mut fst, &mut add_on)?;

        let add_on = (add_on.0.map(Arc::new), add_on.1.map(Arc::new));

        let fst_add_on = FstAddOn::new(fst, add_on);
        Ok(Self {
            fst_add_on,
            matcher: PhantomData,
            w: PhantomData,
        })
    }

    // Construct a new Matcher Fst intended for LookAhead composition and relabel fst2 wrt to the first fst.
    pub fn new_with_relabeling<F2: MutableFst<W>>(
        mut fst: F,
        fst2: &mut F2,
        relabel_input: bool,
    ) -> Result<Self> {
        let imatcher_data = M::create_data::<F, _>(&fst, MatchType::MatchInput)?;
        let omatcher_data = M::create_data::<F, _>(&fst, MatchType::MatchOutput)?;

        let mut add_on = (imatcher_data, omatcher_data);
        LabelLookAheadRelabeler::init(&mut fst, &mut add_on)?;
        LabelLookAheadRelabeler::relabel(fst2, &mut add_on, relabel_input)?;

        let add_on = (add_on.0.map(Arc::new), add_on.1.map(Arc::new));

        let fst_add_on = FstAddOn::new(fst, add_on);
        Ok(Self {
            fst_add_on,
            matcher: PhantomData,
            w: PhantomData,
        })
    }
}

impl<W: Semiring, F: CoreFst<W>, B: Borrow<F>, M, T> CoreFst<W> for MatcherFst<W, F, B, M, T> {
    type TRS = <FstAddOn<F, T> as CoreFst<W>>::TRS;

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

    fn final_weight(&self, state_id: StateId) -> Result<Option<W>> {
        self.fst_add_on.final_weight(state_id)
    }

    unsafe fn final_weight_unchecked(&self, state_id: StateId) -> Option<W> {
        self.fst_add_on.final_weight_unchecked(state_id)
    }

    fn num_trs(&self, s: StateId) -> Result<usize> {
        self.fst_add_on.num_trs(s)
    }

    unsafe fn num_trs_unchecked(&self, s: StateId) -> usize {
        self.fst_add_on.num_trs_unchecked(s)
    }

    fn get_trs(&self, state_id: StateId) -> Result<Self::TRS> {
        self.fst_add_on.get_trs(state_id)
    }

    unsafe fn get_trs_unchecked(&self, state_id: StateId) -> Self::TRS {
        self.fst_add_on.get_trs_unchecked(state_id)
    }

    fn properties(&self) -> FstProperties {
        self.fst_add_on.properties()
    }

    fn num_input_epsilons(&self, state: StateId) -> Result<usize> {
        self.fst_add_on.num_input_epsilons(state)
    }

    fn num_output_epsilons(&self, state: StateId) -> Result<usize> {
        self.fst_add_on.num_output_epsilons(state)
    }
}

impl<'a, W, F: StateIterator<'a>, B: Borrow<F>, M, T> StateIterator<'a>
    for MatcherFst<W, F, B, M, T>
{
    type Iter = <F as StateIterator<'a>>::Iter;

    fn states_iter(&'a self) -> Self::Iter {
        self.fst_add_on.states_iter()
    }
}

impl<'a, W, F, B, M, T> FstIterator<'a, W> for MatcherFst<W, F, B, M, T>
where
    W: Semiring,
    F: FstIterator<'a, W>,
    B: Borrow<F>,
{
    type FstIter = F::FstIter;

    fn fst_iter(&'a self) -> Self::FstIter {
        self.fst_add_on.fst_iter()
    }
}

impl<W, F, B, M, T> Fst<W> for MatcherFst<W, F, B, M, T>
where
    W: Semiring,
    F: Fst<W>,
    B: Borrow<F> + Debug,
    M: Debug,
    T: Debug,
{
    fn input_symbols(&self) -> Option<&Arc<SymbolTable>> {
        self.fst_add_on.input_symbols()
    }

    fn output_symbols(&self) -> Option<&Arc<SymbolTable>> {
        self.fst_add_on.output_symbols()
    }

    fn set_input_symbols(&mut self, symt: Arc<SymbolTable>) {
        self.fst_add_on.set_input_symbols(symt)
    }

    fn set_output_symbols(&mut self, symt: Arc<SymbolTable>) {
        self.fst_add_on.set_output_symbols(symt)
    }

    fn take_input_symbols(&mut self) -> Option<Arc<SymbolTable>> {
        self.fst_add_on.take_input_symbols()
    }

    fn take_output_symbols(&mut self) -> Option<Arc<SymbolTable>> {
        self.fst_add_on.take_output_symbols()
    }
}

impl<W, F, M, B, T> ExpandedFst<W> for MatcherFst<W, F, B, M, T>
where
    W: Semiring,
    F: ExpandedFst<W>,
    B: Borrow<F> + Debug + PartialEq + Clone,
    M: Debug + Clone + PartialEq,
    T: Debug + Clone + PartialEq,
{
    fn num_states(&self) -> usize {
        self.fst_add_on.num_states()
    }
}

impl<W, F, B, M, T> FstIntoIterator<W> for MatcherFst<W, F, B, M, T>
where
    W: Semiring,
    F: FstIntoIterator<W>,
    B: Borrow<F> + Debug + PartialEq + Clone,
    T: Debug,
{
    type TrsIter = F::TrsIter;
    type FstIter = F::FstIter;

    fn fst_into_iter(self) -> Self::FstIter {
        self.fst_add_on.fst_into_iter()
    }
}