Browse Source

.

pull/9/head
Jae Kwon 11 years ago
parent
commit
f2464a5445
3 changed files with 73 additions and 73 deletions
  1. +64
    -64
      merkle/iavl.go
  2. +4
    -4
      merkle/iavl_test.go
  3. +5
    -5
      merkle/util.go

+ 64
- 64
merkle/iavl.go View File

@ -94,12 +94,12 @@ func (self *IAVLNode) Value() Value {
func (self *IAVLNode) Left(db Db) Node { func (self *IAVLNode) Left(db Db) Node {
if self.left == nil { return nil } if self.left == nil { return nil }
return self.left_filled(db)
return self.leftFilled(db)
} }
func (self *IAVLNode) Right(db Db) Node { func (self *IAVLNode) Right(db Db) Node {
if self.right == nil { return nil } if self.right == nil { return nil }
return self.right_filled(db)
return self.rightFilled(db)
} }
func (self *IAVLNode) Size() uint64 { func (self *IAVLNode) Size() uint64 {
@ -117,9 +117,9 @@ func (self *IAVLNode) Has(db Db, key Key) (has bool) {
if self.key.Equals(key) { if self.key.Equals(key) {
return true return true
} else if key.Less(self.key) { } else if key.Less(self.key) {
return self.left_filled(db).Has(db, key)
return self.leftFilled(db).Has(db, key)
} else { } else {
return self.right_filled(db).Has(db, key)
return self.rightFilled(db).Has(db, key)
} }
} }
@ -128,9 +128,9 @@ func (self *IAVLNode) Get(db Db, key Key) (value Value) {
if self.key.Equals(key) { if self.key.Equals(key) {
return self.value return self.value
} else if key.Less(self.key) { } else if key.Less(self.key) {
return self.left_filled(db).Get(db, key)
return self.leftFilled(db).Get(db, key)
} else { } else {
return self.right_filled(db).Get(db, key)
return self.rightFilled(db).Get(db, key)
} }
} }
@ -164,36 +164,36 @@ func (self *IAVLNode) Put(db Db, key Key, value Value) (_ *IAVLNode, updated boo
} }
if key.Less(self.key) { if key.Less(self.key) {
self.left, updated = self.left_filled(db).Put(db, key, value)
self.left, updated = self.leftFilled(db).Put(db, key, value)
} else { } else {
self.right, updated = self.right_filled(db).Put(db, key, value)
self.right, updated = self.rightFilled(db).Put(db, key, value)
} }
if updated { if updated {
return self, updated return self, updated
} else { } else {
self.calc_height_and_size(db)
self.calcHeightAndSize(db)
return self.balance(db), updated return self.balance(db), updated
} }
} }
func (self *IAVLNode) Remove(db Db, key Key) (new_self *IAVLNode, value Value, err error) {
func (self *IAVLNode) Remove(db Db, key Key) (newSelf *IAVLNode, value Value, err error) {
if self == nil { return nil, nil, NotFound(key) } if self == nil { return nil, nil, NotFound(key) }
if self.key.Equals(key) { if self.key.Equals(key) {
if self.left != nil && self.right != nil { if self.left != nil && self.right != nil {
if self.left_filled(db).Size() < self.right_filled(db).Size() {
self, new_self = self.pop_node(db, self.right_filled(db).lmd(db))
if self.leftFilled(db).Size() < self.rightFilled(db).Size() {
self, newSelf = self.popNode(db, self.rightFilled(db).lmd(db))
} else { } else {
self, new_self = self.pop_node(db, self.left_filled(db).rmd(db))
self, newSelf = self.popNode(db, self.leftFilled(db).rmd(db))
} }
new_self.left = self.left
new_self.right = self.right
new_self.calc_height_and_size(db)
return new_self, self.value, nil
newSelf.left = self.left
newSelf.right = self.right
newSelf.calcHeightAndSize(db)
return newSelf, self.value, nil
} else if self.left == nil { } else if self.left == nil {
return self.right_filled(db), self.value, nil
return self.rightFilled(db), self.value, nil
} else if self.right == nil { } else if self.right == nil {
return self.left_filled(db), self.value, nil
return self.leftFilled(db), self.value, nil
} else { } else {
return nil, self.value, nil return nil, self.value, nil
} }
@ -203,30 +203,30 @@ func (self *IAVLNode) Remove(db Db, key Key) (new_self *IAVLNode, value Value, e
if self.left == nil { if self.left == nil {
return self, nil, NotFound(key) return self, nil, NotFound(key)
} }
var new_left *IAVLNode
new_left, value, err = self.left_filled(db).Remove(db, key)
if new_left == self.left_filled(db) { // not found
var newLeft *IAVLNode
newLeft, value, err = self.leftFilled(db).Remove(db, key)
if newLeft == self.leftFilled(db) { // not found
return self, nil, err return self, nil, err
} else if err != nil { // some other error } else if err != nil { // some other error
return self, value, err return self, value, err
} }
self = self.Copy() self = self.Copy()
self.left = new_left
self.left = newLeft
} else { } else {
if self.right == nil { if self.right == nil {
return self, nil, NotFound(key) return self, nil, NotFound(key)
} }
var new_right *IAVLNode
new_right, value, err = self.right_filled(db).Remove(db, key)
if new_right == self.right_filled(db) { // not found
var newRight *IAVLNode
newRight, value, err = self.rightFilled(db).Remove(db, key)
if newRight == self.rightFilled(db) { // not found
return self, nil, err return self, nil, err
} else if err != nil { // some other error } else if err != nil { // some other error
return self, value, err return self, value, err
} }
self = self.Copy() self = self.Copy()
self.right = new_right
self.right = newRight
} }
self.calc_height_and_size(db)
self.calcHeightAndSize(db)
return self.balance(db), value, err return self.balance(db), value, err
} }
@ -297,19 +297,19 @@ func (self *IAVLNode) saveToCountHashes(buf []byte) (int, uint64) {
return cur, hashCount return cur, hashCount
} }
func (self *IAVLNode) left_filled(db Db) *IAVLNode {
func (self *IAVLNode) leftFilled(db Db) *IAVLNode {
// XXX // XXX
return self.left return self.left
} }
func (self *IAVLNode) right_filled(db Db) *IAVLNode {
func (self *IAVLNode) rightFilled(db Db) *IAVLNode {
// XXX // XXX
return self.right return self.right
} }
// Returns a new tree (unless node is the root) & a copy of the popped node. // Returns a new tree (unless node is the root) & a copy of the popped node.
// Can only pop nodes that have one or no children. // Can only pop nodes that have one or no children.
func (self *IAVLNode) pop_node(db Db, node *IAVLNode) (new_self, new_node *IAVLNode) {
func (self *IAVLNode) popNode(db Db, node *IAVLNode) (newSelf, new_node *IAVLNode) {
if self == nil { if self == nil {
panic("self can't be nil") panic("self can't be nil")
} else if node == nil { } else if node == nil {
@ -322,96 +322,96 @@ func (self *IAVLNode) pop_node(db Db, node *IAVLNode) (new_self, new_node *IAVLN
var n *IAVLNode var n *IAVLNode
if node.left != nil { if node.left != nil {
n = node.left_filled(db)
n = node.leftFilled(db)
} else if node.right != nil { } else if node.right != nil {
n = node.right_filled(db)
n = node.rightFilled(db)
} else { } else {
n = nil n = nil
} }
node = node.Copy() node = node.Copy()
node.left = nil node.left = nil
node.right = nil node.right = nil
node.calc_height_and_size(db)
node.calcHeightAndSize(db)
return n, node return n, node
} else { } else {
self = self.Copy() self = self.Copy()
if node.key.Less(self.key) { if node.key.Less(self.key) {
self.left, node = self.left_filled(db).pop_node(db, node)
self.left, node = self.leftFilled(db).popNode(db, node)
} else { } else {
self.right, node = self.right_filled(db).pop_node(db, node)
self.right, node = self.rightFilled(db).popNode(db, node)
} }
self.calc_height_and_size(db)
self.calcHeightAndSize(db)
return self, node return self, node
} }
} }
func (self *IAVLNode) rotate_right(db Db) *IAVLNode {
func (self *IAVLNode) rotateRight(db Db) *IAVLNode {
self = self.Copy() self = self.Copy()
sl := self.left_filled(db).Copy()
sl := self.leftFilled(db).Copy()
slr := sl.right slr := sl.right
sl.right = self sl.right = self
self.left = slr self.left = slr
self.calc_height_and_size(db)
sl.calc_height_and_size(db)
self.calcHeightAndSize(db)
sl.calcHeightAndSize(db)
return sl return sl
} }
func (self *IAVLNode) rotate_left(db Db) *IAVLNode {
func (self *IAVLNode) rotateLeft(db Db) *IAVLNode {
self = self.Copy() self = self.Copy()
sr := self.right_filled(db).Copy()
sr := self.rightFilled(db).Copy()
srl := sr.left srl := sr.left
sr.left = self sr.left = self
self.right = srl self.right = srl
self.calc_height_and_size(db)
sr.calc_height_and_size(db)
self.calcHeightAndSize(db)
sr.calcHeightAndSize(db)
return sr return sr
} }
func (self *IAVLNode) calc_height_and_size(db Db) {
self.height = maxUint8(self.left_filled(db).Height(), self.right_filled(db).Height()) + 1
self.size = self.left_filled(db).Size() + self.right_filled(db).Size() + 1
func (self *IAVLNode) calcHeightAndSize(db Db) {
self.height = maxUint8(self.leftFilled(db).Height(), self.rightFilled(db).Height()) + 1
self.size = self.leftFilled(db).Size() + self.rightFilled(db).Size() + 1
} }
func (self *IAVLNode) calc_balance(db Db) int {
func (self *IAVLNode) calcBalance(db Db) int {
if self == nil { if self == nil {
return 0 return 0
} }
return int(self.left_filled(db).Height()) - int(self.right_filled(db).Height())
return int(self.leftFilled(db).Height()) - int(self.rightFilled(db).Height())
} }
func (self *IAVLNode) balance(db Db) (new_self *IAVLNode) {
balance := self.calc_balance(db)
func (self *IAVLNode) balance(db Db) (newSelf *IAVLNode) {
balance := self.calcBalance(db)
if (balance > 1) { if (balance > 1) {
if (self.left_filled(db).calc_balance(db) >= 0) {
if (self.leftFilled(db).calcBalance(db) >= 0) {
// Left Left Case // Left Left Case
return self.rotate_right(db)
return self.rotateRight(db)
} else { } else {
// Left Right Case // Left Right Case
self = self.Copy() self = self.Copy()
self.left = self.left_filled(db).rotate_left(db)
//self.calc_height_and_size()
return self.rotate_right(db)
self.left = self.leftFilled(db).rotateLeft(db)
//self.calcHeightAndSize()
return self.rotateRight(db)
} }
} }
if (balance < -1) { if (balance < -1) {
if (self.right_filled(db).calc_balance(db) <= 0) {
if (self.rightFilled(db).calcBalance(db) <= 0) {
// Right Right Case // Right Right Case
return self.rotate_left(db)
return self.rotateLeft(db)
} else { } else {
// Right Left Case // Right Left Case
self = self.Copy() self = self.Copy()
self.right = self.right_filled(db).rotate_right(db)
//self.calc_height_and_size()
return self.rotate_left(db)
self.right = self.rightFilled(db).rotateRight(db)
//self.calcHeightAndSize()
return self.rotateLeft(db)
} }
} }
// Nothing changed // Nothing changed
@ -429,11 +429,11 @@ func (self *IAVLNode) _md(side func(*IAVLNode)*IAVLNode) (*IAVLNode) {
} }
func (self *IAVLNode) lmd(db Db) (*IAVLNode) { func (self *IAVLNode) lmd(db Db) (*IAVLNode) {
return self._md(func(node *IAVLNode)*IAVLNode { return node.left_filled(db) })
return self._md(func(node *IAVLNode)*IAVLNode { return node.leftFilled(db) })
} }
func (self *IAVLNode) rmd(db Db) (*IAVLNode) { func (self *IAVLNode) rmd(db Db) (*IAVLNode) {
return self._md(func(node *IAVLNode)*IAVLNode { return node.right_filled(db) })
return self._md(func(node *IAVLNode)*IAVLNode { return node.rightFilled(db) })
} }
func maxUint8(a, b uint8) uint8 { func maxUint8(a, b uint8) uint8 {


+ 4
- 4
merkle/iavl_test.go View File

@ -172,7 +172,7 @@ func TestGriffin(t *testing.T) {
left: l, left: l,
right: r, right: r,
} }
n.calc_height_and_size(nil)
n.calcHeightAndSize(nil)
n.Hash() n.Hash()
return n return n
} }
@ -183,11 +183,11 @@ func TestGriffin(t *testing.T) {
if n.left == nil && n.right == nil { if n.left == nil && n.right == nil {
return fmt.Sprintf("%v", n.key) return fmt.Sprintf("%v", n.key)
} else if n.left == nil { } else if n.left == nil {
return fmt.Sprintf("(- %v %v)", n.key, P(n.right_filled(nil)))
return fmt.Sprintf("(- %v %v)", n.key, P(n.rightFilled(nil)))
} else if n.right == nil { } else if n.right == nil {
return fmt.Sprintf("(%v %v -)", P(n.left_filled(nil)), n.key)
return fmt.Sprintf("(%v %v -)", P(n.leftFilled(nil)), n.key)
} else { } else {
return fmt.Sprintf("(%v %v %v)", P(n.left_filled(nil)), n.key, P(n.right_filled(nil)))
return fmt.Sprintf("(%v %v %v)", P(n.leftFilled(nil)), n.key, P(n.rightFilled(nil)))
} }
} }


