-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
88 lines (76 loc) · 1.32 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package rbtree_test
import (
"fmt"
"github.com/twmb/go-rbtree"
)
type Int int
func (l Int) Less(r rbtree.Item) bool {
return l < r.(Int)
}
func ExampleIter() {
var r rbtree.Tree
for i := 0; i < 10; i++ {
r.Insert(Int(i)) // Int provides Less on int
}
// Declaring iter here to show we can reset it later.
var iter rbtree.Iter
// We can start iterating _at_ the max.
fmt.Println("iterating down...")
for iter = rbtree.IterAt(r.Max()); iter.Ok(); iter.Left() {
fmt.Println(iter.Item())
}
// Or, we can start iterating just before the min so that
// our first call to Right will start at the min.
iter.Reset(rbtree.Into(r.Min()))
fmt.Println("iterating up...")
for {
next := iter.Right()
if next == nil { // when right returns nil, we are done
break
}
fmt.Println(next.Item)
}
// Putting it together...
fmt.Println("iterating down very inefficiently...")
for it := rbtree.IterAt(r.Min()); it.Ok(); it.Right() {
if it.Item() == r.Max().Item {
fmt.Println(it.Item())
r.Delete(it.Node())
it.Reset(rbtree.Into(r.Min()))
}
}
// Output:
// iterating down...
// 9
// 8
// 7
// 6
// 5
// 4
// 3
// 2
// 1
// 0
// iterating up...
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// iterating down very inefficiently...
// 9
// 8
// 7
// 6
// 5
// 4
// 3
// 2
// 1
// 0
}