summaryrefslogtreecommitdiff
path: root/devdocs/go/index%2Fsuffixarray%2Findex.html
blob: 39fab1b830cf364c95b10522e097ed459ad3c308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<h1> Package suffixarray  </h1>     <ul id="short-nav">
<li><code>import "index/suffixarray"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package suffixarray implements substring search in logarithmic time using an in-memory suffix array. </p>
<p>Example use: </p>
<pre data-language="go">// create index for some data
index := suffixarray.New(data)

// lookup byte slice s
offsets1 := index.Lookup(s, -1) // the list of all indices where s occurs in data
offsets2 := index.Lookup(s, 3)  // the list of at most 3 indices where s occurs in data
</pre>      <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#Index">type Index</a></li>
<li> <a href="#New">func New(data []byte) *Index</a>
</li>
<li> <a href="#Index.Bytes">func (x *Index) Bytes() []byte</a>
</li>
<li> <a href="#Index.FindAllIndex">func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int)</a>
</li>
<li> <a href="#Index.Lookup">func (x *Index) Lookup(s []byte, n int) (result []int)</a>
</li>
<li> <a href="#Index.Read">func (x *Index) Read(r io.Reader) error</a>
</li>
<li> <a href="#Index.Write">func (x *Index) Write(w io.Writer) error</a>
</li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_Index_Lookup">Index.Lookup</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>sais.go</span> <span>sais2.go</span> <span>suffixarray.go</span>  </p>   <h2 id="Index">type <span>Index</span>  </h2> <p>Index implements a suffix array for fast substring search. </p>
<pre data-language="go">type Index struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="New">func <span>New</span>  </h3> <pre data-language="go">func New(data []byte) *Index</pre> <p>New creates a new <a href="#Index">Index</a> for data. <a href="#Index">Index</a> creation time is O(N) for N = len(data). </p>
<h3 id="Index.Bytes">func (*Index) <span>Bytes</span>  </h3> <pre data-language="go">func (x *Index) Bytes() []byte</pre> <p>Bytes returns the data over which the index was created. It must not be modified. </p>
<h3 id="Index.FindAllIndex">func (*Index) <span>FindAllIndex</span>  </h3> <pre data-language="go">func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int)</pre> <p>FindAllIndex returns a sorted list of non-overlapping matches of the regular expression r, where a match is a pair of indices specifying the matched slice of x.Bytes(). If n &lt; 0, all matches are returned in successive order. Otherwise, at most n matches are returned and they may not be successive. The result is nil if there are no matches, or if n == 0. </p>
<h3 id="Index.Lookup">func (*Index) <span>Lookup</span>  </h3> <pre data-language="go">func (x *Index) Lookup(s []byte, n int) (result []int)</pre> <p>Lookup returns an unsorted list of at most n indices where the byte string s occurs in the indexed data. If n &lt; 0, all occurrences are returned. The result is nil if s is empty, s is not found, or n == 0. Lookup time is O(log(N)*len(s) + len(result)) where N is the size of the indexed data. </p>   <h4 id="example_Index_Lookup"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">index := suffixarray.New([]byte("banana"))
offsets := index.Lookup([]byte("ana"), -1)
for _, off := range offsets {
    fmt.Println(off)
}

</pre> <p>Output:</p> <pre class="output" data-language="go">1
3
</pre>   <h3 id="Index.Read">func (*Index) <span>Read</span>  </h3> <pre data-language="go">func (x *Index) Read(r io.Reader) error</pre> <p>Read reads the index from r into x; x must not be nil. </p>
<h3 id="Index.Write">func (*Index) <span>Write</span>  </h3> <pre data-language="go">func (x *Index) Write(w io.Writer) error</pre> <p>Write writes the index x to w. </p><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/index/suffixarray/" class="_attribution-link">http://golang.org/pkg/index/suffixarray/</a>
  </p>
</div>