Skip to content

Commit 9335976

Browse files
committed
fix intersection for degenerate intervals
1 parent 6378887 commit 9335976

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/compare.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Interval } from "./interval.ts";
2-
import { Union, typeCheckIsIntervalOrUnion, toUnion, EMPTY } from "./union.ts";
2+
import { Union, typeCheckIsIntervalOrUnion, toUnion, EMPTY, union } from "./union.ts";
33

44
export function ioverlap(a: Interval, b: Interval): boolean {
55
return a.hi >= b.lo && a.lo <= b.hi;
@@ -96,7 +96,8 @@ export function ucomplement(U: Union): Union {
9696
result.push(new Interval(U.intervals[U.intervals.length - 1].hi, Infinity));
9797
}
9898

99-
return new Union(result);
99+
// Need to call the union constructor, because some intervals might touch
100+
return union(result);
100101
}
101102

102103
// Complement of a union or interval

tests/compare.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ describe("intersection of unions", () => {
187187

188188
describe("complement of a union", () => {
189189
it("real union", () => {
190+
assert.deepEqual(
191+
nsf.complement(nsf.union([int(3, 8)])),
192+
nsf.union([int(-inf, 3), int(8, inf)])
193+
);
194+
195+
assert.deepEqual(nsf.complement(nsf.union([int(0, 0)])), nsf.union([nsf.FULL]));
196+
assert.deepEqual(nsf.complement(nsf.union([int(0, 0), int(1, 1)])), nsf.union([nsf.FULL]));
197+
190198
assert.deepEqual(
191199
nsf.complement(nsf.union([int(3, 8), int(12, 13), int(15, 16), int(17, 18)])),
192200
nsf.union([int(-inf, 3), int(8, 12), int(13, 15), int(16, 17), int(18, inf)])

0 commit comments

Comments
 (0)