forked from plar/go-adaptive-radix-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
238 lines (188 loc) · 5.22 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
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
package art
type tree struct {
// version field is updated by each tree modification
version int
root *artNode
size int
}
// make sure that tree implements all methods from the Tree interface
var _ Tree = &tree{}
func (t *tree) Insert(key Key, value Value) (Value, bool) {
oldValue, updated := t.recursiveInsert(&t.root, key, value, 0)
if !updated {
t.version++
t.size++
}
return oldValue, updated
}
func (t *tree) Delete(key Key) (Value, bool) {
value, deleted := t.recursiveDelete(&t.root, key, 0)
if deleted {
t.version++
t.size--
return value, true
}
return nil, false
}
func (t *tree) Search(key Key) (Value, bool) {
current := t.root
depth := uint32(0)
for current != nil {
if current.isLeaf() {
leaf := current.leaf()
if leaf.match(key) {
return leaf.value, true
}
return nil, false
}
curNode := current.node()
if curNode.prefixLen > 0 {
prefixLen := current.match(key, depth)
if prefixLen != min(curNode.prefixLen, MaxPrefixLen) {
return nil, false
}
depth += curNode.prefixLen
}
next := current.findChild(key.charAt(int(depth)), key.valid(int(depth)))
if *next != nil {
current = *next
} else {
current = nil
}
depth++
}
return nil, false
}
func (t *tree) Minimum() (value Value, found bool) {
if t == nil || t.root == nil {
return nil, false
}
leaf := t.root.minimum()
return leaf.value, true
}
func (t *tree) Maximum() (value Value, found bool) {
if t == nil || t.root == nil {
return nil, false
}
leaf := t.root.maximum()
return leaf.value, true
}
func (t *tree) Size() int {
if t == nil || t.root == nil {
return 0
}
return t.size
}
func (t *tree) recursiveInsert(curNode **artNode, key Key, value Value, depth uint32) (Value, bool) {
current := *curNode
if current == nil {
replaceRef(curNode, factory.newLeaf(key, value))
return nil, false
}
if current.isLeaf() {
leaf := current.leaf()
// update exists value
if leaf.match(key) {
oldValue := leaf.value
leaf.value = value
return oldValue, true
}
// new value, split the leaf into new node4
newLeaf := factory.newLeaf(key, value)
leaf2 := newLeaf.leaf()
leafsLCP := t.longestCommonPrefix(leaf, leaf2, depth)
newNode := factory.newNode4()
newNode.setPrefix(key[depth:], leafsLCP)
depth += leafsLCP
newNode.addChild(leaf.key.charAt(int(depth)), leaf.key.valid(int(depth)), current)
newNode.addChild(leaf2.key.charAt(int(depth)), leaf2.key.valid(int(depth)), newLeaf)
replaceRef(curNode, newNode)
return nil, false
}
node := current.node()
if node.prefixLen > 0 {
prefixMismatchIdx := current.matchDeep(key, depth)
if prefixMismatchIdx >= node.prefixLen {
depth += node.prefixLen
goto NEXT_NODE
}
newNode := factory.newNode4()
node4 := newNode.node()
node4.prefixLen = prefixMismatchIdx
for i := 0; i < int(min(prefixMismatchIdx, MaxPrefixLen)); i++ {
node4.prefix[i] = node.prefix[i]
}
if node.prefixLen <= MaxPrefixLen {
node.prefixLen -= (prefixMismatchIdx + 1)
newNode.addChild(node.prefix[prefixMismatchIdx], true, current)
for i, limit := uint32(0), min(node.prefixLen, MaxPrefixLen); i < limit; i++ {
node.prefix[i] = node.prefix[prefixMismatchIdx+i+1]
}
} else {
node.prefixLen -= (prefixMismatchIdx + 1)
leaf := current.minimum()
newNode.addChild(leaf.key.charAt(int(depth+prefixMismatchIdx)), leaf.key.valid(int(depth+prefixMismatchIdx)), current)
for i, limit := uint32(0), min(node.prefixLen, MaxPrefixLen); i < limit; i++ {
node.prefix[i] = leaf.key[depth+prefixMismatchIdx+i+1]
}
}
// Insert the new leaf
newNode.addChild(key.charAt(int(depth+prefixMismatchIdx)), key.valid(int(depth+prefixMismatchIdx)), factory.newLeaf(key, value))
replaceRef(curNode, newNode)
return nil, false
}
NEXT_NODE:
// Find a child to recursive to
next := current.findChild(key.charAt(int(depth)), key.valid(int(depth)))
if *next != nil {
return t.recursiveInsert(next, key, value, depth+1)
}
// No Child, artNode goes with us
current.addChild(key.charAt(int(depth)), key.valid(int(depth)), factory.newLeaf(key, value))
return nil, false
}
func (t *tree) recursiveDelete(curNode **artNode, key Key, depth uint32) (Value, bool) {
if t == nil || *curNode == nil || len(key) == 0 {
return nil, false
}
current := *curNode
if current.isLeaf() {
leaf := current.leaf()
if leaf.match(key) {
replaceRef(curNode, nil)
return leaf.value, true
}
return nil, false
}
node := current.node()
if node.prefixLen > 0 {
prefixLen := current.match(key, depth)
if prefixLen != min(node.prefixLen, MaxPrefixLen) {
return nil, false
}
depth += node.prefixLen
}
next := current.findChild(key.charAt(int(depth)), key.valid(int(depth)))
if *next == nil {
return nil, false
}
if (*next).isLeaf() {
leaf := (*next).leaf()
if leaf.match(key) {
current.deleteChild(key.charAt(int(depth)), key.valid(int(depth)))
return leaf.value, true
}
return nil, false
}
return t.recursiveDelete(next, key, depth+1)
}
func (t *tree) longestCommonPrefix(l1 *leaf, l2 *leaf, depth uint32) uint32 {
l1key, l2key := l1.key, l2.key
idx, limit := depth, min(uint32(len(l1key)), uint32(len(l2key)))
for ; idx < limit; idx++ {
if l1key[idx] != l2key[idx] {
break
}
}
return idx - depth
}