besides method

  1. @override
  2. @useResult
bool besides(
  1. Range<T> other
)
override

Returns true if an empty range exists between this and other.

Discrete ranges are not considered to be besides each other even though there are no elements "between them", i.e. [1..4] is not beside [5..7].

This function is reflexive and symmetric.

Examples

final a = Interval.closedOpen(1, 3);
final b = Interval.closedOpen(3, 5);

a.besides(b); // true, [1..3) is beside [3..5)
final a = Interval.closed(1, 3);
final b = Interval.open(3, 5);

a.besides(b); // true, [1..3] is beside (3..5)
final a = Interval.closed(1, 5);
final b = Interval.open(3, 7);

a.besides(b); // false, [1..5] is not beside (3..7)
final a = Interval.closed(1, 3);
final b = Interval.open(5, 7);

a.besides(b); // false, [1..3] is not beside (5..7)
final a = Interval.closed(1, 4);
final b = Interval.closed(5, 7);

a.besides(b); // false, [1..4] is not beside [5..7], discrete range

Implementation

@override
@useResult bool besides(Range<T> other) => switch (other) {
  Min<T> _ => Besides.minInterval(other, this),
  Max<T> _ => Besides.maxInterval(other, this),
  Interval<T>(:final min, : final max) => (this.min.open == !max.open && this.min.value == max.value)
                                       || (this.max.open == !min.open && this.max.value == min.value),
  Unbound<T> _ => false,
};