-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathIntervalSet.js
More file actions
260 lines (242 loc) · 6.85 KB
/
IntervalSet.js
File metadata and controls
260 lines (242 loc) · 6.85 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
/* Copyright (c) 2012-2022 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
import Token from '../Token.js';
import Interval from "./Interval.js";
export default class IntervalSet {
constructor() {
this.intervals = null;
this.readOnly = false;
}
first(v) {
if (this.intervals === null || this.intervals.length===0) {
return Token.INVALID_TYPE;
} else {
return this.intervals[0].start;
}
}
addOne(v) {
this.addInterval(new Interval(v, v + 1));
}
addRange(l, h) {
this.addInterval(new Interval(l, h + 1));
}
addInterval(toAdd) {
if (this.intervals === null) {
this.intervals = [];
this.intervals.push(toAdd.clone());
} else {
// find insert pos
for (let pos = 0; pos < this.intervals.length; pos++) {
let existing = this.intervals[pos];
// distinct range -> insert
if (toAdd.stop < existing.start) {
this.intervals.splice(pos, 0, toAdd);
return;
}
// contiguous range -> adjust
else if (toAdd.stop === existing.start) {
this.intervals[pos] = new Interval(toAdd.start, existing.stop)
return;
}
// overlapping range -> adjust and reduce
else if (toAdd.start <= existing.stop) {
this.intervals[pos] = new Interval(Math.min(existing.start, toAdd.start), Math.max(existing.stop, toAdd.stop));
this.reduce(pos);
return;
}
}
// greater than any existing
this.intervals.push(toAdd.clone());
}
}
addSet(other) {
if (other.intervals !== null) {
other.intervals.forEach( toAdd => this.addInterval(toAdd), this);
}
return this;
}
reduce(pos) {
// only need to reduce if pos is not the last
if (pos < this.intervals.length - 1) {
let current = this.intervals[pos];
let next = this.intervals[pos + 1];
// if next contained in current
if (current.stop >= next.stop) {
this.intervals.splice(pos + 1, 1);
this.reduce(pos);
} else if (current.stop >= next.start) {
this.intervals[pos] = new Interval(current.start, next.stop);
this.intervals.splice(pos + 1, 1);
}
}
}
complement(start, stop) {
let result = new IntervalSet();
result.addInterval(new Interval(start, stop + 1));
if(this.intervals !== null)
this.intervals.forEach(toRemove => result.removeRange(toRemove));
return result;
}
contains(item) {
if (this.intervals === null) {
return false;
} else {
for (let k = 0; k < this.intervals.length; k++) {
if(this.intervals[k].contains(item)) {
return true;
}
}
return false;
}
}
removeRange(toRemove) {
if(toRemove.start===toRemove.stop-1) {
this.removeOne(toRemove.start);
} else if (this.intervals !== null) {
let pos = 0;
for(let n=0; n<this.intervals.length; n++) {
let existing = this.intervals[pos];
// intervals are ordered
if (toRemove.stop<=existing.start) {
return;
}
// check for including range, split it
else if(toRemove.start>existing.start && toRemove.stop<existing.stop) {
this.intervals[pos] = new Interval(existing.start, toRemove.start);
let x = new Interval(toRemove.stop, existing.stop);
this.intervals.splice(pos, 0, x);
return;
}
// check for included range, remove it
else if(toRemove.start<=existing.start && toRemove.stop>=existing.stop) {
this.intervals.splice(pos, 1);
pos = pos - 1; // need another pass
}
// check for lower boundary
else if(toRemove.start<existing.stop) {
this.intervals[pos] = new Interval(existing.start, toRemove.start);
}
// check for upper boundary
else if(toRemove.stop<existing.stop) {
this.intervals[pos] = new Interval(toRemove.stop, existing.stop);
}
pos += 1;
}
}
}
removeOne(value) {
if (this.intervals !== null) {
for (let i = 0; i < this.intervals.length; i++) {
let existing = this.intervals[i];
// intervals are ordered
if (value < existing.start) {
return;
}
// check for single value range
else if (value === existing.start && value === existing.stop - 1) {
this.intervals.splice(i, 1);
return;
}
// check for lower boundary
else if (value === existing.start) {
this.intervals[i] = new Interval(existing.start + 1, existing.stop);
return;
}
// check for upper boundary
else if (value === existing.stop - 1) {
this.intervals[i] = new Interval(existing.start, existing.stop - 1);
return;
}
// split existing range
else if (value < existing.stop - 1) {
let replace = new Interval(existing.start, value);
existing.start = value + 1;
this.intervals.splice(i, 0, replace);
return;
}
}
}
}
toString(literalNames, symbolicNames, elemsAreChar) {
literalNames = literalNames || null;
symbolicNames = symbolicNames || null;
elemsAreChar = elemsAreChar || false;
if (this.intervals === null) {
return "{}";
} else if(literalNames!==null || symbolicNames!==null) {
return this.toTokenString(literalNames, symbolicNames);
} else if(elemsAreChar) {
return this.toCharString();
} else {
return this.toIndexString();
}
}
toCharString() {
let names = [];
for (let i = 0; i < this.intervals.length; i++) {
let existing = this.intervals[i];
if(existing.stop===existing.start+1) {
if ( existing.start===Token.EOF ) {
names.push("<EOF>");
} else {
names.push("'" + String.fromCharCode(existing.start) + "'");
}
} else {
names.push("'" + String.fromCharCode(existing.start) + "'..'" + String.fromCharCode(existing.stop-1) + "'");
}
}
if (names.length > 1) {
return "{" + names.join(", ") + "}";
} else {
return names[0];
}
}
toIndexString() {
let names = [];
for (let i = 0; i < this.intervals.length; i++) {
let existing = this.intervals[i];
if(existing.stop===existing.start+1) {
if ( existing.start===Token.EOF ) {
names.push("<EOF>");
} else {
names.push(existing.start.toString());
}
} else {
names.push(existing.start.toString() + ".." + (existing.stop-1).toString());
}
}
if (names.length > 1) {
return "{" + names.join(", ") + "}";
} else {
return names[0];
}
}
toTokenString(literalNames, symbolicNames) {
let names = [];
for (let i = 0; i < this.intervals.length; i++) {
let existing = this.intervals[i];
for (let j = existing.start; j < existing.stop; j++) {
names.push(this.elementName(literalNames, symbolicNames, j));
}
}
if (names.length > 1) {
return "{" + names.join(", ") + "}";
} else {
return names[0];
}
}
elementName(literalNames, symbolicNames, token) {
if (token === Token.EOF) {
return "<EOF>";
} else if (token === Token.EPSILON) {
return "<EPSILON>";
} else {
return literalNames[token] || symbolicNames[token];
}
}
get length(){
return this.intervals.map( interval => interval.length ).reduce((acc, val) => acc + val);
}
}