Skip to content

Commit 008b808

Browse files
committed
union constructor: accept interval or union
1 parent ff7f3f4 commit 008b808

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/union.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ export class Union {
6161
}
6262
}
6363

64-
// Construct a union
64+
// Construct a union from a list of intervals or unions
65+
export function union(list: (Interval | Union)[]): Union {
66+
const intervals = list.map((e) => (e instanceof Interval ? e : e.intervals)).flat();
67+
return iunion(intervals);
68+
}
69+
6570
// Minimum disjoint set of a list of intervals
66-
export function union(intervals: Interval[]): Union {
71+
function iunion(intervals: Interval[]): Union {
6772
if (intervals.length <= 1) {
6873
return new Union(intervals);
6974
}

tests/union.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,24 @@ function ordered(U: nsf.Union): boolean {
1616

1717
describe("union", () => {
1818
it("union creation", () => {
19+
// From a list of intervals
1920
assert.ok(ordered(nsf.union([])));
2021
assert.ok(ordered(nsf.union([nsf.interval(-Infinity, Infinity)])));
2122
assert.ok(ordered(nsf.union([nsf.interval(0, 0)])));
2223
assert.ok(ordered(nsf.union([nsf.interval(1, 2), nsf.interval(4, 10)])));
2324
assert.ok(ordered(nsf.union([nsf.interval(1, 2), nsf.interval(-10, -5)])));
2425
assert.ok(ordered(nsf.union([nsf.interval(1, 2), nsf.interval(-5, 5)])));
26+
27+
// From a list of unions
28+
const a = nsf.union([nsf.interval(0, 0)]);
29+
const b = nsf.union([nsf.interval(1, 2), nsf.interval(4, 10)]);
30+
const c = nsf.union([nsf.interval(1, 2), nsf.interval(-10, -5)]);
31+
const d = nsf.union([nsf.interval(10000, Infinity)]);
32+
33+
assert.ok(ordered(nsf.union([a, b, c, d])));
34+
35+
// Mix of intervals and unions
36+
assert.ok(ordered(nsf.union([a, b, nsf.interval(5, 6), nsf.interval(-1000, -500)])));
2537
});
2638

2739
it("union contains", () => {

0 commit comments

Comments
 (0)