Documentation
¶
Overview ¶
Example (LongestPrefix) ¶
package main
import (
"fmt"
"github.com/acomagu/trie"
)
func main() {
keys := [][]byte{
[]byte("ab"),
[]byte("abc"),
[]byte("abd"),
}
values := []interface{}{1, 2, 3}
t := trie.New(keys, values)
var v interface{}
var match bool
for _, c := range []byte("abcxxx") {
if t = t.TraceByte(c); t == nil {
break
}
if vv, ok := t.Terminal(); ok {
v = vv
match = true
}
}
fmt.Println(v, match)
}
Output: 2 true
Example (Match) ¶
package main
import (
"fmt"
"github.com/acomagu/trie"
)
func main() {
keys := [][]byte{
[]byte("ab"),
[]byte("abc"),
[]byte("abd"),
}
values := []interface{}{1, 2, 3}
t := trie.New(keys, values)
v, ok := t.Trace([]byte("abc")).Terminal()
fmt.Println(v, ok)
}
Output: 2 true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Tree ¶
type Tree []node
Tree implements an immutable trie tree.
func (Tree) Children ¶
Children returns the bytes of all direct children of the root of t. The result is sorted in ascending order.
func (Tree) Predict ¶
func (t Tree) Predict() []interface{}
Predict returns the all values in the tree, t. The complexity is proportional to the number of nodes in t(it's not equal to len(t)).
func (Tree) Terminal ¶
Terminal returns the value of the root of t. The second return value indicates whether the node has a value; if it is false, the first return value is nil. It returns nil also when the t is nil.
Example ¶
package main
import (
"fmt"
"github.com/acomagu/trie"
)
func main() {
keys := [][]byte{[]byte("aa")}
values := []interface{}{1}
t := trie.New(keys, values)
t = t.TraceByte('a') // a
fmt.Println(t.Terminal())
t = t.TraceByte('a') // aa
fmt.Println(t.Terminal())
t = t.TraceByte('a') // aaa
fmt.Println(t.Terminal())
}
Output: <nil> false 1 true <nil> false
Click to show internal directories.
Click to hide internal directories.