-
Notifications
You must be signed in to change notification settings - Fork 0
/
sais.go
319 lines (256 loc) · 7.07 KB
/
sais.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package gostr
const bytebits = 8
type bitArray struct {
length int
bytes []byte
}
func newBitArray(size int, bits ...bool) *bitArray {
ba := bitArray{length: size, bytes: make([]byte, (size+bytebits-1)/bytebits)}
for i, b := range bits {
ba.set(int32(i), b)
}
return &ba
}
func (a *bitArray) get(i int32) bool {
return (a.bytes[i/bytebits] & (1 << (i % bytebits))) != 0
}
func (a *bitArray) set(i int32, b bool) {
if b {
a.bytes[i/bytebits] |= 1 << (i % bytebits)
} else {
a.bytes[i/bytebits] = a.bytes[(i)/bytebits] & ^(1 << (i % bytebits))
}
}
func classifyS(isS *bitArray, x []int32) {
// Last element always exists, it is the sentinel and is S
isS.set(int32(len(x)-1), true)
// Otherwise, an index is S if the first letter is smaller
// or the first letters are the same and the next is S.
var secondToLastIndex = int32(len(x) - 2) //nolint:gomnd // -2 because we want second last
for i := secondToLastIndex; i >= 0; i-- {
isS.set(i, x[i] < x[i+1] || (x[i] == x[i+1] && isS.get(i+1)))
}
}
func isLMS(isS *bitArray, i int32) bool {
return (i != 0) && isS.get(i) && !isS.get(i-1)
}
func equalLMS(x []int32, isS *bitArray, i, j int32) bool {
if i == j {
// The same index is obviously the same...
return true
}
// they can't be equal now, so only one is the
// sentinel LMS, thus they cannot be equal
if i == int32(len(x)) || j == int32(len(x)) {
return false
}
// From here on, we assume that neither index points past the end.
for k := int32(0); ; k++ {
iLMS := isLMS(isS, i+k)
jLMS := isLMS(isS, j+k)
if k > 0 && iLMS && jLMS {
return true // reached end of the strings without diff.
}
if iLMS != jLMS || x[i+k] != x[j+k] {
return false // mismatch
}
}
}
func countBuckets(x []int32, asize int) []int32 {
buckets := make([]int32, asize)
for _, a := range x {
buckets[a]++
}
return buckets
}
func bucketsFronts(fronts, buckets []int32) {
var sum int32
for i := range buckets {
fronts[i] = sum
sum += buckets[i]
}
}
func bucketsEnd(ends, buckets []int32) {
var sum int32
for i := range buckets {
sum += buckets[i]
ends[i] = sum
}
}
func insertBucketFront(out, fronts []int32, bucket, val int32) {
out[fronts[bucket]] = val
fronts[bucket]++
}
func insertBucketEnd(out, ends []int32, bucket, val int32) {
ends[bucket]--
out[ends[bucket]] = val //nolint:wsl // There is nothing wrong here!
}
const (
undefined = -1
)
func clearToUndefined(sa []int32) {
for i := range sa {
sa[i] = undefined
}
}
func bucketLMS(x, sa, buckets, bucketEnds []int32, isS *bitArray) {
bucketsEnd(bucketEnds, buckets)
for i := int32(len(x) - 1); i >= 0; i-- {
if isLMS(isS, i) {
insertBucketEnd(sa, bucketEnds, x[i], i)
}
}
}
func induceLS(x, sa, buckets, bucketEnds []int32, isS *bitArray) {
// Induce L sorting
bucketsFronts(bucketEnds, buckets)
for i := 0; i < len(x); i++ {
if sa[i] == 0 || sa[i] == undefined {
continue
}
j := sa[i] - 1
if !isS.get(j) {
insertBucketFront(sa, bucketEnds, x[j], j)
}
}
// Induce S sorting
bucketsEnd(bucketEnds, buckets)
for i := len(x) - 1; i >= 0; i-- {
if sa[i] == 0 {
continue
}
j := sa[i] - 1
if isS.get(j) {
insertBucketEnd(sa, bucketEnds, x[j], j)
}
}
}
func compactLMS(sa []int32, isS *bitArray) (compact, rest []int32) {
k := 0
for _, j := range sa {
if isLMS(isS, j) {
sa[k] = j
k++
}
}
// slice out the part with the LMS strings and the rest
return sa[:k], sa[k:]
}
func compactDefined(x []int32) []int32 {
k := 0
for _, i := range x {
if i != undefined {
x[k] = i
k++
}
}
// Slice out the piece we used
return x[:k]
}
func reduceLMSString(x, sa []int32, isS *bitArray) (reduced, compact []int32, asize int) {
// We split the input SA into two bits, one that is large
// enough to hold the LMS indices and one that can hold the
// indices if we divide them by two. The LMS strings are in the
// first slice after the compaction, in sorted order. Using
// compact and buffer, we can compute the reduced string.
compact, buffer := compactLMS(sa, isS)
clearToUndefined(buffer)
var letter int32
prevLMS := compact[0]
buffer[prevLMS/2] = 0
for i := 1; i < len(compact); i++ {
j := compact[i]
if !equalLMS(x, isS, prevLMS, j) {
letter++
}
buffer[j/2] = letter
prevLMS = j
}
reduced = compactDefined(buffer)
// The compact slice is big enough to store the SA for the
// reduced string, so that is what we return for it.
// The new alphabet size is the largest letter we have assigned
// plus one (size == largest value + 1)
return reduced, compact, int(letter + 1)
}
func reverseLMSMap(x, sa, reducedSA, offsets, buckets, bucketEnds []int32, isS *bitArray) {
// Remap the reduced suffixes to the indices in the longer
// string. Reset the rest of SA so its ready for imputing.
// This figures out the original indices for the LMS strings.
// They originally came in the same order, so index by index
// they match, we just have to skip over the non-LMS indices
var k, i int32
for i = 1; i < int32(len(x)); i++ {
if isLMS(isS, i) {
offsets[k] = i
k++
}
}
// The replace the indices into the reduced string with
// the indices into the original string
for i, j := range reducedSA {
sa[i] = offsets[j]
}
// Move the LMS suffixes to the correct buckets and leave
// the rest of SA undefined. Going right-to-left we are
// ensured that we cannot overwrite a LMS suffix we need to
// move later.
clearToUndefined(sa[len(reducedSA):])
bucketsEnd(bucketEnds, buckets)
var j int32
for i := len(reducedSA) - 1; i >= 0; i-- {
j, reducedSA[i] = reducedSA[i], undefined
insertBucketEnd(sa, bucketEnds, x[j], j)
}
}
func recSais(x, sa []int32, asize int, isS *bitArray) {
// Base case of recursion: unique characters
if len(x) == asize {
for i, a := range x {
sa[a] = int32(i)
}
return
}
// Recursive case...
classifyS(isS, x)
buckets := countBuckets(x, asize)
bucketEnds := make([]int32, len(buckets))
// Induce first sorting
clearToUndefined(sa)
bucketLMS(x, sa, buckets, bucketEnds, isS)
induceLS(x, sa, buckets, bucketEnds, isS)
// Recursion
redX, redSA, redSize := reduceLMSString(x, sa, isS)
if redSize != len(redX) {
// Save some memory if we are going to recurse further
buckets = nil
bucketEnds = nil
}
recSais(redX, redSA, redSize, isS)
classifyS(isS, x) // Recompute S/L types for this function
if redSize != len(redX) {
// Restore the tables we need again now
buckets = countBuckets(x, asize)
bucketEnds = make([]int32, len(buckets))
}
// Second impute
reverseLMSMap(x, sa, redSA, redX, buckets, bucketEnds, isS)
induceLS(x, sa, buckets, bucketEnds, isS)
}
// SaisWithAlphabet builds a suffix array from the string x, first
// mapping it to bytes using the alphabet alpha.
func SaisWithAlphabet(x string, alpha *Alphabet) ([]int32, error) {
xb, err := alpha.MapToIntsWithSentinel(x)
if err != nil {
return []int32{}, err
}
sa := make([]int32, len(xb))
isS := newBitArray(len(xb))
recSais(xb, sa, alpha.Size(), isS)
return sa, nil
}
// Sais builds a suffix array from the string x
func Sais(x string) (sa []int32) {
sa, _ = SaisWithAlphabet(x, NewAlphabet(x))
return sa
}