+ 5
- 5
merkle/util.go View File

@ -7,8 +7,8 @@ import (
func Iterator(node Node) NodeIterator { func Iterator(node Node) NodeIterator {
stack := make([]Node, 0, 10) stack := make([]Node, 0, 10)
var cur Node = node var cur Node = node
var tn_iterator NodeIterator
tn_iterator = func()(tn Node) {
var itr NodeIterator
itr = func()(tn Node) {
if len(stack) > 0 || cur != nil { if len(stack) > 0 || cur != nil {
for cur != nil { for cur != nil {
stack = append(stack, cur) stack = append(stack, cur)
@ -22,7 +22,7 @@ func Iterator(node Node) NodeIterator {
return nil return nil
} }
} }
return tn_iterator
return itr
} }
func pop(stack []Node) ([]Node, Node) { func pop(stack []Node) ([]Node, Node) {
@ -47,8 +47,8 @@ func printIAVLNode(node *IAVLNode, indent int) {
if node == nil { if node == nil {
fmt.Printf("%s--\n", indentPrefix) fmt.Printf("%s--\n", indentPrefix)
} else { } else {
printIAVLNode(node.left_filled(nil), indent+1)
printIAVLNode(node.leftFilled(nil), indent+1)
fmt.Printf("%s%v:%v\n", indentPrefix, node.key, node.height) fmt.Printf("%s%v:%v\n", indentPrefix, node.key, node.height)
printIAVLNode(node.right_filled(nil), indent+1)
printIAVLNode(node.rightFilled(nil), indent+1)
} }
} }

Loading…
Cancel
Save