-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathIntervalSet.php
526 lines (413 loc) · 13.8 KB
/
IntervalSet.php
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
<?php
declare(strict_types=1);
namespace Antlr\Antlr4\Runtime;
use Antlr\Antlr4\Runtime\Comparison\Equality;
use Antlr\Antlr4\Runtime\Comparison\Equatable;
use Antlr\Antlr4\Runtime\Utils\StringUtils;
/**
* This class implements the {@see IntSet} backed by a sorted array of
* non-overlapping intervals. It is particularly efficient for representing
* large collections of numbers, where the majority of elements appear as part
* of a sequential range of numbers that are all part of the set. For example,
* the set { 1, 2, 3, 4, 7, 8 } may be represented as { [1, 4], [7, 8] }.
*
* This class is able to represent sets containing any combination of values in
* the range {@see Integer::MIN_VALUE} to {@see Integer::MAX_VALUE}
* (inclusive).
*/
final class IntervalSet implements Equatable
{
/** @var array<Interval> */
protected array $intervals = [];
protected bool $readOnly = false;
/**
* Create a set with a single element, el.
*/
public static function fromInt(int $number): self
{
$set = new self();
$set->addOne($number);
return $set;
}
/**
* Create a set with all ints within range [start..end] (inclusive).
*/
public static function fromRange(int $start, int $end): self
{
$set = new self();
$set->addRange($start, $end);
return $set;
}
public function complement(IntervalSet $set): ?self
{
if ($set->isNull()) {
return null;
}
return $set->subtract($this);
}
public function subtract(IntervalSet $set): self
{
if ($set->isNull()) {
return $this;
}
return self::subtractSets($this, $set);
}
public function orSet(IntervalSet $other): self
{
$result = new self();
$result->addSet($this);
$result->addSet($other);
return $result;
}
/**
* Compute the set difference between two interval sets. The specific
* operation is `left - right`. If either of the input sets is `null`,
* it is treated as though it was an empty set.
*/
public static function subtractSets(IntervalSet $left, IntervalSet $right): self
{
if ($left->isNull()) {
return new self();
}
if ($right->isNull()) {
// right set has no elements; just return the copy of the current set
return $left;
}
$result = $left;
$resultI = 0;
$rightI = 0;
while ($resultI < \count($result->intervals) && $rightI < \count($right->intervals)) {
$resultInterval = $result->intervals[$resultI];
$rightInterval = $right->intervals[$rightI];
// operation: (resultInterval - rightInterval) and update indexes
if ($rightInterval->stop < $resultInterval->start) {
$rightI++;
continue;
}
if ($rightInterval->start > $resultInterval->stop) {
$resultI++;
continue;
}
$beforeCurrent = null;
$afterCurrent = null;
if ($rightInterval->start > $resultInterval->start) {
$beforeCurrent = new Interval($resultInterval->start, $rightInterval->start - 1);
}
if ($rightInterval->stop < $resultInterval->stop) {
$afterCurrent = new Interval($rightInterval->stop + 1, $resultInterval->stop);
}
if ($beforeCurrent !== null) {
if ($afterCurrent !== null) {
// split the current interval into two
$result->intervals[$resultI] = $beforeCurrent;
$result->intervals[$resultI + 1] = $afterCurrent;
$resultI++;
$rightI++;
continue;
}
// replace the current interval
$result->intervals[$resultI] = $beforeCurrent;
$resultI++;
continue;
}
if ($afterCurrent !== null) {
// replace the current interval
$result->intervals[$resultI] = $afterCurrent;
$rightI++;
continue;
}
// remove the current interval (thus no need to increment resultI)
\array_splice($result->intervals, $resultI, 1);
continue;
}
// If rightI reached right.intervals.size(), no more intervals to subtract from result.
// If resultI reached result.intervals.size(), we would be subtracting from an empty set.
// Either way, we are done.
return $result;
}
public function isReadOnly(): bool
{
return $this->readOnly;
}
public function setReadOnly(bool $readOnly): void
{
$this->readOnly = $readOnly;
}
public function first(): int
{
if (\count($this->intervals) === 0) {
return Token::INVALID_TYPE;
}
return $this->intervals[0]->start;
}
public function addOne(int $value): void
{
$this->addInterval(new Interval($value, $value));
}
public function addRange(int $left, int $right): void
{
$this->addInterval(new Interval($left, $right));
}
protected function addInterval(Interval $addition): void
{
if ($this->readOnly) {
throw new \InvalidArgumentException('Can\'t alter readonly IntervalSet.');
}
if ($addition->stop < $addition->start) {
return;
}
// find position in list
// Use iterators as we modify list in place
for ($i = 0, $count = \count($this->intervals); $i < $count; $i++) {
/** @var Interval $resilt */
$resilt = $this->intervals[$i];
if ($addition->equals($resilt)) {
return;
}
if ($addition->adjacent($resilt) || !$addition->disjoint($resilt)) {
// next to each other, make a single larger interval
$bigger = $addition->union($resilt);
$this->intervals[$i] = $bigger;
// make sure we didn't just create an interval that
// should be merged with next interval in list
$i++;
while ($i < \count($this->intervals)) {
$next = $this->intervals[$i];
if (!$bigger->adjacent($next) && $bigger->disjoint($next)) {
break;
}
// if we bump up against or overlap next, merge
\array_splice($this->intervals, $i, 1); // remove this one
$i--; // move backwards to what we just set
$this->intervals[$i] = $bigger->union($next); // set to 3 merged ones
$i++; // first call to next after previous duplicates the result
}
return;
}
if ($addition->startsBeforeDisjoint($resilt)) {
// insert before r
\array_splice($this->intervals, $i, 0, [$addition]);
return;
}
// if disjoint and after r, a future iteration will handle it
}
// ok, must be after last interval (and disjoint from last interval) just add it
$this->intervals[] = $addition;
}
public function addSet(IntervalSet $other): self
{
foreach ($other->intervals as $i) {
$this->addInterval(new Interval($i->start, $i->stop));
}
return $this;
}
public function contains(int $item): bool
{
$count = \count($this->intervals);
$left = 0;
$right = $count - 1;
// Binary search for the element in the (sorted, disjoint) array of intervals.
while ($left <= $right) {
$m = \intval($left + $right, 2);
$interval = $this->intervals[$m];
$start = $interval->start;
$stop = $interval->stop;
if ($stop < $item) {
$left = $m + 1;
} elseif ($start > $item) {
$right = $m - 1;
} else { // item >= start && item <= stop
return true;
}
}
return false;
}
public function length(): int
{
$length = 0;
foreach ($this->intervals as $i) {
$length += $i->getLength();
}
return $length;
}
public function removeOne(int $v): void
{
foreach ($this->intervals as $k => $i) {
// intervals is ordered
if ($v < $i->start) {
return;
}
// check for single value range
if ($v === $i->start && $v === $i->stop) {
\array_splice($this->intervals, $k, 1);
return;
}
// check for lower boundary
if ($v === $i->start) {
$this->intervals[$k] = new Interval($i->start + 1, $i->stop);
return;
}
// check for upper boundary
if ($v === $i->stop - 1) {
$this->intervals[$k] = new Interval($i->start, $i->stop - 1);
return;
}
// split existing range
if ($v < $i->stop - 1) {
$x = new Interval($i->start, $v);
$i->start = $v + 1;
\array_splice($this->intervals, $k, 0, [$x]);
return;
}
}
}
public function isNull(): bool
{
return \count($this->intervals) === 0;
}
/**
* Returns the maximum value contained in the set if not isNull().
*
* @return int The maximum value contained in the set.
*
* @throws \LogicException If set is empty.
*/
public function getMaxElement(): int
{
if ($this->isNull()) {
throw new \LogicException('The set is empty.');
}
return $this->intervals[\count($this->intervals)-1]->stop;
}
/**
* Returns the minimum value contained in the set if not isNull().
*
* @return int The minimum value contained in the set.
*
* @throws \LogicException If set is empty.
*/
public function getMinElement(): int
{
if ($this->isNull()) {
throw new \LogicException('The set is empty.');
}
return $this->intervals[0]->start;
}
public function toStringChars(bool $elemAreChar): string
{
if (\count($this->intervals) === 0) {
return '{}';
}
$buf = '';
if ($this->length() > 1) {
$buf .= '{';
}
$iter = new \ArrayIterator($this->intervals);
while ($iter->valid()) {
$interval = $iter->current();
$iter->next();
$start = $interval->start;
$stop = $interval->stop;
if ($start === $stop) {
if ($start === Token::EOF) {
$buf .= '<EOF>';
} elseif ($elemAreChar) {
$buf .= '\'' . StringUtils::char($start) . '\'';
} else {
$buf .= $start;
}
} else {
if ($elemAreChar) {
$buf .= \sprintf(
'\'%s\'..\'%s\'',
StringUtils::char($start),
StringUtils::char($stop),
);
} else {
$buf .= \sprintf('%s..%s', $start, $stop);
}
}
if ($iter->valid()) {
$buf .= ', ';
}
}
if ($this->length() > 1) {
$buf .= '}';
}
return $buf;
}
public function toStringVocabulary(Vocabulary $vocabulary): string
{
if (\count($this->intervals) === 0) {
return '{}';
}
$buf = '';
if ($this->length() > 1) {
$buf .= '{';
}
$iterator = new \ArrayIterator($this->intervals);
while ($iterator->valid()) {
$interval = $iterator->current();
$iterator->next();
$start = $interval->start;
$stop = $interval->stop;
if ($start === $stop) {
$buf .= $this->elementName($vocabulary, $start);
} else {
for ($i = $start; $i <= $stop; $i++) {
if ($i > $start) {
$buf .= ', ';
}
$buf .= $this->elementName($vocabulary, $i);
}
}
if ($iterator->valid()) {
$buf .= ', ';
}
}
if ($this->length() > 1) {
$buf .= '}';
}
return $buf;
}
protected function elementName(Vocabulary $vocabulary, int $a): string
{
if ($a === Token::EOF) {
return '<EOF>';
}
if ($a === Token::EPSILON) {
return '<EPSILON>';
}
return $vocabulary->getDisplayName($a);
}
public function equals(object $other): bool
{
if ($this === $other) {
return true;
}
if (!$other instanceof self) {
return false;
}
return $this->readOnly === $other->readOnly
&& Equality::equals($this->intervals, $other->intervals);
}
/**
* @return array<int>
*/
public function toArray(): array
{
$values = [];
foreach ($this->intervals as $interval) {
$start = $interval->start;
$stop = $interval->stop;
for ($value = $start; $value <= $stop; $value++) {
$values[] = $value;
}
}
return $values;
}
public function __toString(): string
{
return $this->toStringChars(false);
}
}