1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package suffixarray
18
19 import (
20 "bytes"
21 "encoding/binary"
22 "errors"
23 "io"
24 "math"
25 "regexp"
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
234 func (x *Index) Bytes() []byte {
235 return x.data
236 }
237
238 func (x *Index) at(i int) []byte {
239 return x.data[x.sa.get(i):]
240 }
241
242
243
244 func (x *Index) lookupAll(s []byte) ints {
245
246
247 i := sort.Search(x.sa.len(), func(i int) bool { return bytes.Compare(x.at(i), s) >= 0 })
248
249 j := i + sort.Search(x.sa.len()-i, func(j int) bool { return !bytes.HasPrefix(x.at(j+i), s) })
250 return x.sa.slice(i, j)
251 }
252
253
254
255
256
257
258
259 func (x *Index) Lookup(s []byte, n int) (result []int) {
260 if len(s) > 0 && n != 0 {
261 matches := x.lookupAll(s)
262 count := matches.len()
263 if n < 0 || count < n {
264 n = count
265 }
266
267 if n > 0 {
268 result = make([]int, n)
269 if matches.int32 != nil {
270 for i := range result {
271 result[i] = int(matches.int32[i])
272 }
273 } else {
274 for i := range result {
275 result[i] = int(matches.int64[i])
276 }
277 }
278 }
279 }
280 return
281 }
282
283
284
285
286
287
288
289
290 func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int) {
291
292
293 prefix, complete := r.LiteralPrefix()
294 lit := []byte(prefix)
295
296
297 if prefix == "" {
298 return r.FindAllIndex(x.data, n)
299 }
300
301
302
303 if complete {
304
305
306
307
308
309
310 for n1 := n; ; n1 += 2 * (n - len(result)) {
311 indices := x.Lookup(lit, n1)
312 if len(indices) == 0 {
313 return
314 }
315 sort.Ints(indices)
316 pairs := make([]int, 2*len(indices))
317 result = make([][]int, len(indices))
318 count := 0
319 prev := 0
320 for _, i := range indices {
321 if count == n {
322 break
323 }
324
325 if prev <= i {
326 j := 2 * count
327 pairs[j+0] = i
328 pairs[j+1] = i + len(lit)
329 result[count] = pairs[j : j+2]
330 count++
331 prev = i + len(lit)
332 }
333 }
334 result = result[0:count]
335 if len(result) >= n || len(indices) != n1 {
336
337
338 break
339 }
340 }
341 if len(result) == 0 {
342 result = nil
343 }
344 return
345 }
346
347
348
349
350
351 r = regexp.MustCompile("^" + r.String())
352
353
354 for n1 := n; ; n1 += 2 * (n - len(result)) {
355 indices := x.Lookup(lit, n1)
356 if len(indices) == 0 {
357 return
358 }
359 sort.Ints(indices)
360 result = result[0:0]
361 prev := 0
362 for _, i := range indices {
363 if len(result) == n {
364 break
365 }
366 m := r.FindIndex(x.data[i:])
367
368 if m != nil && prev <= i {
369 m[0] = i
370 m[1] += i
371 result = append(result, m)
372 prev = m[1]
373 }
374 }
375 if len(result) >= n || len(indices) != n1 {
376
377
378 break
379 }
380 }
381 if len(result) == 0 {
382 result = nil
383 }
384 return
385 }
386
View as plain text