Documentation ¶
Overview ¶
Example (LongestPrefix) ¶
keys := [][]byte{ []byte("ab"), []byte("abc"), []byte("abd"), } values := []int{1, 2, 3} // The type of value doesn't have to be int. Can be anything. t := trie.New(keys, values) var v interface{} var match bool for _, c := range []byte("abcxxx") { if t = t.TraceOne(c); t == nil { break } if vv, ok := t.Terminal(); ok { v = vv match = true } } fmt.Println(v, match)
Output: 2 true
Example (Match) ¶
keys := [][]byte{ []byte("ab"), []byte("abc"), []byte("abd"), } values := []int{1, 2, 3} // The type of value doesn't have to be int. Can be anything. 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[K constraints.Ordered, V any] []node[K, V]
Tree implements an immutable trie tree.
func New ¶
func New[K constraints.Ordered, V any](keys [][]K, values []V) Tree[K, V]
New builds new Tree from keys and values. The len(keys) should equal to len(values).
func (Tree[K, V]) Children ¶
func (t Tree[K, V]) Children() []K
Children returns the bytes of all direct children of the root of t. The result is sorted in ascending order.
func (Tree[K, V]) Predict ¶
func (t Tree[K, V]) Predict() []V
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[K, V]) 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 ¶
keys := [][]byte{[]byte("aa")} values := []int{1} // The type of value doesn't have to be int. Can be anything. t := trie.New(keys, values) t = t.TraceOne('a') // a fmt.Println(t.Terminal()) t = t.TraceOne('a') // aa fmt.Println(t.Terminal()) t = t.TraceOne('a') // aaa fmt.Println(t.Terminal())
Output: 0 false 1 true 0 false
Click to show internal directories.
Click to hide internal directories.