Skip to content

Commit cf6f2dc

Browse files
committed
add new Union methods
1 parent 544bce1 commit cf6f2dc

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ $$
153153
To construct a union, use the `nsf.union()` function:
154154

155155
```typescript
156-
nsf.union(intervals: Interval[]) : Union
156+
function union(list: (Interval | Union)[]): Union
157157
```
158158

159159
Union class member functions:
@@ -163,6 +163,9 @@ class Union {
163163
isEmpty(): boolean;
164164
isFull(): boolean;
165165
isFinite(): boolean;
166+
isSingle(): boolean;
167+
equalsSingle(lower: number, upper: number): boolean;
168+
count(): number;
166169
lower(): number;
167170
upper(): number;
168171
contains(value: number): boolean;
@@ -262,3 +265,4 @@ function complement(A: Union | Interval): Union;
262265
## Future improvements
263266

264267
- Support multiple rounding modes
268+
- Improve unit test coverage

src/union.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ export class Union {
2424
);
2525
}
2626

27+
public isSingle(): boolean {
28+
return this.intervals.length === 1;
29+
}
30+
31+
public equalsSingle(lower: number, upper: number) {
32+
return this.isSingle() && this.intervals[0].lo === lower && this.intervals[0].hi === upper;
33+
}
34+
35+
public count(): number {
36+
return this.intervals.length;
37+
}
38+
2739
public lower(): number {
2840
if (this.isEmpty()) return Infinity;
2941
return this.intervals[0].lo;

tests/union.test.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ function ordered(U: nsf.Union): boolean {
1414
return true;
1515
}
1616

17-
describe("union", () => {
18-
it("union creation", () => {
17+
describe("Union", () => {
18+
it("Union creation", () => {
1919
// From a list of intervals
2020
assert.ok(ordered(nsf.union([])));
2121
assert.ok(ordered(nsf.union([nsf.interval(-Infinity, Infinity)])));
@@ -36,7 +36,7 @@ describe("union", () => {
3636
assert.ok(ordered(nsf.union([a, b, nsf.interval(5, 6), nsf.interval(-1000, -500)])));
3737
});
3838

39-
it("union contains", () => {
39+
it("Union.contains()", () => {
4040
const a = nsf.union([nsf.interval(0, 10), nsf.interval(50, 60)]);
4141
assert.ok(!nsf.EMPTY.contains(0));
4242
assert.ok(a.contains(0));
@@ -50,7 +50,7 @@ describe("union", () => {
5050
assert.ok(!a.contains(15));
5151
});
5252

53-
it("union subset", () => {
53+
it("Union.subset()", () => {
5454
const a = nsf.union([nsf.interval(0, 10), nsf.interval(50, 60)]);
5555
assert.ok(a.subset(nsf.single(0, 100)));
5656
assert.ok(a.subset(nsf.FULL));
@@ -59,15 +59,15 @@ describe("union", () => {
5959
assert.ok(nsf.EMPTY.subset(nsf.FULL));
6060
});
6161

62-
it("union superset", () => {
62+
it("Union.superset()", () => {
6363
const a = nsf.union([nsf.interval(0, 10), nsf.interval(50, 60)]);
6464
assert.ok(a.superset(nsf.single(0, 1)));
6565
assert.ok(a.superset(nsf.EMPTY));
6666
assert.ok(a.superset(nsf.union([nsf.interval(2, 3), nsf.interval(5, 6)])));
6767
assert.ok(nsf.FULL.superset(nsf.EMPTY));
6868
});
6969

70-
it("union isFinite", () => {
70+
it("union.isFinite()", () => {
7171
assert.ok(nsf.EMPTY.isFinite());
7272
assert.ok(nsf.single(0, 1).isFinite());
7373
assert.ok(nsf.union([nsf.single(0, 1), nsf.single(4, 5)]).isFinite());
@@ -90,4 +90,20 @@ describe("union", () => {
9090

9191
assert.deepEqual(nsf.FULL.width(), Infinity);
9292
});
93+
94+
it("Union.count()", () => {
95+
assert.deepEqual(nsf.EMPTY.count(), 0);
96+
assert.deepEqual(nsf.single(0, 0).count(), 1);
97+
assert.deepEqual(nsf.FULL.count(), 1);
98+
assert.deepEqual(nsf.union([nsf.single(0, 0), nsf.single(1, 2)]).count(), 2);
99+
});
100+
101+
it("Union.isSingle()", () => {
102+
assert.deepEqual(nsf.EMPTY.isSingle(), false);
103+
assert.deepEqual(nsf.FULL.isSingle(), true);
104+
assert.deepEqual(nsf.single(0, 0).isSingle(), true);
105+
assert.deepEqual(nsf.single(0, 1).isSingle(), true);
106+
assert.deepEqual(nsf.single(-1, 1).isSingle(), true);
107+
assert.deepEqual(nsf.union([nsf.single(0, 1), nsf.single(10, 11)]).isSingle(), false);
108+
});
93109
});

0 commit comments

Comments
 (0)