-
Notifications
You must be signed in to change notification settings - Fork 0
/
automata.go
58 lines (46 loc) · 816 Bytes
/
automata.go
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
package watermill
import "fmt"
type Automata interface {
Name() string
Type() AutomataType
TransitionGraph() TransitionGraph
Alphabet() Alphabet
}
type AutomataType uint
const (
DFA AutomataType = iota
NFA
EpsilonNFA
)
type State struct {
id int64
accept bool
}
func (s State) ID() int64 {
return s.id
}
func (s State) IsAccept() bool {
return s.accept
}
func (s State) String() string {
return fmt.Sprintf("State {id: %v, accept: %v}", s.id, s.accept)
}
type Alphabet map[rune]bool
func (ab Alphabet) Has(c rune) bool {
return ab[c]
}
func (ab Alphabet) HasAll(s string) bool {
for _, c := range s {
if !ab.Has(c) {
return false
}
}
return true
}
func (ab Alphabet) String() string {
s := ""
for c := range ab {
s += string(c)
}
return fmt.Sprintf("Alphabet {%v}", s)
}