-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytetrie.go
60 lines (49 loc) · 1.23 KB
/
bytetrie.go
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
51
52
53
54
55
56
57
58
59
60
package hairetsu
import (
"io"
da "github.com/ajiyoshi-vg/hairetsu/doublearray"
"github.com/ajiyoshi-vg/hairetsu/keytree"
"github.com/ajiyoshi-vg/hairetsu/node"
"github.com/ajiyoshi-vg/hairetsu/word"
)
type ByteTrie struct {
data da.Nodes
b da.Bytes
}
func (t *ByteTrie) ExactMatchSearch(key []byte) (node.Index, error) {
return t.b.ExactMatchSearch(t.data, key)
}
func (t *ByteTrie) CommonPrefixSearch(key []byte) ([]node.Index, error) {
return t.b.CommonPrefixSearch(t.data, key)
}
func (t *ByteTrie) WriteTo(w io.Writer) (int64, error) {
return t.data.WriteTo(w)
}
func NewByteTrie(data da.Nodes) *ByteTrie {
return &ByteTrie{data: data}
}
type ByteTrieBuilder struct {
builder *da.Builder
}
func NewByteTrieBuilder(opt ...da.Option) *ByteTrieBuilder {
return &ByteTrieBuilder{
builder: da.NewBuilder(opt...),
}
}
func (b *ByteTrieBuilder) BuildSlice(xs [][]byte) (*ByteTrie, error) {
ks := keytree.New()
for i, x := range xs {
err := ks.Put(word.FromBytes(x), uint32(i))
if err != nil {
return nil, err
}
}
return b.Build(ks)
}
func (b *ByteTrieBuilder) Build(ks da.Walker) (*ByteTrie, error) {
x := da.New()
if err := b.builder.Build(x, ks); err != nil {
return nil, err
}
return NewByteTrie(x), nil
}