Debian Codegraph @

example/int.go

raw symbols SCIP
1 // Copyright 2014 The b Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package b
6
7 import (
8 "fmt"
9 "io"
10 "sync"
11 )
12
13 const (
14 kx = 32 //TODO benchmark tune this number if using custom key/value type(s).
15 kd = 32 //TODO benchmark tune this number if using custom key/value type(s).
16 )
17
18 func init() {
19 if kd < 1 {
20 panic(fmt.Errorf("kd %d: out of range", kd))
21 }
22
23 if kx < 2 {
24 panic(fmt.Errorf("kx %d: out of range", kx))
25 }
26 }
27
28 var (
29 btDPool = sync.Pool{New: func() interface{} { return &d{} }}
30 btEPool = btEpool{sync.Pool{New: func() interface{} { return &Enumerator{} }}}
31 btTPool = btTpool{sync.Pool{New: func() interface{} { return &Tree{} }}}
32 btXPool = sync.Pool{New: func() interface{} { return &x{} }}
33 )
34
35 type btTpool struct{ sync.Pool }
36
37 func (p *btTpool) get(cmp Cmp) *Tree {
38 x := p.Get().(*Tree)
39 x.cmp = cmp
40 return x
41 }
42
43 type btEpool struct{ sync.Pool }
44
45 func (p *btEpool) get(err error, hit bool, i int, k int, q *d, t *Tree, ver int64) *Enumerator {
46 x := p.Get().(*Enumerator)
47 x.err, x.hit, x.i, x.k, x.q, x.t, x.ver = err, hit, i, k, q, t, ver
48 return x
49 }
50
51 type (
52 // Cmp compares a and b. Return value is:
53 //
54 // < 0 if a < b
55 // 0 if a == b
56 // > 0 if a > b
57 //
58 Cmp func(a, b int) int
59
60 d struct { // data page
61 c int
62 d [2*kd + 1]de
63 n *d
64 p *d
65 }
66
67 de struct { // d element
68 k int
69 v int
70 }
71
72 // Enumerator captures the state of enumerating a tree. It is returned
73 // from the Seek* methods. The enumerator is aware of any mutations
74 // made to the tree in the process of enumerating it and automatically
75 // resumes the enumeration at the proper key, if possible.
76 //
77 // However, once an Enumerator returns io.EOF to signal "no more
78 // items", it does no more attempt to "resync" on tree mutation(s). In
79 // other words, io.EOF from an Enumaretor is "sticky" (idempotent).
80 Enumerator struct {
81 err error
82 hit bool
83 i int
84 k int
85 q *d
86 t *Tree
87 ver int64
88 }
89
90 // Tree is a B+tree.
91 Tree struct {
92 c int
93 cmp Cmp
94 first *d
95 last *d
96 r interface{}
97 ver int64
98 }
99
100 xe struct { // x element
101 ch interface{}
102 k int
103 }
104
105 x struct { // index page
106 c int
107 x [2*kx + 2]xe
108 }
109 )
110
111 var ( // R/O zero values
112 zd d
113 zde de
114 ze Enumerator
115 zk int
116 zt Tree
117 zx x
118 zxe xe
119 )
120
121 func clr(q interface{}) {
122 switch x := q.(type) {
123 case *x:
124 for i := 0; i <= x.c; i++ { // Ch0 Sep0 ... Chn-1 Sepn-1 Chn
125 clr(x.x[i].ch)
126 }
127 *x = zx
128 btXPool.Put(x)
129 case *d:
130 *x = zd
131 btDPool.Put(x)
132 }
133 }
134
135 // -------------------------------------------------------------------------- x
136
137 func newX(ch0 interface{}) *x {
138 r := btXPool.Get().(*x)
139 r.x[0].ch = ch0
140 return r
141 }
142
143 func (q *x) extract(i int) {
144 q.c--
145 if i < q.c {
146 copy(q.x[i:], q.x[i+1:q.c+1])
147 q.x[q.c].ch = q.x[q.c+1].ch
148 q.x[q.c].k = zk // GC
149 q.x[q.c+1] = zxe // GC
150 }
151 }
152
153 func (q *x) insert(i int, k int, ch interface{}) *x {
154 c := q.c
155 if i < c {
156 q.x[c+1].ch = q.x[c].ch
157 copy(q.x[i+2:], q.x[i+1:c])
158 q.x[i+1].k = q.x[i].k
159 }
160 c++
161 q.c = c
162 q.x[i].k = k
163 q.x[i+1].ch = ch
164 return q
165 }
166
167 func (q *x) siblings(i int) (l, r *d) {
168 if i >= 0 {
169 if i > 0 {
170 l = q.x[i-1].ch.(*d)
171 }
172 if i < q.c {
173 r = q.x[i+1].ch.(*d)
174 }
175 }
176 return
177 }
178
179 // -------------------------------------------------------------------------- d
180
181 func (l *d) mvL(r *d, c int) {
182 copy(l.d[l.c:], r.d[:c])
183 copy(r.d[:], r.d[c:r.c])
184 l.c += c
185 r.c -= c
186 }
187
188 func (l *d) mvR(r *d, c int) {
189 copy(r.d[c:], r.d[:r.c])
190 copy(r.d[:c], l.d[l.c-c:])
191 r.c += c
192 l.c -= c
193 }
194
195 // ----------------------------------------------------------------------- Tree
196
197 // TreeNew returns a newly created, empty Tree. The compare function is used
198 // for key collation.
199 func TreeNew(cmp Cmp) *Tree {
200 return btTPool.get(cmp)
201 }
202
203 // Clear removes all K/V pairs from the tree.
204 func (t *Tree) Clear() {
205 if t.r == nil {
206 return
207 }
208
209 clr(t.r)
210 t.c, t.first, t.last, t.r = 0, nil, nil, nil
211 t.ver++
212 }
213
214 // Close performs Clear and recycles t to a pool for possible later reuse. No
215 // references to t should exist or such references must not be used afterwards.
216 func (t *Tree) Close() {
217 t.Clear()
218 *t = zt
219 btTPool.Put(t)
220 }
221
222 func (t *Tree) cat(p *x, q, r *d, pi int) {
223 t.ver++
224 q.mvL(r, r.c)
225 if r.n != nil {
226 r.n.p = q
227 } else {
228 t.last = q
229 }
230 q.n = r.n
231 *r = zd
232 btDPool.Put(r)
233 if p.c > 1 {
234 p.extract(pi)
235 p.x[pi].ch = q
236 return
237 }
238
239 switch x := t.r.(type) {
240 case *x:
241 *x = zx
242 btXPool.Put(x)
243 case *d:
244 *x = zd
245 btDPool.Put(x)
246 }
247 t.r = q
248 }
249
250 func (t *Tree) catX(p, q, r *x, pi int) {
251 t.ver++
252 q.x[q.c].k = p.x[pi].k
253 copy(q.x[q.c+1:], r.x[:r.c])
254 q.c += r.c + 1
255 q.x[q.c].ch = r.x[r.c].ch
256 *r = zx
257 btXPool.Put(r)
258 if p.c > 1 {
259 p.c--
260 pc := p.c
261 if pi < pc {
262 p.x[pi].k = p.x[pi+1].k
263 copy(p.x[pi+1:], p.x[pi+2:pc+1])
264 p.x[pc].ch = p.x[pc+1].ch
265 p.x[pc].k = zk // GC
266 p.x[pc+1].ch = nil // GC
267 }
268 return
269 }
270
271 switch x := t.r.(type) {
272 case *x:
273 *x = zx
274 btXPool.Put(x)
275 case *d:
276 *x = zd
277 btDPool.Put(x)
278 }
279 t.r = q
280 }
281
282 // Delete removes the k's KV pair, if it exists, in which case Delete returns
283 // true.
284 func (t *Tree) Delete(k int) (ok bool) {
285 pi := -1
286 var p *x
287 q := t.r
288 if q == nil {
289 return false
290 }
291
292 for {
293 var i int
294 i, ok = t.find(q, k)
295 if ok {
296 switch x := q.(type) {
297 case *x:
298 if x.c < kx && q != t.r {
299 x, i = t.underflowX(p, x, pi, i)
300 }
301 pi = i + 1
302 p = x
303 q = x.x[pi].ch
304 ok = false
305 continue
306 case *d:
307 t.extract(x, i)
308 if x.c >= kd {
309 return true
310 }
311
312 if q != t.r {
313 t.underflow(p, x, pi)
314 } else if t.c == 0 {
315 t.Clear()
316 }
317 return true
318 }
319 }
320
321 switch x := q.(type) {
322 case *x:
323 if x.c < kx && q != t.r {
324 x, i = t.underflowX(p, x, pi, i)
325 }
326 pi = i
327 p = x
328 q = x.x[i].ch
329 case *d:
330 return false
331 }
332 }
333 }
334
335 func (t *Tree) extract(q *d, i int) { // (r int) {
336 t.ver++
337 //r = q.d[i].v // prepared for Extract
338 q.c--
339 if i < q.c {
340 copy(q.d[i:], q.d[i+1:q.c+1])
341 }
342 q.d[q.c] = zde // GC
343 t.c--
344 return
345 }
346
347 func (t *Tree) find(q interface{}, k int) (i int, ok bool) {
348 var mk int
349 l := 0
350 switch x := q.(type) {
351 case *x:
352 h := x.c - 1
353 for l <= h {
354 m := (l + h) >> 1
355 mk = x.x[m].k
356 switch cmp := t.cmp(k, mk); {
357 case cmp > 0:
358 l = m + 1
359 case cmp == 0:
360 return m, true
361 default:
362 h = m - 1
363 }
364 }
365 case *d:
366 h := x.c - 1
367 for l <= h {
368 m := (l + h) >> 1
369 mk = x.d[m].k
370 switch cmp := t.cmp(k, mk); {
371 case cmp > 0:
372 l = m + 1
373 case cmp == 0:
374 return m, true
375 default:
376 h = m - 1
377 }
378 }
379 }
380 return l, false
381 }
382
383 // First returns the first item of the tree in the key collating order, or
384 // (zero-value, zero-value) if the tree is empty.
385 func (t *Tree) First() (k int, v int) {
386 if q := t.first; q != nil {
387 q := &q.d[0]
388 k, v = q.k, q.v
389 }
390 return
391 }
392
393 // Get returns the value associated with k and true if it exists. Otherwise Get
394 // returns (zero-value, false).
395 func (t *Tree) Get(k int) (v int, ok bool) {
396 q := t.r
397 if q == nil {
398 return
399 }
400
401 for {
402 var i int
403 if i, ok = t.find(q, k); ok {
404 switch x := q.(type) {
405 case *x:
406 q = x.x[i+1].ch
407 continue
408 case *d:
409 return x.d[i].v, true
410 }
411 }
412 switch x := q.(type) {
413 case *x:
414 q = x.x[i].ch
415 default:
416 return
417 }
418 }
419 }
420
421 func (t *Tree) insert(q *d, i int, k int, v int) *d {
422 t.ver++
423 c := q.c
424 if i < c {
425 copy(q.d[i+1:], q.d[i:c])
426 }
427 c++
428 q.c = c
429 q.d[i].k, q.d[i].v = k, v
430 t.c++
431 return q
432 }
433
434 // Last returns the last item of the tree in the key collating order, or
435 // (zero-value, zero-value) if the tree is empty.
436 func (t *Tree) Last() (k int, v int) {
437 if q := t.last; q != nil {
438 q := &q.d[q.c-1]
439 k, v = q.k, q.v
440 }
441 return
442 }
443
444 // Len returns the number of items in the tree.
445 func (t *Tree) Len() int {
446 return t.c
447 }
448
449 func (t *Tree) overflow(p *x, q *d, pi, i int, k int, v int) {
450 t.ver++
451 l, r := p.siblings(pi)
452
453 if l != nil && l.c < 2*kd {
454 l.mvL(q, 1)
455 t.insert(q, i-1, k, v)
456 p.x[pi-1].k = q.d[0].k
457 return
458 }
459
460 if r != nil && r.c < 2*kd {
461 if i < 2*kd {
462 q.mvR(r, 1)
463 t.insert(q, i, k, v)
464 p.x[pi].k = r.d[0].k
465 return
466 }
467
468 t.insert(r, 0, k, v)
469 p.x[pi].k = k
470 return
471 }
472
473 t.split(p, q, pi, i, k, v)
474 }
475
476 // Seek returns an Enumerator positioned on a an item such that k >= item's
477 // key. ok reports if k == item.key The Enumerator's position is possibly
478 // after the last item in the tree.
479 func (t *Tree) Seek(k int) (e *Enumerator, ok bool) {
480 q := t.r
481 if q == nil {
482 e = btEPool.get(nil, false, 0, k, nil, t, t.ver)
483 return
484 }
485
486 for {
487 var i int
488 if i, ok = t.find(q, k); ok {
489 switch x := q.(type) {
490 case *x:
491 q = x.x[i+1].ch
492 continue
493 case *d:
494 return btEPool.get(nil, ok, i, k, x, t, t.ver), true
495 }
496 }
497
498 switch x := q.(type) {
499 case *x:
500 q = x.x[i].ch
501 case *d:
502 return btEPool.get(nil, ok, i, k, x, t, t.ver), false
503 }
504 }
505 }
506
507 // SeekFirst returns an enumerator positioned on the first KV pair in the tree,
508 // if any. For an empty tree, err == io.EOF is returned and e will be nil.
509 func (t *Tree) SeekFirst() (e *Enumerator, err error) {
510 q := t.first
511 if q == nil {
512 return nil, io.EOF
513 }
514
515 return btEPool.get(nil, true, 0, q.d[0].k, q, t, t.ver), nil
516 }
517
518 // SeekLast returns an enumerator positioned on the last KV pair in the tree,
519 // if any. For an empty tree, err == io.EOF is returned and e will be nil.
520 func (t *Tree) SeekLast() (e *Enumerator, err error) {
521 q := t.last
522 if q == nil {
523 return nil, io.EOF
524 }
525
526 return btEPool.get(nil, true, q.c-1, q.d[q.c-1].k, q, t, t.ver), nil
527 }
528
529 // Set sets the value associated with k.
530 func (t *Tree) Set(k int, v int) {
531 //dbg("--- PRE Set(%v, %v)\n%s", k, v, t.dump())
532 //defer func() {
533 // dbg("--- POST\n%s\n====\n", t.dump())
534 //}()
535
536 pi := -1
537 var p *x
538 q := t.r
539 if q == nil {
540 z := t.insert(btDPool.Get().(*d), 0, k, v)
541 t.r, t.first, t.last = z, z, z
542 return
543 }
544
545 for {
546 i, ok := t.find(q, k)
547 if ok {
548 switch x := q.(type) {
549 case *x:
550 if x.c > 2*kx {
551 x, i = t.splitX(p, x, pi, i)
552 }
553 pi = i + 1
554 p = x
555 q = x.x[i+1].ch
556 continue
557 case *d:
558 x.d[i].v = v
559 }
560 return
561 }
562
563 switch x := q.(type) {
564 case *x:
565 if x.c > 2*kx {
566 x, i = t.splitX(p, x, pi, i)
567 }
568 pi = i
569 p = x
570 q = x.x[i].ch
571 case *d:
572 switch {
573 case x.c < 2*kd:
574 t.insert(x, i, k, v)
575 default:
576 t.overflow(p, x, pi, i, k, v)
577 }
578 return
579 }
580 }
581 }
582
583 // Put combines Get and Set in a more efficient way where the tree is walked
584 // only once. The upd(ater) receives (old-value, true) if a KV pair for k
585 // exists or (zero-value, false) otherwise. It can then return a (new-value,
586 // true) to create or overwrite the existing value in the KV pair, or
587 // (whatever, false) if it decides not to create or not to update the value of
588 // the KV pair.
589 //
590 // tree.Set(k, v) call conceptually equals calling
591 //
592 // tree.Put(k, func(int, bool){ return v, true })
593 //
594 // modulo the differing return values.
595 func (t *Tree) Put(k int, upd func(oldV int, exists bool) (newV int, write bool)) (oldV int, written bool) {
596 pi := -1
597 var p *x
598 q := t.r
599 var newV int
600 if q == nil {
601 // new KV pair in empty tree
602 newV, written = upd(newV, false)
603 if !written {
604 return
605 }
606
607 z := t.insert(btDPool.Get().(*d), 0, k, newV)
608 t.r, t.first, t.last = z, z, z
609 return
610 }
611
612 for {
613 i, ok := t.find(q, k)
614 if ok {
615 switch x := q.(type) {
616 case *x:
617 if x.c > 2*kx {
618 x, i = t.splitX(p, x, pi, i)
619 }
620 pi = i + 1
621 p = x
622 q = x.x[i+1].ch
623 continue
624 case *d:
625 oldV = x.d[i].v
626 newV, written = upd(oldV, true)
627 if !written {
628 return
629 }
630
631 x.d[i].v = newV
632 }
633 return
634 }
635
636 switch x := q.(type) {
637 case *x:
638 if x.c > 2*kx {
639 x, i = t.splitX(p, x, pi, i)
640 }
641 pi = i
642 p = x
643 q = x.x[i].ch
644 case *d: // new KV pair
645 newV, written = upd(newV, false)
646 if !written {
647 return
648 }
649
650 switch {
651 case x.c < 2*kd:
652 t.insert(x, i, k, newV)
653 default:
654 t.overflow(p, x, pi, i, k, newV)
655 }
656 return
657 }
658 }
659 }
660
661 func (t *Tree) split(p *x, q *d, pi, i int, k int, v int) {
662 t.ver++
663 r := btDPool.Get().(*d)
664 if q.n != nil {
665 r.n = q.n
666 r.n.p = r
667 } else {
668 t.last = r
669 }
670 q.n = r
671 r.p = q
672
673 copy(r.d[:], q.d[kd:2*kd])
674 for i := range q.d[kd:] {
675 q.d[kd+i] = zde
676 }
677 q.c = kd
678 r.c = kd
679 var done bool
680 if i > kd {
681 done = true
682 t.insert(r, i-kd, k, v)
683 }
684 if pi >= 0 {
685 p.insert(pi, r.d[0].k, r)
686 } else {
687 t.r = newX(q).insert(0, r.d[0].k, r)
688 }
689 if done {
690 return
691 }
692
693 t.insert(q, i, k, v)
694 }
695
696 func (t *Tree) splitX(p *x, q *x, pi int, i int) (*x, int) {
697 t.ver++
698 r := btXPool.Get().(*x)
699 copy(r.x[:], q.x[kx+1:])
700 q.c = kx
701 r.c = kx
702 if pi >= 0 {
703 p.insert(pi, q.x[kx].k, r)
704 q.x[kx].k = zk
705 for i := range q.x[kx+1:] {
706 q.x[kx+i+1] = zxe
707 }
708
709 switch {
710 case i < kx:
711 return q, i
712 case i == kx:
713 return p, pi
714 default: // i > kx
715 return r, i - kx - 1
716 }
717 }
718
719 nr := newX(q).insert(0, q.x[kx].k, r)
720 t.r = nr
721 q.x[kx].k = zk
722 for i := range q.x[kx+1:] {
723 q.x[kx+i+1] = zxe
724 }
725
726 switch {
727 case i < kx:
728 return q, i
729 case i == kx:
730 return nr, 0
731 default: // i > kx
732 return r, i - kx - 1
733 }
734 }
735
736 func (t *Tree) underflow(p *x, q *d, pi int) {
737 t.ver++
738 l, r := p.siblings(pi)
739
740 if l != nil && l.c+q.c >= 2*kd {
741 l.mvR(q, 1)
742 p.x[pi-1].k = q.d[0].k
743 return
744 }
745
746 if r != nil && q.c+r.c >= 2*kd {
747 q.mvL(r, 1)
748 p.x[pi].k = r.d[0].k
749 r.d[r.c] = zde // GC
750 return
751 }
752
753 if l != nil {
754 t.cat(p, l, q, pi-1)
755 return
756 }
757
758 t.cat(p, q, r, pi)
759 }
760
761 func (t *Tree) underflowX(p *x, q *x, pi int, i int) (*x, int) {
762 t.ver++
763 var l, r *x
764
765 if pi >= 0 {
766 if pi > 0 {
767 l = p.x[pi-1].ch.(*x)
768 }
769 if pi < p.c {
770 r = p.x[pi+1].ch.(*x)
771 }
772 }
773
774 if l != nil && l.c > kx {
775 q.x[q.c+1].ch = q.x[q.c].ch
776 copy(q.x[1:], q.x[:q.c])
777 q.x[0].ch = l.x[l.c].ch
778 q.x[0].k = p.x[pi-1].k
779 q.c++
780 i++
781 l.c--
782 p.x[pi-1].k = l.x[l.c].k
783 return q, i
784 }
785
786 if r != nil && r.c > kx {
787 q.x[q.c].k = p.x[pi].k
788 q.c++
789 q.x[q.c].ch = r.x[0].ch
790 p.x[pi].k = r.x[0].k
791 copy(r.x[:], r.x[1:r.c])
792 r.c--
793 rc := r.c
794 r.x[rc].ch = r.x[rc+1].ch
795 r.x[rc].k = zk
796 r.x[rc+1].ch = nil
797 return q, i
798 }
799
800 if l != nil {
801 i += l.c + 1
802 t.catX(p, l, q, pi-1)
803 q = l
804 return q, i
805 }
806
807 t.catX(p, q, r, pi)
808 return q, i
809 }
810
811 // ----------------------------------------------------------------- Enumerator
812
813 // Close recycles e to a pool for possible later reuse. No references to e
814 // should exist or such references must not be used afterwards.
815 func (e *Enumerator) Close() {
816 *e = ze
817 btEPool.Put(e)
818 }
819
820 // Next returns the currently enumerated item, if it exists and moves to the
821 // next item in the key collation order. If there is no item to return, err ==
822 // io.EOF is returned.
823 func (e *Enumerator) Next() (k int, v int, err error) {
824 if err = e.err; err != nil {
825 return
826 }
827
828 if e.ver != e.t.ver {
829 f, hit := e.t.Seek(e.k)
830 if !e.hit && hit {
831 if err = f.next(); err != nil {
832 return
833 }
834 }
835
836 *e = *f
837 f.Close()
838 }
839 if e.q == nil {
840 e.err, err = io.EOF, io.EOF
841 return
842 }
843
844 if e.i >= e.q.c {
845 if err = e.next(); err != nil {
846 return
847 }
848 }
849
850 i := e.q.d[e.i]
851 k, v = i.k, i.v
852 e.k, e.hit = k, false
853 e.next()
854 return
855 }
856
857 func (e *Enumerator) next() error {
858 if e.q == nil {
859 e.err = io.EOF
860 return io.EOF
861 }
862
863 switch {
864 case e.i < e.q.c-1:
865 e.i++
866 default:
867 if e.q, e.i = e.q.n, 0; e.q == nil {
868 e.err = io.EOF
869 }
870 }
871 return e.err
872 }
873
874 // Prev returns the currently enumerated item, if it exists and moves to the
875 // previous item in the key collation order. If there is no item to return, err
876 // == io.EOF is returned.
877 func (e *Enumerator) Prev() (k int, v int, err error) {
878 if err = e.err; err != nil {
879 return
880 }
881
882 if e.ver != e.t.ver {
883 f, hit := e.t.Seek(e.k)
884 if !e.hit && hit {
885 if err = f.prev(); err != nil {
886 return
887 }
888 }
889
890 *e = *f
891 f.Close()
892 }
893 if e.q == nil {
894 e.err, err = io.EOF, io.EOF
895 return
896 }
897
898 if e.i >= e.q.c {
899 if err = e.next(); err != nil {
900 return
901 }
902 }
903
904 i := e.q.d[e.i]
905 k, v = i.k, i.v
906 e.k, e.hit = k, false
907 e.prev()
908 return
909 }
910
911 func (e *Enumerator) prev() error {
912 if e.q == nil {
913 e.err = io.EOF
914 return io.EOF
915 }
916
917 switch {
918 case e.i > 0:
919 e.i--
920 default:
921 if e.q = e.q.p; e.q == nil {
922 e.err = io.EOF
923 break
924 }
925
926 e.i = e.q.c - 1
927 }
928 return e.err
929 }