This repository has been 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
/
stringtree.go
114 lines (98 loc) · 2.66 KB
/
stringtree.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
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
package avltree
import (
"io"
)
// StringTree is a specialization of Tree that hides the wrapping of Elements around strings.
type StringTree struct {
Tree
}
func stringCompare(s1 interface{}, s2 interface{}) int {
if s1.(string) < s2.(string) {
return -1
} else if s1.(string) > s2.(string) {
return 1
}
return 0
}
// Iterate function
type StringIterateFunc func(v string)
// Initialize or reset a StringTree
func (t *StringTree) Init(flags byte) *StringTree {
t.Tree.Init(stringCompare, flags)
return t
}
// Return an initialized StringTree
func NewStringTree(flags byte) *StringTree { return new(StringTree).Init(flags) }
// At returns the value at the given index
func (t *StringTree) At(index int) string {
v := t.Tree.At(index)
if v != nil {
return v.(string)
}
return ""
}
// Find returns the element where the comparison function matches
// the node's value and the given key value
func (t *StringTree) Find(key string) string {
v := t.Tree.Find(key)
if v != nil {
return v.(string)
}
return ""
}
// 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 *StringTree) Do(f StringIterateFunc) { t.Tree.Do(func(v interface{}) { f(v.(string)) }) }
// chanIterate should be used as a goroutine to produce all the values
// in the tree.
func (t *StringTree) chanIterate(c chan<- string) {
t.Do(func(v string) { c <- v })
close(c)
}
// Iter returns a channel you can read through to fetch all the items
func (t *StringTree) Iter() <-chan string {
c := make(chan string)
go t.chanIterate(c)
return c
}
// Data returns all the elements as a slice.
func (t *StringTree) Data() []string {
arr := make([]string, t.Len())
var i int
i = 0
t.Do(func(v string) {
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 *StringTree) Add(o string) (val string, isDupe bool) {
v, d := t.Tree.Add(o)
if v != nil {
return v.(string), d
}
return "", d
}
// Remove removes the element matching the given value.
func (t *StringTree) Remove(ptr string) string {
v := t.Tree.Remove(ptr)
if v != nil {
return v.(string)
}
return ""
}
// Remove removes the element at the given index
func (t *StringTree) RemoveAt(index int) string {
v := t.Tree.RemoveAt(index)
if v != nil {
return v.(string)
}
return ""
}
func (t *StringTree) Print(w io.Writer, f StringIterateFunc, itemSiz int) {
t.Tree.Print(w, func(v interface{}) { f(v.(string)) }, itemSiz)
}