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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Debug};
use std::io::{self, Write};

use super::{Alphabet, Ensure};
use super::dot::{Family, Edge as DotEdge, GraphWriter, Node as DotNode};
use super::regex::Regex;

/// A node handle.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub struct Node(pub usize);

pub struct Dfa<A: Alphabet> {
    /// Edges of the graph, each list sorted against the alphabet.
    edges: Vec<Vec<(A, Node)>>,

    /// Final or accepting states.
    finals: HashSet<Node>,

    /// A sorted list of the alphabet, to allow quick comparison.
    alphabet: Vec<A>,
}

impl<A: Alphabet> Dfa<A> {
    /// Build a dfa from the connecting edges and final states.
    ///
    /// States are numbered in an arbitrary order, except the start label 0. The automaton will
    /// deduce the used alphabet subset automatically and test whether it has been used
    /// consistently.
    pub fn from_edges<I, V>(edge_iter: I, finals: V) -> Dfa<A>
    where 
        I: IntoIterator<Item=(usize, A, usize)>,
        V: IntoIterator<Item=usize>, 
        A: Clone + Debug,
    {
        let mut edges = vec![Vec::new()];
        let mut check = vec![HashSet::new()];
        let mut states = HashSet::new();
        states.insert(0);

        for (from, a, to) in edge_iter.into_iter() {
            edges.ensure_default(from + 1);
            edges.ensure_default(to + 1);
            check.ensure_default(from + 1);
            check.ensure_default(to + 1);
            
            edges[from].push((a.clone(), Node(to)));
            check[from].insert(a);
            states.insert(from);
            states.insert(to);
        }

        let finals = finals.into_iter()
            .inspect(|c| check.resize(c + 1, HashSet::new()))
            .map(Node)
            .collect();

        let alphabet = check.pop();
        if let Some(sample) = alphabet.as_ref() {
            if let Some(err) = check.iter().find(|&s| s != sample) {
                panic!("Different outgoing edges alphabet: {:?} vs {:?}", &sample, &err);
            }
        }

        let mut alphabet: Vec<_> = alphabet.unwrap().into_iter().collect();
        alphabet.sort_unstable();

        for edge_list in edges.iter_mut() {
            // There are never any duplicates and now the indices correspond to
            // the indices in the alphabet list.
            edge_list.sort_unstable();
        }

        Dfa {
            edges,
            finals,
            alphabet,
        }
    }

    /// Checks if the input word is contained in the language.
    pub fn contains<I: IntoIterator<Item=A>>(&self, sequence: I) -> bool {
        let mut sequence = sequence.into_iter();
        let mut state = 0;

        while let Some(ch) = sequence.next() {
            let edges = &self.edges[state];
            let Node(next) = edges.iter()
                .find(|e| e.0 == ch)
                .map(|e| e.1)
                .expect("Mismatch between DFA alphabet and word alphabet");
            state = next;
        }

        self.finals.contains(&Node(state))
    }

    pub fn to_regex(self) -> Regex<A> {
        unimplemented!()
    }

    pub fn write_to(&self, output: &mut Write) -> io::Result<()> 
        where for<'a> &'a A: Display
    {
        let mut writer = GraphWriter::new(output, Family::Directed, None)?;

        for (from, edges) in self.edges.iter().enumerate() {
            for (label, to) in edges.iter() {
                let edge = DotEdge { 
                    label: Some(format!("{}", label).into()),
                    .. DotEdge::none()
                };

                writer.segment([from, to.0].iter().cloned(), Some(edge))?;
            }
        }

        for Node(fin) in self.finals.iter().cloned() {
            let node = DotNode {
                peripheries: Some(2),
                .. DotNode::none()
            };
            writer.node(fin.into(), Some(node))?;
        }

        writer.end_into_inner().1
    }

    /// The alphabet is the set of symbols in words of that language.
    pub fn alphabet(&self) -> &[A] {
        self.alphabet.as_slice()
    }

    /// Minimize the automata into its language partition.
    ///
    /// Contrary to NFAs, the resulting automaton is guaranteed to be a minimal
    /// automaton exactly equivalent to the languages minimal DFA.
    pub fn minimized(&self) -> Self {
        unimplemented!()
    }

