-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmulti-integer-range.ts
More file actions
553 lines (513 loc) · 16.6 KB
/
multi-integer-range.ts
File metadata and controls
553 lines (513 loc) · 16.6 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*! multi-integer-range (c) 2015 Soichiro Miki */
/**
* A `[min, max]` tuple to denote one integer range.
*/
export type Range = [number, number];
export type Initializer = string | number | (number | Range)[] | MultiRange;
declare var Symbol: any;
export type Options = {
/**
* Parses negative integers enclosed in parentheses.
*/
parseNegative?: boolean;
/**
* Parses unbounded ranges like `10-` or `-10`.
*/
parseUnbounded?: boolean;
};
const defaultOptions: Options = { parseNegative: false, parseUnbounded: false };
const MAX_SAFE_INTEGER = 9007199254740991;
const MIN_SAFE_INTEGER = -9007199254740991;
/**
* Parses and manipulates multiple integer ranges.
*/
export class MultiRange {
private ranges: Range[];
private options: Options;
/**
* Creates a new MultiRange object.
*/
constructor(data?: Initializer, options: Options = defaultOptions) {
function isArray(x: any): x is Array<any> {
return Object.prototype.toString.call(x) === '[object Array]';
}
this.ranges = [];
this.options = {
parseNegative: !!options.parseNegative,
parseUnbounded: !!options.parseUnbounded
};
if (typeof data === 'string') {
this.parseString(data);
} else if (typeof data === 'number') {
this.appendRange(data, data);
} else if (data instanceof MultiRange) {
this.ranges = data.getRanges();
if (arguments[1] === undefined) {
this.options = {
parseNegative: data.options.parseNegative,
parseUnbounded: data.options.parseUnbounded
};
}
} else if (isArray(data)) {
for (let item of <(number | Range)[]>data) {
if (isArray(item)) {
if (item.length === 2) {
this.appendRange(item[0], item[1]);
} else {
throw new TypeError('Invalid array initializer');
}
} else if (typeof item === 'number') {
this.append(item);
} else {
throw new TypeError('Invalid array initialzer');
}
}
} else if (data !== undefined) {
throw new TypeError('Invalid input');
}
}
/**
* Parses the initializer string and build the range data.
* Override this if you need to customize the parsing strategy.
*/
protected parseString(data: string): void {
function toInt(str: string): number {
const m = str.match(/^\(?(\-?\d+)/) as any;
const int = parseInt(m[1], 10);
if (int < MIN_SAFE_INTEGER || MAX_SAFE_INTEGER < int)
throw new RangeError('The number is too big or too small.');
return int;
}
const s = data.replace(/\s/g, '');
if (!s.length) return;
let match;
const int = this.options.parseNegative ? '(\\d+|\\(\\-?\\d+\\))' : '(\\d+)';
const intMatch = new RegExp('^' + int + '$');
const rangeMatch = new RegExp('^' + int + '?\\-' + int + '?$');
for (let r of s.split(',')) {
if ((match = r.match(intMatch))) {
const val = toInt(match[1]);
this.appendRange(val, val);
} else if ((match = r.match(rangeMatch))) {
if (
!this.options.parseUnbounded &&
(match[1] === undefined || match[2] === undefined)
) {
throw new SyntaxError('Unexpected unbouded range notation');
}
const min = match[1] === undefined ? -Infinity : toInt(match[1]);
const max = match[2] === undefined ? +Infinity : toInt(match[2]);
this.appendRange(min, max);
} else {
throw new SyntaxError('Invalid input');
}
}
}
/**
* Clones this instance.
* @returns The cloned instance.
*/
public clone(): MultiRange {
return new MultiRange(this);
}
/**
* Appends to this instance.
* @param value The data to append.
*/
public append(value: Initializer): MultiRange {
if (value === undefined) {
throw new TypeError('Invalid input');
} else if (value instanceof MultiRange) {
for (let r of value.ranges) this.appendRange(r[0], r[1]);
return this;
} else {
return this.append(new MultiRange(value, this.options));
}
}
/**
* Appends a specified range of integers to this isntance.
* @param min The minimum value of the range to append.
* @param max The maximum value of the range to append.
*/
private appendRange(min: number, max: number): MultiRange {
let newRange: Range = [min, max];
if (newRange[0] > newRange[1]) {
newRange = [newRange[1], newRange[0]];
}
if (
(newRange[0] === Infinity && newRange[1] === Infinity) ||
(newRange[0] === -Infinity && newRange[1] === -Infinity)
) {
throw new RangeError(
'Infinity can be used only within an unbounded range segment'
);
}
const overlap = this.findOverlap(newRange);
this.ranges.splice(overlap.lo, overlap.count, overlap.union);
return this;
}
/**
* Subtracts from this instance.
* @param value The data to subtract.
*/
public subtract(value: Initializer): MultiRange {
if (value === undefined) {
throw new TypeError('Invalid input');
} else if (value instanceof MultiRange) {
for (let r of value.ranges) this.subtractRange(r[0], r[1]);
return this;
} else {
return this.subtract(new MultiRange(value, this.options));
}
}
/**
* Subtracts a specified range of integers from this instance.
* @param min The minimum value of the range to subtract.
* @param max The maximum value of the range to subtract.
*/
private subtractRange(min: number, max: number): MultiRange {
let newRange: Range = [min, max];
if (newRange[0] > newRange[1]) {
newRange = [newRange[1], newRange[0]];
}
const overlap = this.findOverlap(newRange);
if (overlap.count > 0) {
const remain: Range[] = [];
if (this.ranges[overlap.lo][0] < newRange[0]) {
remain.push([this.ranges[overlap.lo][0], newRange[0] - 1]);
}
if (newRange[1] < this.ranges[overlap.lo + overlap.count - 1][1]) {
remain.push([
newRange[1] + 1,
this.ranges[overlap.lo + overlap.count - 1][1]
]);
}
this.ranges.splice.apply(
this.ranges,
(<any>[overlap.lo, overlap.count]).concat(remain)
);
}
return this;
}
/**
* Remove integers which are not included in `value`,
* yielding the intersection of this and `value`.
* @param value The data to calculate the intersetion.
*/
public intersect(value: Initializer): MultiRange {
if (value === undefined) {
throw new TypeError('Invalid input');
} else if (value instanceof MultiRange) {
const result: Range[] = [];
let jstart = 0; // used for optimization
for (let i = 0; i < this.ranges.length; i++) {
const r1 = this.ranges[i];
for (let j = jstart; j < value.ranges.length; j++) {
const r2 = value.ranges[j];
if (r1[0] <= r2[1] && r1[1] >= r2[0]) {
jstart = j;
const min = Math.max(r1[0], r2[0]);
const max = Math.min(r1[1], r2[1]);
result.push([min, max]);
} else if (r1[1] < r2[0]) {
break;
}
}
}
this.ranges = result;
return this;
} else {
return this.intersect(new MultiRange(value, this.options));
}
}
/**
* Determines how the given range overlaps or touches the existing ranges.
* This is a helper method that calculates how an append/subtract operation
* affects the existing range members.
* @param target The range array to test.
* @returns An object containing information about how the given range
* overlaps or touches this instance.
*/
private findOverlap(target: Range): {
lo: number;
count: number;
union: Range;
} {
// a b c d e f g h i j k l m
//--------------------------------------------------------------------
// |----(0)----| |---(1)---| |---(2)---| |--(3)--|
// |------------(A)--------------|
// |-(B)-|
// |-(C)-|
//
// (0)-(3) represent the existing ranges (this.ranges),
// and (A)-(C) are the ranges being passed to this function.
//
// A pseudocode findOverlap(A) returns { lo: 0, count: 3, union: <a-h> },
// meaning (A) overlaps the 3 existing ranges from index 0.
//
// findOverlap(B) returns { lo: 2, count: 1, union: <f-j> },
// meaning (B) "touches" one range element, (2).
//
// findOverlap(C) returns { lo: 3, count: 0, union: <i-k> }
// meaning (C) is between (2) and (3) but overlaps/touches neither of them.
for (let hi = this.ranges.length - 1; hi >= 0; hi--) {
const r = this.ranges[hi];
let union;
if ((union = this.calcUnion(r, target))) {
let count = 1;
let tmp;
while (
hi - count >= 0 &&
(tmp = this.calcUnion(union, this.ranges[hi - count]))
) {
union = tmp;
count++;
}
// The given target touches/overlaps one or more of the existing ranges
return { lo: hi + 1 - count, count, union };
} else if (r[1] < target[0]) {
// The given target does not touch nor overlap the existing ranges
return { lo: hi + 1, count: 0, union: target };
}
}
// The given target is smaller than the smallest existing range
return { lo: 0, count: 0, union: target };
}
/**
* Calculates the union of two specified ranges.
* @param a Range A.
* @param b Range B.
* @returns Union of `a` and `b`.
* Returns `null` if `a` and `b` do not touch nor intersect.
*/
private calcUnion(a: Range, b: Range): Range | null {
if (a[1] + 1 < b[0] || a[0] - 1 > b[1]) {
return null; // cannot make union
}
return [Math.min(a[0], b[0]), Math.max(a[1], b[1])];
}
/**
* Exports the whole range data as an array of arrays.
* @returns An copied array of range segments.
*/
public getRanges(): Range[] {
const result: Range[] = [];
for (let r of this.ranges) result.push([r[0], r[1]]);
return result;
}
/**
* Checks if this instance contains the specified value.
* @param value Value to be checked.
* @returns True if the specified value is included in the instance.
*/
public has(value: Initializer): boolean {
if (value === undefined) {
throw new TypeError('Invalid input');
} else if (value instanceof MultiRange) {
const s = 0;
const len = this.ranges.length;
for (let tr of value.ranges) {
let i: number;
for (i = s; i < len; i++) {
let my = this.ranges[i];
if (
tr[0] >= my[0] &&
tr[1] <= my[1] &&
tr[1] >= my[0] &&
tr[1] <= my[1]
)
break;
}
if (i === len) return false;
}
return true;
} else {
return this.has(new MultiRange(value, this.options));
}
}
/**
* Checks if this instance contains the range specified by the two parameters.
* @param min The minimum value of the range to subtract.
* @param max The minimum value of the range to subtract.
* @returns True if the specified value is included in the instance.
*/
private hasRange(min: number, max: number): boolean {
return this.has(new MultiRange([[min, max]]));
}
/**
* Returns the number of range segments.
* For example, the segmentLength of `2-5,7,9-11` is 3.
* @returns The number of segments. Returns 0 for an empty instance.
*/
public segmentLength(): number {
return this.ranges.length;
}
/**
* Calculates how many numbers are effectively included in this instance.
* For example, the length of `1-10,51-60,90` is 21.
* @returns The number of integer values in this instance.
* Returns `Infinity` for an unbounded range.
*/
public length(): number {
if (this.isUnbounded()) return Infinity;
let result = 0;
for (let r of this.ranges) result += r[1] - r[0] + 1;
return result;
}
/**
* Checks if two instances of MultiRange are identical.
* @param cmp The data to compare.
* @returns True if `cmp` is exactly the same as this instance.
*/
public equals(cmp: Initializer): boolean {
if (cmp === undefined) {
throw new TypeError('Invalid input');
} else if (cmp instanceof MultiRange) {
if (cmp === this) return true;
if (this.ranges.length !== cmp.ranges.length) return false;
for (let i = 0; i < this.ranges.length; i++) {
if (
this.ranges[i][0] !== cmp.ranges[i][0] ||
this.ranges[i][1] !== cmp.ranges[i][1]
)
return false;
}
return true;
} else {
return this.equals(new MultiRange(cmp, this.options));
}
}
/**
* Checks if the current instance is unbounded (i.e., infinite).
*/
public isUnbounded(): boolean {
return (
this.ranges.length > 0 &&
(this.ranges[0][0] === -Infinity ||
this.ranges[this.ranges.length - 1][1] === Infinity)
);
}
/**
* Returns the minimum integer contained in this insntance.
* Can be -Infinity or undefined.
* @returns The minimum integer of this instance.
*/
public min(): number | undefined {
if (this.ranges.length === 0) return undefined;
return this.ranges[0][0];
}
/**
* Returns the maximum number contained in this insntance.
* Can be Infinity or undefined.
* @returns The maximum integer of this instance.
*/
public max(): number | undefined {
if (this.ranges.length === 0) return undefined;
return this.ranges[this.ranges.length - 1][1];
}
/**
* Removes the smallest integer from this instance and returns it.
* @returns The minimum integer removed from this instance.
*/
public shift(): number | undefined {
const min = this.min();
if (min === -Infinity)
throw new RangeError(
'shift() was invoked on an unbounded MultiRange which contains -Infinity'
);
if (min !== undefined) this.subtract(min);
return min;
}
/**
* Removes the largest integer from this instance and returns it.
* @returns The maximum integer removed from this instance.
*/
public pop(): number | undefined {
const max = this.max();
if (max === Infinity)
throw new RangeError(
'pop() was invoked on an unbounded MultiRange which contains +Infinity'
);
if (max !== undefined) this.subtract(max);
return max;
}
/**
* Returns the string respresentation of this MultiRange.
*/
public toString(): string {
function wrap(i: number): string {
return i >= 0 ? String(i) : `(${i})`;
}
const ranges: string[] = [];
for (let r of this.ranges) {
if (r[0] === -Infinity) {
if (r[1] === Infinity) {
ranges.push('-');
} else {
ranges.push(`-${wrap(r[1])}`);
}
} else if (r[1] === Infinity) {
ranges.push(`${wrap(r[0])}-`);
} else if (r[0] == r[1]) {
ranges.push(wrap(r[0]));
} else {
ranges.push(`${wrap(r[0])}-${wrap(r[1])}`);
}
}
return ranges.join(',');
}
/**
* Builds a flat array of integers which holds all elements in this instance.
* Note that this may be slow and memory-consuming for large ranges.
* Consider using the iterator whenever possible.
*/
public toArray(): number[] {
if (this.isUnbounded()) {
throw new RangeError('You cannot build an array from an unbounded range');
}
const result = new Array(this.length());
let idx = 0;
for (let r of this.ranges) {
for (let n = r[0]; n <= r[1]; n++) {
result[idx++] = n;
}
}
return result;
}
/**
* Returns an ES6-compatible iterator.
*/
public getIterator(): { next: () => { done?: boolean; value?: number } } {
if (this.isUnbounded()) {
throw new RangeError('Unbounded ranges cannot be iterated over');
}
let i = 0,
curRange: Range = this.ranges[i],
j = curRange ? curRange[0] : undefined;
return {
next: () => {
if (!curRange || j === undefined) return { done: true };
const ret = j;
if (++j > curRange[1]) {
curRange = this.ranges[++i];
j = curRange ? curRange[0] : undefined;
}
return { value: ret };
}
};
}
}
export default MultiRange;
// Set ES6 iterator, if Symbol.iterator is defined
/* istanbul ignore else */
if (typeof Symbol === 'function' && 'iterator' in Symbol) {
MultiRange.prototype[Symbol.iterator] = MultiRange.prototype.getIterator;
}
/**
* A shorthand function to construct a new MultiRange instance.
* @returns The new MultiRnage instance.
*/
export function multirange(data?: Initializer, options?: Options): MultiRange {
return new MultiRange(data, options);
}