-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
64 lines (52 loc) · 1.18 KB
/
example_test.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
61
62
63
64
package trie_test
import (
"fmt"
"github.com/acomagu/trie/v2"
)
func 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
}
func 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
}
func ExampleTree_Terminal() {
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
}