This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypemap.go
313 lines (270 loc) · 7.54 KB
/
typemap.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
package suffixarray
import (
"bytes"
bigbitvector "github.com/team-spectre/go-bigbitvector"
)
const (
// SType indicates that an offset in the corresponding text contains an
// "S-type" symbol, in the terminology of the SA-IS algorithm.
SType = false
// LType indicates that an offset in the corresponding text contains an
// "L-type" symbol, in the terminology of the SA-IS algorithm.
LType = true
)
// TypeMap catalogues the SA-IS type of each symbol in a text.
type TypeMap struct {
bv bigbitvector.BigBitVector
}
// TypeMapIterator iterates over a TypeMap.
//
// The basic usage pattern is:
//
// iter := typeMap.Iterate(i, j)
// for iter.Next() {
// ... // call Index(), Type(), IsLMS(), and/or SetType()
// }
// err := iter.Close()
// if err != nil {
// ... // handle error
// }
//
// Iterators are created in an indeterminate state; the caller must invoke
// Next() to advance to the first item.
//
type TypeMapIterator struct {
impl bigbitvector.Iterator
last bool
primed bool
}
// NewTypeMap constructs a new TypeMap.
func NewTypeMap(opts ...Option) (*TypeMap, error) {
bv, err := makeBigBitVector(opts)
if err != nil {
return nil, err
}
return &TypeMap{bv}, nil
}
// Len returns the number of symbol types in the TypeMap.
func (typeMap *TypeMap) Len() uint64 { return typeMap.bv.Len() }
// TypeAt returns the type for the symbol at index.
func (typeMap *TypeMap) TypeAt(index uint64) (bool, error) {
return typeMap.bv.BitAt(index)
}
// SetTypeAt replaces the type for the symbol at index.
func (typeMap *TypeMap) SetTypeAt(index uint64, typeBit bool) error {
return typeMap.bv.SetBitAt(index, typeBit)
}
// Iterate constructs a TypeMapIterator in the forward direction.
func (typeMap *TypeMap) Iterate(i, j uint64) *TypeMapIterator {
return &TypeMapIterator{impl: typeMap.bv.Iterate(i, j)}
}
// ForEach is a convenience method that iterates over the entire TypeMap.
func (typeMap *TypeMap) ForEach(fn func(uint64, bool, bool) error) error {
iter := typeMap.Iterate(0, typeMap.Len())
defer iter.Close()
for iter.Next() {
err := fn(iter.Index(), iter.Type(), iter.IsLMS())
if err != nil {
return err
}
}
return iter.Close()
}
// CopyFrom copies the symbol types from another TypeMap to this one. The two
// TypeMaps must have the same Len().
func (typeMap *TypeMap) CopyFrom(src *TypeMap) error {
return typeMap.bv.CopyFrom(src.bv)
}
// Truncate trims the TypeMap to the given length.
func (typeMap *TypeMap) Truncate(length uint64) error { return typeMap.bv.Truncate(length) }
// Freeze makes the TypeMap read-only.
func (typeMap *TypeMap) Freeze() error { return typeMap.bv.Freeze() }
// Flush ensures that all pending writes have reached the OS.
func (typeMap *TypeMap) Flush() error { return typeMap.bv.Flush() }
// Close flushes any writes and frees the resources used by the TypeMap.
func (typeMap *TypeMap) Close() error { return typeMap.bv.Close() }
// IsLMS returns true iff the symbol at the given offset is a "left-most S".
func (typeMap *TypeMap) IsLMS(index uint64) (bool, error) {
if index < 1 {
return false, nil
}
a, err := typeMap.bv.BitAt(index - 1)
if err != nil {
return false, err
}
b, err := typeMap.bv.BitAt(index)
if err != nil {
return false, err
}
return b == SType && a == LType, nil
}
// LMSSubstringsAreEqual returns true iff the two LMS-terminated strings at
// offsets i and j are equal.
func (typeMap *TypeMap) LMSSubstringsAreEqual(text *Text, i, j uint64) (bool, error) {
n := text.Len()
if i >= n || j >= n {
return i == j, nil
}
k := uint64(0)
for {
lmsI, err := typeMap.IsLMS(i + k)
if err != nil {
return false, err
}
lmsJ, err := typeMap.IsLMS(j + k)
if err != nil {
return false, err
}
if k > 0 && lmsI && lmsJ {
return true, nil
}
if lmsI != lmsJ {
return false, nil
}
symI, err := text.SymbolAt(i + k)
if err != nil {
return false, err
}
symJ, err := text.SymbolAt(j + k)
if err != nil {
return false, err
}
if symI != symJ {
return false, nil
}
k++
}
}
// Debug returns a human-friendly debugging representation of the TypeMap.
func (typeMap *TypeMap) Debug() string {
var buf bytes.Buffer
var last bool
buf.WriteByte('[')
err := bigbitvector.ForEach(typeMap.bv, func(index uint64, bit bool) error {
if index > 0 && last == LType && bit == SType {
buf.WriteByte('@')
} else if bit == SType {
buf.WriteByte('S')
} else {
buf.WriteByte('L')
}
last = bit
return nil
})
if err != nil {
panic(err)
}
buf.WriteByte(']')
return buf.String()
}
// Next advances the iterator to the next item and returns true, or returns
// false if the end of the iteration has been reached or if an error has
// occurred.
func (iter *TypeMapIterator) Next() bool { return iter.Skip(1) }
// Index returns the index of the current symbol.
func (iter *TypeMapIterator) Index() uint64 { return iter.impl.Index() }
// Type returns the type of the current symbol.
func (iter *TypeMapIterator) Type() bool { return iter.impl.Bit() }
// IsLMS returns true iff the current symbol is a "left-most S".
func (iter *TypeMapIterator) IsLMS() bool { return iter.last == LType && iter.Type() == SType }
// SetType replaces the type for the current symbol.
func (iter *TypeMapIterator) SetType(bit bool) { iter.impl.SetBit(bit) }
// Err returns the error which caused Next() to return false.
func (iter *TypeMapIterator) Err() error { return iter.impl.Err() }
// Flush ensures that all pending writes have reached the OS.
func (iter *TypeMapIterator) Flush() error { return iter.impl.Flush() }
// Close flushes writes and frees the resources used by the iterator.
func (iter *TypeMapIterator) Close() error { return iter.impl.Close() }
// Skip is equivalent to calling Next() n times, but faster.
func (iter *TypeMapIterator) Skip(n uint64) bool {
if n == 1 && iter.primed {
iter.last = iter.impl.Bit()
} else if n == 1 {
iter.last = SType
iter.primed = true
} else if n > 1 {
if !iter.impl.Skip(n - 1) {
return false
}
iter.last = iter.impl.Bit()
iter.primed = true
n = 1
}
return iter.impl.Skip(n)
}
// BuildTypeMap scans a text, constructing a TypeMap from it.
func BuildTypeMap(text *Text, opts ...Option) (*TypeMap, error) {
opts = extendOptions(
opts,
NumValues(text.Len()+1))
typeMap, err := NewTypeMap(opts...)
if err != nil {
return nil, err
}
needClose := true
defer func() {
if needClose {
typeMap.Close()
}
}()
baIter := typeMap.bv.ReverseIterate(0, typeMap.bv.Len())
defer baIter.Close()
if !baIter.Next() {
return nil, baIter.Close()
}
baIter.SetBit(SType)
if text.Len() == 0 {
if err := baIter.Close(); err != nil {
return nil, err
}
needClose = false
return typeMap, nil
}
if !baIter.Next() {
return nil, baIter.Close()
}
baIter.SetBit(LType)
if text.Len() == 1 {
if err := baIter.Close(); err != nil {
return nil, err
}
needClose = false
return typeMap, nil
}
textIter := text.ReverseIterate(0, text.Len())
defer textIter.Close()
haveLast := false
var lastType bool
var lastSymbol uint64
for textIter.Next() {
if !haveLast {
lastType = LType
lastSymbol = textIter.Symbol()
haveLast = true
continue
}
if !baIter.Next() {
return nil, baIter.Close()
}
thisSymbol := textIter.Symbol()
var bit bool
if thisSymbol > lastSymbol {
bit = LType
} else if thisSymbol == lastSymbol {
bit = lastType
} else {
bit = SType
}
baIter.SetBit(bit)
lastType = bit
lastSymbol = thisSymbol
}
if err := textIter.Close(); err != nil {
return nil, err
}
if err := baIter.Close(); err != nil {
return nil, err
}
needClose = false
return typeMap, nil
}