Skip to content

Commit

Permalink
use simple interval algebra
Browse files Browse the repository at this point in the history
  • Loading branch information
bradNASA authored and dandelany committed Mar 29, 2024
1 parent 0e1829f commit aabdc0a
Showing 1 changed file with 13 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,8 @@ public static boolean startBeforeEnd(Interval x, Interval y) {
* @return whether the operands overlap
*/
static boolean overlaps(Interval x, Interval y) {
// First try for a fast shortcut that doesn't require allocating a new interval.
if (!x.isEmpty() && !y.isEmpty()) {
return !endBeforeStart(x, y) && !endBeforeStart(y, x);
}
return !isEmpty(intersect(x, y));
if (x.isEmpty() || y.isEmpty()) return false;
return !endBeforeStart(x, y) && !endBeforeStart(y, x);
}

/**
Expand All @@ -173,14 +170,8 @@ static boolean overlaps(Interval x, Interval y) {
* @return whether `outer` contains every point in `inner`
*/
static boolean contains(Interval outer, Interval inner) {
// First try for a fast shortcut that doesn't require allocating a new interval.
if (!outer.isEmpty() && !inner.isEmpty()) {
return !startBeforeStart(inner, outer) && !endBeforeEnd(outer, inner);
}

// If `inner` doesn't overlap with the complement of `outer`,
// then `inner` must exist entirely within `outer`.
return !(overlaps(inner, strictUpperBoundsOf(outer)) || overlaps(inner, strictLowerBoundsOf(outer)));
if (outer.isEmpty() || inner.isEmpty()) return false;
return !startBeforeStart(inner, outer) && !endBeforeEnd(outer, inner);
}

/**
Expand Down Expand Up @@ -215,11 +206,8 @@ static boolean equals(Interval x, Interval y) {
* @return whether the start point of x is before all points in y
*/
static boolean startsBefore(Interval x, Interval y) {
// First try for a fast shortcut that doesn't require allocating a new interval.
if (!x.isEmpty() && !y.isEmpty()) {
return startBeforeStart(x, y);
}
return strictlyContains(strictLowerBoundsOf(y), strictLowerBoundsOf(x));
if (x.isEmpty() || y.isEmpty()) return false;
return startBeforeStart(x, y);
}

/**
Expand All @@ -230,11 +218,8 @@ static boolean startsBefore(Interval x, Interval y) {
* @return whether the end point of x is after all points in y
*/
static boolean endsAfter(Interval x, Interval y) {
// First try for a fast shortcut that doesn't require allocating a new interval.
if (!x.isEmpty() && !y.isEmpty()) {
return endBeforeEnd(y, x);
}
return strictlyContains(strictUpperBoundsOf(y), strictUpperBoundsOf(x));
if (x.isEmpty() || y.isEmpty()) return false;
return endBeforeEnd(y, x);
}

/**
Expand Down Expand Up @@ -278,12 +263,9 @@ static boolean startsStrictlyAfter(Interval x, Interval y) {
* @return whether the end point of x is strictly before all points in y
*/
static boolean endsStrictlyBefore(Interval x, Interval y) {
// First try for a fast shortcut that doesn't require allocating a new interval.
if (!x.isEmpty() && !y.isEmpty()) {
return x.end.shorterThan(y.start) ||
(x.end.isEqualTo(y.start) && (!x.includesEnd() && !y.includesStart()));
}
return !isEmpty(intersect(strictUpperBoundsOf(x), strictLowerBoundsOf(y)));
if (x.isEmpty() || y.isEmpty()) return false;
return x.end.shorterThan(y.start) ||
(x.end.isEqualTo(y.start) && (!x.includesEnd() && !y.includesStart()));
}

/**
Expand All @@ -294,12 +276,8 @@ static boolean endsStrictlyBefore(Interval x, Interval y) {
* @return whether x ends when y begins, with no overlap and no gap
*/
static boolean meets(Interval x, Interval y) {
// First try for a fast shortcut that doesn't require allocating a new interval.
if (!x.isEmpty() && !y.isEmpty()) {
return x.end.isEqualTo(y.start) && (x.endInclusivity != y.startInclusivity);
}

return equals(strictUpperBoundsOf(x), strictUpperBoundsOf(strictLowerBoundsOf(y)));
if (x.isEmpty() || y.isEmpty()) return false;
return x.end.isEqualTo(y.start) && (x.endInclusivity != y.startInclusivity);
}

/**
Expand Down

0 comments on commit aabdc0a

Please sign in to comment.