-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
48 lines (42 loc) · 806 Bytes
/
state.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
package triegun
type state struct {
Id int
Nexts map[byte]*state
IsGoal bool
}
var state_id_seq = 0
func newState() *state {
state_id_seq++
return &state{Id: state_id_seq - 1, Nexts: map[byte]*state{}}
}
func newDFAFromStrings(inputs []string) *state {
start_s := newState()
for _, str := range inputs {
start_s.addString(str)
}
return start_s
}
func (st *state) addBytes(bytes []byte) {
for len(bytes) == 0 {
st.IsGoal = true
return
}
var next = st.Nexts[bytes[0]]
if next == nil {
next := newState()
st.Nexts[bytes[0]] = next
}
next.addBytes(bytes[1:])
}
func (st *state) addString(str string) {
for len(str) == 0 {
st.IsGoal = true
return
}
next := st.Nexts[str[0]]
if next == nil {
next = newState()
st.Nexts[str[0]] = next
}
next.addString(str[1:])
}