-
Notifications
You must be signed in to change notification settings - Fork 2
/
btree.got
527 lines (449 loc) · 16.3 KB
/
btree.got
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright (c) 2010, Jonathan Wills ([email protected])
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package btree(type T)
import "fmt"
const max int = 22
type Less func(a,b T) bool
type SortedSet interface {
Front() T
Insert(T) bool
Remove(T) bool
Contains(T) bool
Len() int
// Returns a channel that gets passed every element in the set in order, and is then closed.
Data() <-chan T
}
type BTree struct {
node bNode
size int
less Less
}
func (t *BTree) Front() T {
if t.size == 0 { panic("Can't call Front() on an empty tree.") }
return t.node.front()
}
func (t *BTree) Contains(data T) bool {
return t.node.contains(data, t.less)
}
func (t *BTree) Data() <-chan T {
c := make(chan T)
go func() {
t.node.traverse(c)
close(c)
} ()
return c
}
func (t *BTree) Remove(data T) bool {
if t.size == 0 { return false }
if !t.node.remove(data, t.less) { return false }
t.size--
if inode,ok := t.node.(*internalNode); ok {
if inode.lenKeys() == 0 {
t.node = inode.child[0]
}
}
return true
}
func (t *BTree) Len() int {
return t.size
}
func (t *BTree) show() {
t.node.show(0)
}
func (t *BTree) fsck() bool {
return t.node.fsck(0, t.less)
}
func (t *BTree) Insert(data T) bool {
added,spawn,val := t.node.insert(data, t.less)
if !added { return false }
t.size++
if spawn == nil { return true }
root := new(internalNode)
root.child = make([]bNode, max+2)[0:0]
root.key = make([]T, max+1)[0:0]
root.child = root.child[0:2]
root.child[0] = t.node
root.child[1] = spawn
root.key = root.key[0:1]
root.key[0] = val
t.node = root
return true
}
func NewTree(less Less) *BTree {
t := new(BTree)
kn := new(keyNode)
kn.key = make([]T, max+1)[0:0]
t.node = kn
t.less = less
return t
}
type visitor func(bNode) (int,int)
type bNode interface {
// Inserts the value into the appropriate place in the tree, rebalancing as necessary. The first
// return value specifies if the value was actually added (i.e. if it wasn't already there). If a
// new node is created it is returned along with a separator value.
insert(T, Less) (bool, bNode, T)
// Removes the value from the tree, rebalancing as necessary. Returns true iff an element was
// actually deleted.
remove(T, Less) bool
// Returns true iff this tree contains the value.
contains(T, Less) bool
// Returns the lowest element in the tree.
front() T
// Convenient development methods
show(int) // Textual display of a tree
fsck(int, Less) bool // Does a quick sanity check to make sure the tree is in order.
// Grabs all of the data in bNode and merges it into this node. Performs no checks while doing
// so.
merge(bNode)
// Like merge, but additionally adds an additional separator between the keys in the two nodes.
mergeWithSeparator(bNode,T)
// We frequently need to ask about the number of keys in the node, and even though all nodes
// contain keyNode, we still can't access that without either a type check or exposing this
// method.
lenKeys() int
// Passes all of the data in this node and its children through the cannel in proper order.
traverse(chan<- T)
// This is a method that provides a flexible way of traversing and deleting and element from the
// tree. The function returns two elements (traverse, delete), which indicate the index of which
// element should be traversed or deleted. A value of -1 is ignored, and at least one of them
// will always be -1. This allows us to traverse and delete a specific value from the tree, as
// well as deleting the upper or lower bound of a value.
traverseAndDelete(visitor) (bool, T)
// The following methods are used in conjunction with traverseAndDelete
deleteMin() (int,int)
deleteMax() (int,int)
deleteTarget(T, Less) (int,int)
}
type keyNode struct {
key []T
}
type internalNode struct {
keyNode
child []bNode
}
func (node *keyNode) lenKeys() int {
return len(node.key)
}
func (node *keyNode) traverse(c chan<- T) {
for _,v := range node.key {
c <- v
}
}
func (node *internalNode) traverse(c chan<- T) {
for i := range node.key {
node.child[i].traverse(c)
c <- node.key[i]
}
node.child[len(node.child)-1].traverse(c)
}
func (node *keyNode) front() T {
return node.key[0]
}
func (node *internalNode) front() T {
return node.child[0].front()
}
func (node *keyNode) contains(t T, less Less) bool {
_,exists := node.nodeUpperBound(t, less)
return exists
}
func (node *internalNode) contains(t T, less Less) bool {
index,exists := node.nodeUpperBound(t, less)
if exists { return true }
return node.child[index].contains(t, less)
}
func (node *keyNode) deleteKeyAtIndex(index int) {
node.key = node.key[0 : len(node.key)+1] // Even though the space is there, copy won't work
copy(node.key[index:], node.key[index+1:]) // unless we've made room for it. We could also have
node.key = node.key[0 : len(node.key)-2] // just skipped the copy if index == len(node.key)-1
}
// returns (successful,data)
// successful is true iff a value was deleted
// if successful, data is the element that was deleted
func (node *keyNode) traverseAndDelete(traverse_or_delete visitor) (bool, T) {
// in a leaf we are no longer traversing, so we just call the delete function
traverse, delete := traverse_or_delete(node)
if traverse >= 0 { panic("Cannot traverse past a leaf.") }
if delete < 0 { return false, node.key[0] }
data := node.key[delete]
node.deleteKeyAtIndex(delete)
return true, data
}
func (node *internalNode) deleteChildAtIndex(index int) {
node.child = node.child[0 : len(node.child)+1]
copy(node.child[index:], node.child[index+1:])
node.child = node.child[0 : len(node.child)-2]
}
func (node *internalNode) rebalance(index int) bool {
if node.child[index].lenKeys() >= max/2 { return false }
if index > 0 && node.child[index-1].lenKeys() > max/2 {
// Our target child's left sibling has extra elements, so we'll take one of those
// fmt.Printf("RotateRight around %d\n", node.key[index])
node.rotateRight(index)
return false
}
// fmt.Printf("%d %d %d\n", index, len(node.key), node.child[index+1].lenKeys())
if index < len(node.key) && node.child[index+1].lenKeys() > max/2 {
node.rotateLeft(index)
return false
}
// Neither sibling had any elements we could steal, so we are going to merge two nodes together.
// We know there will be room for this because if there isn't that means a sibling has an extra
// element, and we already would have gotten it.
low_index := index
if index > 0 {
low_index = low_index-1
}
node.child[low_index].mergeWithSeparator(node.child[low_index+1], node.key[low_index])
node.deleteKeyAtIndex(low_index)
node.deleteChildAtIndex(low_index+1)
return true
}
func deleteMax(node bNode) (int,int) { return node.deleteMax() }
func (node *internalNode) deleteMax() (int,int) { return len(node.child) - 1, -1 }
func (node *keyNode) deleteMax() (int,int) { return -1, len(node.key) - 1 }
func deleteMin(node bNode) (int,int) { return node.deleteMin() }
func (node *internalNode) deleteMin() (int,int) { return 0, -1 }
func (node *keyNode) deleteMin() (int,int) { return -1, 0 }
func (node *internalNode) traverseAndDelete(traverse_or_delete visitor) (bool, T) {
traverse, delete := traverse_or_delete(node)
if traverse >= 0 && delete >= 0 { panic("Can't delete and traverse at the same node.") }
if traverse < 0 && delete < 0 { return false, node.key[0] }
if traverse >= 0 {
deleted, data := node.child[traverse].traverseAndDelete(traverse_or_delete)
node.rebalance(traverse)
return deleted, data
}
if delete >= 0 {
data := node.key[delete]
if node.child[delete].lenKeys() + node.child[delete+1].lenKeys() == max {
_, val := node.child[delete].traverseAndDelete(deleteMax)
if node.child[delete].lenKeys() >= max/2 {
node.key[delete] = val
} else {
node.child[delete].mergeWithSeparator(node.child[delete+1], val)
node.deleteKeyAtIndex(delete)
node.deleteChildAtIndex(delete+1)
}
return true,data
}
if node.child[delete].lenKeys() > max/2 {
_, val := node.child[delete].traverseAndDelete(deleteMax)
node.key[delete] = val
} else {
_, val := node.child[delete+1].traverseAndDelete(deleteMin)
node.key[delete] = val
}
return true,data
}
return false, node.key[0]
}
// Utility methods *********************************************************************************
// Returns the index of the smallest value that is not less than t, and whether or not it equals t
// If there are no elements which t is smaller than it returns (len(node.key), false)
func (node *keyNode) nodeUpperBound(t T, less Less) (int,bool) {
if len(node.key) == 0 { return 0, false }
low,high := 0,len(node.key)
for high-low > 8 {
mid := (high+low) / 2
if less(t, node.key[mid]) {
high = mid
} else {
low = mid
}
}
for i := low; i < high; i++ {
if !less(node.key[i], t) {
return i, !less(t, node.key[i])
}
}
return high, false
}
func (node *keyNode) merge(_sib bNode) {
sib := _sib.(*keyNode)
node.key = node.key[0 : len(node.key) + len(sib.key)]
copy(node.key[len(node.key)-len(sib.key):], sib.key)
}
// Rotates right such that the child at the specified index will have one more element, and the
// child to its left will have one less.
func (node *internalNode) rotateRight(index int) {
separator := node.key[index-1]
switch target := node.child[index].(type) {
case *keyNode:
source := node.child[index-1].(*keyNode)
node.key[index-1] = source.key[len(source.key)-1]
source.key = source.key[0 : len(source.key)-1]
target.key = target.key[0 : len(target.key)+1]
copy(target.key[1:], target.key[0:])
target.key[0] = separator
case *internalNode:
source := node.child[index-1].(*internalNode)
node.key[index-1] = source.key[len(source.key)-1] // These five lines are an exact
source.key = source.key[0 : len(source.key)-1] // copy of the ones above.
target.key = target.key[0 : len(target.key)+1] // TODO: Remove this duplication
copy(target.key[1:], target.key[0:])
target.key[0] = separator
target.child = target.child[0 : len(target.child)+1]
copy(target.child[1:], target.child[0:])
target.child[0] = source.child[len(source.child)-1]
source.child = source.child[0 : len(source.child)-1]
default:
panic("Undefined node type.")
}
}
// Rotates left such that the child at the specified index will have one more element, and the
// child to its right will have one less.
func (node *internalNode) rotateLeft(index int) {
separator := node.key[index]
switch target := node.child[index].(type) {
case *keyNode:
source := node.child[index+1].(*keyNode)
node.key[index] = source.key[0]
copy(source.key[0:], source.key[1:])
source.key = source.key[0 : len(source.key)-1]
target.key = target.key[0 : len(target.key)+1]
target.key[len(target.key)-1] = separator
case *internalNode:
source := node.child[index+1].(*internalNode)
node.key[index] = source.key[0]
copy(source.key[0:], source.key[1:])
source.key = source.key[0 : len(source.key)-1]
target.key = target.key[0 : len(target.key)+1]
target.key[len(target.key)-1] = separator
target.child = target.child[0 : len(target.child)+1]
target.child[len(target.child)-1] = source.child[0]
copy(source.child[0:], source.child[1:])
source.child = source.child[0 : len(source.child)-1]
default:
panic("Undefined node type.")
}
}
func (node *internalNode) merge(_sib bNode) {
sib := _sib.(*internalNode)
node.key = node.key[0 : len(node.key) + len(sib.key)]
copy(node.key[len(node.key)-len(sib.key):], sib.key)
node.child = node.child[0 : len(node.child) + len(sib.child)]
copy(node.child[len(node.child)-len(sib.child):], sib.child)
}
func (node *internalNode) mergeWithSeparator(sib bNode, separator T) {
node.key = node.key[0 : len(node.key) + 1]
node.key[len(node.key)-1] = separator
node.merge(sib)
}
func (node *keyNode) mergeWithSeparator(sib bNode, separator T) {
node.key = node.key[0 : len(node.key) + 1]
node.key[len(node.key)-1] = separator
node.merge(sib)
}
func (node *keyNode) deleteTarget(t T, less Less) (int,int) {
index, exists := node.nodeUpperBound(t, less)
if !exists { index = -1}
return -1, index
}
func (node *internalNode) deleteTarget(t T, less Less) (int,int) {
index, exists := node.nodeUpperBound(t, less)
if exists { return -1, index }
return index, -1
}
func deleteTargetFunc(t T, less Less) func(bNode) (int,int) {
return func(node bNode) (int,int) {
return node.deleteTarget(t, less)
}
}
func (node *internalNode) remove(t T, less Less) bool {
deleted,_ := node.traverseAndDelete(deleteTargetFunc(t, less))
return deleted
}
func (node *keyNode) remove(t T, less Less) bool {
deleted,_ := node.traverseAndDelete(deleteTargetFunc(t, less))
return deleted
}
// Inserts *****************************************************************************************
func (node *keyNode) insert(t T, less Less) (bool, bNode, T) {
index, exists := node.nodeUpperBound(t, less)
if exists { return false, nil, node.key[0] }
node.key = node.key[0 : len(node.key) + 1] // Make space in the key array
copy(node.key[index+1:], node.key[index:]) // Shift existing data out of the way
node.key[index] = t // Add our new datum in its place
if len(node.key) <= max {
return true,nil,node.key[0]
}
// We have too many elements, so we need to split
spawn := new(keyNode)
spawn.key = make([]T, max+1)[0:max/2]
copy(spawn.key[0:], node.key[max/2+1:])
median := node.key[max/2]
node.key = node.key[0:max/2]
return true,spawn,median
}
func (node *internalNode) insert(t T, less Less) (bool, bNode, T) {
index, exists := node.nodeUpperBound(t, less)
if exists { return false, nil, node.key[0] }
added,infant,val := node.child[index].insert(t, less)
if infant == nil { return added,nil,node.key[0] }
// A child node split when we added t, so we have to add in the new node
node.key = node.key[0 : len(node.key)+1]
copy(node.key[index+1:], node.key[index:])
node.key[index] = val
node.child = node.child[0 : len(node.child)+1]
copy(node.child[index+1:], node.child[index:])
node.child[index+1] = infant
if len(node.key) <= max { return true,nil,node.key[0] }
// Now this node is too full, so we have to split
spawn := new(internalNode)
spawn.key = make([]T, max+1)[0:max/2]
spawn.child = make([]bNode, max+2)[0:max/2+1]
copy(spawn.key, node.key[max/2+1:])
copy(spawn.child, node.child[max/2+1:])
median := node.key[max/2]
node.key = node.key[0:max/2]
node.child = node.child[0:max/2+1]
return true,spawn,median
}
// Development and testing utilities ***************************************************************
func (node *keyNode) show(depth int) {
for i := 0; i < depth*2; i++ {
fmt.Printf(" ")
}
fmt.Printf("%v\n", node.key)
}
func (node *internalNode) show(depth int) {
for i := range node.key {
node.child[i].show(depth+1)
for i := 0; i < depth*2; i++ {
fmt.Printf(" ")
}
fmt.Printf("%d\n", node.key[i])
}
node.child[len(node.child)-1].show(depth+1)
}
func (node *keyNode) fsck(depth int, less Less) bool {
if len(node.key) < max/2 && depth > 0 { return false }
for i := 0; i < len(node.key) - 1; i++ {
if !less(node.key[i], node.key[i+1]) { return false }
}
return true
}
func (node *internalNode) fsck(depth int, less Less) bool {
if len(node.key) < max/2 && depth > 0 { return false }
for i := range node.child {
if !node.child[i].fsck(depth+1, less) { return false }
}
for i := 0; i < len(node.key) - 1; i++ {
if !less(node.key[i], node.key[i+1]) { return false }
if inode,ok := node.child[i].(*internalNode); ok {
if less(node.key[i], inode.key[len(inode.key)-1]) { return false }
}
if lnode,ok := node.child[i].(*keyNode); ok {
if less(node.key[i], lnode.key[len(lnode.key)-1]) { return false }
}
if inode,ok := node.child[i+1].(*internalNode); ok {
if less(inode.key[len(inode.key)-1], node.key[i]) { return false }
}
if lnode,ok := node.child[i+1].(*keyNode); ok {
if less(lnode.key[len(lnode.key)-1], node.key[i]) { return false }
}
}
return true
}