-
Notifications
You must be signed in to change notification settings - Fork 0
/
submatch.go
59 lines (52 loc) · 1.27 KB
/
submatch.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
59
package triegun
func allowSubmatch(start *state) *state {
for _, state := range allStates(start)[1:] { // Ignoring the start state
for k, n := range start.Nexts {
if next := state.Nexts[k]; next != nil {
bridge(next, n)
}
}
}
return start
}
/*
What `bridge` does is as follows:
For example, when we want to match "sushi" or "sukiyaski", the initial state
(DFA) is like this:
s u k i y a k i
START --> 1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> OK
\ s h i
\-> 8 --> 9 --> OK
`bridge` modifies it as follow:
s u k i y a k i
START --> 1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> OK
^\ s h i
| \-> 8 --> 9 --> OK
+---/
u
Connect state8 and state2, because 's' is already found during matcing "sushi".
*/
func bridge(src, dst *state) {
marked := map[int]bool{}
n := 0
var traverse func(*state, *state)
traverse = func(src, dst *state) {
n++
if n > 10 {
return
}
if marked[src.Id] {
return
}
marked[src.Id] = true
for k, n := range dst.Nexts {
next := src.Nexts[k]
if next == nil {
src.Nexts[k] = n
} else {
traverse(next, n)
}
}
}
traverse(src, dst)
}