Replace faulty iterative ol.structs.RBush#remove with less faulty recursive version

This commit is contained in:
Tom Payne
2014-02-06 01:06:15 +01:00
parent c94b78144f
commit 09326519d3
+29 -32
View File
@@ -616,51 +616,48 @@ ol.structs.RBush.prototype.remove = function(value) {
* @private * @private
*/ */
ol.structs.RBush.prototype.remove_ = function(extent, value) { ol.structs.RBush.prototype.remove_ = function(extent, value) {
var node = this.root_; var path = [this.root_];
var index = 0; var removed = this.removeRecursive_(this.root_, extent, value, path);
/** @type {Array.<ol.structs.RBushNode.<T>>} */ goog.asserts.assert(removed);
var path = [node]; this.condense_(path);
/** @type {Array.<number>} */ };
var indexes = [0];
var childrenDone, child, children, i, ii;
while (path.length > 0) { /**
childrenDone = false; * @param {ol.structs.RBushNode.<T>} node Node.
goog.asserts.assert(node.height > 0); * @param {ol.Extent} extent Extent.
* @param {T} value Value.
* @param {Array.<ol.structs.RBushNode.<T>>} path Path.
* @private
* @return {boolean} Removed.
*/
ol.structs.RBush.prototype.removeRecursive_ =
function(node, extent, value, path) {
var children = node.children;
var ii = children.length;
var child, i;
if (node.height == 1) { if (node.height == 1) {
children = node.children; for (i = 0; i < ii; ++i) {
for (i = 0, ii = children.length; i < ii; ++i) {
child = children[i]; child = children[i];
if (child.value === value) { if (child.value === value) {
goog.array.removeAt(children, i); goog.array.removeAt(children, i);
this.condense_(path); return true;
return;
} }
} }
childrenDone = true; } else {
} else if (index < node.children.length) { goog.asserts.assert(node.height > 1);
child = node.children[index]; for (i = 0; i < ii; ++i) {
child = children[i];
if (ol.extent.containsExtent(child.extent, extent)) { if (ol.extent.containsExtent(child.extent, extent)) {
path.push(child); path.push(child);
indexes.push(index + 1); if (this.removeRecursive_(child, extent, value, path)) {
node = child; return true;
index = 0;
} else {
++index;
} }
} else {
childrenDone = true;
}
if (childrenDone) {
var lastPathIndex = path.length - 1;
node = path[lastPathIndex];
index = ++indexes[lastPathIndex];
if (index > node.children.length) {
path.pop(); path.pop();
indexes.pop();
} }
} }
} }
goog.asserts.fail(); return false;
}; };