-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmp.go
82 lines (77 loc) · 1.4 KB
/
kmp.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package bpm
import (
"fmt"
)
type _KMPSearchEngine struct {
*_BaseEngine
pattern []byte
prefix []int64
}
func (kmp *_KMPSearchEngine) PreprocessPattern(pattern []byte) {
kmp.pattern = pattern
kmp.computePrefix()
}
func (kmp *_KMPSearchEngine) computePrefix() {
pl := len(kmp.pattern)
if pl == 1 {
kmp.prefix = []int64{-1}
return
}
var (
count int64 = 0
pos int = 2
)
kmp.prefix = make([]int64, pl)
kmp.prefix[0], kmp.prefix[1] = -1, 0
for pos < pl {
if kmp.pattern[pos-1] == kmp.pattern[count] {
count++
kmp.prefix[pos] = count
pos++
} else {
if count > 0 {
count = kmp.prefix[count]
} else {
kmp.prefix[pos] = 0
pos++
}
}
}
return
}
func (kmp *_KMPSearchEngine) FindAllOccurrences() (srs SearchResults, err error) {
dl, pl := kmp.bfr.FileSize(), int64(len(kmp.pattern))
if pl > dl {
err = fmt.Errorf("pattern must not be longer than data")
return
}
var (
i, m int64
indices []int64
plm1 int64 = pl - 1
)
for m+i < dl {
if kmp.pattern[i] == kmp.bfr.ReadByteAt(m+i) {
if i == plm1 {
indices = append(indices, m)
m += i - kmp.prefix[i]
if kmp.prefix[i] > -1 {
i = kmp.prefix[i]
} else {
i = 0
}
} else {
i++
}
} else {
m += i - kmp.prefix[i]
if kmp.prefix[i] > -1 {
i = kmp.prefix[i]
} else {
i = 0
}
}
}
srs = newSearchResults().putMany(kmp.pattern, indices)
return
}