add getter and setter for root of tree and map

This commit is contained in:
Chris Lu 2021-08-20 18:34:50 -07:00
parent 88d68cad87
commit 0c360eb6b2
2 changed files with 36 additions and 22 deletions

View File

@ -9,6 +9,13 @@ import (
*/ */
type BpMap BpTree type BpMap BpTree
func (self *BpMap) getRoot() *BpNode {
return self.root
}
func (self *BpMap) setRoot(root *BpNode) {
self.root = root
}
func NewBpMap(node_size int) *BpMap { func NewBpMap(node_size int) *BpMap {
return &BpMap{ return &BpMap{
root: NewLeaf(node_size), root: NewLeaf(node_size),
@ -20,16 +27,16 @@ func (self *BpMap) Has(key Hashable) bool {
} }
func (self *BpMap) Put(key Hashable, value interface{}) (err error) { func (self *BpMap) Put(key Hashable, value interface{}) (err error) {
new_root, err := self.root.put(key, value) new_root, err := self.getRoot().put(key, value)
if err != nil { if err != nil {
return err return err
} }
self.root = new_root self.setRoot(new_root)
return nil return nil
} }
func (self *BpMap) Get(key Hashable) (value interface{}, err error) { func (self *BpMap) Get(key Hashable) (value interface{}, err error) {
j, l := self.root.get_start(key) j, l := self.getRoot().get_start(key)
if l.keys[j].Equals(key) { if l.keys[j].Equals(key) {
return l.values[j], nil return l.values[j], nil
} }
@ -41,15 +48,15 @@ func (self *BpMap) Remove(key Hashable) (value interface{}, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
ns := self.root.NodeSize() ns := self.getRoot().NodeSize()
new_root, err := self.root.remove(key, func(value interface{}) bool { return true }) new_root, err := self.getRoot().remove(key, func(value interface{}) bool { return true })
if err != nil { if err != nil {
return nil, err return nil, err
} }
if new_root == nil { if new_root == nil {
self.root = NewLeaf(ns) self.setRoot(NewLeaf(ns))
} else { } else {
self.root = new_root self.setRoot(new_root)
} }
return value, nil return value, nil
} }

View File

@ -10,6 +10,13 @@ type BpTree struct {
root *BpNode root *BpNode
} }
func (self *BpTree) getRoot() *BpNode {
return self.root
}
func (self *BpTree) setRoot(root *BpNode) {
self.root = root
}
type loc_iterator func() (i int, leaf *BpNode, li loc_iterator) type loc_iterator func() (i int, leaf *BpNode, li loc_iterator)
func NewBpTree(node_size int) *BpTree { func NewBpTree(node_size int) *BpTree {
@ -19,24 +26,24 @@ func NewBpTree(node_size int) *BpTree {
} }
func (self *BpTree) Has(key Hashable) bool { func (self *BpTree) Has(key Hashable) bool {
if len(self.root.keys) == 0 { if len(self.getRoot().keys) == 0 {
return false return false
} }
j, l := self.root.get_start(key) j, l := self.getRoot().get_start(key)
return l.keys[j].Equals(key) return l.keys[j].Equals(key)
} }
func (self *BpTree) Add(key Hashable, value interface{}) (err error) { func (self *BpTree) Add(key Hashable, value interface{}) (err error) {
new_root, err := self.root.put(key, value) new_root, err := self.getRoot().put(key, value)
if err != nil { if err != nil {
return err return err
} }
self.root = new_root self.setRoot(new_root)
return nil return nil
} }
func (self *BpTree) Replace(key Hashable, where WhereFunc, value interface{}) (err error) { func (self *BpTree) Replace(key Hashable, where WhereFunc, value interface{}) (err error) {
li := self.root.forward(key, key) li := self.getRoot().forward(key, key)
for i, leaf, next := li(); next != nil; i, leaf, next = next() { for i, leaf, next := li(); next != nil; i, leaf, next = next() {
if where(leaf.values[i]) { if where(leaf.values[i]) {
leaf.values[i] = value leaf.values[i] = value
@ -52,9 +59,9 @@ func (self *BpTree) Find(key Hashable) (kvi KVIterator) {
func (self *BpTree) Range(from, to Hashable) (kvi KVIterator) { func (self *BpTree) Range(from, to Hashable) (kvi KVIterator) {
var li loc_iterator var li loc_iterator
if !to.Less(from) { if !to.Less(from) {
li = self.root.forward(from, to) li = self.getRoot().forward(from, to)
} else { } else {
li = self.root.backward(from, to) li = self.getRoot().backward(from, to)
} }
kvi = func() (key Hashable, value interface{}, next KVIterator) { kvi = func() (key Hashable, value interface{}, next KVIterator) {
var i int var i int
@ -69,21 +76,21 @@ func (self *BpTree) Range(from, to Hashable) (kvi KVIterator) {
} }
func (self *BpTree) RemoveWhere(key Hashable, where WhereFunc) (err error) { func (self *BpTree) RemoveWhere(key Hashable, where WhereFunc) (err error) {
ns := self.root.NodeSize() ns := self.getRoot().NodeSize()
new_root, err := self.root.remove(key, where) new_root, err := self.getRoot().remove(key, where)
if err != nil { if err != nil {
return err return err
} }
if new_root == nil { if new_root == nil {
self.root = NewLeaf(ns) self.setRoot(NewLeaf(ns))
} else { } else {
self.root = new_root self.setRoot(new_root)
} }
return nil return nil
} }
func (self *BpTree) Keys() (ki KIterator) { func (self *BpTree) Keys() (ki KIterator) {
li := self.root.all() li := self.getRoot().all()
var prev Equatable var prev Equatable
ki = func() (key Hashable, next KIterator) { ki = func() (key Hashable, next KIterator) {
var i int var i int
@ -110,7 +117,7 @@ func (self *BpTree) Items() (vi KIterator) {
} }
func (self *BpTree) Iterate() (kvi KVIterator) { func (self *BpTree) Iterate() (kvi KVIterator) {
li := self.root.all() li := self.getRoot().all()
kvi = func() (key Hashable, value interface{}, next KVIterator) { kvi = func() (key Hashable, value interface{}, next KVIterator) {
var i int var i int
var leaf *BpNode var leaf *BpNode
@ -124,7 +131,7 @@ func (self *BpTree) Iterate() (kvi KVIterator) {
} }
func (self *BpTree) Backward() (kvi KVIterator) { func (self *BpTree) Backward() (kvi KVIterator) {
li := self.root.all_backward() li := self.getRoot().all_backward()
kvi = func() (key Hashable, value interface{}, next KVIterator) { kvi = func() (key Hashable, value interface{}, next KVIterator) {
var i int var i int
var leaf *BpNode var leaf *BpNode
@ -135,4 +142,4 @@ func (self *BpTree) Backward() (kvi KVIterator) {
return leaf.keys[i], leaf.values[i], kvi return leaf.keys[i], leaf.values[i], kvi
} }
return kvi return kvi
} }