Struct redfa::dfa::Dfa

source ·
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>

source

pub fn from_derivatives(initial: Vec<V>) -> (Dfa<T, V>, BTreeMap<V, u32>)
where T: Ord, V: Differentiable<T> + Normalize + Ord + Clone,

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);
            }
        }
    }
}
source

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);
            }
        }
    }
}
source

pub fn reverse(&self) -> Vec<(BTreeMap<&T, BTreeSet<u32>>, BTreeSet<u32>)>
where T: Ord,

Find the reverse transitions from each state in the DFA.

source

pub fn minimize(&self) -> Dfa<T, &V>
where T: Ord + Clone, V: Ord,

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);
            }
        }
    }
}
source§

impl<T, V> Dfa<T, V>

source

pub fn equiv<U>(&self, other: &Dfa<T, U>) -> bool
where T: Ord + Clone, U: Ord, V: Ord + PartialEq<U>,

Compare DFAs by language equality.

Trait Implementations§

source§

impl<T: Clone, V: Clone> Clone for Dfa<T, V>

source§

fn clone(&self) -> Dfa<T, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug, V: Debug> Debug for Dfa<T, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Ord, U, V: PartialEq<U>> PartialEq<Dfa<T, U>> for Dfa<T, V>

Compare DFAs by graph isomorphism.

source§

fn eq(&self, other: &Dfa<T, U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Ord, V: Eq> Eq for Dfa<T, V>

Auto Trait Implementations§

§

impl<T, V> RefUnwindSafe for Dfa<T, V>

§

impl<T, V> Send for Dfa<T, V>
where T: Send, V: Send,

§

impl<T, V> Sync for Dfa<T, V>
where T: Sync, V: Sync,

§

impl<T, V> Unpin for Dfa<T, V>
where V: Unpin,

§

impl<T, V> UnwindSafe for Dfa<T, V>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.