in accord-core/src/main/java/accord/primitives/AbstractRanges.java [335:380]
static long supersetLinearMerge(Range[] as, Range[] bs)
{
int ai = 0, bi = 0;
out: while (ai < as.length && bi < bs.length)
{
Range a = as[ai];
Range b = bs[bi];
int c = a.compareIntersecting(b);
if (c < 0)
{
ai++;
}
else if (c > 0)
{
break;
}
else if (b.start().compareTo(a.start()) < 0)
{
break;
}
else if ((c = b.end().compareTo(a.end())) <= 0)
{
bi++;
if (c == 0) ai++;
}
else
{
// use a temporary counter, so that if we don't find a run of ranges that enforce the superset
// condition we exit at the start of the mismatch run (and permit it to be merged)
// TODO (easy, efficiency): use exponentialSearch
int tmpai = ai;
do
{
if (++tmpai == as.length || !a.end().equals(as[tmpai].start()))
break out;
a = as[tmpai];
}
while (a.end().compareTo(b.end()) < 0);
bi++;
ai = tmpai;
}
}
return ((long)ai << 32) | bi;
}