| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | package suffixarray |
| |
|
| | import ( |
| | "bytes" |
| | "encoding/binary" |
| | "errors" |
| | "io" |
| | "math" |
| | "regexp" |
| | "slices" |
| | "sort" |
| | ) |
| |
|
| | |
| | var maxData32 int = realMaxData32 |
| |
|
| | const realMaxData32 = math.MaxInt32 |
| |
|
| | |
| | type Index struct { |
| | data []byte |
| | sa ints |
| | } |
| |
|
| | |
| | |
| | |
| | type ints struct { |
| | int32 []int32 |
| | int64 []int64 |
| | } |
| |
|
| | func (a *ints) len() int { |
| | return len(a.int32) + len(a.int64) |
| | } |
| |
|
| | func (a *ints) get(i int) int64 { |
| | if a.int32 != nil { |
| | return int64(a.int32[i]) |
| | } |
| | return a.int64[i] |
| | } |
| |
|
| | func (a *ints) set(i int, v int64) { |
| | if a.int32 != nil { |
| | a.int32[i] = int32(v) |
| | } else { |
| | a.int64[i] = v |
| | } |
| | } |
| |
|
| | func (a *ints) slice(i, j int) ints { |
| | if a.int32 != nil { |
| | return ints{a.int32[i:j], nil} |
| | } |
| | return ints{nil, a.int64[i:j]} |
| | } |
| |
|
| | |
| | |
| | func New(data []byte) *Index { |
| | ix := &Index{data: data} |
| | if len(data) <= maxData32 { |
| | ix.sa.int32 = make([]int32, len(data)) |
| | text_32(data, ix.sa.int32) |
| | } else { |
| | ix.sa.int64 = make([]int64, len(data)) |
| | text_64(data, ix.sa.int64) |
| | } |
| | return ix |
| | } |
| |
|
| | |
| | func writeInt(w io.Writer, buf []byte, x int) error { |
| | binary.PutVarint(buf, int64(x)) |
| | _, err := w.Write(buf[0:binary.MaxVarintLen64]) |
| | return err |
| | } |
| |
|
| | |
| | func readInt(r io.Reader, buf []byte) (int64, error) { |
| | _, err := io.ReadFull(r, buf[0:binary.MaxVarintLen64]) |
| | x, _ := binary.Varint(buf) |
| | return x, err |
| | } |
| |
|
| | |
| | |
| | func writeSlice(w io.Writer, buf []byte, data ints) (n int, err error) { |
| | |
| | p := binary.MaxVarintLen64 |
| | m := data.len() |
| | for ; n < m && p+binary.MaxVarintLen64 <= len(buf); n++ { |
| | p += binary.PutUvarint(buf[p:], uint64(data.get(n))) |
| | } |
| |
|
| | |
| | binary.PutVarint(buf, int64(p)) |
| |
|
| | |
| | _, err = w.Write(buf[0:p]) |
| | return |
| | } |
| |
|
| | var errTooBig = errors.New("suffixarray: data too large") |
| |
|
| | |
| | |
| | func readSlice(r io.Reader, buf []byte, data ints) (n int, err error) { |
| | |
| | var size64 int64 |
| | size64, err = readInt(r, buf) |
| | if err != nil { |
| | return |
| | } |
| | if int64(int(size64)) != size64 || int(size64) < 0 { |
| | |
| | return 0, errTooBig |
| | } |
| | size := int(size64) |
| |
|
| | |
| | if _, err = io.ReadFull(r, buf[binary.MaxVarintLen64:size]); err != nil { |
| | return |
| | } |
| |
|
| | |
| | for p := binary.MaxVarintLen64; p < size; n++ { |
| | x, w := binary.Uvarint(buf[p:]) |
| | data.set(n, int64(x)) |
| | p += w |
| | } |
| |
|
| | return |
| | } |
| |
|
| | const bufSize = 16 << 10 |
| |
|
| | |
| | func (x *Index) Read(r io.Reader) error { |
| | |
| | buf := make([]byte, bufSize) |
| |
|
| | |
| | n64, err := readInt(r, buf) |
| | if err != nil { |
| | return err |
| | } |
| | if int64(int(n64)) != n64 || int(n64) < 0 { |
| | return errTooBig |
| | } |
| | n := int(n64) |
| |
|
| | |
| | if 2*n < cap(x.data) || cap(x.data) < n || x.sa.int32 != nil && n > maxData32 || x.sa.int64 != nil && n <= maxData32 { |
| | |
| | |
| | x.data = make([]byte, n) |
| | x.sa.int32 = nil |
| | x.sa.int64 = nil |
| | if n <= maxData32 { |
| | x.sa.int32 = make([]int32, n) |
| | } else { |
| | x.sa.int64 = make([]int64, n) |
| | } |
| | } else { |
| | |
| | x.data = x.data[0:n] |
| | x.sa = x.sa.slice(0, n) |
| | } |
| |
|
| | |
| | if _, err := io.ReadFull(r, x.data); err != nil { |
| | return err |
| | } |
| |
|
| | |
| | sa := x.sa |
| | for sa.len() > 0 { |
| | n, err := readSlice(r, buf, sa) |
| | if err != nil { |
| | return err |
| | } |
| | sa = sa.slice(n, sa.len()) |
| | } |
| | return nil |
| | } |
| |
|
| | |
| | func (x *Index) Write(w io.Writer) error { |
| | |
| | buf := make([]byte, bufSize) |
| |
|
| | |
| | if err := writeInt(w, buf, len(x.data)); err != nil { |
| | return err |
| | } |
| |
|
| | |
| | if _, err := w.Write(x.data); err != nil { |
| | return err |
| | } |
| |
|
| | |
| | sa := x.sa |
| | for sa.len() > 0 { |
| | n, err := writeSlice(w, buf, sa) |
| | if err != nil { |
| | return err |
| | } |
| | sa = sa.slice(n, sa.len()) |
| | } |
| | return nil |
| | } |
| |
|
| | |
| | |
| | func (x *Index) Bytes() []byte { |
| | return x.data |
| | } |
| |
|
| | func (x *Index) at(i int) []byte { |
| | return x.data[x.sa.get(i):] |
| | } |
| |
|
| | |
| | |
| | func (x *Index) lookupAll(s []byte) ints { |
| | |
| | |
| | i := sort.Search(x.sa.len(), func(i int) bool { return bytes.Compare(x.at(i), s) >= 0 }) |
| | |
| | j := i + sort.Search(x.sa.len()-i, func(j int) bool { return !bytes.HasPrefix(x.at(j+i), s) }) |
| | return x.sa.slice(i, j) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | func (x *Index) Lookup(s []byte, n int) (result []int) { |
| | if len(s) > 0 && n != 0 { |
| | matches := x.lookupAll(s) |
| | count := matches.len() |
| | if n < 0 || count < n { |
| | n = count |
| | } |
| | |
| | if n > 0 { |
| | result = make([]int, n) |
| | if matches.int32 != nil { |
| | for i := range result { |
| | result[i] = int(matches.int32[i]) |
| | } |
| | } else { |
| | for i := range result { |
| | result[i] = int(matches.int64[i]) |
| | } |
| | } |
| | } |
| | } |
| | return |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int) { |
| | |
| | |
| | prefix, complete := r.LiteralPrefix() |
| | lit := []byte(prefix) |
| |
|
| | |
| | if prefix == "" { |
| | return r.FindAllIndex(x.data, n) |
| | } |
| |
|
| | |
| | |
| | if complete { |
| | |
| | |
| | |
| | |
| | |
| | |
| | for n1 := n; ; n1 += 2 * (n - len(result)) { |
| | indices := x.Lookup(lit, n1) |
| | if len(indices) == 0 { |
| | return |
| | } |
| | slices.Sort(indices) |
| | pairs := make([]int, 2*len(indices)) |
| | result = make([][]int, len(indices)) |
| | count := 0 |
| | prev := 0 |
| | for _, i := range indices { |
| | if count == n { |
| | break |
| | } |
| | |
| | if prev <= i { |
| | j := 2 * count |
| | pairs[j+0] = i |
| | pairs[j+1] = i + len(lit) |
| | result[count] = pairs[j : j+2] |
| | count++ |
| | prev = i + len(lit) |
| | } |
| | } |
| | result = result[0:count] |
| | if len(result) >= n || len(indices) != n1 { |
| | |
| | |
| | break |
| | } |
| | } |
| | if len(result) == 0 { |
| | result = nil |
| | } |
| | return |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | r = regexp.MustCompile("^" + r.String()) |
| |
|
| | |
| | for n1 := n; ; n1 += 2 * (n - len(result)) { |
| | indices := x.Lookup(lit, n1) |
| | if len(indices) == 0 { |
| | return |
| | } |
| | slices.Sort(indices) |
| | result = result[0:0] |
| | prev := 0 |
| | for _, i := range indices { |
| | if len(result) == n { |
| | break |
| | } |
| | m := r.FindIndex(x.data[i:]) |
| | |
| | if m != nil && prev <= i { |
| | m[0] = i |
| | m[1] += i |
| | result = append(result, m) |
| | prev = m[1] |
| | } |
| | } |
| | if len(result) >= n || len(indices) != n1 { |
| | |
| | |
| | break |
| | } |
| | } |
| | if len(result) == 0 { |
| | result = nil |
| | } |
| | return |
| | } |
| |
|