-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
182 lines (143 loc) · 4 KB
/
index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function __unite( ints: HOInterval[ ] ): number[ ] {
var result: number[ ] = [ ]
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: number, b: number){
return a - b
}
class HOInterval {
static length( int: HOInterval ): number {
return int.b - int.a
}
static isValid( int: HOInterval ): boolean
static isValid( a: number, b?: number ): boolean
static isValid( a: any, b: number = a ): boolean {
if ( a instanceof <any>HOInterval ) {
a = a.a
b = a.b
}
return a <= b
}
static unite( ints: HOInterval[ ] ): HOInterval[ ]
static unite( ...input: any[ ] ): HOInterval[ ] {
var ints: HOInterval[ ]
if ( input.length === 1 ) {
ints = input[ 0 ]
} else {
ints = input
}
// Holds indexes of united intervals
var united
while ( ( united = __unite( ints ) ) && united.length ) {
united.sort(sortNum).reverse( )
// Remove united intervals from input
for ( var idx in united ) {
ints.splice( united[ idx ], 1 )
}
}
return ints
}
static gaps( ints: HOInterval[ ] ): HOInterval[ ]
static gaps( ...input: any[ ] ): HOInterval[ ] {
var ints: HOInterval[ ]
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
}
static intersect( a: HOInterval, b: HOInterval ): HOInterval {
return ( new HOInterval( a ) ).intersect( b )
}
public a: number
public b: number
constructor( other: HOInterval )
constructor( a: number, b: number )
constructor( x: any, b: number = x ) {
if ( x instanceof <any>HOInterval ) {
this.a = x.a
this.b = x.b
} else {
this.a = x
this.b = b
}
}
isCloseTo( other: HOInterval ): boolean {
return ( this.b == other.a ) || ( this.a == other.b )
}
isEqualsTo( other: HOInterval ): boolean {
return ( this.a == other.a ) && ( this.b == other.b )
}
isIntersect( other: HOInterval ): boolean
isIntersect( x: number, y?: number ): boolean
isIntersect( x: any, y: number = x ): boolean {
if ( x instanceof <any>HOInterval ) {
y = ( <HOInterval> x ).b
x = ( <HOInterval> 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
}
}
unite( other: HOInterval ): HOInterval {
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
}
intersect( other ): HOInterval {
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
}
add( val: number ): HOInterval {
this.a += val
this.b += val
return this
}
}
export = HOInterval