?
This document uses PHP Chinese website manual Release
import "index/suffixarray"
概況
索引
示例
包后綴數(shù)組使用內(nèi)存后綴數(shù)組實現(xiàn)對數(shù)時間的子串搜索。
使用示例:
// 為某些數(shù)據(jù)創(chuàng)建索引index := suffixarray.New(data)// 查找字節(jié)切片soffsets1 := index.Lookup(s, -1) // 數(shù)據(jù)中出現(xiàn)s的所有索引的列表offsets2 := index.Lookup(s, 3) // 最多3個索引的列表,其中s出現(xiàn)在數(shù)據(jù)中
type Index
func New(data []byte) *Index
func (x *Index) Bytes() []byte
func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int)
func (x *Index) Lookup(s []byte, n int) (result []int)
func (x *Index) Read(r io.Reader) error
func (x *Index) Write(w io.Writer) error
Index.Lookup
qsufsort.go suffixarray.go
Index 為快速子串搜索實現(xiàn)后綴數(shù)組。
type Index struct { // 包含已過濾或未導(dǎo)出的字段}
func New(data []byte) *Index
New 為數(shù)據(jù)創(chuàng)建一個新的索引。對于N = len(data),索引創(chuàng)建時間為 O(N*log(N))。
func (x *Index) Bytes() []byte
Bytes 返回索引創(chuàng)建的數(shù)據(jù)。它不能被修改。
func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int)
FindAllIndex 返回正則表達(dá)式r的非重疊匹配的排序列表,其中匹配是指定 x.Bytes()的匹配切片的一對索引。如果 n <0,則所有匹配按照連續(xù)順序返回。否則,最多返回 n 個匹配,并且可能不連續(xù)。如果沒有匹配,或者如果 n == 0,結(jié)果為零。
func (x *Index) Lookup(s []byte, n int) (result []int)
Lookup 返回索引數(shù)據(jù)中至多有 n 個索引的未排序列表,其中字節(jié)串 s 出現(xiàn)。如果 n <0,則返回所有發(fā)生的事件。如果 s 為空,s 未找到或 n == 0,則結(jié)果為 nil 。查找時間為 O(log(N)*len(s) + len(result))其中 N 是索引數(shù)據(jù)的大小。
package mainimport ("fmt""index/suffixarray")func main() { index := suffixarray.New([]byte("banana")) offsets := index.Lookup([]byte("ana"), -1)for _, off := range offsets { fmt.Println(off)}}
func (x *Index) Read(r io.Reader) error
Read 讀取從 r 到 x 的索引;x 不能為零。
func (x *Index) Write(w io.Writer) error
Write 將索引 x 寫入 w 。