-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuffixtree.go
216 lines (190 loc) · 4.42 KB
/
suffixtree.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package suffixtree
import (
"bytes"
"fmt"
"math"
"strings"
)
const infinity = math.MaxInt32
// Pos denotes position in data slice.
type Pos int32
type Token interface {
Val() int
}
// STree is a struct representing a suffix tree.
type STree struct {
data []Token
root *state
auxState *state // auxiliary state
// active point
s *state
start, end Pos
}
// New creates new suffix tree.
func New() *STree {
t := new(STree)
t.data = make([]Token, 0, 50)
t.root = newState(t)
t.auxState = newState(t)
t.root.linkState = t.auxState
t.s = t.root
return t
}
// Update refreshes the suffix tree to by new data.
func (t *STree) Update(data ...Token) {
t.data = append(t.data, data...)
for range data {
t.update()
t.s, t.start = t.canonize(t.s, t.start, t.end)
t.end++
}
}
// update transforms suffix tree T(n) to T(n+1).
func (t *STree) update() {
oldr := t.root
// (s, (start, end)) is the canonical reference pair for the active point
s := t.s
start, end := t.start, t.end
var r *state
for {
var endPoint bool
r, endPoint = t.testAndSplit(s, start, end-1)
if endPoint {
break
}
r.fork(end)
if oldr != t.root {
oldr.linkState = r
}
oldr = r
s, start = t.canonize(s.linkState, start, end-1)
}
if oldr != t.root {
oldr.linkState = r
}
// update active point
t.s = s
t.start = start
}
// testAndSplit tests whether a state with canonical ref. pair
// (s, (start, end)) is the end point, that is, a state that have
// a c-transition. If not, then state (exs, (start, end)) is made
// explicit (if not already so).
func (t *STree) testAndSplit(s *state, start, end Pos) (exs *state, endPoint bool) {
c := t.data[t.end]
if start <= end {
tr := s.findTran(t.data[start])
splitPoint := tr.start + end - start + 1
if t.data[splitPoint].Val() == c.Val() {
return s, true
}
// make the (s, (start, end)) state explicit
newSt := newState(s.tree)
newSt.addTran(splitPoint, tr.end, tr.state)
tr.end = splitPoint - 1
tr.state = newSt
return newSt, false
}
if s == t.auxState || s.findTran(c) != nil {
return s, true
}
return s, false
}
// canonize returns updated state and start position for ref. pair
// (s, (start, end)) of state r so the new ref. pair is canonical,
// that is, referenced from the closest explicit ancestor of r.
func (t *STree) canonize(s *state, start, end Pos) (*state, Pos) {
if s == t.auxState {
s, start = t.root, start+1
}
if start > end {
return s, start
}
var tr *tran
for {
if start <= end {
tr = s.findTran(t.data[start])
if tr == nil {
panic(fmt.Sprintf("there should be some transition for '%d' at %d",
t.data[start].Val(), start))
}
}
if tr.end-tr.start > end-start {
break
}
start += tr.end - tr.start + 1
s = tr.state
}
if s == nil {
panic("there should always be some suffix link resolution")
}
return s, start
}
func (t *STree) At(p Pos) Token {
if p < 0 || p >= Pos(len(t.data)) {
panic("position out of bounds")
}
return t.data[p]
}
func (t *STree) String() string {
buf := new(bytes.Buffer)
printState(buf, t.root, 0)
return buf.String()
}
func printState(buf *bytes.Buffer, s *state, ident int) {
for _, tr := range s.trans {
fmt.Fprint(buf, strings.Repeat(" ", ident))
fmt.Fprintf(buf, "* (%d, %d)\n", tr.start, tr.ActEnd())
printState(buf, tr.state, ident+1)
}
}
// state is an explicit state of the suffix tree.
type state struct {
tree *STree
trans []*tran
linkState *state
}
func newState(t *STree) *state {
return &state{
tree: t,
trans: make([]*tran, 0),
linkState: nil,
}
}
func (s *state) addTran(start, end Pos, r *state) {
s.trans = append(s.trans, newTran(start, end, r))
}
// fork creates a new branch from the state s.
func (s *state) fork(i Pos) *state {
r := newState(s.tree)
s.addTran(i, infinity, r)
return r
}
// findTran finds c-transition.
func (s *state) findTran(c Token) *tran {
for _, tran := range s.trans {
if s.tree.data[tran.start].Val() == c.Val() {
return tran
}
}
return nil
}
// tran represents a state's transition.
type tran struct {
start, end Pos
state *state
}
func newTran(start, end Pos, s *state) *tran {
return &tran{start, end, s}
}
func (t *tran) len() int {
return int(t.end - t.start + 1)
}
// ActEnd returns actual end position as consistent with
// the actual length of the data in the STree.
func (t *tran) ActEnd() Pos {
if t.end == infinity {
return Pos(len(t.state.tree.data)) - 1
}
return t.end
}