-
Notifications
You must be signed in to change notification settings - Fork 1
/
interval.ts
142 lines (129 loc) · 2.86 KB
/
interval.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
type endpoint = {
value: number,
inclusive: boolean
}
export class Interval {
/**
* The left endpoint of the interval.
*/
readonly left: endpoint;
/**
* The right endpoint of the interval.
*/
readonly right: endpoint;
private constructor(a: endpoint, b: endpoint) {
this.left = a;
this.right = b;
}
/**
* Creates and returns a closed interval onject.
* @param a The left side limit of the interval.
* @param b The right side limit of the interval.
*/
public static closed(a: number, b: number) {
return new Interval({
value: a,
inclusive: true
},
{
value: b,
inclusive: true
});
}
/**
* Creates and returns a closed interval onject.
* @param a The left side limit of the interval.
* @param b The right side limit of the interval.
*/
public static open(a: number, b: number) {
return new Interval({
value: a,
inclusive: false
},
{
value: b,
inclusive: false
});
}
/**
* Creates and returns a left-open right-closed interval onject.
* @param a The left side limit of the interval.
* @param b The right side limit of the interval.
*/
public static open_closed(a: number, b: number) {
return new Interval({
value: a,
inclusive: false
},
{
value: b,
inclusive: true
});
}
/**
* Creates and returns a left-closed and right-open interval onject.
* @param a The left side limit of the interval.
* @param b The right side limit of the interval.
*/
public static closed_open(a: number, b: number) {
return new Interval({
value: a,
inclusive: true
},
{
value: b,
inclusive: false
});
}
/**
* Checks whether the left side of the interval is open.
*/
public isLeftOpen() {
return !this.left.inclusive;
}
/**
* Checks whether the left side of the interval is closed.
*/
public isLeftClosed() {
return this.left.inclusive;
}
/**
* Checks whether the right side of the interval is open.
*/
public isRightOpen() {
return !this.right.inclusive;
}
/**
* Checks whether the right side of the interval is closed.
*/
public isRightClosed() {
return this.right.inclusive;
}
/**
* Checks whether both sides of `this` interval are open.
*/
public isOpen() {
return this.isLeftOpen() && this.isRightOpen();
}
/**
* Checks whether both sides of `this` interval are closed.
*/
public isClosed() {
return this.isLeftClosed() && this.isRightClosed();
}
/**
* Checks whether a given number falls within the range specified by `this`
* interval or not.
* @param x The number to check for.
*/
public contains(x: number) {
if(this.isOpen())
return this.left.value < x && x < this.right.value;
if(this.isClosed())
return this.left.value <= x && x <= this.right.value;
if(this.isLeftClosed())
return this.left.value <= x && x < this.right.value;
if(this.isRightClosed())
return this.left.value < x && x <= this.right.value;
}
}