-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_tree.go
265 lines (243 loc) · 5.64 KB
/
match_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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package match_tree
import (
"reflect"
"strings"
)
type Node struct {
copyOnWrite *copyOnWrite
path string
token string
nextMap map[string]*Node
values []interface{}
}
type copyOnWrite struct {
_ int
}
func (copyOnWrite *copyOnWrite) mutableNode(node *Node) *Node {
if copyOnWrite == node.copyOnWrite {
return node
}
copyNode := &Node{
copyOnWrite: copyOnWrite,
path: node.path,
token: node.token,
nextMap: make(map[string]*Node, len(node.nextMap)),
}
for k, v := range node.nextMap {
copyNode.nextMap[k] = v
}
if node.values != nil {
copyNode.values = append(copyNode.values, node.values...)
}
return copyNode
}
type MatchTree struct {
copyOnWrite *copyOnWrite
root *Node
}
func (node *Node) appendValue(value interface{}) {
node.values = append(node.values, value)
}
func (node *Node) RangeNext(f func(next *Node) bool) {
for _, next := range node.nextMap {
if f(next) == false {
return
}
}
}
func (node *Node) loadOrCreateNext(token string, path string) *Node {
next, ok := node.nextMap[token]
if ok {
if next.copyOnWrite != node.copyOnWrite {
next = node.copyOnWrite.mutableNode(next)
node.nextMap[token] = next
}
return next
}
next = newNode(token, path, node.copyOnWrite)
node.nextMap[token] = next
return next
}
func (node *Node) insert(tokens []string, prefix string, value interface{}) {
if len(tokens) == 0 {
node.values = append(node.values, value)
return
}
if len(prefix) > 0 {
prefix += "."
}
prefix += tokens[0]
node.loadOrCreateNext(tokens[0], prefix).insert(tokens[1:], prefix, value)
}
func (node *Node) match(token, remain string, set map[string][]interface{}) {
//fmt.Println("token", token, "remain", remain, "node.token", node.token, "node.path", node.path, "node.values", node.values)
if len(token) == 0 {
if node.values != nil {
set[node.path] = node.values
}
var last *Node
for next := node.findNext("#"); next != nil; next = next.findNext("#") {
last = next
}
if last != nil && last.values != nil {
set[last.path] = last.values
}
return
}
if next := node.findNext(token); next != nil {
token, remain := nextToken(remain)
next.match(token, remain, set)
}
if next := node.findNext("*"); next != nil {
token, remain := nextToken(remain)
next.match(token, remain, set)
}
if next := node.findNext("#"); next != nil {
next.match(token, remain, set)
}
if node.token == "#" {
if node.nextEmpty() {
set[node.path] = node.values
} else {
token, remain = nextToken(remain)
node.match(token, remain, set)
}
}
}
func (node *Node) findNext(token string) *Node {
next, _ := node.nextMap[token]
return next
}
func (node *Node) mutableNext(token string) *Node {
next, _ := node.nextMap[token]
if next == nil {
return nil
}
if next.copyOnWrite != node.copyOnWrite {
next = node.copyOnWrite.mutableNode(next)
node.nextMap[token] = next
}
return next
}
func (node *Node) nextEmpty() bool {
return len(node.nextMap) == 0
}
func (node *Node) Walk(f func(path string, objs []interface{}) bool) bool {
for _, next := range node.nextMap {
if next.values != nil {
if f(next.path, next.values) == false {
return false
}
}
if next.Walk(f) == false {
return false
}
}
return true
}
func newNode(token string, path string, copyOnWrite *copyOnWrite) *Node {
return &Node{
token: token,
copyOnWrite: copyOnWrite,
path: path,
nextMap: map[string]*Node{},
}
}
func NewMatchTree() *MatchTree {
copyOnWrite := new(copyOnWrite)
return &MatchTree{
copyOnWrite: copyOnWrite,
root: newNode("", "", copyOnWrite),
}
}
func (tree *MatchTree) Insert(key string, value interface{}) *MatchTree {
root := tree.copyOnWrite.mutableNode(tree.root)
root.insert(strings.Split(key, "."), "", value)
tree.root = root
return tree
}
func (tree *MatchTree) Match(key string) []interface{} {
var set = make(map[string][]interface{})
token, remain := nextToken(key)
tree.root.match(token, remain, set)
var objs []interface{}
for _, values := range set {
objs = append(objs, values...)
}
return objs
}
func (tree *MatchTree) Clone() *MatchTree {
var clone = NewMatchTree()
copyOnWrite := *clone.copyOnWrite
tree.copyOnWrite = ©OnWrite
clone.root = tree.root
return clone
}
func (tree *MatchTree) Walk(f func(path string, objs []interface{}) bool) {
tree.root.Walk(f)
}
func objRE(first, second interface{}) bool {
if reflect.DeepEqual(first, second) {
return true
}
return first == second
}
func (tree *MatchTree) Delete(key string, obj interface{}) {
root := tree.copyOnWrite.mutableNode(tree.root)
node := root
var stack = []*Node{node}
for token, remain := nextToken(key); len(token) != 0; token, remain = nextToken(remain) {
node = node.mutableNext(token)
if node == nil {
return
}
stack = append(stack, node)
}
if node.path != key {
return
}
var find = false
for i, val := range node.values {
if objRE(val, obj) {
node.values = append(node.values[:i], node.values[i+1:]...)
find = true
if len(node.values) == 0 {
node.values = nil
}
break
}
}
if find == false {
return
}
defer func() {
tree.root = root
}()
if node.values == nil {
return
}
stackPop := func() *Node {
if len(stack) == 0 {
return nil
}
node := stack[len(stack)-1]
stack = stack[:len(stack)-1]
return node
}
_ = stackPop()
for pre := stackPop(); pre != nil; pre = stackPop() {
delete(pre.nextMap, node.token)
if len(pre.nextMap) == 0 && pre.values == nil {
node = pre
} else {
break
}
}
}
func nextToken(str string) (string, string) {
pos := strings.IndexByte(str, '.')
if pos == -1 {
return str, ""
}
return str[:pos], str[pos+1:]
}