pub struct Dfa<T, V> {
pub states: Vec<State<T, V>>,
}
Expand description
A deterministic finite automaton (DFA), over the alphabet T
.
Each state is annotated with a value of type V
.
State 0 is the starting state.
Fields§
§states: Vec<State<T, V>>
The list of states.
Implementations§
source§impl<T, V> Dfa<T, V>
impl<T, V> Dfa<T, V>
sourcepub fn from_derivatives(initial: Vec<V>) -> (Dfa<T, V>, BTreeMap<V, u32>)
pub fn from_derivatives(initial: Vec<V>) -> (Dfa<T, V>, BTreeMap<V, u32>)
Construct a DFA from a list of differentiable objects.
The elements of initial
form the first states of the DFA.
Returns the DFA, together with a mapping from derivatives to state numbers.
Examples found in repository?
examples/test.rs (line 16)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let stdin = std::io::stdin();
for line in stdin.lock().lines() {
let line = line.unwrap();
match line.trim().parse::<Regex<char>>() {
Err(e) => println!("error: {}", e),
Ok(x) => {
println!("ok: {:?}", x);
let x = x.normalize();
println!("{:?}", x);
println!("{:?}", x.derivative().map(Normalize::normalize));
let (dfa, _mapping) = Dfa::from_derivatives(vec![x, Regex::Null]);
let dfa = dfa.map(|reg| reg.nullable());
println!("DFA: {:?}\n", dfa);
let mdfa = dfa.minimize().map(|x| *x);
println!("Minimized DFA: {:?}\n", mdfa);
println!("dfa == mdfa: {:?}\n", dfa == mdfa);
}
}
}
}
sourcepub fn map<U, F>(self, f: F) -> Dfa<T, U>where
F: FnMut(V) -> U,
pub fn map<U, F>(self, f: F) -> Dfa<T, U>where
F: FnMut(V) -> U,
Apply a function to each state’s value.
Examples found in repository?
examples/test.rs (line 17)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let stdin = std::io::stdin();
for line in stdin.lock().lines() {
let line = line.unwrap();
match line.trim().parse::<Regex<char>>() {
Err(e) => println!("error: {}", e),
Ok(x) => {
println!("ok: {:?}", x);
let x = x.normalize();
println!("{:?}", x);
println!("{:?}", x.derivative().map(Normalize::normalize));
let (dfa, _mapping) = Dfa::from_derivatives(vec![x, Regex::Null]);
let dfa = dfa.map(|reg| reg.nullable());
println!("DFA: {:?}\n", dfa);
let mdfa = dfa.minimize().map(|x| *x);
println!("Minimized DFA: {:?}\n", mdfa);
println!("dfa == mdfa: {:?}\n", dfa == mdfa);
}
}
}
}
sourcepub fn reverse(&self) -> Vec<(BTreeMap<&T, BTreeSet<u32>>, BTreeSet<u32>)>where
T: Ord,
pub fn reverse(&self) -> Vec<(BTreeMap<&T, BTreeSet<u32>>, BTreeSet<u32>)>where
T: Ord,
Find the reverse transitions from each state in the DFA.
sourcepub fn minimize(&self) -> Dfa<T, &V>
pub fn minimize(&self) -> Dfa<T, &V>
Minimize a DFA; i.e. find a DFA with the fewest states that is equivalent to the given DFA. Two DFAs are equivalent if, given the same string, they always lead to a state with the same associated value.
Examples found in repository?
examples/test.rs (line 19)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let stdin = std::io::stdin();
for line in stdin.lock().lines() {
let line = line.unwrap();
match line.trim().parse::<Regex<char>>() {
Err(e) => println!("error: {}", e),
Ok(x) => {
println!("ok: {:?}", x);
let x = x.normalize();
println!("{:?}", x);
println!("{:?}", x.derivative().map(Normalize::normalize));
let (dfa, _mapping) = Dfa::from_derivatives(vec![x, Regex::Null]);
let dfa = dfa.map(|reg| reg.nullable());
println!("DFA: {:?}\n", dfa);
let mdfa = dfa.minimize().map(|x| *x);
println!("Minimized DFA: {:?}\n", mdfa);
println!("dfa == mdfa: {:?}\n", dfa == mdfa);
}
}
}
}
Trait Implementations§
source§impl<T: Ord, U, V: PartialEq<U>> PartialEq<Dfa<T, U>> for Dfa<T, V>
impl<T: Ord, U, V: PartialEq<U>> PartialEq<Dfa<T, U>> for Dfa<T, V>
Compare DFAs by graph isomorphism.
impl<T: Ord, V: Eq> Eq for Dfa<T, V>
Auto Trait Implementations§
impl<T, V> RefUnwindSafe for Dfa<T, V>where
T: RefUnwindSafe,
V: RefUnwindSafe,
impl<T, V> Send for Dfa<T, V>
impl<T, V> Sync for Dfa<T, V>
impl<T, V> Unpin for Dfa<T, V>where
V: Unpin,
impl<T, V> UnwindSafe for Dfa<T, V>where
T: RefUnwindSafe,
V: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more