This repository was archived by the owner on Nov 24, 2020. It is now read-only.
forked from ancientlore/go-avltree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairtree.go
More file actions
127 lines (110 loc) · 2.84 KB
/
pairtree.go
File metadata and controls
127 lines (110 loc) · 2.84 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package avltree
import (
"io"
)
// PairTree is a specialization of Tree that hides the wrapping of Elements around Pair structures.
type PairTree struct {
ObjectTree
}
// Pair structure holds your key and value
type Pair struct {
Key string
Value interface{}
}
// compare function for Pairs
func (a Pair) Compare(b Interface) int {
if a.Key < b.(Pair).Key {
return -1
} else if a.Key > b.(Pair).Key {
return 1
}
return 0
}
// Iterate function
type PairIterateFunc func(v Pair)
// Initialize or reset a StringTree
func (t *PairTree) Init(flags byte) *PairTree {
t.ObjectTree.Init(flags)
return t
}
// Return an initialized StringTree
func NewPairTree(flags byte) *PairTree { return new(PairTree).Init(flags) }
// At returns the value at the given index
func (t *PairTree) At(index int) *Pair {
v := t.ObjectTree.At(index)
if v != nil {
x := v.(Pair)
return &x
}
return nil
}
// Find returns the element where the comparison function matches
// the node's value and the given key value
func (t *PairTree) Find(key string) *Pair {
v := t.ObjectTree.Find(Pair{key, nil})
if v != nil {
x := v.(Pair)
return &x
}
return nil
}
// Do calls function f for each element of the tree, in order.
// The function should not change the structure of the tree underfoot.
func (t *PairTree) Do(f PairIterateFunc) { t.ObjectTree.Do(func(v interface{}) { f(v.(Pair)) }) }
// chanIterate should be used as a goroutine to produce all the values
// in the tree.
func (t *PairTree) chanIterate(c chan<- Pair) {
t.Do(func(v Pair) { c <- v })
close(c)
}
// Iter returns a channel you can read through to fetch all the items
func (t *PairTree) Iter() <-chan Pair {
c := make(chan Pair)
go t.chanIterate(c)
return c
}
// Data returns all the elements as a slice.
func (t *PairTree) Data() []Pair {
arr := make([]Pair, t.Len())
var i int
i = 0
t.Do(func(v Pair) {
arr[i] = v
i++
})
return arr
}
// Add adds an item to the tree, returning a pair indicating the added
// (or duplicate) item, and a flag indicating whether the item is the
// duplicate that was found. A duplicate will never be returned if the
// tree's AllowDuplicates flag is set.
func (t *PairTree) Add(o Pair) (val *Pair, isDupe bool) {
v, d := t.ObjectTree.Add(o)
if v != nil {
x := v.(Pair)
return &x, d
}
return nil, d
}
// Remove removes the element matching the given value.
func (t *PairTree) Remove(ptr string) *Pair {
v := t.ObjectTree.Remove(Pair{ptr, nil})
if v != nil {
x := v.(Pair)
return &x
}
return nil
}
// Remove removes the element at the given index
func (t *PairTree) RemoveAt(index int) *Pair {
v := t.ObjectTree.RemoveAt(index)
if v != nil {
x := v.(Pair)
return &x
}
return nil
}
// Print the values in the tree
func (t *PairTree) Print(w io.Writer, f PairIterateFunc, itemSiz int) {
t.ObjectTree.Print(w, func(v interface{}) { f(v.(Pair)) }, itemSiz)
}