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
use std::{fmt::Display, ops::BitOr};
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(u8)]
pub(crate) enum StateAttribute {
Accepting = 0x01,
Rejecting = 0x02,
Unitary = 0x04,
TransitionsToAccepting = 0x08,
}
pub(crate) struct StateAttributesBuilder {
attrs: StateAttributes,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub struct StateAttributes(u8);
impl StateAttributesBuilder {
pub(crate) fn new() -> Self {
Self {
attrs: StateAttributes(0),
}
}
pub(crate) fn accepting(self) -> Self {
self.set(StateAttribute::Accepting)
}
pub(crate) fn rejecting(self) -> Self {
self.set(StateAttribute::Rejecting)
}
pub(crate) fn unitary(self) -> Self {
self.set(StateAttribute::Unitary)
}
pub(crate) fn transitions_to_accepting(self) -> Self {
self.set(StateAttribute::TransitionsToAccepting)
}
pub(crate) fn build(self) -> StateAttributes {
self.attrs
}
fn set(self, attr: StateAttribute) -> Self {
Self {
attrs: StateAttributes(self.attrs.0 | attr as u8),
}
}
}
impl From<StateAttributesBuilder> for StateAttributes {
#[inline(always)]
fn from(value: StateAttributesBuilder) -> Self {
value.build()
}
}
impl BitOr for StateAttributes {
type Output = Self;
#[inline(always)]
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl StateAttributes {
pub const ACCEPTING: Self = Self(StateAttribute::Accepting as u8);
pub const EMPTY: Self = Self(0);
pub const REJECTING: Self = Self(StateAttribute::Rejecting as u8);
pub const TRANSITIONS_TO_ACCEPTING: Self = Self(StateAttribute::TransitionsToAccepting as u8);
pub const UNITARY: Self = Self(StateAttribute::Unitary as u8);
#[inline(always)]
#[must_use]
pub fn is_accepting(&self) -> bool {
self.is_set(StateAttribute::Accepting)
}
#[inline(always)]
#[must_use]
pub fn is_rejecting(&self) -> bool {
self.is_set(StateAttribute::Rejecting)
}
#[inline(always)]
#[must_use]
pub fn has_transition_to_accepting(&self) -> bool {
self.is_set(StateAttribute::TransitionsToAccepting)
}
#[inline(always)]
#[must_use]
pub fn is_unitary(&self) -> bool {
self.is_set(StateAttribute::Unitary)
}
#[inline(always)]
#[must_use]
fn is_set(&self, attr: StateAttribute) -> bool {
(self.0 & attr as u8) != 0
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct State(
pub(super) u8,
);
impl Display for State {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "DFA({})", self.0)
}
}