use std::cmp::{max, min};
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Interval {
pub a: i32,
pub b: i32,
}
pub(super) const INVALID: Interval = Interval { a: -1, b: -2 };
impl Interval {
pub fn new(a: i32, b: i32) -> Interval {
Interval { a, b }
}
pub fn invalid() -> Interval {
INVALID
}
pub fn length(&self) -> i32 {
self.b - self.a
}
pub fn union(&self, another: &Interval) -> Interval {
Interval {
a: min(self.a, another.a),
b: max(self.b, another.b),
}
}
pub fn starts_before_disjoint(&self, other: &Interval) -> bool {
self.a < other.a && self.b < other.a
}
pub fn starts_before_non_disjoint(&self, other: &Interval) -> bool {
self.a <= other.a && self.b >= other.a
}
pub fn starts_after(&self, other: &Interval) -> bool {
self.a > other.a
}
pub fn starts_after_disjoint(&self, other: &Interval) -> bool {
self.a > other.b
}
pub fn starts_after_non_disjoint(&self, other: &Interval) -> bool {
self.a > other.a && self.a <= other.b }
pub fn disjoint(&self, other: &Interval) -> bool {
self.starts_before_disjoint(other) || self.starts_after_disjoint(other)
}
pub fn adjacent(&self, other: &Interval) -> bool {
self.a == other.b + 1 || self.b == other.a - 1
}
}