-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
334 lines (280 loc) · 8.28 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
export type Repr = Literal | Range;
export type Literal = {
/** @internal */ _isLiteral: 1;
/** @internal */ _text: string;
};
export type Range = {
/** @internal */ _isLiteral: 0;
/** @internal */ _min: number;
/** @internal */ _max: number;
};
const numberPattern = /^(?:0|[1-9]\d*)$/;
const rangePattern = /^(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)$/;
export function _compare(reprA: Repr, reprB: Repr): number {
if (reprA._isLiteral !== reprB._isLiteral) {
return reprB._isLiteral - reprA._isLiteral;
}
if (reprA._isLiteral) {
return reprA._text >= (reprB as Literal)._text
? reprA._text > (reprB as Literal)._text
? 1
: 0
: -1;
}
return (
reprA._min - (reprB as Range)._min || reprA._max - (reprB as Range)._max
);
}
export function _createLiteral(text: string): Repr {
return { _isLiteral: 1, _text: text };
}
export function _createRange(min: number, max: number): Repr {
return { _isLiteral: 0, _min: min, _max: max };
}
export function difference(textA: string, textB: string): string {
const reprsA = _parse(textA);
const reprsB = _parse(textB);
const reprs = _differenceReprs(reprsA, reprsB);
return _serialize(reprs);
}
// eslint-disable-next-line complexity -- This function IS complex.
export function _differenceReprs(reprsA: Repr[], reprsB: Repr[]): Repr[] {
const reprs: Repr[] = [];
loop: for (let indexA = 0; indexA < reprsA.length; ++indexA) {
const reprA = reprsA[indexA];
if (reprA._isLiteral) {
for (let indexB = 0; indexB < reprsB.length; ++indexB) {
const reprB = reprsB[indexB];
if (reprB._isLiteral && reprB._text === reprA._text) {
continue loop;
}
}
} else {
for (let indexB = 0; indexB < reprsB.length; ++indexB) {
const reprB = reprsB[indexB];
if (!reprB._isLiteral) {
if (reprA._min >= reprB._min && reprA._max <= reprB._max) {
continue loop;
}
if (reprA._min <= reprB._min && reprA._max >= reprB._max) {
if (reprA._max > reprB._max) {
reprsA.splice(
indexA + 1,
0,
_createRange(reprB._max + 1, reprA._max),
);
}
if (reprA._min < reprB._min) {
reprsA.splice(
indexA + 1,
0,
_createRange(reprA._min, reprB._min - 1),
);
}
continue loop;
}
if (reprA._min >= reprB._min && reprA._min <= reprB._max) {
reprA._min = reprB._max + 1;
} else if (reprA._max >= reprB._min && reprA._max <= reprB._max) {
reprA._max = reprB._min - 1;
}
}
}
}
reprs.push(reprA);
}
return reprs;
}
export function equal(textA: string, textB: string): boolean {
const reprsA = _parse(textA);
const reprsB = _parse(textB);
return _equalReprs(reprsA, reprsB);
}
export function _equalReprs(reprsA: Repr[], reprsB: Repr[]): boolean {
if (reprsA.length !== reprsB.length) {
return false;
}
for (let index = 0; index < reprsA.length; ++index) {
if (_compare(reprsA[index], reprsB[index])) {
return false;
}
}
return true;
}
export function expand(text: string): string[] {
const reprs = _parse(text);
return _expandReprs(reprs);
}
export function _expandReprs(reprs: Repr[]): string[] {
const texts: string[] = [];
for (let index = 0; index < reprs.length; ++index) {
const repr = reprs[index];
if (repr._isLiteral) {
texts.push(repr._text);
} else {
for (let index = repr._min; index <= repr._max; ++index) {
texts.push(`${index}`);
}
}
}
return texts;
}
export function intersection(textA: string, textB: string): string {
const reprsA = _parse(textA);
const reprsB = _parse(textB);
const reprs = _intersectionReprs(reprsA, reprsB);
return _serialize(reprs);
}
export function _intersectionRepr(reprA: Repr, reprB: Repr): Repr | null {
if (reprA._isLiteral !== reprB._isLiteral) {
return null;
}
if (reprA._isLiteral) {
return reprA._text === (reprB as Literal)._text ? reprA : null;
}
const min = Math.max(reprA._min, (reprB as Range)._min);
const max = Math.min(reprA._max, (reprB as Range)._max);
return min > max ? null : _createRange(min, max);
}
export function _intersectionReprs(reprsA: Repr[], reprsB: Repr[]): Repr[] {
const reprs: Repr[] = [];
for (let indexA = 0; indexA < reprsA.length; ++indexA) {
const reprA = reprsA[indexA];
for (let indexB = 0; indexB < reprsB.length; ++indexB) {
const reprB = reprsB[indexB];
const repr = _intersectionRepr(reprA, reprB);
if (repr !== null) {
reprs.push(repr);
}
}
}
return reprs;
}
export function normalize(text: string): string {
const reprs = _parse(text);
return _serialize(reprs);
}
export function _parse(text: string): Repr[] {
const reprs: Repr[] = [];
const chunks = text.split(',');
for (let index = 0; index < chunks.length; ++index) {
const chunk = chunks[index].trim();
if (chunk) {
const repr = _parseOne(chunk);
_unionReprs(reprs, repr);
}
}
return reprs;
}
export function _parseOne(text: string): Repr {
if (numberPattern.test(text)) {
const number = +text;
return _createRange(number, number);
}
const rangeMatch = rangePattern.exec(text);
if (rangeMatch) {
const a = +rangeMatch[1];
const b = +rangeMatch[2];
return _createRange(Math.min(a, b), Math.max(a, b));
}
return _createLiteral(text);
}
export function _serialize(reprs: Repr[]): string {
return reprs.map(_serializeOne).join();
}
export function _serializeOne(repr: Repr): string {
if (repr._isLiteral) {
return repr._text;
}
return repr._min === repr._max ? `${repr._min}` : `${repr._min}-${repr._max}`;
}
export function subset(textA: string, textB: string): boolean {
const reprsA = _parse(textA);
const reprsB = _parse(textB);
return _subsetReprs(reprsA, reprsB);
}
export function _subsetReprs(reprsA: Repr[], reprsB: Repr[]): boolean {
loop: for (let indexB = 0; indexB < reprsB.length; ++indexB) {
const reprB = reprsB[indexB];
if (reprB._isLiteral) {
for (let indexA = 0; indexA < reprsA.length; ++indexA) {
const reprA = reprsA[indexA];
if (reprA._isLiteral && reprA._text === reprB._text) {
continue loop;
}
}
} else {
for (let indexA = 0; indexA < reprsA.length; ++indexA) {
const reprA = reprsA[indexA];
if (
!reprA._isLiteral &&
reprA._min <= reprB._min &&
reprA._max >= reprB._max
) {
continue loop;
}
}
}
return false;
}
return true;
}
export function union(textA: string, textB: string): string {
const reprs = _parse(`${textA},${textB}`);
return _serialize(reprs);
}
export function _unionRepr(reprA: Repr, reprB: Repr): boolean {
if (reprA._isLiteral !== reprB._isLiteral) {
return false;
}
if (reprA._isLiteral) {
const same = reprA._text === (reprB as Literal)._text;
return same;
}
const unionable =
(reprA._min <= (reprB as Range)._max + 1 &&
reprA._max >= (reprB as Range)._min) ||
((reprB as Range)._min <= reprA._max + 1 &&
(reprB as Range)._max >= reprA._min);
if (unionable) {
reprA._min = Math.min(reprA._min, (reprB as Range)._min);
reprA._max = Math.max(reprA._max, (reprB as Range)._max);
}
return unionable;
}
export function _unionReprs(reprs: Repr[], repr: Repr): void {
let low = 0;
let high = reprs.length;
while (low < high) {
// eslint-disable-next-line no-bitwise -- This is much faster than Math.floor.
const middle = (low + high) >>> 1;
const result = _compare(repr, reprs[middle]);
if (!result) {
return;
}
if (result < 0) {
high = middle;
} else {
low = middle + 1;
}
}
if (
!_unionReprsAt(reprs, repr, low) &&
!_unionReprsAt(reprs, repr, low + 1)
) {
reprs.splice(low, 0, repr);
}
}
export function _unionReprsAt(
reprs: Repr[],
repr: Repr,
index: number,
): boolean {
if (index && index <= reprs.length && _unionRepr(reprs[index - 1], repr)) {
while (index < reprs.length && _unionRepr(reprs[index - 1], reprs[index])) {
reprs.splice(index, 1);
}
return true;
}
return false;
}