-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
238 lines (200 loc) · 5.72 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
// tree - adaptive radix tree type.
type tree struct {
root *artNode
size int64
}
// newArt returns art with 0 nodes.
func newArt() *tree {
return &tree{root: nil, size: 0}
}
// Search returns the node that contains the passed in key, or nil if not found.
func (t *tree) Search(key Key) Value {
return t.searchHelper(t.root, key, 0)
}
// searchHelper is a helper function for Search.
func (t *tree) searchHelper(current *artNode, key []byte, depth int) interface{} {
for current != nil {
if current.isLeaf() {
if current.isMatch(key) {
return current.leafNode().value
}
return nil
}
if current.prefixMismatch(key, depth) != current.node().prefixLen {
return nil
}
depth += current.node().prefixLen
var keyChar byte
if depth < 0 || depth >= len(key) {
keyChar = byte(0)
} else {
keyChar = key[depth]
}
current = *(current.findChild(keyChar))
depth++
}
return nil
}
// Insert inserts the passed in value that is indexed by the passed in key into the tree.
func (t *tree) Insert(key Key, value Value) {
t.insertHelper(&t.root, key, value, 0)
}
// insertHelper is a helper function for Insert.
func (t *tree) insertHelper(currentRef **artNode, key []byte, value interface{}, depth int) {
if *currentRef == nil {
*currentRef = newLeafNode(key, value)
t.size++
return
}
current := *currentRef
if current.isLeaf() {
// NOTE: Currently, overwrite if the key matches.
if current.isMatch(key) {
current.leafNode().value = value
return
}
newNode4 := newNode4()
newLeafNode := newLeafNode(key, value)
limit := current.longestCommonPrefix(newLeafNode, depth)
newNode4.node().prefixLen = limit
memcpy(newNode4.node().prefix[:], key[depth:], min(newNode4.node().prefixLen, maxPrefixLen))
if depth+newNode4.node().prefixLen < 0 || depth+newNode4.node().prefixLen >= len(current.leafNode().key) {
newNode4.addChild(0, current)
} else {
newNode4.addChild(current.leafNode().key[depth+newNode4.node().prefixLen], current)
}
if depth+newNode4.node().prefixLen < 0 || depth+newNode4.node().prefixLen >= len(key) {
newNode4.addChild(0, newLeafNode)
} else {
newNode4.addChild(key[depth+newNode4.node().prefixLen], newLeafNode)
}
*currentRef = newNode4
t.size++
return
}
node := current.node()
if node.prefixLen != 0 {
mismatch := current.prefixMismatch(key, depth)
if mismatch != node.prefixLen {
newNode4 := newNode4()
*currentRef = newNode4
newNode4.node().prefixLen = mismatch
memcpy(newNode4.node().prefix[:], node.prefix[:], mismatch)
if node.prefixLen < maxPrefixLen {
newNode4.addChild(node.prefix[mismatch], current)
node.prefixLen -= mismatch + 1
memmove(node.prefix[:], node.prefix[mismatch+1:], min(node.prefixLen, maxPrefixLen))
} else {
node.prefixLen -= mismatch + 1
minKey := current.minimum().leafNode().key
newNode4.addChild(minKey[depth+mismatch], current)
memmove(node.prefix[:], minKey[depth+mismatch+1:], min(node.prefixLen, maxPrefixLen))
}
newLeafNode := newLeafNode(key, value)
newNode4.addChild(key[depth+mismatch], newLeafNode)
t.size++
return
}
depth += node.prefixLen
}
next := current.findChild(key[depth])
if *next != nil {
t.insertHelper(next, key, value, depth+1)
} else {
current.addChild(key[depth], newLeafNode(key, value))
t.size++
}
}
// Delete deletes the child of the passed in key.
func (t *tree) Delete(key []byte) bool {
return t.deleteHelper(&t.root, key, 0)
}
// deleteHelper is a helper function of Delete.
func (t *tree) deleteHelper(currentRef **artNode, key []byte, depth int) bool {
if t == nil || *currentRef == nil || len(key) == 0 {
return false
}
current := *currentRef
if current.isLeaf() {
if current.isMatch(key) {
*currentRef = nil
t.size--
return true
}
}
if current.node().prefixLen != 0 {
mismatch := current.prefixMismatch(key, depth)
if mismatch != current.node().prefixLen {
return false
}
depth += current.node().prefixLen
}
var keyChar byte
if depth < 0 || depth >= len(key) {
keyChar = byte(0)
} else {
keyChar = key[depth]
}
next := current.findChild(keyChar)
if *next != nil && (*next).isLeaf() && (*next).isMatch(key) {
current.RemoveChild(keyChar)
t.size--
return true
}
return t.deleteHelper(next, key, depth+1)
}
// Each iterate the whole tree with the lexicographical order,
// and will call the given callback for each tree node.
func (t *tree) Each(callback Callback) {
t.eachHelper(t.root, callback)
}
// Size returns the number of leafNodes (key-value) in the tree.
func (t *tree) Size() int {
return int(t.size)
}
// eachHelper is a helper function of Each.
func (t *tree) eachHelper(current *artNode, callback Callback) {
if current == nil {
return
}
callback(current)
switch current.nodeType {
case Node4:
t.eachChildren(current.node4().children[:], callback)
case Node16:
t.eachChildren(current.node16().children[:], callback)
case Node48:
node := current.node48()
for _, i := range node.keys {
if i > 0 {
next := current.node48().children[i]
if next != nil {
t.eachHelper(next, callback)
}
}
}
case Node256:
t.eachChildren(current.node256().children[:], callback)
}
}
// eachChildren is used by eachHelper to iterate children of artNode.
func (t *tree) eachChildren(children []*artNode, callback Callback) {
for _, child := range children {
if child != nil {
t.eachHelper(child, callback)
}
}
}
// memcpy copies numBytes bytes from src to dst.
func memcpy(dst []byte, src []byte, numBytes int) {
for i := 0; i < numBytes && i < len(src) && i < len(dst); i++ {
dst[i] = src[i]
}
}
// memmove moves numBytes bytes from src to dst.
func memmove(dst []byte, src []byte, numBytes int) {
for i := 0; i < numBytes; i++ {
dst[i] = src[i]
}
}