Set sets the value associated with k.
(k int64, v *primitive)
| 578 | |
| 579 | // Set sets the value associated with k. |
| 580 | func (t *Tree) Set(k int64, v *primitive) { |
| 581 | //dbg("--- PRE Set(%v, %v)\n%s", k, v, t.dump()) |
| 582 | //defer func() { |
| 583 | // dbg("--- POST\n%s\n====\n", t.dump()) |
| 584 | //}() |
| 585 | |
| 586 | pi := -1 |
| 587 | var p *x |
| 588 | q := t.r |
| 589 | if q == nil { |
| 590 | z := t.insert(btDPool.Get().(*d), 0, k, v) |
| 591 | t.r, t.first, t.last = z, z, z |
| 592 | return |
| 593 | } |
| 594 | |
| 595 | for { |
| 596 | i, ok := t.find(q, k) |
| 597 | if ok { |
| 598 | switch x := q.(type) { |
| 599 | case *x: |
| 600 | if x.c > 2*kx { |
| 601 | x, i = t.splitX(p, x, pi, i) |
| 602 | } |
| 603 | pi = i + 1 |
| 604 | p = x |
| 605 | q = x.x[i+1].ch |
| 606 | continue |
| 607 | case *d: |
| 608 | x.d[i].v = v |
| 609 | } |
| 610 | return |
| 611 | } |
| 612 | |
| 613 | switch x := q.(type) { |
| 614 | case *x: |
| 615 | if x.c > 2*kx { |
| 616 | x, i = t.splitX(p, x, pi, i) |
| 617 | } |
| 618 | pi = i |
| 619 | p = x |
| 620 | q = x.x[i].ch |
| 621 | case *d: |
| 622 | switch { |
| 623 | case x.c < 2*kd: |
| 624 | t.insert(x, i, k, v) |
| 625 | default: |
| 626 | t.overflow(p, x, pi, i, k, v) |
| 627 | } |
| 628 | return |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | // Put combines Get and Set in a more efficient way where the tree is walked |
| 634 | // only once. The upd(ater) receives (old-value, true) if a KV pair for k |