|
@@ -4,9 +4,11 @@
|
4
|
4
|
|
5
|
5
|
package btree
|
6
|
6
|
|
7
|
|
-import "sync"
|
|
7
|
+import (
|
|
8
|
+ "sync"
|
|
9
|
+)
|
8
|
10
|
|
9
|
|
-const maxItems = 255
|
|
11
|
+const maxItems = 255 // max items per node. max children is +1
|
10
|
12
|
const minItems = maxItems * 40 / 100
|
11
|
13
|
|
12
|
14
|
type cow struct {
|
|
@@ -17,18 +19,19 @@ type node struct {
|
17
|
19
|
cow *cow
|
18
|
20
|
leaf bool
|
19
|
21
|
numItems int16
|
|
22
|
+ count int
|
20
|
23
|
items [maxItems]interface{}
|
21
|
24
|
children *[maxItems + 1]*node
|
22
|
25
|
}
|
23
|
26
|
|
24
|
27
|
// BTree is an ordered set items
|
25
|
28
|
type BTree struct {
|
26
|
|
- mu *sync.RWMutex
|
27
|
|
- cow *cow
|
28
|
|
- root *node
|
29
|
|
- length int
|
30
|
|
- less func(a, b interface{}) bool
|
31
|
|
- lnode *node
|
|
29
|
+ mu *sync.RWMutex
|
|
30
|
+ cow *cow
|
|
31
|
+ root *node
|
|
32
|
+ count int
|
|
33
|
+ less func(a, b interface{}) bool
|
|
34
|
+ locks bool
|
32
|
35
|
}
|
33
|
36
|
|
34
|
37
|
func (tr *BTree) newNode(leaf bool) *node {
|
|
@@ -43,17 +46,32 @@ func (tr *BTree) newNode(leaf bool) *node {
|
43
|
46
|
// PathHint is a utility type used with the *Hint() functions. Hints provide
|
44
|
47
|
// faster operations for clustered keys.
|
45
|
48
|
type PathHint struct {
|
|
49
|
+ used [8]bool
|
46
|
50
|
path [8]uint8
|
47
|
51
|
}
|
48
|
52
|
|
49
|
53
|
// New returns a new BTree
|
50
|
54
|
func New(less func(a, b interface{}) bool) *BTree {
|
|
55
|
+ return newBTree(less, true)
|
|
56
|
+}
|
|
57
|
+
|
|
58
|
+// NewNonConcurrent returns a new BTree which is not safe for concurrent
|
|
59
|
+// write operations by multiple goroutines.
|
|
60
|
+//
|
|
61
|
+// This is useful for when you do not need the BTree to manage the locking,
|
|
62
|
+// but would rather do it yourself.
|
|
63
|
+func NewNonConcurrent(less func(a, b interface{}) bool) *BTree {
|
|
64
|
+ return newBTree(less, false)
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+func newBTree(less func(a, b interface{}) bool, locks bool) *BTree {
|
51
|
68
|
if less == nil {
|
52
|
69
|
panic("nil less")
|
53
|
70
|
}
|
54
|
71
|
tr := new(BTree)
|
55
|
72
|
tr.mu = new(sync.RWMutex)
|
56
|
73
|
tr.less = less
|
|
74
|
+ tr.locks = locks
|
57
|
75
|
return tr
|
58
|
76
|
}
|
59
|
77
|
|
|
@@ -68,18 +86,32 @@ func (n *node) find(key interface{}, less func(a, b interface{}) bool,
|
68
|
86
|
) (index int16, found bool) {
|
69
|
87
|
low := int16(0)
|
70
|
88
|
high := n.numItems - 1
|
71
|
|
- if hint != nil && depth < 8 {
|
|
89
|
+ if hint != nil && depth < 8 && hint.used[depth] {
|
72
|
90
|
index = int16(hint.path[depth])
|
73
|
|
- if index > n.numItems-1 {
|
|
91
|
+ if index >= n.numItems {
|
|
92
|
+ // tail item
|
|
93
|
+ if less(n.items[n.numItems-1], key) {
|
|
94
|
+ if less(key, n.items[n.numItems-1]) {
|
|
95
|
+ index = n.numItems - 1
|
|
96
|
+ found = true
|
|
97
|
+ goto path_match
|
|
98
|
+ } else {
|
|
99
|
+ index = n.numItems
|
|
100
|
+ goto path_match
|
|
101
|
+ }
|
|
102
|
+ }
|
74
|
103
|
index = n.numItems - 1
|
75
|
104
|
}
|
76
|
105
|
if less(key, n.items[index]) {
|
|
106
|
+ if index == 0 || less(n.items[index-1], key) {
|
|
107
|
+ goto path_match
|
|
108
|
+ }
|
77
|
109
|
high = index - 1
|
78
|
110
|
} else if less(n.items[index], key) {
|
79
|
111
|
low = index + 1
|
80
|
112
|
} else {
|
81
|
113
|
found = true
|
82
|
|
- goto done
|
|
114
|
+ goto path_match
|
83
|
115
|
}
|
84
|
116
|
}
|
85
|
117
|
for low <= high {
|
|
@@ -97,13 +129,16 @@ func (n *node) find(key interface{}, less func(a, b interface{}) bool,
|
97
|
129
|
index = low
|
98
|
130
|
found = false
|
99
|
131
|
}
|
100
|
|
-done:
|
101
|
|
- if hint != nil && depth < 8 {
|
102
|
|
- if n.leaf && found {
|
103
|
|
- hint.path[depth] = byte(index + 1)
|
104
|
|
- } else {
|
105
|
|
- hint.path[depth] = byte(index)
|
106
|
|
- }
|
|
132
|
+ if hint == nil || depth >= 8 {
|
|
133
|
+ return index, found
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+path_match:
|
|
137
|
+ hint.used[depth] = true
|
|
138
|
+ if n.leaf && found {
|
|
139
|
+ hint.path[depth] = byte(index + 1)
|
|
140
|
+ } else {
|
|
141
|
+ hint.path[depth] = byte(index)
|
107
|
142
|
}
|
108
|
143
|
return index, found
|
109
|
144
|
}
|
|
@@ -113,10 +148,10 @@ func (tr *BTree) SetHint(item interface{}, hint *PathHint) (prev interface{}) {
|
113
|
148
|
if item == nil {
|
114
|
149
|
panic("nil item")
|
115
|
150
|
}
|
116
|
|
- tr.mu.Lock()
|
117
|
|
- prev = tr.setHint(item, hint)
|
118
|
|
- tr.mu.Unlock()
|
119
|
|
- return prev
|
|
151
|
+ if tr.lock() {
|
|
152
|
+ defer tr.unlock()
|
|
153
|
+ }
|
|
154
|
+ return tr.setHint(item, hint)
|
120
|
155
|
}
|
121
|
156
|
|
122
|
157
|
func (tr *BTree) setHint(item interface{}, hint *PathHint) (prev interface{}) {
|
|
@@ -124,14 +159,14 @@ func (tr *BTree) setHint(item interface{}, hint *PathHint) (prev interface{}) {
|
124
|
159
|
tr.root = tr.newNode(true)
|
125
|
160
|
tr.root.items[0] = item
|
126
|
161
|
tr.root.numItems = 1
|
127
|
|
- tr.length = 1
|
|
162
|
+ tr.root.count = 1
|
|
163
|
+ tr.count = 1
|
128
|
164
|
return
|
129
|
165
|
}
|
130
|
166
|
prev = tr.nodeSet(&tr.root, item, tr.less, hint, 0)
|
131
|
167
|
if prev != nil {
|
132
|
168
|
return prev
|
133
|
169
|
}
|
134
|
|
- tr.lnode = nil
|
135
|
170
|
if tr.root.numItems == maxItems {
|
136
|
171
|
n := tr.cowLoad(&tr.root)
|
137
|
172
|
right, median := tr.nodeSplit(n)
|
|
@@ -140,8 +175,9 @@ func (tr *BTree) setHint(item interface{}, hint *PathHint) (prev interface{}) {
|
140
|
175
|
tr.root.items[0] = median
|
141
|
176
|
tr.root.children[1] = right
|
142
|
177
|
tr.root.numItems = 1
|
|
178
|
+ tr.root.count = n.count + 1 + right.count
|
143
|
179
|
}
|
144
|
|
- tr.length++
|
|
180
|
+ tr.count++
|
145
|
181
|
return prev
|
146
|
182
|
}
|
147
|
183
|
|
|
@@ -167,10 +203,25 @@ func (tr *BTree) nodeSplit(n *node) (right *node, median interface{}) {
|
167
|
203
|
n.items[i] = nil
|
168
|
204
|
}
|
169
|
205
|
n.numItems = maxItems / 2
|
|
206
|
+ // update counts
|
|
207
|
+ n.updateCount()
|
|
208
|
+ right.updateCount()
|
170
|
209
|
return right, median
|
171
|
210
|
}
|
172
|
211
|
|
173
|
|
-//go:noinline
|
|
212
|
+func (n *node) updateCount() {
|
|
213
|
+ n.count = int(n.numItems)
|
|
214
|
+ if !n.leaf {
|
|
215
|
+ for i := 0; i <= int(n.numItems); i++ {
|
|
216
|
+ n.count += n.children[i].count
|
|
217
|
+ }
|
|
218
|
+ }
|
|
219
|
+}
|
|
220
|
+
|
|
221
|
+// This operation should not be inlined because it's expensive and rarely
|
|
222
|
+// called outside of heavy copy-on-write situations. Marking it "noinline"
|
|
223
|
+// allows for the parent cowLoad to be inlined.
|
|
224
|
+// go:noinline
|
174
|
225
|
func (tr *BTree) copy(n *node) *node {
|
175
|
226
|
n2 := *n
|
176
|
227
|
n2.cow = tr.cow
|
|
@@ -182,7 +233,7 @@ func (tr *BTree) copy(n *node) *node {
|
182
|
233
|
return &n2
|
183
|
234
|
}
|
184
|
235
|
|
185
|
|
-// cowLoad loaded the provide node and, if needed, performs a copy-on-write.
|
|
236
|
+// cowLoad loads the provide node and, if needed, performs a copy-on-write.
|
186
|
237
|
func (tr *BTree) cowLoad(cn **node) *node {
|
187
|
238
|
if (*cn).cow != tr.cow {
|
188
|
239
|
*cn = tr.copy(*cn)
|
|
@@ -204,6 +255,7 @@ func (tr *BTree) nodeSet(cn **node, item interface{},
|
204
|
255
|
copy(n.items[i+1:n.numItems+1], n.items[i:n.numItems])
|
205
|
256
|
n.items[i] = item
|
206
|
257
|
n.numItems++
|
|
258
|
+ n.count++
|
207
|
259
|
return nil
|
208
|
260
|
}
|
209
|
261
|
prev = tr.nodeSet(&n.children[i], item, less, hint, depth+1)
|
|
@@ -218,7 +270,8 @@ func (tr *BTree) nodeSet(cn **node, item interface{},
|
218
|
270
|
n.children[i+1] = right
|
219
|
271
|
n.numItems++
|
220
|
272
|
}
|
221
|
|
- return prev
|
|
273
|
+ n.count++
|
|
274
|
+ return nil
|
222
|
275
|
}
|
223
|
276
|
|
224
|
277
|
func (n *node) scan(iter func(item interface{}) bool) bool {
|
|
@@ -248,8 +301,9 @@ func (tr *BTree) Get(key interface{}) interface{} {
|
248
|
301
|
|
249
|
302
|
// GetHint gets a value for key using a path hint
|
250
|
303
|
func (tr *BTree) GetHint(key interface{}, hint *PathHint) interface{} {
|
251
|
|
- tr.mu.RLock()
|
252
|
|
- defer tr.mu.RUnlock()
|
|
304
|
+ if tr.rlock() {
|
|
305
|
+ defer tr.runlock()
|
|
306
|
+ }
|
253
|
307
|
if tr.root == nil || key == nil {
|
254
|
308
|
return nil
|
255
|
309
|
}
|
|
@@ -270,7 +324,7 @@ func (tr *BTree) GetHint(key interface{}, hint *PathHint) interface{} {
|
270
|
324
|
|
271
|
325
|
// Len returns the number of items in the tree
|
272
|
326
|
func (tr *BTree) Len() int {
|
273
|
|
- return tr.length
|
|
327
|
+ return tr.count
|
274
|
328
|
}
|
275
|
329
|
|
276
|
330
|
// Delete a value for a key
|
|
@@ -280,10 +334,10 @@ func (tr *BTree) Delete(key interface{}) interface{} {
|
280
|
334
|
|
281
|
335
|
// DeleteHint deletes a value for a key using a path hint
|
282
|
336
|
func (tr *BTree) DeleteHint(key interface{}, hint *PathHint) interface{} {
|
283
|
|
- tr.mu.Lock()
|
284
|
|
- prev := tr.deleteHint(key, hint)
|
285
|
|
- tr.mu.Unlock()
|
286
|
|
- return prev
|
|
337
|
+ if tr.lock() {
|
|
338
|
+ defer tr.unlock()
|
|
339
|
+ }
|
|
340
|
+ return tr.deleteHint(key, hint)
|
287
|
341
|
}
|
288
|
342
|
|
289
|
343
|
func (tr *BTree) deleteHint(key interface{}, hint *PathHint) interface{} {
|
|
@@ -294,12 +348,11 @@ func (tr *BTree) deleteHint(key interface{}, hint *PathHint) interface{} {
|
294
|
348
|
if prev == nil {
|
295
|
349
|
return nil
|
296
|
350
|
}
|
297
|
|
- tr.lnode = nil
|
298
|
351
|
if tr.root.numItems == 0 && !tr.root.leaf {
|
299
|
352
|
tr.root = tr.root.children[0]
|
300
|
353
|
}
|
301
|
|
- tr.length--
|
302
|
|
- if tr.length == 0 {
|
|
354
|
+ tr.count--
|
|
355
|
+ if tr.count == 0 {
|
303
|
356
|
tr.root = nil
|
304
|
357
|
}
|
305
|
358
|
return prev
|
|
@@ -323,6 +376,7 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
323
|
376
|
copy(n.items[i:], n.items[i+1:n.numItems])
|
324
|
377
|
n.items[n.numItems-1] = nil
|
325
|
378
|
n.numItems--
|
|
379
|
+ n.count--
|
326
|
380
|
return prev
|
327
|
381
|
}
|
328
|
382
|
return nil
|
|
@@ -344,6 +398,7 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
344
|
398
|
if prev == nil {
|
345
|
399
|
return nil
|
346
|
400
|
}
|
|
401
|
+ n.count--
|
347
|
402
|
if n.children[i].numItems >= minItems {
|
348
|
403
|
return prev
|
349
|
404
|
}
|
|
@@ -364,10 +419,11 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
364
|
419
|
n.children[i+1].children[:n.children[i+1].numItems+1])
|
365
|
420
|
}
|
366
|
421
|
n.children[i].numItems += n.children[i+1].numItems + 1
|
|
422
|
+ n.children[i].count += n.children[i+1].count + 1
|
367
|
423
|
copy(n.items[i:], n.items[i+1:n.numItems])
|
368
|
424
|
copy(n.children[i+1:], n.children[i+2:n.numItems+1])
|
369
|
|
- n.items[n.numItems] = nil
|
370
|
|
- n.children[n.numItems+1] = nil
|
|
425
|
+ n.items[n.numItems-1] = nil
|
|
426
|
+ n.children[n.numItems] = nil
|
371
|
427
|
n.numItems--
|
372
|
428
|
} else if n.children[i].numItems > n.children[i+1].numItems {
|
373
|
429
|
// move left -> right
|
|
@@ -381,30 +437,42 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
381
|
437
|
if !n.children[0].leaf {
|
382
|
438
|
n.children[i+1].children[0] =
|
383
|
439
|
n.children[i].children[n.children[i].numItems]
|
|
440
|
+ n.children[i+1].count += n.children[i+1].children[0].count
|
384
|
441
|
}
|
385
|
442
|
n.children[i+1].numItems++
|
|
443
|
+ n.children[i+1].count++
|
386
|
444
|
n.items[i] = n.children[i].items[n.children[i].numItems-1]
|
387
|
445
|
n.children[i].items[n.children[i].numItems-1] = nil
|
388
|
446
|
if !n.children[0].leaf {
|
389
|
447
|
n.children[i].children[n.children[i].numItems] = nil
|
|
448
|
+ n.children[i].count -= n.children[i+1].children[0].count
|
390
|
449
|
}
|
391
|
450
|
n.children[i].numItems--
|
|
451
|
+ n.children[i].count--
|
392
|
452
|
} else {
|
393
|
|
- // move right -> left
|
|
453
|
+ // move left <- right
|
394
|
454
|
n.children[i].items[n.children[i].numItems] = n.items[i]
|
395
|
455
|
if !n.children[0].leaf {
|
396
|
456
|
n.children[i].children[n.children[i].numItems+1] =
|
397
|
457
|
n.children[i+1].children[0]
|
|
458
|
+ n.children[i].count +=
|
|
459
|
+ n.children[i].children[n.children[i].numItems+1].count
|
398
|
460
|
}
|
399
|
461
|
n.children[i].numItems++
|
|
462
|
+ n.children[i].count++
|
400
|
463
|
n.items[i] = n.children[i+1].items[0]
|
401
|
464
|
copy(n.children[i+1].items[:],
|
402
|
465
|
n.children[i+1].items[1:n.children[i+1].numItems])
|
|
466
|
+ n.children[i+1].items[n.children[i+1].numItems-1] = nil
|
403
|
467
|
if !n.children[0].leaf {
|
404
|
468
|
copy(n.children[i+1].children[:],
|
405
|
469
|
n.children[i+1].children[1:n.children[i+1].numItems+1])
|
|
470
|
+ n.children[i+1].children[n.children[i+1].numItems] = nil
|
|
471
|
+ n.children[i+1].count -=
|
|
472
|
+ n.children[i].children[n.children[i].numItems].count
|
406
|
473
|
}
|
407
|
474
|
n.children[i+1].numItems--
|
|
475
|
+ n.children[i+1].count--
|
408
|
476
|
}
|
409
|
477
|
return prev
|
410
|
478
|
}
|
|
@@ -413,8 +481,9 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
413
|
481
|
// Pass nil for pivot to scan all item in ascending order
|
414
|
482
|
// Return false to stop iterating
|
415
|
483
|
func (tr *BTree) Ascend(pivot interface{}, iter func(item interface{}) bool) {
|
416
|
|
- tr.mu.RLock()
|
417
|
|
- defer tr.mu.RUnlock()
|
|
484
|
+ if tr.rlock() {
|
|
485
|
+ defer tr.runlock()
|
|
486
|
+ }
|
418
|
487
|
if tr.root == nil {
|
419
|
488
|
return
|
420
|
489
|
}
|
|
@@ -476,8 +545,9 @@ func (n *node) reverse(iter func(item interface{}) bool) bool {
|
476
|
545
|
// Pass nil for pivot to scan all item in descending order
|
477
|
546
|
// Return false to stop iterating
|
478
|
547
|
func (tr *BTree) Descend(pivot interface{}, iter func(item interface{}) bool) {
|
479
|
|
- tr.mu.RLock()
|
480
|
|
- defer tr.mu.RUnlock()
|
|
548
|
+ if tr.rlock() {
|
|
549
|
+ defer tr.runlock()
|
|
550
|
+ }
|
481
|
551
|
if tr.root == nil {
|
482
|
552
|
return
|
483
|
553
|
}
|
|
@@ -518,40 +588,46 @@ func (tr *BTree) Load(item interface{}) interface{} {
|
518
|
588
|
if item == nil {
|
519
|
589
|
panic("nil item")
|
520
|
590
|
}
|
521
|
|
- tr.mu.Lock()
|
522
|
|
- defer tr.mu.Unlock()
|
523
|
|
-
|
524
|
|
- // Load does not need a cowGrid because the Copy operation sets the
|
525
|
|
- // lnode to nil.
|
526
|
|
-
|
527
|
|
- if tr.lnode != nil && tr.lnode.numItems < maxItems-2 {
|
528
|
|
- if tr.less(tr.lnode.items[tr.lnode.numItems-1], item) {
|
529
|
|
- tr.lnode.items[tr.lnode.numItems] = item
|
530
|
|
- tr.lnode.numItems++
|
531
|
|
- tr.length++
|
532
|
|
- return nil
|
533
|
|
- }
|
|
591
|
+ if tr.lock() {
|
|
592
|
+ defer tr.unlock()
|
534
|
593
|
}
|
535
|
|
- prev := tr.setHint(item, nil)
|
536
|
|
- if prev != nil {
|
537
|
|
- return prev
|
|
594
|
+ if tr.root == nil {
|
|
595
|
+ return tr.setHint(item, nil)
|
538
|
596
|
}
|
539
|
|
- n := tr.root
|
|
597
|
+ n := tr.cowLoad(&tr.root)
|
540
|
598
|
for {
|
|
599
|
+ n.count++ // optimistically update counts
|
|
600
|
+ if n.leaf {
|
|
601
|
+ if n.numItems < maxItems-2 {
|
|
602
|
+ if tr.less(n.items[n.numItems-1], item) {
|
|
603
|
+ n.items[n.numItems] = item
|
|
604
|
+ n.numItems++
|
|
605
|
+ tr.count++
|
|
606
|
+ return nil
|
|
607
|
+ }
|
|
608
|
+ }
|
|
609
|
+ break
|
|
610
|
+ }
|
|
611
|
+ n = tr.cowLoad(&n.children[n.numItems])
|
|
612
|
+ }
|
|
613
|
+ // revert the counts
|
|
614
|
+ n = tr.root
|
|
615
|
+ for {
|
|
616
|
+ n.count--
|
541
|
617
|
if n.leaf {
|
542
|
|
- tr.lnode = n
|
543
|
618
|
break
|
544
|
619
|
}
|
545
|
620
|
n = n.children[n.numItems]
|
546
|
621
|
}
|
547
|
|
- return nil
|
|
622
|
+ return tr.setHint(item, nil)
|
548
|
623
|
}
|
549
|
624
|
|
550
|
625
|
// Min returns the minimum item in tree.
|
551
|
626
|
// Returns nil if the tree has no items.
|
552
|
627
|
func (tr *BTree) Min() interface{} {
|
553
|
|
- tr.mu.RLock()
|
554
|
|
- defer tr.mu.RUnlock()
|
|
628
|
+ if tr.rlock() {
|
|
629
|
+ defer tr.runlock()
|
|
630
|
+ }
|
555
|
631
|
if tr.root == nil {
|
556
|
632
|
return nil
|
557
|
633
|
}
|
|
@@ -567,8 +643,9 @@ func (tr *BTree) Min() interface{} {
|
567
|
643
|
// Max returns the maximum item in tree.
|
568
|
644
|
// Returns nil if the tree has no items.
|
569
|
645
|
func (tr *BTree) Max() interface{} {
|
570
|
|
- tr.mu.RLock()
|
571
|
|
- defer tr.mu.RUnlock()
|
|
646
|
+ if tr.rlock() {
|
|
647
|
+ defer tr.runlock()
|
|
648
|
+ }
|
572
|
649
|
if tr.root == nil {
|
573
|
650
|
return nil
|
574
|
651
|
}
|
|
@@ -584,65 +661,179 @@ func (tr *BTree) Max() interface{} {
|
584
|
661
|
// PopMin removes the minimum item in tree and returns it.
|
585
|
662
|
// Returns nil if the tree has no items.
|
586
|
663
|
func (tr *BTree) PopMin() interface{} {
|
587
|
|
- tr.mu.Lock()
|
588
|
|
- defer tr.mu.Unlock()
|
|
664
|
+ if tr.lock() {
|
|
665
|
+ defer tr.unlock()
|
|
666
|
+ }
|
589
|
667
|
if tr.root == nil {
|
590
|
668
|
return nil
|
591
|
669
|
}
|
592
|
|
- tr.lnode = nil
|
593
|
670
|
n := tr.cowLoad(&tr.root)
|
|
671
|
+ var item interface{}
|
594
|
672
|
for {
|
|
673
|
+ n.count-- // optimistically update counts
|
595
|
674
|
if n.leaf {
|
596
|
|
- item := n.items[0]
|
|
675
|
+ item = n.items[0]
|
597
|
676
|
if n.numItems == minItems {
|
598
|
|
- return tr.deleteHint(item, nil)
|
|
677
|
+ break
|
599
|
678
|
}
|
600
|
679
|
copy(n.items[:], n.items[1:])
|
601
|
680
|
n.items[n.numItems-1] = nil
|
602
|
681
|
n.numItems--
|
603
|
|
- tr.length--
|
604
|
|
- if tr.length == 0 {
|
|
682
|
+ tr.count--
|
|
683
|
+ if tr.count == 0 {
|
605
|
684
|
tr.root = nil
|
606
|
685
|
}
|
607
|
686
|
return item
|
608
|
687
|
}
|
609
|
688
|
n = tr.cowLoad(&n.children[0])
|
610
|
689
|
}
|
|
690
|
+ // revert the counts
|
|
691
|
+ n = tr.root
|
|
692
|
+ for {
|
|
693
|
+ n.count++
|
|
694
|
+ if n.leaf {
|
|
695
|
+ break
|
|
696
|
+ }
|
|
697
|
+ n = n.children[0]
|
|
698
|
+ }
|
|
699
|
+ return tr.deleteHint(item, nil)
|
611
|
700
|
}
|
612
|
701
|
|
613
|
702
|
// PopMax removes the minimum item in tree and returns it.
|
614
|
703
|
// Returns nil if the tree has no items.
|
615
|
704
|
func (tr *BTree) PopMax() interface{} {
|
616
|
|
- tr.mu.Lock()
|
617
|
|
- defer tr.mu.Unlock()
|
|
705
|
+ if tr.lock() {
|
|
706
|
+ defer tr.unlock()
|
|
707
|
+ }
|
618
|
708
|
if tr.root == nil {
|
619
|
709
|
return nil
|
620
|
710
|
}
|
621
|
|
- tr.lnode = nil
|
622
|
711
|
n := tr.cowLoad(&tr.root)
|
|
712
|
+ var item interface{}
|
623
|
713
|
for {
|
|
714
|
+ n.count-- // optimistically update counts
|
624
|
715
|
if n.leaf {
|
625
|
|
- item := n.items[n.numItems-1]
|
|
716
|
+ item = n.items[n.numItems-1]
|
626
|
717
|
if n.numItems == minItems {
|
627
|
|
- return tr.deleteHint(item, nil)
|
|
718
|
+ break
|
628
|
719
|
}
|
629
|
720
|
n.items[n.numItems-1] = nil
|
630
|
721
|
n.numItems--
|
631
|
|
- tr.length--
|
632
|
|
- if tr.length == 0 {
|
|
722
|
+ tr.count--
|
|
723
|
+ if tr.count == 0 {
|
633
|
724
|
tr.root = nil
|
634
|
725
|
}
|
635
|
726
|
return item
|
636
|
727
|
}
|
637
|
728
|
n = tr.cowLoad(&n.children[n.numItems])
|
638
|
729
|
}
|
|
730
|
+ // revert the counts
|
|
731
|
+ n = tr.root
|
|
732
|
+ for {
|
|
733
|
+ n.count++
|
|
734
|
+ if n.leaf {
|
|
735
|
+ break
|
|
736
|
+ }
|
|
737
|
+ n = n.children[n.numItems]
|
|
738
|
+ }
|
|
739
|
+ return tr.deleteHint(item, nil)
|
|
740
|
+}
|
|
741
|
+
|
|
742
|
+// GetAt returns the value at index.
|
|
743
|
+// Return nil if the tree is empty or the index is out of bounds.
|
|
744
|
+func (tr *BTree) GetAt(index int) interface{} {
|
|
745
|
+ if tr.rlock() {
|
|
746
|
+ defer tr.runlock()
|
|
747
|
+ }
|
|
748
|
+ if tr.root == nil || index < 0 || index >= tr.count {
|
|
749
|
+ return nil
|
|
750
|
+ }
|
|
751
|
+ n := tr.root
|
|
752
|
+ for {
|
|
753
|
+ if n.leaf {
|
|
754
|
+ return n.items[index]
|
|
755
|
+ }
|
|
756
|
+ i := 0
|
|
757
|
+ for ; i < int(n.numItems); i++ {
|
|
758
|
+ if index < n.children[i].count {
|
|
759
|
+ break
|
|
760
|
+ } else if index == n.children[i].count {
|
|
761
|
+ return n.items[i]
|
|
762
|
+ }
|
|
763
|
+ index -= n.children[i].count + 1
|
|
764
|
+ }
|
|
765
|
+ n = n.children[i]
|
|
766
|
+ }
|
|
767
|
+}
|
|
768
|
+
|
|
769
|
+// DeleteAt deletes the item at index.
|
|
770
|
+// Return nil if the tree is empty or the index is out of bounds.
|
|
771
|
+func (tr *BTree) DeleteAt(index int) interface{} {
|
|
772
|
+ if tr.lock() {
|
|
773
|
+ defer tr.unlock()
|
|
774
|
+ }
|
|
775
|
+ if tr.root == nil || index < 0 || index >= tr.count {
|
|
776
|
+ return nil
|
|
777
|
+ }
|
|
778
|
+ var pathbuf [8]uint8 // track the path
|
|
779
|
+ path := pathbuf[:0]
|
|
780
|
+ var item interface{}
|
|
781
|
+ n := tr.cowLoad(&tr.root)
|
|
782
|
+outer:
|
|
783
|
+ for {
|
|
784
|
+ n.count-- // optimistically update counts
|
|
785
|
+ if n.leaf {
|
|
786
|
+ // the index is the item position
|
|
787
|
+ item = n.items[index]
|
|
788
|
+ if n.numItems == minItems {
|
|
789
|
+ path = append(path, uint8(index))
|
|
790
|
+ break outer
|
|
791
|
+ }
|
|
792
|
+ copy(n.items[index:], n.items[index+1:n.numItems])
|
|
793
|
+ n.items[n.numItems-1] = nil
|
|
794
|
+ n.numItems--
|
|
795
|
+ tr.count--
|
|
796
|
+ if tr.count == 0 {
|
|
797
|
+ tr.root = nil
|
|
798
|
+ }
|
|
799
|
+ return item
|
|
800
|
+ }
|
|
801
|
+ i := 0
|
|
802
|
+ for ; i < int(n.numItems); i++ {
|
|
803
|
+ if index < n.children[i].count {
|
|
804
|
+ break
|
|
805
|
+ } else if index == n.children[i].count {
|
|
806
|
+ item = n.items[i]
|
|
807
|
+ path = append(path, uint8(i))
|
|
808
|
+ break outer
|
|
809
|
+ }
|
|
810
|
+ index -= n.children[i].count + 1
|
|
811
|
+ }
|
|
812
|
+ path = append(path, uint8(i))
|
|
813
|
+ n = tr.cowLoad(&n.children[i])
|
|
814
|
+ }
|
|
815
|
+ // revert the counts
|
|
816
|
+ var hint PathHint
|
|
817
|
+ n = tr.root
|
|
818
|
+ for i := 0; i < len(path); i++ {
|
|
819
|
+ if i < len(hint.path) {
|
|
820
|
+ hint.path[i] = path[i]
|
|
821
|
+ hint.used[i] = true
|
|
822
|
+ }
|
|
823
|
+ n.count++
|
|
824
|
+ if !n.leaf {
|
|
825
|
+ n = n.children[uint8(path[i])]
|
|
826
|
+ }
|
|
827
|
+ }
|
|
828
|
+ return tr.deleteHint(item, &hint)
|
639
|
829
|
}
|
640
|
830
|
|
641
|
831
|
// Height returns the height of the tree.
|
642
|
832
|
// Returns zero if tree has no items.
|
643
|
833
|
func (tr *BTree) Height() int {
|
644
|
|
- tr.mu.RLock()
|
645
|
|
- defer tr.mu.RUnlock()
|
|
834
|
+ if tr.rlock() {
|
|
835
|
+ defer tr.runlock()
|
|
836
|
+ }
|
646
|
837
|
var height int
|
647
|
838
|
if tr.root != nil {
|
648
|
839
|
n := tr.root
|
|
@@ -660,8 +851,9 @@ func (tr *BTree) Height() int {
|
660
|
851
|
// Walk iterates over all items in tree, in order.
|
661
|
852
|
// The items param will contain one or more items.
|
662
|
853
|
func (tr *BTree) Walk(iter func(item []interface{})) {
|
663
|
|
- tr.mu.RLock()
|
664
|
|
- defer tr.mu.RUnlock()
|
|
854
|
+ if tr.rlock() {
|
|
855
|
+ defer tr.runlock()
|
|
856
|
+ }
|
665
|
857
|
if tr.root != nil {
|
666
|
858
|
tr.root.walk(iter)
|
667
|
859
|
}
|
|
@@ -682,12 +874,34 @@ func (n *node) walk(iter func(item []interface{})) {
|
682
|
874
|
// Copy the tree. This operation is very fast because it only performs a
|
683
|
875
|
// shadowed copy.
|
684
|
876
|
func (tr *BTree) Copy() *BTree {
|
685
|
|
- tr.mu.Lock()
|
686
|
|
- tr.lnode = nil
|
|
877
|
+ if tr.lock() {
|
|
878
|
+ defer tr.unlock()
|
|
879
|
+ }
|
687
|
880
|
tr.cow = new(cow)
|
688
|
881
|
tr2 := *tr
|
689
|
882
|
tr2.mu = new(sync.RWMutex)
|
690
|
883
|
tr2.cow = new(cow)
|
691
|
|
- tr.mu.Unlock()
|
692
|
884
|
return &tr2
|
693
|
885
|
}
|
|
886
|
+
|
|
887
|
+func (tr *BTree) lock() bool {
|
|
888
|
+ if tr.locks {
|
|
889
|
+ tr.mu.Lock()
|
|
890
|
+ }
|
|
891
|
+ return tr.locks
|
|
892
|
+}
|
|
893
|
+
|
|
894
|
+func (tr *BTree) unlock() {
|
|
895
|
+ tr.mu.Unlock()
|
|
896
|
+}
|
|
897
|
+
|
|
898
|
+func (tr *BTree) rlock() bool {
|
|
899
|
+ if tr.locks {
|
|
900
|
+ tr.mu.RLock()
|
|
901
|
+ }
|
|
902
|
+ return tr.locks
|
|
903
|
+}
|
|
904
|
+
|
|
905
|
+func (tr *BTree) runlock() {
|
|
906
|
+ tr.mu.RUnlock()
|
|
907
|
+}
|