1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! `Interval<T>` for capturing intervals.
pub use crate::interval_tree::interval_type::IntervalType;
use std::fmt::{Debug, Display, Formatter};
use std::ops::RangeInclusive;

/// Structure to represent an interval.
#[derive(Default, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
pub struct Interval<T>
where
    T: IntervalType,
{
    pub start: T,
    pub end: T,
}

impl<T> Interval<T>
where
    T: IntervalType,
{
    /// Constructs a new interval.
    ///
    /// # Example
    /// ```rust
    /// use space_partitioning::interval_tree::Interval;
    /// let interval = Interval::new(-2.0, 10.0);
    /// assert_eq!(interval.start, -2.0);
    /// assert_eq!(interval.end, 10.0);
    /// ```
    pub fn new(low: T, high: T) -> Self {
        Self {
            start: low,
            end: high,
        }
    }

    /// Checks whether the current interval overlaps with another one.
    ///
    /// # Example
    /// ```rust
    /// use space_partitioning::interval_tree::Interval;
    /// let interval = Interval::from(-2.0..=10.0);
    /// assert!(interval.overlaps_with(&(0.0..=2.0).into()));
    /// assert!(!interval.overlaps_with(&(20.0..=30.0).into()));
    /// ```
    pub fn overlaps_with(&self, other: &Interval<T>) -> bool {
        (self.start <= other.end) && (other.start <= self.end)
    }
}

impl<T> Debug for Interval<T>
where
    T: Debug + IntervalType,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{:?}, {:?}]", self.start, self.end)
    }
}

impl<T> Display for Interval<T>
where
    T: Display + IntervalType,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{}, {}]", self.start, self.end)
    }
}

impl<T> From<(T, T)> for Interval<T>
where
    T: IntervalType,
{
    /// Constructs an interval from a tuple.
    ///
    /// # Example
    /// ```rust
    /// use space_partitioning::interval_tree::Interval;
    /// let interval: Interval<_> = (-2.0, 10.0).into();
    /// assert_eq!(interval.start, -2.0);
    /// assert_eq!(interval.end, 10.0);
    /// assert_eq!(interval, Interval::from((-2.0, 10.0)));
    /// ```
    fn from(interval: (T, T)) -> Self {
        Self {
            start: interval.0,
            end: interval.1,
        }
    }
}

impl<T> From<std::ops::RangeInclusive<T>> for Interval<T>
where
    T: IntervalType,
{
    /// Constructs an interval from a `RangeInclusive<T>``.
    ///
    /// # Example
    /// ```rust
    /// use space_partitioning::interval_tree::Interval;
    /// let interval: Interval<_> = (-2.0..=10.0).into();
    /// assert_eq!(interval.start, -2.0);
    /// assert_eq!(interval.end, 10.0);
    /// assert_eq!(interval, Interval::from(-2.0..=10.0));
    /// ```
    fn from(range: RangeInclusive<T>) -> Self {
        Self {
            start: range.start().clone(),
            end: range.end().clone(),
        }
    }
}

impl<T> From<&std::ops::RangeInclusive<T>> for Interval<T>
where
    T: IntervalType,
{
    /// Constructs an interval from a `&RangeInclusive<T>``.
    ///
    /// # Example
    /// ```rust
    /// use space_partitioning::interval_tree::Interval;
    /// let range = -2.0..=10.0;
    /// let interval: Interval<_> = (&range).into();
    /// assert_eq!(interval.start, -2.0);
    /// assert_eq!(interval.end, 10.0);
    /// ```
    fn from(range: &RangeInclusive<T>) -> Self {
        Self {
            start: range.start().clone(),
            end: range.end().clone(),
        }
    }
}