Merge pull request #1324 from twpayne/rbush-improvements
RBush improvements
This commit is contained in:
@@ -454,7 +454,7 @@ ol.interaction.Modify.prototype.handleMouseMove_ = function(evt) {
|
|||||||
this.modifiable_ = false;
|
this.modifiable_ = false;
|
||||||
var vertexFeature = this.vertexFeature_;
|
var vertexFeature = this.vertexFeature_;
|
||||||
var rBush = this.rBush_;
|
var rBush = this.rBush_;
|
||||||
var nodes = rBush.allInExtent(box);
|
var nodes = rBush.getAllInExtent(box);
|
||||||
var renderIntent = ol.layer.VectorLayerRenderIntent.HIDDEN;
|
var renderIntent = ol.layer.VectorLayerRenderIntent.HIDDEN;
|
||||||
if (nodes.length > 0) {
|
if (nodes.length > 0) {
|
||||||
nodes.sort(sortByDistance);
|
nodes.sort(sortByDistance);
|
||||||
|
|||||||
+84
-34
@@ -189,22 +189,14 @@ ol.structs.RBush = function(opt_maxEntries) {
|
|||||||
*/
|
*/
|
||||||
this.valueExtent_ = {};
|
this.valueExtent_ = {};
|
||||||
|
|
||||||
};
|
if (goog.DEBUG) {
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
this.readers_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return {Array.<T>} All.
|
|
||||||
*/
|
|
||||||
ol.structs.RBush.prototype.all = function() {
|
|
||||||
var values = [];
|
|
||||||
this.forEach(
|
|
||||||
/**
|
|
||||||
* @param {T} value Value.
|
|
||||||
*/
|
|
||||||
function(value) {
|
|
||||||
values.push(value);
|
|
||||||
});
|
|
||||||
return values;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -237,23 +229,6 @@ ol.structs.RBush.prototype.allDistMargin_ = function(node, compare) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {ol.Extent} extent Extent.
|
|
||||||
* @return {Array.<T>} All in extent.
|
|
||||||
*/
|
|
||||||
ol.structs.RBush.prototype.allInExtent = function(extent) {
|
|
||||||
var values = [];
|
|
||||||
this.forEachInExtent(extent,
|
|
||||||
/**
|
|
||||||
* @param {T} value Value.
|
|
||||||
*/
|
|
||||||
function(value) {
|
|
||||||
values.push(value);
|
|
||||||
});
|
|
||||||
return values;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FIXME empty description for jsdoc
|
* FIXME empty description for jsdoc
|
||||||
*/
|
*/
|
||||||
@@ -387,7 +362,16 @@ ol.structs.RBush.prototype.condense_ = function(path) {
|
|||||||
* @template S
|
* @template S
|
||||||
*/
|
*/
|
||||||
ol.structs.RBush.prototype.forEach = function(callback, opt_obj) {
|
ol.structs.RBush.prototype.forEach = function(callback, opt_obj) {
|
||||||
return this.forEach_(this.root_, callback, opt_obj);
|
if (goog.DEBUG) {
|
||||||
|
++this.readers_;
|
||||||
|
try {
|
||||||
|
return this.forEach_(this.root_, callback, opt_obj);
|
||||||
|
} finally {
|
||||||
|
--this.readers_;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return this.forEach_(this.root_, callback, opt_obj);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -432,6 +416,29 @@ ol.structs.RBush.prototype.forEach_ = function(node, callback, opt_obj) {
|
|||||||
*/
|
*/
|
||||||
ol.structs.RBush.prototype.forEachInExtent =
|
ol.structs.RBush.prototype.forEachInExtent =
|
||||||
function(extent, callback, opt_obj) {
|
function(extent, callback, opt_obj) {
|
||||||
|
if (goog.DEBUG) {
|
||||||
|
++this.readers_;
|
||||||
|
try {
|
||||||
|
return this.forEachInExtent_(extent, callback, opt_obj);
|
||||||
|
} finally {
|
||||||
|
--this.readers_;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return this.forEachInExtent_(extent, callback, opt_obj);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ol.Extent} extent Extent.
|
||||||
|
* @param {function(this: S, T): *} callback Callback.
|
||||||
|
* @param {S=} opt_obj Scope.
|
||||||
|
* @private
|
||||||
|
* @return {*} Callback return value.
|
||||||
|
* @template S
|
||||||
|
*/
|
||||||
|
ol.structs.RBush.prototype.forEachInExtent_ =
|
||||||
|
function(extent, callback, opt_obj) {
|
||||||
/** @type {Array.<ol.structs.RBushNode.<T>>} */
|
/** @type {Array.<ol.structs.RBushNode.<T>>} */
|
||||||
var toVisit = [this.root_];
|
var toVisit = [this.root_];
|
||||||
var result;
|
var result;
|
||||||
@@ -480,6 +487,39 @@ ol.structs.RBush.prototype.forEachNode = function(callback, opt_obj) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {Array.<T>} All.
|
||||||
|
*/
|
||||||
|
ol.structs.RBush.prototype.getAll = function() {
|
||||||
|
var values = [];
|
||||||
|
this.forEach(
|
||||||
|
/**
|
||||||
|
* @param {T} value Value.
|
||||||
|
*/
|
||||||
|
function(value) {
|
||||||
|
values.push(value);
|
||||||
|
});
|
||||||
|
return values;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ol.Extent} extent Extent.
|
||||||
|
* @return {Array.<T>} All in extent.
|
||||||
|
*/
|
||||||
|
ol.structs.RBush.prototype.getAllInExtent = function(extent) {
|
||||||
|
var values = [];
|
||||||
|
this.forEachInExtent(extent,
|
||||||
|
/**
|
||||||
|
* @param {T} value Value.
|
||||||
|
*/
|
||||||
|
function(value) {
|
||||||
|
values.push(value);
|
||||||
|
});
|
||||||
|
return values;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {T} value Value.
|
* @param {T} value Value.
|
||||||
* @private
|
* @private
|
||||||
@@ -496,6 +536,9 @@ ol.structs.RBush.prototype.getKey_ = function(value) {
|
|||||||
* @param {T} value Value.
|
* @param {T} value Value.
|
||||||
*/
|
*/
|
||||||
ol.structs.RBush.prototype.insert = function(extent, value) {
|
ol.structs.RBush.prototype.insert = function(extent, value) {
|
||||||
|
if (goog.DEBUG && this.readers_) {
|
||||||
|
throw Error('cannot insert value while reading');
|
||||||
|
}
|
||||||
var key = this.getKey_(value);
|
var key = this.getKey_(value);
|
||||||
goog.asserts.assert(!this.valueExtent_.hasOwnProperty(key));
|
goog.asserts.assert(!this.valueExtent_.hasOwnProperty(key));
|
||||||
this.insert_(extent, value, this.root_.height - 1);
|
this.insert_(extent, value, this.root_.height - 1);
|
||||||
@@ -535,6 +578,9 @@ ol.structs.RBush.prototype.insert_ = function(extent, value, level) {
|
|||||||
* @param {T} value Value.
|
* @param {T} value Value.
|
||||||
*/
|
*/
|
||||||
ol.structs.RBush.prototype.remove = function(value) {
|
ol.structs.RBush.prototype.remove = function(value) {
|
||||||
|
if (goog.DEBUG && this.readers_) {
|
||||||
|
throw Error('cannot remove value while reading');
|
||||||
|
}
|
||||||
var key = this.getKey_(value);
|
var key = this.getKey_(value);
|
||||||
goog.asserts.assert(this.valueExtent_.hasOwnProperty(key));
|
goog.asserts.assert(this.valueExtent_.hasOwnProperty(key));
|
||||||
var extent = this.valueExtent_[key];
|
var extent = this.valueExtent_[key];
|
||||||
@@ -568,7 +614,8 @@ ol.structs.RBush.prototype.remove_ = function(extent, value) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
++index;
|
node = path.pop();
|
||||||
|
index = indexes.pop();
|
||||||
} else if (index < node.children.length) {
|
} else if (index < node.children.length) {
|
||||||
child = node.children[index];
|
child = node.children[index];
|
||||||
if (ol.extent.containsExtent(child.extent, extent)) {
|
if (ol.extent.containsExtent(child.extent, extent)) {
|
||||||
@@ -629,6 +676,9 @@ ol.structs.RBush.prototype.splitRoot_ = function(node1, node2) {
|
|||||||
* @param {T} value Value.
|
* @param {T} value Value.
|
||||||
*/
|
*/
|
||||||
ol.structs.RBush.prototype.update = function(extent, value) {
|
ol.structs.RBush.prototype.update = function(extent, value) {
|
||||||
|
if (goog.DEBUG && this.readers_) {
|
||||||
|
throw Error('cannot update value while reading');
|
||||||
|
}
|
||||||
var key = this.getKey_(value);
|
var key = this.getKey_(value);
|
||||||
var currentExtent = this.valueExtent_[key];
|
var currentExtent = this.valueExtent_[key];
|
||||||
goog.asserts.assert(goog.isDef(currentExtent));
|
goog.asserts.assert(goog.isDef(currentExtent));
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ describe('ol.structs.RBush', function() {
|
|||||||
|
|
||||||
describe('when empty', function() {
|
describe('when empty', function() {
|
||||||
|
|
||||||
describe('#all', function() {
|
describe('#getAll', function() {
|
||||||
|
|
||||||
it('returns the expected number of objects', function() {
|
it('returns the expected number of objects', function() {
|
||||||
expect(rBush.all()).to.be.empty();
|
expect(rBush.getAll()).to.be.empty();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -33,15 +33,15 @@ describe('ol.structs.RBush', function() {
|
|||||||
rBush.insert([-3, -3, -2, -2], objs[5]);
|
rBush.insert([-3, -3, -2, -2], objs[5]);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#allInExtent', function() {
|
describe('#getAllInExtent', function() {
|
||||||
|
|
||||||
it('returns the expected objects', function() {
|
it('returns the expected objects', function() {
|
||||||
var result;
|
var result;
|
||||||
result = rBush.allInExtent([2, 2, 3, 3]);
|
result = rBush.getAllInExtent([2, 2, 3, 3]);
|
||||||
expect(result).to.contain(objs[1]);
|
expect(result).to.contain(objs[1]);
|
||||||
expect(result).to.contain(objs[2]);
|
expect(result).to.contain(objs[2]);
|
||||||
expect(result.length).to.be(2);
|
expect(result.length).to.be(2);
|
||||||
result = rBush.allInExtent([-1, -1, 2, 2]);
|
result = rBush.getAllInExtent([-1, -1, 2, 2]);
|
||||||
expect(result).to.contain(objs[0]);
|
expect(result).to.contain(objs[0]);
|
||||||
expect(result).to.contain(objs[1]);
|
expect(result).to.contain(objs[1]);
|
||||||
expect(result).to.contain(objs[2]);
|
expect(result).to.contain(objs[2]);
|
||||||
@@ -50,22 +50,82 @@ describe('ol.structs.RBush', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('returns an empty array when given a disjoint extent', function() {
|
it('returns an empty array when given a disjoint extent', function() {
|
||||||
expect(rBush.allInExtent([5, 5, 6, 6]).length).to.be(0);
|
expect(rBush.getAllInExtent([5, 5, 6, 6]).length).to.be(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#insert', function() {
|
||||||
|
|
||||||
|
it('throws an exception if called while iterating over all values',
|
||||||
|
function() {
|
||||||
|
expect(function() {
|
||||||
|
rBush.forEach(function(value) {
|
||||||
|
rBush.insert([0, 0, 1, 1], {});
|
||||||
|
});
|
||||||
|
}).to.throwException();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws an exception if called while iterating over an extent',
|
||||||
|
function() {
|
||||||
|
expect(function() {
|
||||||
|
rBush.forEachInExtent([-10, -10, 10, 10], function(value) {
|
||||||
|
rBush.insert([0, 0, 1, 1], {});
|
||||||
|
});
|
||||||
|
}).to.throwException();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#remove', function() {
|
describe('#remove', function() {
|
||||||
|
|
||||||
it('can remove each object', function() {
|
it('can remove each object', function() {
|
||||||
var i, ii;
|
var i, ii;
|
||||||
for (i = 0, ii = objs.length; i < ii; ++i) {
|
for (i = 0, ii = objs.length; i < ii; ++i) {
|
||||||
expect(rBush.all()).to.contain(objs[i]);
|
expect(rBush.getAll()).to.contain(objs[i]);
|
||||||
rBush.remove(objs[i]);
|
rBush.remove(objs[i]);
|
||||||
expect(rBush.all()).not.to.contain(objs[i]);
|
expect(rBush.getAll()).not.to.contain(objs[i]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('throws an exception if called while iterating over all values',
|
||||||
|
function() {
|
||||||
|
expect(function() {
|
||||||
|
rBush.forEach(function(value) {
|
||||||
|
rBush.remove(value);
|
||||||
|
});
|
||||||
|
}).to.throwException();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws an exception if called while iterating over an extent',
|
||||||
|
function() {
|
||||||
|
expect(function() {
|
||||||
|
rBush.forEachInExtent([-10, -10, 10, 10], function(value) {
|
||||||
|
rBush.remove(value);
|
||||||
|
});
|
||||||
|
}).to.throwException();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#update', function() {
|
||||||
|
|
||||||
|
it('throws an exception if called while iterating over all values',
|
||||||
|
function() {
|
||||||
|
expect(function() {
|
||||||
|
rBush.forEach(function(value) {
|
||||||
|
rBush.update([0, 0, 1, 1], objs[1]);
|
||||||
|
});
|
||||||
|
}).to.throwException();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws an exception if called while iterating over an extent',
|
||||||
|
function() {
|
||||||
|
expect(function() {
|
||||||
|
rBush.forEachInExtent([-10, -10, 10, 10], function(value) {
|
||||||
|
rBush.update([0, 0, 1, 1], objs[1]);
|
||||||
|
});
|
||||||
|
}).to.throwException();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -84,12 +144,12 @@ describe('ol.structs.RBush', function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#allInExtent', function() {
|
describe('#getAllInExtent', function() {
|
||||||
|
|
||||||
it('returns the expected objects', function() {
|
it('returns the expected objects', function() {
|
||||||
var i, ii;
|
var i, ii;
|
||||||
for (i = 0, ii = objs.length; i < ii; ++i) {
|
for (i = 0, ii = objs.length; i < ii; ++i) {
|
||||||
expect(rBush.allInExtent(extents[i])).to.eql([objs[i]]);
|
expect(rBush.getAllInExtent(extents[i])).to.eql([objs[i]]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -100,11 +160,11 @@ describe('ol.structs.RBush', function() {
|
|||||||
it('can remove each object in turn', function() {
|
it('can remove each object in turn', function() {
|
||||||
var i, ii;
|
var i, ii;
|
||||||
for (i = 0, ii = objs.length; i < ii; ++i) {
|
for (i = 0, ii = objs.length; i < ii; ++i) {
|
||||||
expect(rBush.allInExtent(extents[i])).to.eql([objs[i]]);
|
expect(rBush.getAllInExtent(extents[i])).to.eql([objs[i]]);
|
||||||
rBush.remove(objs[i]);
|
rBush.remove(objs[i]);
|
||||||
expect(rBush.allInExtent(extents[i])).to.be.empty();
|
expect(rBush.getAllInExtent(extents[i])).to.be.empty();
|
||||||
}
|
}
|
||||||
expect(rBush.all()).to.be.empty();
|
expect(rBush.getAll()).to.be.empty();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can remove objects in random order', function() {
|
it('can remove objects in random order', function() {
|
||||||
@@ -118,11 +178,11 @@ describe('ol.structs.RBush', function() {
|
|||||||
}
|
}
|
||||||
for (i = 0, ii = objs.length; i < ii; ++i) {
|
for (i = 0, ii = objs.length; i < ii; ++i) {
|
||||||
var index = indexes[i];
|
var index = indexes[i];
|
||||||
expect(rBush.allInExtent(extents[index])).to.eql([objs[index]]);
|
expect(rBush.getAllInExtent(extents[index])).to.eql([objs[index]]);
|
||||||
rBush.remove(objs[index]);
|
rBush.remove(objs[index]);
|
||||||
expect(rBush.allInExtent(extents[index])).to.be.empty();
|
expect(rBush.getAllInExtent(extents[index])).to.be.empty();
|
||||||
}
|
}
|
||||||
expect(rBush.all()).to.be.empty();
|
expect(rBush.getAll()).to.be.empty();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -141,18 +201,18 @@ describe('ol.structs.RBush', function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#all', function() {
|
describe('#getAll', function() {
|
||||||
|
|
||||||
it('returns the expected number of objects', function() {
|
it('returns the expected number of objects', function() {
|
||||||
expect(rBush.all().length).to.be(1000);
|
expect(rBush.getAll().length).to.be(1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#allInExtent', function() {
|
describe('#getAllInExtent', function() {
|
||||||
|
|
||||||
it('returns the expected number of objects', function() {
|
it('returns the expected number of objects', function() {
|
||||||
expect(rBush.allInExtent([0, 0, 10600, 10600]).length).to.be(1000);
|
expect(rBush.getAllInExtent([0, 0, 10600, 10600]).length).to.be(1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can perform 1000 in-extent searches', function() {
|
it('can perform 1000 in-extent searches', function() {
|
||||||
@@ -163,7 +223,7 @@ describe('ol.structs.RBush', function() {
|
|||||||
var max = [min[0] + Math.random() * 500,
|
var max = [min[0] + Math.random() * 500,
|
||||||
min[1] + Math.random() * 500];
|
min[1] + Math.random() * 500];
|
||||||
var extent = [min[0], min[1], max[0], max[1]];
|
var extent = [min[0], min[1], max[0], max[1]];
|
||||||
n += rBush.allInExtent(extent).length;
|
n += rBush.getAllInExtent(extent).length;
|
||||||
}
|
}
|
||||||
expect(n).not.to.be(0);
|
expect(n).not.to.be(0);
|
||||||
});
|
});
|
||||||
@@ -177,7 +237,7 @@ describe('ol.structs.RBush', function() {
|
|||||||
var max = [min[0] + Math.random() * 500,
|
var max = [min[0] + Math.random() * 500,
|
||||||
min[1] + Math.random() * 500];
|
min[1] + Math.random() * 500];
|
||||||
var extent = [min[0], min[1], max[0], max[1]];
|
var extent = [min[0], min[1], max[0], max[1]];
|
||||||
n += rBush.allInExtent(extent).length;
|
n += rBush.getAllInExtent(extent).length;
|
||||||
}
|
}
|
||||||
expect(n).to.be(0);
|
expect(n).to.be(0);
|
||||||
});
|
});
|
||||||
@@ -195,7 +255,7 @@ describe('ol.structs.RBush', function() {
|
|||||||
var extent = [min[0], min[1], max[0], max[1]];
|
var extent = [min[0], min[1], max[0], max[1]];
|
||||||
rBush.insert(extent, {id: i});
|
rBush.insert(extent, {id: i});
|
||||||
}
|
}
|
||||||
expect(rBush.allInExtent([0, 0, 10600, 10600]).length).to.be(2000);
|
expect(rBush.getAllInExtent([0, 0, 10600, 10600]).length).to.be(2000);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user