    /// Pairs two automata with a given binary boolean operation
    ///
    /// If there are no final states, returns `None`.
    pub fn pair(&self, rhs: &Self, decider: &Fn(bool, bool) -> bool) -> Option<Self> {
        assert!(self.alphabet() == rhs.alphabet(), "Automata alphabets differ");

        let mut assigned = HashMap::new();
        let mut working = vec![(0, 0, 0)];
        let mut edges: Vec<Vec<(A, Node)>> = Vec::new();
        let mut finals = HashSet::new();
        assigned.insert((0, 0), 0);

        while let Some((left, right, self_id)) = working.pop() {
            let decide = decider(
                self.finals.contains(&Node(left)),
                rhs.finals.contains(&Node(right)));

            if decide {
                finals.insert(Node(self_id));
            }

            edges.ensure_default(self_id + 1);
            let edges = &mut edges[self_id];

            for (pos, symbol) in self.alphabet().iter().enumerate() {
                // Gets updated when we encounter an existing node.
                let mut node_id = assigned.len();

                let new_left = self.edges[left][pos].1;
                let new_right = rhs.edges[right][pos].1;

                assigned.entry((new_left.0, new_right.0))
                    .and_modify(|&mut id| node_id = id)
                    .or_insert_with(|| {
                        working.push((new_left.0, new_right.0, node_id));
                        node_id
                    });

                edges.push((symbol.clone(), Node(node_id)));
            }
        }

        if finals.is_empty() {
            None
        } else {
            Some(Dfa {
                edges,
                finals,
                alphabet: self.alphabet.clone(),
            })
        }
    }

    /// Like `pair` but only determines if the result would be an empty automaton.
    ///
    /// This speeds up operations such as equivalence checks.
    pub fn pair_empty(&self, _rhs: &Self, _decider: &Fn(bool, bool) -> bool) -> bool {
        unimplemented!()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn build_and_format() {
        let automaton = Dfa::from_edges(vec![
            (0, '0', 0),
            (0, '1', 1),
            (1, '0', 2),
            (1, '1', 0),
            (2, '0', 1),
            (2, '1', 2),
        ], vec![1]);

        let mut output = Vec::new();
        automaton.write_to(&mut output)
            .expect("failed to format to dot file");
        let output = String::from_utf8(output)
            .expect("output should be utf8 encoded");
        assert_eq!(output, r#"digraph {
	0 -> 0 [label=0,];
	0 -> 1 [label=1,];
	1 -> 2 [label=0,];
	1 -> 0 [label=1,];
	2 -> 1 [label=0,];
	2 -> 2 [label=1,];
	1 [peripheries=2,];
}
"#);
    }

    #[test]
    fn contains() {
        let automaton = Dfa::from_edges(vec![
            (0, '0', 0),
            (0, '1', 1),
            (1, '0', 2),
            (1, '1', 0),
            (2, '0', 1),
            (2, '1', 2),
        ], vec![1]);
        
        assert!( automaton.contains("1".chars()));
        assert!( automaton.contains("100".chars()));
        assert!(!automaton.contains("0".chars()));
        assert!(!automaton.contains("10".chars()));
        assert!(!automaton.contains("".chars()));
    }

    #[test]
    fn pairing() {
        // Accepts even length words
        let automaton_2 = Dfa::from_edges(vec![
            (0, '.', 1),
            (1, '.', 0),
        ], vec![0]);

        // Accepts words with `len(w) % 3 == 0`
        let automaton_3 = Dfa::from_edges(vec![
            (0, '.', 1),
            (1, '.', 2),
            (2, '.', 0),
        ], vec![0]);

        let accept_6_0 = automaton_2.pair(&automaton_3, &|lhs, rhs| lhs & rhs).unwrap();
        assert!( accept_6_0.contains("".chars()));
        assert!(!accept_6_0.contains(".".chars()));
        assert!(!accept_6_0.contains("..".chars()));
        assert!(!accept_6_0.contains("...".chars()));
        assert!(!accept_6_0.contains("....".chars()));
        assert!(!accept_6_0.contains(".....".chars()));
        assert!( accept_6_0.contains("......".chars()));

        let accept_6_1 = automaton_2.pair(&automaton_3, &|lhs, rhs| lhs | rhs).unwrap();
        assert!( accept_6_1.contains("".chars()));
        assert!(!accept_6_1.contains(".".chars()));
        assert!( accept_6_1.contains("..".chars()));
        assert!( accept_6_1.contains("...".chars()));
        assert!( accept_6_1.contains("....".chars()));
        assert!(!accept_6_1.contains(".....".chars()));
        assert!( accept_6_1.contains("......".chars()));
    }
}