Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed a couple functions to enable optimization. 10% performance boost

  • Loading branch information...
commit dec71caec40607110fba5933e7c49966841931aa 1 parent c82bae2
@josephg authored
Showing with 31 additions and 32 deletions.
  1. +31 −32 lib/cpBBTree.js
View
63 lib/cpBBTree.js
@@ -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;
@@ -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
@@ -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);
}
};
@@ -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();
Please sign in to comment.
Something went wrong with that request. Please try again.