0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 17:00:36 -05:00

Add the ability to remove interval from intervaltree.

This commit is contained in:
Andrey Antukh 2016-06-08 21:20:36 +03:00
parent a9a7b830be
commit a8c79d192a
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95

View file

@ -54,6 +54,12 @@ goog.scope(function() {
return this; return this;
} }
remove(item) {
const interval = makeInterval(item)
this.root = remove(this.root, interval);
return this;
}
contains(point) { contains(point) {
assert(goog.isNumber(point)); assert(goog.isNumber(point));
assert(this.root !== null); assert(this.root !== null);
@ -109,6 +115,50 @@ goog.scope(function() {
return root; return root;
} }
function remove(root, interval) {
if (root === null) {
return root;
} else if (root.interval.start === interval.start &&
root.interval.end === interval.end) {
if (root.left === null) {
return root.right;
} else if (root.right === null) {
return root.left;
} else {
const result = removeLeftMost(root.right);
const newroot = result[0];
const newright = result[1];
newroot.left = root.left;
newroot.right = newright;
return newroot;
}
} else {
root.left = remove(root.left, interval);
root.right = remove(root.right, interval);
root.height = calculateHeight(root);
root.maxEnd = calculateMaxEnd(root);
return root;
}
}
function removeLeftMost(root) {
if (root === null) {
return [null, null];
} else if (root.left === null) {
return [root, root.right];
} else {
const result = removeLeftMost(root.left);
const newroot = result[0];
const newleft = result[1];
root.left = newleft;
return [newroot, root];
}
}
function calculateMaxEnd(node) { function calculateMaxEnd(node) {
const left = node.left ? node.left.maxEnd : 0; const left = node.left ? node.left.maxEnd : 0;
const right = node.right ? node.right.maxEnd: 0; const right = node.right ? node.right.maxEnd: 0;