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
pub mod dot;
mod iter;

use crate::fst::iter::*;

#[derive(Debug, PartialEq, Clone)]
pub struct TaskFst {
  states: Vec<TaskFstState>,
  start_states: Vec<usize>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct TaskFstState {
  label: String,
  id: usize,
  next: Vec<usize>,
  prev: Vec<usize>,
}

impl TaskFst {
  pub fn new() -> TaskFst {
    TaskFst {
      states: vec![],
      start_states: vec![],
    }
  }

  pub fn len(&self) -> usize {
    self.states.len()
  }

  pub fn add_state<S: Into<String>>(&mut self, label: S) -> usize {
    self.states.push(TaskFstState {
      label: label.into(),
      id: self.states.len(),
      next: vec![],
      prev: vec![],
    });
    self.states.len() - 1
  }

  pub fn add_start_state(&mut self, id: usize) {
    self.start_states.push(id)
  }

  pub fn add_arc(&mut self, from: usize, to: usize) {
    self.states[from].next.push(to);
    self.states[to].prev.push(from);
  }

  pub fn get_state_from_id(&self, id: usize) -> &TaskFstState {
    &self.states[id]
  }

  pub fn is_cyclic(&self) -> bool {
    let visited: &mut Vec<usize> = &mut Vec::with_capacity(self.states.len());
    for s in &self.start_states {
      visited.push(*s);
      if self.is_cyclic_dfs(visited) {
        return true;
      }
      visited.pop();
    }
    false
  }

  fn is_cyclic_dfs(&self, visited: &mut Vec<usize>) -> bool {
    let cur = visited[visited.len() - 1];
    for s in &self.states[cur].next {
      if visited.contains(s) {
        return true;
      }
      visited.push(*s);
      if self.is_cyclic_dfs(visited) {
        return true;
      }
      visited.pop();
    }
    false
  }

  pub fn reachable_states(&self) -> Vec<bool> {
    let visited: &mut Vec<usize> = &mut Vec::with_capacity(self.states.len());
    let reachable: &mut Vec<bool> = &mut vec![false; self.states.len()];
    for s in &self.start_states {
      reachable[*s] = true;
      visited.push(*s);
      self.reachable_states_dfs(visited, reachable);
      visited.pop();
    }
    reachable.clone()
  }

  fn reachable_states_dfs(&self, visited: &mut Vec<usize>, reachable: &mut Vec<bool>) {
    let cur = visited[visited.len() - 1];
    for s in &self.states[cur].next {
      reachable[*s] = true;
      if visited.contains(s) {
        return;
      }
      visited.push(*s);
      self.reachable_states_dfs(visited, reachable);
      visited.pop();
    }
  }

  pub fn iter(&self) -> TaskIter {
    TaskIter::new(&self)
  }
}

impl TaskFstState {
  pub fn label(&self) -> &String {
    &self.label
  }

  pub fn id(&self) -> usize {
    self.id
  }
}

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

  #[test]
  pub fn create_task_fst() {
    assert_eq!(
      TaskFst::new(),
      TaskFst {
        states: vec![],
        start_states: vec![]
      }
    );
  }

  #[test]
  pub fn add_arc() {
    let mut fst = TaskFst::new();
    fst.add_state("a");
    fst.add_state("b");
    fst.add_arc(0, 1);

    assert_eq!(
      fst,
      TaskFst {
        states: vec![
          TaskFstState {
            label: "a".to_string(),
            id: 0,
            next: vec![1],
            prev: vec![]
          },
          TaskFstState {
            label: "b".to_string(),
            id: 1,
            next: vec![],
            prev: vec![0]
          }
        ],
        start_states: vec![]
      }
    )
  }

  #[test]
  pub fn is_cyclic_and_reachable() {
    let mut fst = TaskFst::new();
    fst.add_state("a");
    fst.add_state("b");

    fst.add_arc(0, 1);
    assert_eq!(fst.reachable_states(), vec![false, false]);
    fst.add_start_state(0);
    assert_eq!(fst.reachable_states(), vec![true, true]);

    assert!(!fst.is_cyclic());

    fst.add_state("c");

    assert_eq!(fst.reachable_states(), vec![true, true, false]);
    fst.add_arc(0, 2);
    assert_eq!(fst.reachable_states(), vec![true, true, true]);
    assert!(!fst.is_cyclic());
    fst.add_arc(1, 2);
    assert!(!fst.is_cyclic());

    fst.add_state("d");
    assert_eq!(fst.reachable_states(), vec![true, true, true, false]);
    fst.add_arc(2, 3);
    assert_eq!(fst.reachable_states(), vec![true, true, true, true]);
    assert!(!fst.is_cyclic());

    fst.add_state("e");
    assert_eq!(fst.reachable_states(), vec![true, true, true, true, false]);
    fst.add_start_state(4);
    assert_eq!(fst.reachable_states(), vec![true, true, true, true, true]);
    assert!(!fst.is_cyclic());

    fst.add_arc(4, 3);
    assert!(!fst.is_cyclic());

    fst.add_arc(3, 1);
    assert!(fst.is_cyclic());
  }
}