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
use std::cmp::Eq;

#[derive(Debug)]
pub struct Automaton<T>
    where T: Eq {
    states: Vec<StateData>,
    edges: Vec<EdgeData<T>>,
    start: StateIndex,
    current: StateIndex,
    end: Vec<StateIndex>,
}

/// An `StateIndex` represents a state in the automaton.
pub type StateIndex = usize;

#[derive(Debug)]
struct StateData {
    first_edge: Option<EdgeIndex>,
}

/// An `EdgeIndex` represents an edge in the automaton.
pub type EdgeIndex = usize;

#[derive(Debug)]
struct EdgeData<T>
    where T: Eq {
    value: T,
    target: StateIndex,
    next_brother_edge: Option<EdgeIndex>, 
}

#[derive(PartialEq, Eq, Copy, Clone, Debug)]
struct State {
    id: usize,
    end: bool
}

impl<T> Automaton<T>
    where T: Eq {

    /// Creates a new automaton with 1 state and no edges.
    pub fn new() -> Self {
        let first_state = StateData { first_edge: None };
        Automaton {
            states: vec![first_state],
            edges: Vec::new(),
            start: 0,
            current: 0,
            end: Vec::new(),
        }
    }

    /// Adds a state in the automaton and return it's StateIndex.
    pub fn add_state(&mut self) -> StateIndex {
        let index = self.states.len();
        self.states.push(StateData {
            first_edge: None,
        });
        index
    }

    /// Adds an edge to the automaton, requires the starting state, the ending state
    /// and a value of type T representing the transition value.
    pub fn add_edge(&mut self, source: StateIndex, target: StateIndex, value: T) {
        let edge_index  = self.edges.len();
        let state_data = &mut self.states[source];
        // TODO: Check duplicates
        self.edges.push(EdgeData {
            value,
            target,
            next_brother_edge: state_data.first_edge,
        });
        state_data.first_edge = Some(edge_index);
    }

    /// Sets the starting state of the automaton.
    pub fn set_start(&mut self, new_start: StateIndex) {
        self.start = new_start;
        self.current = new_start;
    }

    /// Adds an accepting state to the automaton.
    pub fn add_end(&mut self, new_end: StateIndex) {
        // TODO: Check duplicates
        self.end.push(new_end);
    }

    /// Consumes a value and advances, if the value is not present in some transition 
    /// then it stays in the same state.
    pub fn consume(&mut self, val: T) {
        let state = &self.states[self.current];
        if let Some(edge) = state.first_edge {
            if self.edges[edge].value == val {
                self.current = self.edges[edge].target;
                return;
            }
            while let Some(edge) = self.edges[edge].next_brother_edge {
                if self.edges[edge].value == val {
                    self.current = self.edges[edge].target;
                    return;
                }   
            }
        }
    }

    /// Restarts the automaton, setting the current state to start.
    pub fn restart(&mut self){
        self.current = self.start;
    }

    /// Returns a boolean value telling if the current state is an accepting state.
    pub fn accepted(&self) -> bool {
        self.end.contains(&self.current)
    }

    /// Returns StateIndex of the current value.
    pub fn current(&self) -> StateIndex {
        self.current
    }
}

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

        let mut dfa = Automaton::new();

        let s0 = dfa.add_state();
        let s1 = dfa.add_state();

        dfa.set_start(s0);
        dfa.add_end(s1);

        assert_eq!(s0, dfa.start);

        dfa.add_edge(s0, s1, 10);
        dfa.add_edge(s1, s0, 15);

        assert_eq!(s0, dfa.current());

        dfa.consume(1);
        assert_eq!(s0, dfa.current());

        dfa.consume(10);
        assert_eq!(s1, dfa.current());
        assert_eq!(true, dfa.accepted());

        dfa.restart();
        assert_eq!(s0, dfa.current());
    }
}