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