Skip to content

Commit

Permalink
Changed a couple functions to enable optimization. 10% performance boost
Browse files Browse the repository at this point in the history
  • Loading branch information
josephg committed Jan 29, 2013
1 parent c82bae2 commit dec71ca
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions lib/cpBBTree.js
Expand Up @@ -430,40 +430,43 @@ var bbTreeIntersectsNode = function(a, b)
return (a.bb_l <= b.bb_r && b.bb_l <= a.bb_r && a.bb_b <= b.bb_t && b.bb_b <= a.bb_t);
};

var markLeafQuery = function(subtree, leaf, left, tree, func)
Leaf.prototype.markLeafQuery = function(leaf, left, tree, func)
{
if(bbTreeIntersectsNode(leaf, subtree)){
if(subtree.isLeaf){
if(left){
pairInsert(leaf, subtree, tree);
} else {
if(subtree.stamp < leaf.stamp) pairInsert(subtree, leaf, tree);
if(func) func(leaf.obj, subtree.obj);
}
} else {
markLeafQuery(subtree.A, leaf, left, tree, func);
markLeafQuery(subtree.B, leaf, left, tree, func);
}
if(bbTreeIntersectsNode(leaf, this)){
if(left){
pairInsert(leaf, this, tree);
} else {
if(this.stamp < leaf.stamp) pairInsert(this, leaf, tree);
if(func) func(leaf.obj, this.obj);
}
}
};

Node.prototype.markLeafQuery = function(leaf, left, tree, func)
{
if(bbTreeIntersectsNode(leaf, this)){
this.A.markLeafQuery(leaf, left, tree, func);
this.B.markLeafQuery(leaf, left, tree, func);
}
};

var markLeaf = function(leaf, tree, staticRoot, func)
Leaf.prototype.markSubtree = function(tree, staticRoot, func)
{
if(leaf.stamp == tree.getStamp()){
if(staticRoot) markLeafQuery(staticRoot, leaf, false, tree, func);
if(this.stamp == tree.getStamp()){
if(staticRoot) staticRoot.markLeafQuery(this, false, tree, func);

for(var node = leaf; node.parent; node = node.parent){
for(var node = this; node.parent; node = node.parent){
if(node == node.parent.A){
markLeafQuery(node.parent.B, leaf, true, tree, func);
node.parent.B.markLeafQuery(this, true, tree, func);
} else {
markLeafQuery(node.parent.A, leaf, false, tree, func);
node.parent.A.markLeafQuery(this, false, tree, func);
}
}
} else {
var pair = leaf.pairs;
var pair = this.pairs;
while(pair){
if(leaf === pair.leafB){
if(func) func(pair.leafA.obj, leaf.obj);
if(this === pair.leafB){
if(func) func(pair.leafA.obj, this.obj);
pair = pair.nextB;
} else {
pair = pair.nextA;
Expand All @@ -472,14 +475,10 @@ var markLeaf = function(leaf, tree, staticRoot, func)
}
};

var markSubtree = function(subtree, tree, staticRoot, func)
Node.prototype.markSubtree = function(tree, staticRoot, func)
{
if(subtree.isLeaf){
markLeaf(subtree, tree, staticRoot, func);
} else {
markSubtree(subtree.A, tree, staticRoot, func);
markSubtree(subtree.B, tree, staticRoot, func);
}
this.A.markSubtree(tree, staticRoot, func);
this.B.markSubtree(tree, staticRoot, func);
};

// **** Leaf Functions
Expand Down Expand Up @@ -516,11 +515,11 @@ Leaf.prototype.addPairs = function(tree)
if(dynamicIndex){
var dynamicRoot = dynamicIndex.root;
if(dynamicRoot){
markLeafQuery(dynamicRoot, this, true, dynamicIndex, null);
dynamicRoot.markLeafQuery(this, true, dynamicIndex, null);
}
} else {
var staticRoot = tree.staticIndex.root;
markLeaf(this, tree, staticRoot, null);
this.markSubtree(tree, staticRoot, null);
}
};

Expand Down Expand Up @@ -574,7 +573,7 @@ BBTree.prototype.reindexQuery = function(func)
var staticIndex = this.staticIndex;
var staticRoot = staticIndex && staticIndex.root;

markSubtree(this.root, this, staticRoot, func);
this.root.markSubtree(this, staticRoot, func);
if(staticIndex && !staticRoot) this.collideStatic(this, staticIndex, func);

this.incrementStamp();
Expand Down

0 comments on commit dec71ca

Please sign in to comment.