-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample_test.go
71 lines (65 loc) · 1.82 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
65
66
67
68
69
70
71
// Copyright 2015-2023 Brett Vickers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package prefixtree_test
import (
"fmt"
"github.com/beevik/prefixtree/v2"
)
// Build a prefix tree and search it for various prefix matches.
func ExampleTree_usage() {
// Create the tree. Add 5 strings, each with an associated
// integer.
tree := prefixtree.New[int]()
for i, s := range []string{
"apple",
"orange",
"apple pie",
"lemon meringue",
"lemon",
} {
tree.Add(s, i)
}
// Attempt to find various prefixes in the tree, and output
// the result.
fmt.Printf("%-18s %-8s %s\n", "prefix", "value", "error")
fmt.Printf("%-18s %-8s %s\n", "------", "-----", "-----")
for _, s := range []string{
"a",
"appl",
"apple",
"apple p",
"apple pie",
"apple pies",
"o",
"orang",
"orange",
"oranges",
"lemo",
"lemon",
"lemon m",
"lemon meringue",
"lemon meringues",
} {
value, err := tree.FindValue(s)
fmt.Printf("%-18s %-8v %v\n", s, value, err)
}
// Output:
// prefix value error
// ------ ----- -----
// a 0 prefixtree: prefix ambiguous
// appl 0 prefixtree: prefix ambiguous
// apple 0 <nil>
// apple p 2 <nil>
// apple pie 2 <nil>
// apple pies 0 prefixtree: prefix not found
// o 1 <nil>
// orang 1 <nil>
// orange 1 <nil>
// oranges 0 prefixtree: prefix not found
// lemo 0 prefixtree: prefix ambiguous
// lemon 4 <nil>
// lemon m 3 <nil>
// lemon meringue 3 <nil>
// lemon meringues 0 prefixtree: prefix not found
}