LongRangeSet (@NotNull LongRangeSet other) { LongRangeSet intersection = super.meet(other); if (intersection instanceof RangeSet) { long min = intersection.min(); long max = intersection.max(); long diff = max - min; if (diff > 0 && diff < Long.SIZE) { for (byte newMod = (byte)(diff + 1); newMod <= Long.SIZE; newMod++) { if (newMod % myMod == 0) { long bits = widenBits(newMod); for (long pos = min; pos <= max; pos++) { int bit = remainder(pos, newMod); if (isSet(bits, bit) && !intersection.contains(pos)) { bits = clearBit(bits, bit); } } return modRange(min, max, newMod, bits); } } } long[] ranges = ((RangeSet)intersection).myRanges; boolean emptyHoles = true; for (int i = 2; i < ranges.length; i += 2) { if (range(ranges[i - 1] + 1, ranges[i] - 1).intersects(this)) { emptyHoles = false; break; } } if (emptyHoles) { intersection = range(min, max); } } if (intersection instanceof Range || intersection instanceof Point) { long bits = myBits; int mod = myMod; if (other instanceof ModRange modRange) { int lcm = lcm(modRange.myMod); if (lcm <= Long.SIZE) { bits = widenBits(lcm) & modRange.widenBits(lcm); mod = (byte)lcm; } else if (modRange.myMod > myMod) { bits = modRange.myBits; // new LCM doesn't fit the Long.SIZE: just select bigger mod mod = modRange.myMod; } } return modRange(intersection.min(), intersection.max(), mod, bits); } return intersection; }