-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (149 loc) · 4.26 KB
/
index.js
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
143
144
145
146
147
148
149
function __unite(ints) {
var result = [];
for (var idx1 = 0; idx1 < ints.length; ++idx1) {
if (result.indexOf(idx1) === -1) {
var x = ints[idx1];
for (var idx2 = idx1 + 1; idx2 < ints.length; ++idx2) {
if (result.indexOf(idx2) === -1) {
var y = ints[idx2];
if (x.isIntersect(y) || x.isCloseTo(y)) {
x.unite(y);
result.push(idx2);
}
}
}
}
}
return result;
}
function sortNum(a, b) {
return a - b;
}
var HOInterval = (function () {
function HOInterval(x, b) {
if (b === void 0) { b = x; }
if (x instanceof HOInterval) {
this.a = x.a;
this.b = x.b;
}
else {
this.a = x;
this.b = b;
}
}
HOInterval.length = function (int) {
return int.b - int.a;
};
HOInterval.isValid = function (a, b) {
if (b === void 0) { b = a; }
if (a instanceof HOInterval) {
a = a.a;
b = a.b;
}
return a <= b;
};
HOInterval.unite = function () {
var input = [];
for (var _i = 0; _i < arguments.length; _i++) {
input[_i - 0] = arguments[_i];
}
var ints;
if (input.length === 1) {
ints = input[0];
}
else {
ints = input;
}
var united;
while ((united = __unite(ints)) && united.length) {
united.sort(sortNum).reverse();
for (var idx in united) {
ints.splice(united[idx], 1);
}
}
return ints;
};
HOInterval.gaps = function () {
var input = [];
for (var _i = 0; _i < arguments.length; _i++) {
input[_i - 0] = arguments[_i];
}
var ints;
if (input.length === 1) {
ints = input[0];
}
else {
ints = input;
}
if (ints.length < 2) {
return [];
}
var result = [];
for (var idx = 0; idx < ints.length - 1; ++idx) {
var aInt = ints[idx];
var bInt = ints[idx + 1];
if (aInt.isIntersect(bInt) || aInt.isCloseTo(bInt)) {
continue;
}
var maxA = (aInt.a > bInt.a) ? aInt.a : bInt.a;
var minB = (aInt.b < bInt.b) ? aInt.b : bInt.b;
result.push(new HOInterval(minB, maxA));
}
return result;
};
HOInterval.intersect = function (a, b) {
return (new HOInterval(a)).intersect(b);
};
HOInterval.prototype.isCloseTo = function (other) {
return (this.b == other.a) || (this.a == other.b);
};
HOInterval.prototype.isEqualsTo = function (other) {
return (this.a == other.a) && (this.b == other.b);
};
HOInterval.prototype.isIntersect = function (x, y) {
if (y === void 0) { y = x; }
if (x instanceof HOInterval) {
y = x.b;
x = x.a;
}
var a = this.a;
var b = this.b;
if ((a < b) && (x < y)) {
return ((a <= x) && (x < b)) || ((x < a) && (a < y));
}
else if ((a < b) && (x == y)) {
return (a <= x) && (x < b);
}
else if ((a == b) && (x < b)) {
return (x <= a) && (a < b);
}
else if ((a == b) && (x == y)) {
return a == x;
}
};
HOInterval.prototype.unite = function (other) {
if (this.isIntersect(other) || this.isCloseTo(other)) {
var minA = (this.a < other.a) ? this.a : other.a;
var maxB = (this.b > other.b) ? this.b : other.b;
this.a = minA;
this.b = maxB;
}
return this;
};
HOInterval.prototype.intersect = function (other) {
if (this.isIntersect(other)) {
var maxA = (this.a > other.a) ? this.a : other.a;
var minB = (this.b < other.b) ? this.b : other.b;
this.a = maxA;
this.b = minB;
}
return this;
};
HOInterval.prototype.add = function (val) {
this.a += val;
this.b += val;
return this;
};
return HOInterval;
}());
module.exports = HOInterval;