github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/index/suffixarray/suffixarray.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package suffixarray は、インメモリの接尾辞配列を使用して対数時間での部分文字列検索を実装します。 6 // 7 // 使用例: 8 // 9 // // データに対してインデックスを作成する 10 // index := suffixarray.New(data) 11 // 12 // // バイトスライス s を検索する 13 // offsets1 := index.Lookup(s, -1) // data 内の s の出現インデックスのリスト 14 // offsets2 := index.Lookup(s, 3) // data 内で最大3つのインデックスでの s の出現リスト 15 16 package suffixarray 17 18 import ( 19 "github.com/shogo82148/std/io" 20 "github.com/shogo82148/std/regexp" 21 ) 22 23 // Indexは高速な部分文字列検索のための接尾辞配列を実装しています。 24 type Index struct { 25 data []byte 26 sa ints 27 } 28 29 // Newはデータのために新しい [Index] を作成します。 30 // [Index] の作成時間はO(N)であり、Nはdataの長さです。 31 func New(data []byte) *Index 32 33 // Readはrからインデックスをxに読み込む。xはnilであってはならない。 34 func (x *Index) Read(r io.Reader) error 35 36 // Writeはインデックスxをwに書き込みます。 37 func (x *Index) Write(w io.Writer) error 38 39 // Bytes はインデックスが作成されたデータを返します。 40 // このデータは変更してはいけません。 41 func (x *Index) Bytes() []byte 42 43 // Lookupは、バイト文字列sがインデックス付きデータに出現する最大n箇所のインデックスの非ソートリストを返します。n < 0の場合、すべての出現箇所が返されます。 44 // sが空である、sが見つからない、またはn == 0の場合、結果はnilです。 45 // ルックアップ時間はO(log(N)*len(s) + len(result))であり、Nはインデックス付きデータのサイズです。 46 func (x *Index) Lookup(s []byte, n int) (result []int) 47 48 // FindAllIndexは正規表現rの非重複マッチのソートされたリストを返します。 49 // マッチは、x.Bytes()の一致するスライスを指定するインデックスのペアです。 50 // nが0未満の場合、すべてのマッチが連続して返されます。 51 // そうでない場合、最大でnマッチが返される可能性がありますが、連続しているとは限りません。 52 // マッチがない場合、またはn == 0の場合は、結果はnilです。 53 func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int)