Skip to content

Commit

Permalink
workspace:
Browse files Browse the repository at this point in the history
[FIXED] Algorithm.difference()
  • Loading branch information
lichaozhy committed Sep 17, 2023
1 parent e81ddf2 commit 36491a3
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Algorithm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,30 @@ export class RangeAxisAlgorithm {
}

difference(A, B, R) {
const { lt, gt, ge } = this.#COMP;

for (let i = 0, j = 0; i < A.length; i++) {
let { from, to } = A[i];
let { from, to } = A[i], spent = false;

while (j < B.length) {
while (j < B.length && !spent) {
const b = B[j];

if (from.number < b.from.number && b.from.number < to.number) {
if (lt(from.number, b.from.number) && lt(b.from.number, to.number)) {
R.push([from, b.from]);
}

if (b.to.number < to.number) {
if (b.to.number > from.number) {
if (lt(b.to.number, to.number)) {
if (gt(b.to.number, from.number)) {
from = b.to;
}

j++;
}

if (b.to.number > to.number) {
break;
}
spent = gt(b.to.number, to.number);
}

if (j === B.length || B[j].from.number >= to.number) {
if (j === B.length || ge(B[j].from.number, to.number)) {
R.push([from, to]);
}
}
Expand Down

0 comments on commit 36491a3

Please sign in to comment.