Skip to content

Commit

Permalink
Optimise exist(...) and getNode(...) #3
Browse files Browse the repository at this point in the history
  • Loading branch information
bokkypoobah committed Feb 24, 2019
1 parent d540dfc commit d1d3649
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 225 deletions.
29 changes: 4 additions & 25 deletions contracts/BokkyPoobahsRedBlackTreeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,14 @@ library BokkyPooBahsRedBlackTreeLibrary {
return y;
}
function exists(Tree storage self, uint key) internal view returns (bool) {
require(key != SENTINEL);
uint _key = self.root;
while (_key != SENTINEL) {
if (key == _key) {
return true;
}
if (key < _key) {
_key = self.nodes[_key].left;
} else {
_key = self.nodes[_key].right;
}
if (key != SENTINEL) {
return (key == self.root) || (self.nodes[key].parent != SENTINEL);
}
return false;
}
function getNode(Tree storage self, uint key) internal view returns (uint _returnKey, uint _parent, uint _left, uint _right, bool _red) {
require(key != SENTINEL);
uint _key = self.root;
while (_key != SENTINEL) {
if (key == _key) {
Node memory node = self.nodes[key];
return (key, node.parent, node.left, node.right, node.red);
}
if (key < _key) {
_key = self.nodes[_key].left;
} else {
_key = self.nodes[_key].right;
}
}
return (SENTINEL, SENTINEL, SENTINEL, SENTINEL, false);
require(exists(self, key));
return(key, self.nodes[key].parent, self.nodes[key].left, self.nodes[key].right, self.nodes[key].red);
}
function parent(Tree storage self, uint key) internal view returns (uint _parent) {
require(key != SENTINEL);
Expand Down
29 changes: 4 additions & 25 deletions flattened/TestBokkyPooBahsRedBlackTreeRaw_flattened.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,14 @@ library BokkyPooBahsRedBlackTreeLibrary {
return y;
}
function exists(Tree storage self, uint key) internal view returns (bool) {
require(key != SENTINEL);
uint _key = self.root;
while (_key != SENTINEL) {
if (key == _key) {
return true;
}
if (key < _key) {
_key = self.nodes[_key].left;
} else {
_key = self.nodes[_key].right;
}
if (key != SENTINEL) {
return (key == self.root) || (self.nodes[key].parent != SENTINEL);
}
return false;
}
function getNode(Tree storage self, uint key) internal view returns (uint _returnKey, uint _parent, uint _left, uint _right, bool _red) {
require(key != SENTINEL);
uint _key = self.root;
while (_key != SENTINEL) {
if (key == _key) {
Node memory node = self.nodes[key];
return (key, node.parent, node.left, node.right, node.red);
}
if (key < _key) {
_key = self.nodes[_key].left;
} else {
_key = self.nodes[_key].right;
}
}
return (SENTINEL, SENTINEL, SENTINEL, SENTINEL, false);
require(exists(self, key));
return(key, self.nodes[key].parent, self.nodes[key].left, self.nodes[key].right, self.nodes[key].red);
}
function parent(Tree storage self, uint key) internal view returns (uint _parent) {
require(key != SENTINEL);
Expand Down
29 changes: 4 additions & 25 deletions test/BokkyPoobahsRedBlackTreeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,14 @@ library BokkyPooBahsRedBlackTreeLibrary {
return y;
}
function exists(Tree storage self, uint key) internal view returns (bool) {
require(key != SENTINEL);
uint _key = self.root;
while (_key != SENTINEL) {
if (key == _key) {
return true;
}
if (key < _key) {
_key = self.nodes[_key].left;
} else {
_key = self.nodes[_key].right;
}
if (key != SENTINEL) {
return (key == self.root) || (self.nodes[key].parent != SENTINEL);
}
return false;
}
function getNode(Tree storage self, uint key) internal view returns (uint _returnKey, uint _parent, uint _left, uint _right, bool _red) {
require(key != SENTINEL);
uint _key = self.root;
while (_key != SENTINEL) {
if (key == _key) {
Node memory node = self.nodes[key];
return (key, node.parent, node.left, node.right, node.red);
}
if (key < _key) {
_key = self.nodes[_key].left;
} else {
_key = self.nodes[_key].right;
}
}
return (SENTINEL, SENTINEL, SENTINEL, SENTINEL, false);
require(exists(self, key));
return(key, self.nodes[key].parent, self.nodes[key].left, self.nodes[key].right, self.nodes[key].red);
}
function parent(Tree storage self, uint key) internal view returns (uint _parent) {
require(key != SENTINEL);
Expand Down
Loading

0 comments on commit d1d3649

Please sign in to comment.