/
examples_test.go
73 lines (61 loc) · 1.12 KB
/
examples_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
package bst_test
import (
"fmt"
"github.com/bsm/bst"
)
func ExampleSet() {
set := bst.NewSet(5)
set.Add(bst.Int(3))
set.Add(bst.Int(5))
set.Add(bst.Int(1))
set.Add(bst.Int(3))
fmt.Println(set.Len())
// Output: 3
}
func ExampleSet_Add() {
set := bst.NewSet(5)
fmt.Println(set.Add(bst.Int(1)))
fmt.Println(set.Add(bst.Int(2)))
fmt.Println(set.Add(bst.Int(1)))
// Output:
// true
// true
// false
}
func ExampleSet_Iterator() {
set := bst.NewSet(5)
set.Add(bst.Int(3))
set.Add(bst.Int(5))
set.Add(bst.Int(1))
set.Add(bst.Int(3))
for iter := set.Iterator(); iter.Next(); {
fmt.Println(iter.Value())
}
// Output:
// 1
// 3
// 5
}
func ExampleMap() {
set := bst.NewMap(5)
set.Set(bst.Int(5), "bar")
set.Set(bst.Int(3), "foo")
value, ok := set.Get(bst.Int(3))
fmt.Println(value, ok)
value, ok = set.Get(bst.Int(4))
fmt.Println(value, ok)
// Output:
// foo true
// <nil> false
}
func ExampleMap_Iterator() {
set := bst.NewMap(5)
set.Set(bst.Int(5), "bar")
set.Set(bst.Int(3), "foo")
for iter := set.Iterator(); iter.Next(); {
fmt.Println(iter.Key(), iter.Value())
}
// Output:
// 3 foo
// 5 bar
}