operator + method
{@inheritDoc} */
Implementation
IntervalSet operator +(IntervalSet other) {
final myIntervals = intervals;
final theirIntervals = (other).intervals;
IntervalSet? intersection;
final mySize = myIntervals.length;
final theirSize = theirIntervals.length;
var i = 0;
var j = 0;
// iterate down both interval lists looking for nondisjoint intervals
while (i < mySize && j < theirSize) {
final mine = myIntervals[i];
final theirs = theirIntervals[j];
//System.out.println("mine="+mine+" and theirs="+theirs);
if (mine.startsBeforeDisjoint(theirs)) {
// move this iterator looking for interval that might overlap
i++;
} else if (theirs.startsBeforeDisjoint(mine)) {
// move other iterator looking for interval that might overlap
j++;
} else if (mine.properlyContains(theirs)) {
// overlap, add intersection, get next theirs
intersection ??= IntervalSet();
intersection.add(mine.intersection(theirs));
j++;
} else if (theirs.properlyContains(mine)) {
// overlap, add intersection, get next mine
intersection ??= IntervalSet();
intersection.add(mine.intersection(theirs));
i++;
} else if (!mine.disjoint(theirs)) {
// overlap, add intersection
intersection ??= IntervalSet();
intersection.add(mine.intersection(theirs));
// Move the iterator of lower range [a..b], but not
// the upper range as it may contain elements that will collide
// with the next iterator. So, if mine=[0..115] and
// theirs=[115..200], then intersection is 115 and move mine
// but not theirs as theirs may collide with the next range
// in thisIter.
// move both iterators to next ranges
if (mine.startsAfterNonDisjoint(theirs)) {
j++;
} else if (theirs.startsAfterNonDisjoint(mine)) {
i++;
}
}
}
if (intersection == null) {
return IntervalSet();
}
return intersection;
}