-
Notifications
You must be signed in to change notification settings - Fork 9
/
doc_test.go
80 lines (70 loc) · 1.47 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
package radixtree_test
import (
"fmt"
"github.com/gammazero/radixtree"
)
func ExampleTree_Iter() {
rt := radixtree.New[int]()
rt.Put("mercury", 1)
rt.Put("venus", 2)
rt.Put("earth", 3)
rt.Put("mars", 4)
// Find all items that that have a key that is a prefix of "tomato".
for key, value := range rt.Iter() {
fmt.Println(key, "=", value)
}
// Output:
// earth = 3
// mars = 4
// mercury = 1
// venus = 2
}
func ExampleTree_IterAt() {
rt := radixtree.New[string]()
rt.Put("tomato", "TOMATO")
rt.Put("tom", "TOM")
rt.Put("tommy", "TOMMY")
rt.Put("tornado", "TORNADO")
// Find all items whose keys start with "tom".
for _, value := range rt.IterAt("tom") {
fmt.Println(value)
}
// Output:
// TOM
// TOMATO
// TOMMY
}
func ExampleTree_IterPath() {
rt := radixtree.New[string]()
rt.Put("tomato", "TOMATO")
rt.Put("tom", "TOM")
rt.Put("tommy", "TOMMY")
rt.Put("tornado", "TORNADO")
// Find all items that that have a key that is a prefix of "tomato".
for key, value := range rt.IterPath("tomato") {
fmt.Println(key, "=", value)
}
// Output:
// tom = TOM
// tomato = TOMATO
}
func ExampleTree_NewStepper() {
rt := radixtree.New[string]()
rt.Put("tomato", "TOMATO")
rt.Put("tom", "TOM")
rt.Put("tommy", "TOMMY")
rt.Put("tornado", "TORNADO")
stepper := rt.NewStepper()
word := "tomato"
for i := range word {
if !stepper.Next(word[i]) {
break
}
if val, ok := stepper.Value(); ok {
fmt.Println(val)
}
}
// Output:
// TOM
// TOMATO
}