-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
204 lines (161 loc) · 3.99 KB
/
tree.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package tl
type Tree[Key comparable, Value any] struct {
data Optional[Value]
name Key
path []Key
depth int
nodes []*Tree[Key, Value]
}
func (tree *Tree[Key, Value]) Trees() []*Tree[Key, Value] {
return tree.nodes
}
func (tree *Tree[Key, Value]) Name() Key {
return tree.name
}
func (tree *Tree[Key, Value]) Path() []Key {
return tree.path
}
func (tree *Tree[Key, Value]) Data() Optional[Value] {
return tree.data
}
func (tree *Tree[Key, Value]) Depth() int {
return tree.depth
}
func (tree *Tree[Key, Value]) Get(path ...Key) (depth int, opt Optional[Value]) {
depth = -1
lastTree := tree.getLastTree(path...)
if lastTree != nil {
depth = lastTree.depth
opt = lastTree.data
}
return
}
func (tree *Tree[Key, Value]) Fetch(path ...Key) (opt Optional[Value]) {
if len(path) == 0 {
return tree.data
}
for _, node := range tree.nodes {
if node.Name() == path[0] {
return node.Fetch(path[1:]...)
}
}
return
}
func (tree *Tree[Key, Value]) GetTree(path ...Key) *Tree[Key, Value] {
return tree.getLastTree(path...)
}
func (tree *Tree[Key, Value]) Range(fn func(*Tree[Key, Value]) bool, path ...Key) {
tree.rangeOver(fn, -1, path...)
}
func (tree *Tree[Key, Value]) RangeAll(fn func(*Tree[Key, Value]) bool) {
for _, child := range tree.nodes {
child.travel(-1, fn)
}
}
func (tree *Tree[Key, Value]) rangeOver(fn func(*Tree[Key, Value]) bool, maxDepth int, path ...Key) {
nn := tree.getLastTree(path...)
if nn == nil {
return
}
// inner node
for _, in := range nn.nodes {
if !in.travel(maxDepth, fn) {
break
}
}
}
func (tree *Tree[Key, Value]) RangeLimit(fn func(*Tree[Key, Value]) bool, maxDepth int) {
tree.rangeOver(fn, maxDepth)
}
func (tree *Tree[Key, Value]) RangeLevel(fn func(*Tree[Key, Value]) bool, level int) {
for _, child := range tree.nodes {
child.rangeLevel(fn, 0, level)
}
}
func (tree *Tree[Key, Value]) rangeLevel(fn func(*Tree[Key, Value]) bool, depth, level int) bool {
if level-depth == 0 {
return fn(tree)
}
for _, child := range tree.nodes {
if !child.rangeLevel(fn, depth+1, level) {
return false
}
}
return true
}
func (tree *Tree[Key, Value]) travel(maxDepth int, fn func(*Tree[Key, Value]) bool) bool {
if maxDepth > -1 && tree.depth >= maxDepth {
return true
}
for _, nn := range tree.nodes {
if !nn.travel(maxDepth, fn) {
return false
}
}
return fn(tree)
}
func (tree *Tree[Key, Value]) getLastTree(path ...Key) *Tree[Key, Value] {
if len(path) == 0 {
return tree
}
for _, newTree := range tree.nodes {
// if the path is found, advance the index, otherwise, look for the path in the next Tree
if newTree.name == path[0] {
nLastTree := newTree.getLastTree(path[1:]...)
if nLastTree != nil {
return nLastTree
}
}
}
return tree
}
func (tree *Tree[Key, Value]) Set(data Value, path ...Key) {
tree.set(data, 0, []Key{}, path...)
}
func (tree *Tree[Key, Value]) SetRange(data Value, lvl int) {
tree.RangeLevel(func(subNode *Tree[Key, Value]) bool {
subNode.Set(data)
return true
}, lvl)
}
func (tree *Tree[Key, Value]) set(data Value, depth int, cumPath []Key, path ...Key) {
if len(path) == 0 {
tree.data.Set(data)
return
}
cumPath = append(cumPath, path[0])
// iterate over the nodes
for _, newTree := range tree.nodes {
// if the node was found, then continue iterating
if newTree.name == path[0] {
newTree.set(data, depth+1, cumPath, path[1:]...)
return
}
}
// if not found, create the node
newTree := &Tree[Key, Value]{
name: path[0],
depth: depth,
path: cumPath,
}
tree.nodes = append(tree.nodes, newTree)
// and try to go until the end
newTree.set(data, depth+1, cumPath, path[1:]...)
}
func (tree *Tree[Key, Value]) Del(path ...Key) {
tree.del(path...)
}
func (tree *Tree[Key, Value]) del(path ...Key) bool {
for i, newTree := range tree.nodes {
if newTree.name == path[0] {
if len(path) == 1 {
tree.nodes = append(tree.nodes[:i], tree.nodes[i+1:]...)
return true
}
if newTree.del(path[1:]...) {
return true
}
}
}
return false
}