-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskew.go
240 lines (185 loc) · 4.16 KB
/
skew.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Straightforward implementation of the skew/DC3 algorithm
- https://www.cs.helsinki.fi/u/tpkarkka/publications/jacm05-revised.pdf
*/
package gostr
func safeIdx(x []int32, i int32) int32 {
if i >= int32(len(x)) {
return 0
}
return x[i]
}
func sa3len(n int32) int32 {
if n == 0 {
return 0
}
return (n-1)/3 + 1
}
func sa12len(n int32) int32 {
return n - sa3len(n)
}
func symbcount(x, idx []int32, offset, asize int32) []int32 {
counts := make([]int32, asize)
for _, i := range idx {
counts[safeIdx(x, i+offset)]++
}
return counts
}
func cumsum(counts []int32) []int32 {
var acc int32
res := make([]int32, len(counts))
for i, k := range counts {
res[i] = acc
acc += k
}
return res
}
func bucketSort(x []int32, asize int32, idx []int32, offset int32) []int32 {
counts := symbcount(x, idx, offset, asize)
buckets := cumsum(counts)
out := make([]int32, len(idx))
for _, i := range idx {
bucket := safeIdx(x, i+offset)
out[buckets[bucket]] = i
buckets[bucket]++
}
return out
}
func radix3(x []int32, asize int32, idx []int32) []int32 {
idx = bucketSort(x, asize, idx, 2) //nolint:gomnd // 2 is an offset
idx = bucketSort(x, asize, idx, 1)
idx = bucketSort(x, asize, idx, 0)
return idx
}
func getSA12(x []int32) []int32 {
sa12 := make([]int32, sa12len(int32(len(x))))
for i, j := 0, 0; i < len(x); i++ {
if i%3 != 0 {
sa12[j] = int32(i)
j++
}
}
return sa12
}
func getSA3(x, sa12 []int32) []int32 {
k, sa3 := 0, make([]int32, sa3len(int32(len(x))))
if len(x)%3 == 1 {
sa3[k] = int32(len(x) - 1)
k++
}
for _, i := range sa12 {
if i%3 == 1 {
sa3[k] = i - 1
k++
}
}
return sa3
}
type triplet = [3]int32
type tripletMap = map[triplet]int32
func trip(x []int32, i int32) triplet {
return triplet{safeIdx(x, i), safeIdx(x, i+1), safeIdx(x, i+2)} //nolint:gomnd // 2 is an offset
}
func collectAlphabet(x, idx []int32) tripletMap {
alpha := tripletMap{}
for _, i := range idx {
t := trip(x, i)
if _, ok := alpha[t]; !ok {
alpha[t] = int32(len(alpha) + 1) // + 1 for sentinel
}
}
return alpha
}
func less(x []int32, i, j int32, isa map[int32]int32) bool {
a, b := safeIdx(x, i), safeIdx(x, j)
if a < b {
return true
}
if a > b {
return false
}
if i%3 != 0 && j%3 != 0 {
return isa[i] < isa[j]
}
return less(x, i+1, j+1, isa)
}
func merge(x, sa12, sa3 []int32) []int32 {
isa := map[int32]int32{}
for i := 0; i < len(sa12); i++ {
isa[sa12[i]] = int32(i)
}
sa := make([]int32, len(sa12)+len(sa3))
i, j, k := 0, 0, 0
for i < len(sa12) && j < len(sa3) {
if less(x, sa12[i], sa3[j], isa) {
sa[k] = sa12[i]
i++
k++
} else {
sa[k] = sa3[j]
j++
k++
}
}
for ; i < len(sa12); i++ {
sa[k] = sa12[i]
k++
}
for ; j < len(sa3); j++ {
sa[k] = sa3[j]
k++
}
return sa
}
func buildU(x []int32, alpha tripletMap) []int32 {
var (
u = make([]int32, sa12len(int32(len(x))))
k int32
i int32
)
for i = 1; i < int32(len(x)); i += 3 {
u[k] = alpha[trip(x, i)]
k++
}
for i = 2; i < int32(len(x)); i += 3 {
u[k] = alpha[trip(x, i)]
k++
}
return u
}
func uidx(i, m int32) int32 {
if i < m {
return 1 + 3*i
}
return 2 + 3*(i-m) //nolint:gomnd // 2 and 3 makes sense in the context of the algo.
}
func skew(x []int32, asize int32) []int32 {
sa12 := radix3(x, asize, getSA12(x))
alpha := collectAlphabet(x, sa12)
if len(alpha) < len(sa12) {
// Build u and its SA.
u := buildU(x, alpha)
usa := skew(u, int32(len(alpha)+1)) // +1 for sentinel
// Then map back to SA12 indices
m := int32((len(u) + 1) / 2) //nolint:gomnd // 2 is just half, not magic
for k, i := range usa {
sa12[k] = uidx(i, m)
}
}
SA3 := bucketSort(x, asize, getSA3(x, sa12), 0)
return merge(x, sa12, SA3)
}
// SkewWithAlphabet builds the suffix array of a string, first mapping
// it to a byte slice using the alphabet alpha.
func SkewWithAlphabet(x string, alpha *Alphabet) ([]int32, error) {
xb, err := alpha.MapToIntsWithSentinel(x)
if err != nil {
return []int32{}, err
}
return skew(xb, int32(alpha.Size())), nil
}
// Skew builds the suffix array of a string using the skew algorithm.
func Skew(x string) []int32 {
sa, _ := SkewWithAlphabet(x, NewAlphabet(x))
return sa
}