1 | 'use strict';
|
---|
2 | /* eslint indent: 4 */
|
---|
3 |
|
---|
4 |
|
---|
5 | // Private helper class
|
---|
6 | class SubRange {
|
---|
7 | constructor(low, high) {
|
---|
8 | this.low = low;
|
---|
9 | this.high = high;
|
---|
10 | this.length = 1 + high - low;
|
---|
11 | }
|
---|
12 |
|
---|
13 | overlaps(range) {
|
---|
14 | return !(this.high < range.low || this.low > range.high);
|
---|
15 | }
|
---|
16 |
|
---|
17 | touches(range) {
|
---|
18 | return !(this.high + 1 < range.low || this.low - 1 > range.high);
|
---|
19 | }
|
---|
20 |
|
---|
21 | // Returns inclusive combination of SubRanges as a SubRange.
|
---|
22 | add(range) {
|
---|
23 | return new SubRange(
|
---|
24 | Math.min(this.low, range.low),
|
---|
25 | Math.max(this.high, range.high)
|
---|
26 | );
|
---|
27 | }
|
---|
28 |
|
---|
29 | // Returns subtraction of SubRanges as an array of SubRanges.
|
---|
30 | // (There's a case where subtraction divides it in 2)
|
---|
31 | subtract(range) {
|
---|
32 | if (range.low <= this.low && range.high >= this.high) {
|
---|
33 | return [];
|
---|
34 | } else if (range.low > this.low && range.high < this.high) {
|
---|
35 | return [
|
---|
36 | new SubRange(this.low, range.low - 1),
|
---|
37 | new SubRange(range.high + 1, this.high)
|
---|
38 | ];
|
---|
39 | } else if (range.low <= this.low) {
|
---|
40 | return [new SubRange(range.high + 1, this.high)];
|
---|
41 | } else {
|
---|
42 | return [new SubRange(this.low, range.low - 1)];
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | toString() {
|
---|
47 | return this.low == this.high ?
|
---|
48 | this.low.toString() : this.low + '-' + this.high;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | class DRange {
|
---|
54 | constructor(a, b) {
|
---|
55 | this.ranges = [];
|
---|
56 | this.length = 0;
|
---|
57 | if (a != null) this.add(a, b);
|
---|
58 | }
|
---|
59 |
|
---|
60 | _update_length() {
|
---|
61 | this.length = this.ranges.reduce((previous, range) => {
|
---|
62 | return previous + range.length;
|
---|
63 | }, 0);
|
---|
64 | }
|
---|
65 |
|
---|
66 | add(a, b) {
|
---|
67 | var _add = (subrange) => {
|
---|
68 | var i = 0;
|
---|
69 | while (i < this.ranges.length && !subrange.touches(this.ranges[i])) {
|
---|
70 | i++;
|
---|
71 | }
|
---|
72 | var newRanges = this.ranges.slice(0, i);
|
---|
73 | while (i < this.ranges.length && subrange.touches(this.ranges[i])) {
|
---|
74 | subrange = subrange.add(this.ranges[i]);
|
---|
75 | i++;
|
---|
76 | }
|
---|
77 | newRanges.push(subrange);
|
---|
78 | this.ranges = newRanges.concat(this.ranges.slice(i));
|
---|
79 | this._update_length();
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (a instanceof DRange) {
|
---|
83 | a.ranges.forEach(_add);
|
---|
84 | } else {
|
---|
85 | if (b == null) b = a;
|
---|
86 | _add(new SubRange(a, b));
|
---|
87 | }
|
---|
88 | return this;
|
---|
89 | }
|
---|
90 |
|
---|
91 | subtract(a, b) {
|
---|
92 | var _subtract = (subrange) => {
|
---|
93 | var i = 0;
|
---|
94 | while (i < this.ranges.length && !subrange.overlaps(this.ranges[i])) {
|
---|
95 | i++;
|
---|
96 | }
|
---|
97 | var newRanges = this.ranges.slice(0, i);
|
---|
98 | while (i < this.ranges.length && subrange.overlaps(this.ranges[i])) {
|
---|
99 | newRanges = newRanges.concat(this.ranges[i].subtract(subrange));
|
---|
100 | i++;
|
---|
101 | }
|
---|
102 | this.ranges = newRanges.concat(this.ranges.slice(i));
|
---|
103 | this._update_length();
|
---|
104 | };
|
---|
105 |
|
---|
106 | if (a instanceof DRange) {
|
---|
107 | a.ranges.forEach(_subtract);
|
---|
108 | } else {
|
---|
109 | if (b == null) b = a;
|
---|
110 | _subtract(new SubRange(a, b));
|
---|
111 | }
|
---|
112 | return this;
|
---|
113 | }
|
---|
114 |
|
---|
115 | intersect(a, b) {
|
---|
116 | var newRanges = [];
|
---|
117 | var _intersect = (subrange) => {
|
---|
118 | var i = 0;
|
---|
119 | while (i < this.ranges.length && !subrange.overlaps(this.ranges[i])) {
|
---|
120 | i++;
|
---|
121 | }
|
---|
122 | while (i < this.ranges.length && subrange.overlaps(this.ranges[i])) {
|
---|
123 | var low = Math.max(this.ranges[i].low, subrange.low);
|
---|
124 | var high = Math.min(this.ranges[i].high, subrange.high);
|
---|
125 | newRanges.push(new SubRange(low, high));
|
---|
126 | i++;
|
---|
127 | }
|
---|
128 | };
|
---|
129 |
|
---|
130 | if (a instanceof DRange) {
|
---|
131 | a.ranges.forEach(_intersect);
|
---|
132 | } else {
|
---|
133 | if (b == null) b = a;
|
---|
134 | _intersect(new SubRange(a, b));
|
---|
135 | }
|
---|
136 | this.ranges = newRanges;
|
---|
137 | this._update_length();
|
---|
138 | return this;
|
---|
139 | }
|
---|
140 |
|
---|
141 | index(index) {
|
---|
142 | var i = 0;
|
---|
143 | while (i < this.ranges.length && this.ranges[i].length <= index) {
|
---|
144 | index -= this.ranges[i].length;
|
---|
145 | i++;
|
---|
146 | }
|
---|
147 | return this.ranges[i].low + index;
|
---|
148 | }
|
---|
149 |
|
---|
150 | toString() {
|
---|
151 | return '[ ' + this.ranges.join(', ') + ' ]';
|
---|
152 | }
|
---|
153 |
|
---|
154 | clone() {
|
---|
155 | return new DRange(this);
|
---|
156 | }
|
---|
157 |
|
---|
158 | numbers() {
|
---|
159 | return this.ranges.reduce((result, subrange) => {
|
---|
160 | var i = subrange.low;
|
---|
161 | while (i <= subrange.high) {
|
---|
162 | result.push(i);
|
---|
163 | i++;
|
---|
164 | }
|
---|
165 | return result;
|
---|
166 | }, []);
|
---|
167 | }
|
---|
168 |
|
---|
169 | subranges() {
|
---|
170 | return this.ranges.map((subrange) => ({
|
---|
171 | low: subrange.low,
|
---|
172 | high: subrange.high,
|
---|
173 | length: 1 + subrange.high - subrange.low
|
---|
174 | }));
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | module.exports = DRange;
|
---|