bpnode use get prev and next

This commit is contained in:
Chris Lu 2021-08-20 18:50:16 -07:00
parent 01661ec6a7
commit 172da83449
3 changed files with 105 additions and 92 deletions

View file

@ -108,9 +108,9 @@ func (self *BpNode) get_start(key Hashable) (i int, leaf *BpNode) {
func next_location(i int, leaf *BpNode) (int, *BpNode, bool) {
j := i + 1
for j >= len(leaf.keys) && leaf.next != nil {
for j >= len(leaf.keys) && leaf.getNext() != nil {
j = 0
leaf = leaf.next
leaf = leaf.getNext()
}
if j >= len(leaf.keys) {
return -1, nil, true
@ -120,8 +120,8 @@ func next_location(i int, leaf *BpNode) (int, *BpNode, bool) {
func prev_location(i int, leaf *BpNode) (int, *BpNode, bool) {
j := i - 1
for j < 0 && leaf.prev != nil {
leaf = leaf.prev
for j < 0 && leaf.getPrev() != nil {
leaf = leaf.getPrev()
j = len(leaf.keys) - 1
}
if j < 0 {
@ -165,8 +165,8 @@ func (self *BpNode) leaf_get_start(key Hashable) (i int, leaf *BpNode) {
if i >= len(self.keys) && i > 0 {
i = len(self.keys) - 1
}
if !has && (len(self.keys) == 0 || self.keys[i].Less(key)) && self.next != nil {
return self.next.leaf_get_start(key)
if !has && (len(self.keys) == 0 || self.keys[i].Less(key)) && self.getNext() != nil {
return self.getNext().leaf_get_start(key)
}
return i, self
}
@ -299,7 +299,7 @@ func (self *BpNode) leaf_split(key Hashable, value interface{}) (a, b *BpNode, e
}
a = self
b = NewLeaf(self.NodeSize())
insert_linked_list_node(b, a, a.next)
insert_linked_list_node(b, a, a.getNext())
balance_nodes(a, b)
if key.Less(b.keys[0]) {
if err := a.put_kv(key, value); err != nil {
@ -335,7 +335,7 @@ func (self *BpNode) pure_leaf_split(key Hashable, value interface{}) (a, b *BpNo
if err := a.put_kv(key, value); err != nil {
return nil, nil, err
}
insert_linked_list_node(a, b.prev, b)
insert_linked_list_node(a, b.getPrev(), b)
return a, b, nil
} else {
a = self
@ -350,7 +350,7 @@ func (self *BpNode) pure_leaf_split(key Hashable, value interface{}) (a, b *BpNo
if err := b.put_kv(key, value); err != nil {
return nil, nil, err
}
insert_linked_list_node(b, e, e.next)
insert_linked_list_node(b, e, e.getNext())
if e.keys[0].Equals(key) {
return a, nil, nil
}
@ -516,12 +516,12 @@ func (self *BpNode) leaf_remove(key, stop Hashable, where WhereFunc) (a *BpNode,
}
if len(l.keys) == 0 {
remove_linked_list_node(l)
if l.next == nil {
if l.getNext() == nil {
a = nil
} else if stop == nil {
a = nil
} else if !l.next.keys[0].Equals(stop) {
a = l.next
} else if !l.getNext().keys[0].Equals(stop) {
a = l.getNext()
} else {
a = nil
}
@ -587,10 +587,10 @@ func (self *BpNode) find(key Hashable) (int, bool) {
func (self *BpNode) find_end_of_pure_run() *BpNode {
k := self.keys[0]
p := self
n := self.next
n := self.getNext()
for n != nil && n.Pure() && k.Equals(n.keys[0]) {
p = n
n = n.next
n = n.getNext()
}
return p
}
@ -659,25 +659,25 @@ func (self *BpNode) backward(from, to Hashable) (li loc_iterator) {
}
func insert_linked_list_node(n, prev, next *BpNode) {
if (prev != nil && prev.next != next) || (next != nil && next.prev != prev) {
if (prev != nil && prev.getNext() != next) || (next != nil && next.getPrev() != prev) {
panic(BpTreeError("prev and next not hooked up"))
}
n.prev = prev
n.next = next
n.setPrev(prev)
n.setNext(next)
if prev != nil {
prev.next = n
prev.setNext(n)
}
if next != nil {
next.prev = n
next.setPrev(n)
}
}
func remove_linked_list_node(n *BpNode) {
if n.prev != nil {
n.prev.next = n.next
if n.getPrev() != nil {
n.getPrev().setNext(n.getNext())
}
if n.next != nil {
n.next.prev = n.prev
if n.getNext() != nil {
n.getNext().setPrev(n.getPrev())
}
}

View file

@ -378,7 +378,7 @@ func Test_get_end(t *testing.T) {
}
i, n = root.get_end(Int(3))
t.Log(n)
if n != root.pointers[1].next {
if n != root.pointers[1].getNext() {
t.Error("wrong node from get_end")
}
if i != 1 {
@ -452,11 +452,11 @@ func Test_put_no_root_split(t *testing.T) {
if !p.has(Int(1)) {
t.Error("p didn't have the right keys", p)
}
if p.next == nil {
if p.getNext() == nil {
t.Error("p.next should not be nil")
}
t.Log(p)
t.Log(p.next)
t.Log(p.getNext())
}
}
@ -839,34 +839,34 @@ func Test_pure_leaf_insert_split_less(t *testing.T) {
if p == nil || len(p.keys) != 1 || !p.keys[0].Equals(Int(2)) {
t.Errorf("p did not contain the right key")
}
if p.prev != nil {
if p.getPrev() != nil {
t.Errorf("expected p.prev == nil")
}
if p.next != a {
if p.getNext() != a {
t.Errorf("expected p.next == a")
}
if a.prev != p {
if a.getPrev() != p {
t.Errorf("expected a.prev == p")
}
if a.next != b {
if a.getNext() != b {
t.Errorf("expected a.next == b")
}
if b.prev != a {
if b.getPrev() != a {
t.Errorf("expected b.prev == a")
}
if b.next != c {
if b.getNext() != c {
t.Errorf("expected b.next == c")
}
if c.prev != b {
if c.getPrev() != b {
t.Errorf("expected c.prev == b")
}
if c.next != d {
if c.getNext() != d {
t.Errorf("expected c.next == d")
}
if d.prev != c {
if d.getPrev() != c {
t.Errorf("expected d.prev == c")
}
if d.next != nil {
if d.getNext() != nil {
t.Errorf("expected d.next == nil")
}
}
@ -912,34 +912,34 @@ func Test_pure_leaf_split_less(t *testing.T) {
if p == nil || len(p.keys) != 1 || !p.keys[0].Equals(Int(2)) {
t.Errorf("p did not contain the right key")
}
if p.prev != nil {
if p.getPrev() != nil {
t.Errorf("expected p.prev == nil")
}
if p.next != a {
if p.getNext() != a {
t.Errorf("expected p.next == a")
}
if a.prev != p {
if a.getPrev() != p {
t.Errorf("expected a.prev == p")
}
if a.next != b {
if a.getNext() != b {
t.Errorf("expected a.next == b")
}
if b.prev != a {
if b.getPrev() != a {
t.Errorf("expected b.prev == a")
}
if b.next != c {
if b.getNext() != c {
t.Errorf("expected b.next == c")
}
if c.prev != b {
if c.getPrev() != b {
t.Errorf("expected c.prev == b")
}
if c.next != d {
if c.getNext() != d {
t.Errorf("expected c.next == d")
}
if d.prev != c {
if d.getPrev() != c {
t.Errorf("expected d.prev == c")
}
if d.next != nil {
if d.getNext() != nil {
t.Errorf("expected d.next == nil")
}
}
@ -982,28 +982,28 @@ func Test_pure_leaf_split_equal(t *testing.T) {
if q != nil {
t.Errorf("q != nil")
}
if a.prev != nil {
if a.getPrev() != nil {
t.Errorf("expected a.prev == nil")
}
if a.next != b {
if a.getNext() != b {
t.Errorf("expected a.next == b")
}
if b.prev != a {
if b.getPrev() != a {
t.Errorf("expected b.prev == a")
}
if b.next != c {
if b.getNext() != c {
t.Errorf("expected b.next == c")
}
if c.prev != b {
if c.getPrev() != b {
t.Errorf("expected c.prev == b")
}
if c.next != d {
if c.getNext() != d {
t.Errorf("expected c.next == d")
}
if d.prev != c {
if d.getPrev() != c {
t.Errorf("expected d.prev == c")
}
if d.next != nil {
if d.getNext() != nil {
t.Errorf("expected d.next == nil")
}
}
@ -1046,34 +1046,34 @@ func Test_pure_leaf_split_greater(t *testing.T) {
if q == nil || len(q.keys) != 1 || !q.keys[0].Equals(Int(4)) {
t.Errorf("q != nil")
}
if a.prev != nil {
if a.getPrev() != nil {
t.Errorf("expected a.prev == nil")
}
if a.next != b {
if a.getNext() != b {
t.Errorf("expected a.next == b")
}
if b.prev != a {
if b.getPrev() != a {
t.Errorf("expected b.prev == a")
}
if b.next != c {
if b.getNext() != c {
t.Errorf("expected b.next == c")
}
if c.prev != b {
if c.getPrev() != b {
t.Errorf("expected c.prev == b")
}
if c.next != q {
if c.getNext() != q {
t.Errorf("expected c.next == q")
}
if q.prev != c {
if q.getPrev() != c {
t.Errorf("expected q.prev == c")
}
if q.next != d {
if q.getNext() != d {
t.Errorf("expected q.next == d")
}
if d.prev != q {
if d.getPrev() != q {
t.Errorf("expected d.prev == q")
}
if d.next != nil {
if d.getNext() != nil {
t.Errorf("expected d.next == nil")
}
}
@ -1124,28 +1124,28 @@ func Test_insert_linked_list_node(t *testing.T) {
insert_linked_list_node(c, b, nil)
d := NewLeaf(4)
insert_linked_list_node(d, a, b)
if a.prev != nil {
if a.getPrev() != nil {
t.Errorf("expected a.prev == nil")
}
if a.next != d {
if a.getNext() != d {
t.Errorf("expected a.next == d")
}
if d.prev != a {
if d.getPrev() != a {
t.Errorf("expected d.prev == a")
}
if d.next != b {
if d.getNext() != b {
t.Errorf("expected d.next == b")
}
if b.prev != d {
if b.getPrev() != d {
t.Errorf("expected b.prev == d")
}
if b.next != c {
if b.getNext() != c {
t.Errorf("expected b.next == c")
}
if c.prev != b {
if c.getPrev() != b {
t.Errorf("expected c.prev == b")
}
if c.next != nil {
if c.getNext() != nil {
t.Errorf("expected c.next == nil")
}
}
@ -1159,67 +1159,67 @@ func Test_remove_linked_list_node(t *testing.T) {
insert_linked_list_node(c, b, nil)
d := NewLeaf(4)
insert_linked_list_node(d, a, b)
if a.prev != nil {
if a.getPrev() != nil {
t.Errorf("expected a.prev == nil")
}
if a.next != d {
if a.getNext() != d {
t.Errorf("expected a.next == d")
}
if d.prev != a {
if d.getPrev() != a {
t.Errorf("expected d.prev == a")
}
if d.next != b {
if d.getNext() != b {
t.Errorf("expected d.next == b")
}
if b.prev != d {
if b.getPrev() != d {
t.Errorf("expected b.prev == d")
}
if b.next != c {
if b.getNext() != c {
t.Errorf("expected b.next == c")
}
if c.prev != b {
if c.getPrev() != b {
t.Errorf("expected c.prev == b")
}
if c.next != nil {
if c.getNext() != nil {
t.Errorf("expected c.next == nil")
}
remove_linked_list_node(d)
if a.prev != nil {
if a.getPrev() != nil {
t.Errorf("expected a.prev == nil")
}
if a.next != b {
if a.getNext() != b {
t.Errorf("expected a.next == b")
}
if b.prev != a {
if b.getPrev() != a {
t.Errorf("expected b.prev == a")
}
if b.next != c {
if b.getNext() != c {
t.Errorf("expected b.next == c")
}
if c.prev != b {
if c.getPrev() != b {
t.Errorf("expected c.prev == b")
}
if c.next != nil {
if c.getNext() != nil {
t.Errorf("expected c.next == nil")
}
remove_linked_list_node(a)
if b.prev != nil {
if b.getPrev() != nil {
t.Errorf("expected b.prev == nil")
}
if b.next != c {
if b.getNext() != c {
t.Errorf("expected b.next == c")
}
if c.prev != b {
if c.getPrev() != b {
t.Errorf("expected c.prev == b")
}
if c.next != nil {
if c.getNext() != nil {
t.Errorf("expected c.next == nil")
}
remove_linked_list_node(c)
if b.prev != nil {
if b.getPrev() != nil {
t.Errorf("expected b.prev == nil")
}
if b.next != nil {
if b.getNext() != nil {
t.Errorf("expected b.next == nil")
}
remove_linked_list_node(b)

View file

@ -13,3 +13,16 @@ func (self *BpTree) getRoot() *BpNode {
func (self *BpTree) setRoot(root *BpNode) {
self.root = root
}
func (self *BpNode) getNext() *BpNode {
return self.next
}
func (self *BpNode) setNext(next *BpNode) {
self.next = next
}
func (self *BpNode) getPrev() *BpNode {
return self.prev
}
func (self *BpNode) setPrev(prev *BpNode) {
self.prev = prev
}