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
use std::rc::Rc;
use terms::Term;
use crate::{Symbol, State, Label, Labeled};
use crate::utils::combinations;
use super::{Automaton, Configuration, Configurations};

/// Common configurations searcher.
pub struct CommonConfigurations<'a, F: Symbol, Q: State, L: Label> {
    automata: &'a [&'a Automaton<F, Q, L>],
    positions: &'a [Q],
    iterators: Vec<Configurations<'a, F, Q, L>>,
    configurations: Vec<Labeled<Configuration<F, Q>, L>>,
    // pattern: P
}

impl<'a, F: Symbol, Q: State, L: Label> CommonConfigurations<'a, F, Q, L> {
    pub fn new(
        automata: &'a [&'a Automaton<F, Q, L>],
        positions: &'a [Q]
    ) -> CommonConfigurations<'a, F, Q, L>
    {
        // println!("looking for common confs: {:?} {:?}", positions, patterns);

        let mut iterators = Vec::with_capacity(positions.len());
        if !positions.is_empty() {
            iterators.push(automata[0].configurations_for_state(&positions[0]))
        };

        CommonConfigurations {
            automata: automata,
            positions: positions,
            iterators: iterators,
            configurations: Vec::with_capacity(positions.len())
        }
    }
}

impl<'a, F: Symbol, Q: State, L: Label> Iterator for CommonConfigurations<'a, F, Q, L> {
    type Item = Vec<Labeled<Configuration<F, Q>, L>>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.positions.is_empty() {
            None
        } else {
            while self.configurations.len() < self.positions.len() {
                let i = self.configurations.len();
                let j = i + 1;

                match self.iterators.last_mut().unwrap().next() {
                    Some((Configuration(f, sub_states), label)) => {
                        if self.configurations.is_empty() || f == self.configurations.first().unwrap().0.symbol() {
                            if j < self.positions.len() {
                                let it = self.automata[j].configurations_for_state(&self.positions[j]);
                                // println!("new it");
                                self.iterators.push(it);
                            }
                            self.configurations.push((Configuration(f.clone(), sub_states.clone()), label.clone()))
                        }
                    },
                    None => {
                        match self.configurations.pop() {
                            Some(_) => {
                                if i < self.positions.len() {
                                    // println!("drop it");
                                    self.iterators.pop();
                                }
                            },
                            None => {
                                // println!("no other common confs.");
                                return None
                            }
                        }
                    }
                }
            }

            let result = self.configurations.clone();
            self.configurations.pop();
            Some(result)
        }
    }
}

#[derive(Clone)]
struct VisitedTransitions<'a, F: Symbol, Q: State, L: Label> {
    previously_visited: Option<Rc<VisitedTransitions<'a, F, Q, L>>>,
    conf: &'a Configuration<F, Q>,
    label: &'a L,
    q: &'a Q
}

impl<'a, F: Symbol, Q: State, L: Label> VisitedTransitions<'a, F, Q, L> {
    fn new(previously_visited: &Option<Rc<VisitedTransitions<'a, F, Q, L>>>, conf: &'a Configuration<F, Q>, label: &'a L, q: &'a Q) -> VisitedTransitions<'a, F, Q, L> {
        VisitedTransitions {
            previously_visited: previously_visited.clone(),
            conf: conf,
            label: label,
            q: q
        }
    }

    fn contains(&self, conf: &'a Configuration<F, Q>, label: &'a L, q: &'a Q) -> bool {
        // println!("compare {:?} -{:?}-> {:?} and {:?} -{:?}-> {:?}", self.conf, self.label, self.q, conf, label, q);
        if self.q == q && self.label == label && self.conf == conf {
            // println!("visited");
            true
        } else {
            match &self.previously_visited {
                Some(previously_visited) => previously_visited.contains(conf, label, q),
                None => false
            }
        }
    }
}

pub struct Representatives<'a, F: Symbol, Q: State, L: Label> {
    automaton: &'a Automaton<F, Q, L>,
    visited_transitions: Option<Rc<VisitedTransitions<'a, F, Q, L>>>,
    pending_states: Vec<&'a Q>,
    current_state: Option<(&'a Q, Configurations<'a, F, Q, L>)>,
    current_configuration: Option<(&'a F, Box<dyn Iterator<Item=Vec<Term<F>>> + 'a>)>
}

impl<'a, F: Symbol, Q: State, L: Label> Representatives<'a, F, Q, L> {
    pub fn new(aut: &'a Automaton<F, Q, L>) -> Representatives<'a, F, Q, L> {
        let mut pending_states: Vec<&'a Q> = aut.final_states.iter().collect();
        let current_state = match pending_states.pop() {
            Some(q) => Some((q, aut.configurations_for_state(q))),
            None => None
        };
        Representatives {
            automaton: aut,
            visited_transitions: None,
            pending_states: pending_states,
            current_state: current_state,
            current_configuration: None
        }
    }

    pub fn visited(&self, conf: &'a Configuration<F, Q>, label: &'a L, q: &'a Q) -> bool {
        match &self.visited_transitions {
            Some(visited_transitions) => visited_transitions.contains(conf, label, q),
            None => false
        }
    }
}

impl<'a, F: Symbol, Q: State, L: Label> Iterator for Representatives<'a, F, Q, L> {
    type Item = Term<F>;

    fn next(&mut self) -> Option<Term<F>> {
        loop {
            match self.current_configuration {
                Some((f, ref mut iterator)) => {
                    match iterator.next() {
                        Some(sub_terms) => {
                            return Some(Term::new(f.clone(), sub_terms))
                        },
                        None => self.current_configuration = None
                    }
                },
                None => {
                    match self.current_state {
                        Some((q, ref mut configurations)) => {
                            match configurations.next() {
                                Some((conf, label)) => {
                                    if !self.visited(conf, label, q) {
                                        let aut = self.automaton;
                                        let visited_transitions = Some(Rc::new(VisitedTransitions::new(&self.visited_transitions, conf, label, q)));
                                        let sub_terms_it = combinations(conf.states(), move |q| Representatives {
                                            automaton: aut,
                                            visited_transitions: visited_transitions.clone(),
                                            pending_states: Vec::new(),
                                            current_state: Some((q, aut.configurations_for_state(q))),
                                            current_configuration: None
                                        });
                                        // println!("conf: {:?}", conf);
                                        self.current_configuration = Some((conf.symbol(), Box::new(sub_terms_it)));
                                    }
                                },
                                None => self.current_state = None
                            }
                        },
                        None => {
                            match self.pending_states.pop() {
                                Some(q) => {
                                    self.current_state = Some((q, self.automaton.configurations_for_state(q)))
                                },
                                None => return None
                            }
                        }
                    }
                }
            }
        }
    }
}