/
node64s.go
357 lines (281 loc) · 6.6 KB
/
node64s.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
353
354
355
356
357
package iptree8
// !!!DON'T EDIT!!! Generated by infobloxopen/go-trees/etc from <name>tree{{.bits}} with etc -s uint8 -d uintX.yaml -t ./<name>tree\{\{.bits\}\}
import (
"fmt"
"math/bits"
)
// node64s is an element of radix tree with 64-bit unsigned integer as a key.
type node64s struct {
// key stores key for current node.
key uint64
// bits is a number of significant bits in key.
bits uint8
// leaf indicates if the node is leaf node and contains any data in value.
leaf bool
// value contains data associated with key.
value *node64
chld [2]*node64s
}
// Dot dumps tree to Graphviz .dot format
func (n *node64s) Dot() string {
body := ""
i := 0
queue := []*node64s{n}
for len(queue) > 0 {
c := queue[0]
body += fmt.Sprintf("N%d %s\n", i, c.dotString())
if c != nil && (c.chld[0] != nil || c.chld[1] != nil) {
body += fmt.Sprintf("N%d -> { N%d N%d }\n", i, i+len(queue), i+len(queue)+1)
queue = append(append(queue, c.chld[0]), c.chld[1])
}
queue = queue[1:]
i++
}
return "digraph d {\n" + body + "}\n"
}
// Insert puts new leaf to radix tree and returns pointer to new root. The method uses copy on write strategy so old root doesn't see the change.
func (n *node64s) Insert(key uint64, bits int, value *node64) *node64s {
if bits < 0 {
bits = 0
} else if bits > key64BitSize {
bits = key64BitSize
}
return n.insert(newNode64s(key, uint8(bits), true, value))
}
// InplaceInsert puts new leaf to radix tree (or replaces value in existing one). The method inserts data directly to current tree so make sure you have exclusive access to it.
func (n *node64s) InplaceInsert(key uint64, bits int, value *node64) *node64s {
// Adjust bits.
if bits < 0 {
bits = 0
} else if bits > key64BitSize {
bits = key64BitSize
}
return n.inplaceInsert(key, uint8(bits), value)
}
// Enumerate returns channel which is populated by nodes in order of their keys.
func (n *node64s) Enumerate() chan *node64s {
ch := make(chan *node64s)
go func() {
defer close(ch)
if n == nil {
return
}
n.enumerate(ch)
}()
return ch
}
// Match locates node which key is equal to or "contains" the key passed as argument.
func (n *node64s) Match(key uint64, bits int) (*node64, bool) {
if n == nil {
return nil, false
}
if bits < 0 {
bits = 0
} else if bits > key64BitSize {
bits = key64BitSize
}
r := n.match(key, uint8(bits))
if r == nil {
return nil, false
}
return r.value, true
}
// ExactMatch locates node which exactly matches given key.
func (n *node64s) ExactMatch(key uint64, bits int) (*node64, bool) {
if n == nil {
return nil, false
}
if bits < 0 {
bits = 0
} else if bits > key64BitSize {
bits = key64BitSize
}
r := n.exactMatch(key, uint8(bits))
if r == nil {
return nil, false
}
return r.value, true
}
// Delete removes subtree which is contained by given key. The method uses copy on write strategy.
func (n *node64s) Delete(key uint64, bits int) (*node64s, bool) {
if n == nil {
return n, false
}
if bits < 0 {
bits = 0
} else if bits > key64BitSize {
bits = key64BitSize
}
return n.del(key, uint8(bits))
}
func (n *node64s) dotString() string {
if n == nil {
return "[label=\"nil\"]"
}
if n.leaf {
return fmt.Sprintf("[label=\"k: %016x, b: %d, v: \\\"%p\\\"\"]", n.key, n.bits, n.value)
}
return fmt.Sprintf("[label=\"k: %016x, b: %d\"]", n.key, n.bits)
}
func (n *node64s) insert(c *node64s) *node64s {
if n == nil {
return c
}
bits := uint8(bits.LeadingZeros64((n.key ^ c.key) | ^masks64[n.bits] | ^masks64[c.bits]))
if bits < n.bits {
branch := (n.key >> (key64BitSize - 1 - bits)) & 1
if bits == c.bits {
c.chld[branch] = n
return c
}
m := newNode64s(c.key&masks64[bits], bits, false, nil)
m.chld[branch] = n
m.chld[1-branch] = c
return m
}
if c.bits == n.bits {
c.chld = n.chld
return c
}
m := newNode64s(n.key, n.bits, n.leaf, n.value)
m.chld = n.chld
branch := (c.key >> (key64BitSize - 1 - bits)) & 1
m.chld[branch] = m.chld[branch].insert(c)
return m
}
func (n *node64s) inplaceInsert(key uint64, sbits uint8, value *node64) *node64s {
var (
p *node64s
branch uint64
)
r := n
for n != nil {
cbits := uint8(bits.LeadingZeros64((n.key ^ key) | ^masks64[n.bits] | ^masks64[sbits]))
if cbits < n.bits {
pBranch := branch
branch = (n.key >> (key64BitSize - 1 - cbits)) & 1
var m *node64s
if cbits == sbits {
m = newNode64s(key, sbits, true, value)
m.chld[branch] = n
} else {
m = newNode64s(key&masks64[cbits], cbits, false, nil)
m.chld[1-branch] = newNode64s(key, sbits, true, value)
}
m.chld[branch] = n
if p == nil {
r = m
} else {
p.chld[pBranch] = m
}
return r
}
if sbits == n.bits {
n.key = key
n.leaf = true
n.value = value
return r
}
p = n
branch = (key >> (key64BitSize - 1 - cbits)) & 1
n = n.chld[branch]
}
n = newNode64s(key, sbits, true, value)
if p == nil {
return n
}
p.chld[branch] = n
return r
}
func (n *node64s) enumerate(ch chan *node64s) {
if n.leaf {
ch <- n
}
if n.chld[0] != nil {
n.chld[0].enumerate(ch)
}
if n.chld[1] != nil {
n.chld[1].enumerate(ch)
}
}
func (n *node64s) match(key uint64, bits uint8) *node64s {
if n.bits > bits {
return nil
}
if n.bits == bits {
if n.leaf && (n.key^key)&masks64[n.bits] == 0 {
return n
}
return nil
}
if (n.key^key)&masks64[n.bits] != 0 {
return nil
}
c := n.chld[(key>>(key64BitSize-1-n.bits))&1]
if c != nil {
r := c.match(key, bits)
if r != nil {
return r
}
}
if n.leaf {
return n
}
return nil
}
func (n *node64s) exactMatch(key uint64, bits uint8) *node64s {
if n.bits > bits {
return nil
}
if n.bits == bits {
if n.leaf && (n.key^key)&masks64[n.bits] == 0 {
return n
}
return nil
}
if (n.key^key)&masks64[n.bits] != 0 {
return nil
}
c := n.chld[(key>>(key64BitSize-1-n.bits))&1]
if c != nil {
r := c.exactMatch(key, bits)
if r != nil {
return r
}
}
return nil
}
func (n *node64s) del(key uint64, bits uint8) (*node64s, bool) {
if bits <= n.bits {
if (n.key^key)&masks64[bits] == 0 {
return nil, true
}
return n, false
}
if (n.key^key)&masks64[n.bits] != 0 {
return n, false
}
branch := (key >> (key64BitSize - 1 - n.bits)) & 1
c := n.chld[branch]
if c == nil {
return n, false
}
c, ok := c.del(key, bits)
if !ok {
return n, false
}
if c == nil && !n.leaf {
return n.chld[1-branch], true
}
m := newNode64s(n.key, n.bits, n.leaf, n.value)
m.chld = n.chld
m.chld[branch] = c
return m, true
}
func newNode64s(key uint64, bits uint8, leaf bool, value *node64) *node64s {
return &node64s{
key: key,
bits: bits,
leaf: leaf,
value: value}
}