autour_core/dfa/implem/
run.rs

1/*
2Copyright 2023 Erwan Mahe (github.com/erwanM974)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use std::collections::HashSet;
18use maplit::hashset;
19
20use crate::traits::letter::AutLetter;
21use crate::traits::run::AutRunnable;
22use crate::dfa::dfa::AutDFA;
23use crate::traits::error::AutError;
24
25
26impl<Letter: AutLetter> AutRunnable<Letter> for AutDFA<Letter> {
27    fn runs_trace(&self, trace : &[Letter]) -> Result<bool,AutError<Letter>> {
28        let mut current_state = self.initial;
29        for letter in trace {
30            match self.transitions[current_state].get(letter) {
31                None => {
32                    return Ok(false);
33                },
34                Some(target_state) => {
35                    current_state = *target_state;
36                }
37            }
38        }
39        // ***
40        Ok(self.finals.contains(&current_state))
41    }
42
43    fn run_transition(&self,
44                      active_states: &HashSet<usize>,
45                      letter: &Letter) ->  Result<HashSet<usize>,AutError<Letter>> {
46        if active_states.len() != 1 {
47            return Err(AutError::Other(format!("dfa has at most one active state and not {:?}", active_states)));
48        }
49        let init_state = *active_states.iter().next().unwrap();
50        match self.transitions[init_state].get(letter) {
51            None => {
52                Ok(hashset!{})
53            },
54            Some(target_state) => {
55                Ok(hashset!{*target_state})
56            }
57        }
58    }
59}