1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package suffixarray
17
18 import (
19 "bytes"
20 "encoding/binary"
21 "errors"
22 "io"
23 "math"
24 "regexp"
25 "slices"
26 "sort"
27 )
28
29
30 var maxData32 int = realMaxData32
31
32 const realMaxData32 = math.MaxInt32
33
34
35 type Index struct {
36 data []byte
37 sa ints
38 }
39
40
41
42
43 type ints struct {
44 int32 []int32
45 int64 []int64
46 }
47
48 func (a *ints) len() int {
49 return len(a.int32) + len(a.int64)
50 }
51
52 func (a *ints) get(i int) int64 {
53 if a.int32 != nil {
54 return int64(a.int32[i])
55 }
56 return a.int64[i]
57 }
58
59 func (a *ints) set(i int, v int64) {
60 if a.int32 != nil {
61 a.int32[i] = int32(v)
62 } else {
63 a.int64[i] = v
64 }
65 }
66
67 func (a *ints) slice(i, j int) ints {
68 if a.int32 != nil {
69 return ints{a.int32[i:j], nil}
70 }
71 return ints{nil, a.int64[i:j]}
72 }
73
74
75
76 func New(data []byte) *Index {
77 ix := &Index{data: data}
78 if len(data) <= maxData32 {
79 ix.sa.int32 = make([]int32, len(data))
80 text_32(data, ix.sa.int32)
81 } else {
82 ix.sa.int64 = make([]int64, len(data))
83 text_64(data, ix.sa.int64)
84 }
85 return ix
86 }
87
88
89 func writeInt(w io.Writer, buf []byte, x int) error {
90 binary.PutVarint(buf, int64(x))
91 _, err := w.Write(buf[0:binary.MaxVarintLen64])
92 return err
93 }
94
95
96 func readInt(r io.Reader, buf []byte) (int64, error) {
97 _, err := io.ReadFull(r, buf[0:binary.MaxVarintLen64])
98 x, _ := binary.Varint(buf)
99 return x, err
100 }
101
102
103
104 func writeSlice(w io.Writer, buf []byte, data ints) (n int, err error) {
105
106 p := binary.MaxVarintLen64
107 m := data.len()
108 for ; n < m && p+binary.MaxVarintLen64 <= len(buf); n++ {
109 p += binary.PutUvarint(buf[p:], uint64(data.get(n)))
110 }
111
112
113 binary.PutVarint(buf, int64(p))
114
115
116 _, err = w.Write(buf[0:p])
117 return
118 }
119
120 var errTooBig = errors.New("suffixarray: data too large")
121
122
123
124 func readSlice(r io.Reader, buf []byte, data ints) (n int, err error) {
125
126 var size64 int64
127 size64, err = readInt(r, buf)
128 if err != nil {
129 return
130 }
131 if int64(int(size64)) != size64 || int(size64) < 0 {
132
133 return 0, errTooBig
134 }
135 size := int(size64)
136
137
138 if _, err = io.ReadFull(r, buf[binary.MaxVarintLen64:size]); err != nil {
139 return
140 }
141
142
143 for p := binary.MaxVarintLen64; p < size; n++ {
144 x, w := binary.Uvarint(buf[p:])
145 data.set(n, int64(x))
146 p += w
147 }
148
149 return
150 }
151
152 const bufSize = 16 << 10
153
154
155 func (x *Index) Read(r io.Reader) error {
156
157 buf := make([]byte, bufSize)
158
159
160 n64, err := readInt(r, buf)
161 if err != nil {
162 return err
163 }
164 if int64(int(n64)) != n64 || int(n64) < 0 {
165 return errTooBig
166 }
167 n := int(n64)
168
169
170 if 2*n < cap(x.data) || cap(x.data) < n || x.sa.int32 != nil && n > maxData32 || x.sa.int64 != nil && n <= maxData32 {
171
172
173 x.data = make([]byte, n)
174 x.sa.int32 = nil
175 x.sa.int64 = nil
176 if n <= maxData32 {
177 x.sa.int32 = make([]int32, n)
178 } else {
179 x.sa.int64 = make([]int64, n)
180 }
181 } else {
182
183 x.data = x.data[0:n]
184 x.sa = x.sa.slice(0, n)
185 }
186
187
188 if _, err := io.ReadFull(r, x.data); err != nil {
189 return err
190 }
191
192
193 sa := x.sa
194 for sa.len() > 0 {
195 n, err := readSlice(r, buf, sa)
196 if err != nil {
197 return err
198 }
199 sa = sa.slice(n, sa.len())
200 }
201 return nil
202 }
203
204
205 func (x *Index) Write(w io.Writer) error {
206
207 buf := make([]byte, bufSize)
208
209
210 if err := writeInt(w, buf, len(x.data)); err != nil {
211 return err
212 }
213
214
215 if _, err := w.Write(x.data); err != nil {
216 return err
217 }
218
219
220 sa := x.sa
221 for sa.len() > 0 {
222 n, err := writeSlice(w, buf, sa)
223 if err != nil {
224 return err
225 }
226 sa = sa.slice(n, sa.len())
227 }
228 return nil
229 }
230
231
232
233 func (x *Index) Bytes() []byte {
234 return x.data
235 }
236
237 func (x *Index) at(i int) []byte {
238 return x.data[x.sa.get(i):]
239 }
240
241
242
243 func (x *Index) lookupAll(s []byte) ints {
244
245
246 i := sort.Search(x.sa.len(), func(i int) bool { return bytes.Compare(x.at(i), s) >= 0 })
247
248 j := i + sort.Search(x.sa.len()-i, func(j int) bool { return !bytes.HasPrefix(x.at(j+i), s) })
249 return x.sa.slice(i, j)
250 }
251
252
253
254
255
256
257 func (x *Index) Lookup(s []byte, n int) (result []int) {
258 if len(s) > 0 && n != 0 {
259 matches := x.lookupAll(s)
260 count := matches.len()
261 if n < 0 || count < n {
262 n = count
263 }
264
265 if n > 0 {
266 result = make([]int, n)
267 if matches.int32 != nil {
268 for i := range result {
269 result[i] = int(matches.int32[i])
270 }
271 } else {
272 for i := range result {
273 result[i] = int(matches.int64[i])
274 }
275 }
276 }
277 }
278 return
279 }
280
281
282
283
284
285
286
287 func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int) {
288
289
290 prefix, complete := r.LiteralPrefix()
291 lit := []byte(prefix)
292
293
294 if prefix == "" {
295 return r.FindAllIndex(x.data, n)
296 }
297
298
299
300 if complete {
301
302
303
304
305
306
307 for n1 := n; ; n1 += 2 * (n - len(result)) {
308 indices := x.Lookup(lit, n1)
309 if len(indices) == 0 {
310 return
311 }
312 slices.Sort(indices)
313 pairs := make([]int, 2*len(indices))
314 result = make([][]int, len(indices))
315 count := 0
316 prev := 0
317 for _, i := range indices {
318 if count == n {
319 break
320 }
321
322 if prev <= i {
323 j := 2 * count
324 pairs[j+0] = i
325 pairs[j+1] = i + len(lit)
326 result[count] = pairs[j : j+2]
327 count++
328 prev = i + len(lit)
329 }
330 }
331 result = result[0:count]
332 if len(result) >= n || len(indices) != n1 {
333
334
335 break
336 }
337 }
338 if len(result) == 0 {
339 result = nil
340 }
341 return
342 }
343
344
345
346
347
348 r = regexp.MustCompile("^" + r.String())
349
350
351 for n1 := n; ; n1 += 2 * (n - len(result)) {
352 indices := x.Lookup(lit, n1)
353 if len(indices) == 0 {
354 return
355 }
356 slices.Sort(indices)
357 result = result[0:0]
358 prev := 0
359 for _, i := range indices {
360 if len(result) == n {
361 break
362 }
363 m := r.FindIndex(x.data[i:])
364
365 if m != nil && prev <= i {
366 m[0] = i
367 m[1] += i
368 result = append(result, m)
369 prev = m[1]
370 }
371 }
372 if len(result) >= n || len(indices) != n1 {
373
374
375 break
376 }
377 }
378 if len(result) == 0 {
379 result = nil
380 }
381 return
382 }
383
View as plain text