diff options
| author | Robert Griesemer <gri@golang.org> | 2011-09-15 16:21:21 -0700 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2011-09-15 16:21:21 -0700 |
| commit | bd80b1198bf26e6abd317638d3861101b6ff72ad (patch) | |
| tree | 0385b78404a1b70db6ebb7bd05944157303053b8 /src/pkg/index/suffixarray/suffixarray.go | |
| parent | f5181ae9d7cf35894efc3e8d7b042dc0314dc86b (diff) | |
| download | go-bd80b1198bf26e6abd317638d3861101b6ff72ad.tar.xz | |
index/suffixarray: support for serialization
R=r
CC=golang-dev
https://golang.org/cl/5040041
Diffstat (limited to 'src/pkg/index/suffixarray/suffixarray.go')
| -rw-r--r-- | src/pkg/index/suffixarray/suffixarray.go | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/src/pkg/index/suffixarray/suffixarray.go b/src/pkg/index/suffixarray/suffixarray.go index c78de854de..c2a999483b 100644 --- a/src/pkg/index/suffixarray/suffixarray.go +++ b/src/pkg/index/suffixarray/suffixarray.go @@ -18,14 +18,17 @@ package suffixarray import ( "bytes" + "encoding/binary" "exp/regexp" + "io" + "os" "sort" ) // Index implements a suffix array for fast substring search. type Index struct { data []byte - sa []int // suffix array for data + sa []int32 // suffix array for data; len(sa) == len(data) } // New creates a new Index for data. @@ -34,6 +37,48 @@ func New(data []byte) *Index { return &Index{data, qsufsort(data)} } +// Read reads the index from r into x; x must not be nil. +func (x *Index) Read(r io.Reader) os.Error { + var n int32 + if err := binary.Read(r, binary.LittleEndian, &n); err != nil { + return err + } + if 2*n < int32(cap(x.data)) || int32(cap(x.data)) < n { + // new data is significantly smaller or larger then + // existing buffers - allocate new ones + x.data = make([]byte, n) + x.sa = make([]int32, n) + } else { + // re-use existing buffers + x.data = x.data[0:n] + x.sa = x.sa[0:n] + } + + if err := binary.Read(r, binary.LittleEndian, x.data); err != nil { + return err + } + if err := binary.Read(r, binary.LittleEndian, x.sa); err != nil { + return err + } + + return nil +} + +// Write writes the index x to w. +func (x *Index) Write(w io.Writer) os.Error { + n := int32(len(x.data)) + if err := binary.Write(w, binary.LittleEndian, n); err != nil { + return err + } + if err := binary.Write(w, binary.LittleEndian, x.data); err != nil { + return err + } + if err := binary.Write(w, binary.LittleEndian, x.sa); err != nil { + return err + } + return nil +} + // Bytes returns the data over which the index was created. // It must not be modified. // @@ -47,7 +92,7 @@ func (x *Index) at(i int) []byte { // lookupAll returns a slice into the matching region of the index. // The runtime is O(log(N)*len(s)). -func (x *Index) lookupAll(s []byte) []int { +func (x *Index) lookupAll(s []byte) []int32 { // find matching suffix index range [i:j] // find the first index where s would be the prefix i := sort.Search(len(x.sa), func(i int) bool { return bytes.Compare(x.at(i), s) >= 0 }) @@ -65,12 +110,15 @@ func (x *Index) lookupAll(s []byte) []int { func (x *Index) Lookup(s []byte, n int) (result []int) { if len(s) > 0 && n != 0 { matches := x.lookupAll(s) - if len(matches) < n || n < 0 { + if n < 0 || len(matches) < n { n = len(matches) } + // 0 <= n <= len(matches) if n > 0 { result = make([]int, n) - copy(result, matches) + for i, x := range matches[0:n] { + result[i] = int(x) + } } } return |
