-
Notifications
You must be signed in to change notification settings - Fork 52
/
tree_traversal.go
352 lines (289 loc) · 7.19 KB
/
tree_traversal.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package art
type traverseAction int
const (
traverseStop traverseAction = iota
traverseContinue
)
type iteratorLevel struct {
node *artNode
childIdx int
}
type iterator struct {
version int // tree version
tree *tree
nextNode *artNode
depthLevel int
depth []*iteratorLevel
}
type bufferedIterator struct {
options int
nextNode Node
err error
it *iterator
}
func traverseOptions(opts ...int) int {
options := 0
for _, opt := range opts {
options |= opt
}
options &= TraverseAll
if options == 0 {
// By default filter only leafs
options = TraverseLeaf
}
return options
}
func traverseFilter(options int, callback Callback) Callback {
if options == TraverseAll {
return callback
}
return func(node Node) bool {
if options&TraverseLeaf == TraverseLeaf && node.Kind() == Leaf {
return callback(node)
} else if options&TraverseNode == TraverseNode && node.Kind() != Leaf {
return callback(node)
}
return true
}
}
func (t *tree) ForEach(callback Callback, opts ...int) {
options := traverseOptions(opts...)
t.recursiveForEach(t.root, traverseFilter(options, callback))
}
func (t *tree) recursiveForEach(current *artNode, callback Callback) traverseAction {
if current == nil {
return traverseContinue
}
if !callback(current) {
return traverseStop
}
switch current.kind {
case Node4:
return t.forEachChildren(current.node().zeroChild, current.node4().children[:], callback)
case Node16:
return t.forEachChildren(current.node().zeroChild, current.node16().children[:], callback)
case Node48:
node := current.node48()
child := node.zeroChild
if child != nil {
if t.recursiveForEach(child, callback) == traverseStop {
return traverseStop
}
}
for i, c := range node.keys {
if node.present[uint16(i)>>n48s]&(1<<(uint16(i)%n48m)) == 0 {
continue
}
child := node.children[c]
if child != nil {
if t.recursiveForEach(child, callback) == traverseStop {
return traverseStop
}
}
}
case Node256:
return t.forEachChildren(current.node().zeroChild, current.node256().children[:], callback)
}
return traverseContinue
}
func (t *tree) forEachChildren(nullChild *artNode, children []*artNode, callback Callback) traverseAction {
if nullChild != nil {
if t.recursiveForEach(nullChild, callback) == traverseStop {
return traverseStop
}
}
for _, child := range children {
if child != nil && child != nullChild {
if t.recursiveForEach(child, callback) == traverseStop {
return traverseStop
}
}
}
return traverseContinue
}
func (t *tree) ForEachPrefix(key Key, callback Callback) {
t.forEachPrefix(t.root, key, callback)
}
func (t *tree) forEachPrefix(current *artNode, key Key, callback Callback) traverseAction {
if current == nil {
return traverseContinue
}
depth := uint32(0)
for current != nil {
if current.isLeaf() {
leaf := current.leaf()
if leaf.prefixMatch(key) {
if !callback(current) {
return traverseStop
}
}
break
}
if depth == uint32(len(key)) {
leaf := current.minimum()
if leaf.prefixMatch(key) {
if t.recursiveForEach(current, callback) == traverseStop {
return traverseStop
}
}
break
}
node := current.node()
if node.prefixLen > 0 {
prefixLen := current.matchDeep(key, depth)
if prefixLen > node.prefixLen {
prefixLen = node.prefixLen
}
if prefixLen == 0 {
break
} else if depth+prefixLen == uint32(len(key)) {
return t.recursiveForEach(current, callback)
}
depth += node.prefixLen
}
// Find a child to recursive to
next := current.findChild(key.charAt(int(depth)), key.valid(int(depth)))
if *next == nil {
break
}
current = *next
depth++
}
return traverseContinue
}
// Iterator pattern
func (t *tree) Iterator(opts ...int) Iterator {
options := traverseOptions(opts...)
it := &iterator{
version: t.version,
tree: t,
nextNode: t.root,
depthLevel: 0,
depth: []*iteratorLevel{{t.root, nullIdx}}}
if options&TraverseAll == TraverseAll {
return it
}
bti := &bufferedIterator{
options: options,
it: it,
}
return bti
}
func (ti *iterator) checkConcurrentModification() error {
if ti.version == ti.tree.version {
return nil
}
return ErrConcurrentModification
}
func (ti *iterator) HasNext() bool {
return ti != nil && ti.nextNode != nil
}
func (ti *iterator) Next() (Node, error) {
if !ti.HasNext() {
return nil, ErrNoMoreNodes
}
err := ti.checkConcurrentModification()
if err != nil {
return nil, err
}
cur := ti.nextNode
ti.next()
return cur, nil
}
const nullIdx = -1
func nextChild(childIdx int, nullChild *artNode, children []*artNode) ( /*nextChildIdx*/ int /*nextNode*/, *artNode) {
if childIdx == nullIdx {
if nullChild != nil {
return 0, nullChild
}
childIdx = 0
}
for i := childIdx; i < len(children); i++ {
child := children[i]
if child != nil && child != nullChild {
return i + 1, child
}
}
return 0, nil
}
func (ti *iterator) next() {
for {
var nextNode *artNode
nextChildIdx := nullIdx
curNode := ti.depth[ti.depthLevel].node
curChildIdx := ti.depth[ti.depthLevel].childIdx
switch curNode.kind {
case Node4:
nextChildIdx, nextNode = nextChild(curChildIdx, curNode.node().zeroChild, curNode.node4().children[:])
case Node16:
nextChildIdx, nextNode = nextChild(curChildIdx, curNode.node().zeroChild, curNode.node16().children[:])
case Node48:
node := curNode.node48()
nullChild := node.zeroChild
if curChildIdx == nullIdx {
if nullChild == nil {
curChildIdx = 0 // try from 0 based child
} else {
nextChildIdx = 0 // we have a child with null suffix
nextNode = nullChild
break
}
}
for i := curChildIdx; i < len(node.keys); i++ {
// if node.present[i] == 0 {
if node.present[uint16(i)>>n48s]&(1<<(uint16(i)%n48m)) == 0 {
continue
}
child := node.children[node.keys[i]]
if child != nil && child != nullChild {
nextChildIdx = i + 1
nextNode = child
break
}
}
case Node256:
nextChildIdx, nextNode = nextChild(curChildIdx, curNode.node().zeroChild, curNode.node256().children[:])
}
if nextNode == nil {
if ti.depthLevel > 0 {
// return to previous level
ti.depthLevel--
} else {
ti.nextNode = nil // done!
return
}
} else {
// star from the next when we come back from the child node
ti.depth[ti.depthLevel].childIdx = nextChildIdx
ti.nextNode = nextNode
// make sure that we have enough space for levels
if ti.depthLevel+1 >= cap(ti.depth) {
newDepthLevel := make([]*iteratorLevel, ti.depthLevel+2)
copy(newDepthLevel, ti.depth)
ti.depth = newDepthLevel
}
ti.depthLevel++
ti.depth[ti.depthLevel] = &iteratorLevel{nextNode, nullIdx}
return
}
}
}
func (bti *bufferedIterator) HasNext() bool {
for bti.it.HasNext() {
bti.nextNode, bti.err = bti.it.Next()
if bti.err != nil {
return true
}
if bti.options&TraverseLeaf == TraverseLeaf && bti.nextNode.Kind() == Leaf {
return true
} else if bti.options&TraverseNode == TraverseNode && bti.nextNode.Kind() != Leaf {
return true
}
}
bti.nextNode = nil
bti.err = nil
return false
}
func (bti *bufferedIterator) Next() (Node, error) {
return bti.nextNode, bti.err
}