-
Notifications
You must be signed in to change notification settings - Fork 9
/
doc_test.go
83 lines (72 loc) · 1.42 KB
/
doc_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package radixtree_test
import (
"fmt"
"github.com/gammazero/radixtree"
)
func ExampleTree_Walk() {
rt := radixtree.New()
rt.Put("tomato", "TOMATO")
rt.Put("tom", "TOM")
rt.Put("tommy", "TOMMY")
rt.Put("tornado", "TORNADO")
// Find all items whose keys start with "tom"
rt.Walk("tom", func(key string, value any) bool {
fmt.Println(value)
return false
})
}
func ExampleTree_WalkPath() {
rt := radixtree.New()
rt.Put("tomato", "TOMATO")
rt.Put("tom", "TOM")
rt.Put("tommy", "TOMMY")
rt.Put("tornado", "TORNADO")
// Find all items that are a prefix of "tomato"
rt.WalkPath("tomato", func(key string, value any) bool {
fmt.Println(value)
return false
})
// Output:
// TOM
// TOMATO
}
func ExampleTree_NewIterator() {
rt := radixtree.New()
rt.Put("tomato", "TOMATO")
rt.Put("tom", "TOM")
rt.Put("tommy", "TOMMY")
rt.Put("tornado", "TORNADO")
iter := rt.NewIterator()
for {
key, val, done := iter.Next()
if done {
break
}
fmt.Println(key, "=", val)
}
// Output:
// tom = TOM
// tomato = TOMATO
// tommy = TOMMY
// tornado = TORNADO
}
func ExampleTree_NewStepper() {
rt := radixtree.New()
rt.Put("tomato", "TOMATO")
rt.Put("tom", "TOM")
rt.Put("tommy", "TOMMY")
rt.Put("tornado", "TORNADO")
iter := rt.NewStepper()
word := "tomato"
for i := range word {
if !iter.Next(word[i]) {
break
}
if val, ok := iter.Value(); ok {
fmt.Println(val)
}
}
// Output:
// TOM
// TOMATO
}