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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#![allow(dead_code)]

use crate::ops::*;
use num_traits::{Float};
use std::ops::{Sub};

/// Enum for accessing the endpoints of an interval.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum EndPoint {
    Lower,
    Upper,
}

/// Interval from a to b that contains both endponts,
/// where a < b on the floating point line.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub struct ClosedInterval<T> {
    /// The lower limit of the interval
    pub a: T,
    /// The upper limit of the interval
    pub b: T,
}

impl<T: PartialOrd + PartialEq + Copy> ClosedInterval<T> {
    pub fn new(a: T, b: T) -> ClosedInterval<T> {
        assert!(a <= b); // we shouldn't be creating empty intervals this way
        ClosedInterval { a, b }
    }

    /// Check if this interval contains just one point
    pub fn is_singleton(&self) -> bool {
        self.a == self.b
    }

    pub fn endpoint(&self, which: EndPoint) -> T {
        match which {
            EndPoint::Lower => self.a,
            EndPoint::Upper => self.b,
        }
    }
}

impl<T: Sub<Output = T> + Copy + PartialOrd> ClosedInterval<T> {
    pub fn length(&self) -> T {
        assert!(self.a <= self.b);
        self.b - self.a
    }
}

impl<T: Float> Default for ClosedInterval<T> {
    /// Create an empty interval.
    /// The choice of enpoints here is arbitrary as long as b < a.
    fn default() -> Self {
        ClosedInterval::empty()
    }
}

impl<T: Float> Empty for ClosedInterval<T> {
    #[inline]
    fn empty() -> Self {
        ClosedInterval {
            a: T::infinity(),
            b: T::neg_infinity(),
        }
    }
    #[inline]
    fn is_empty(&self) -> bool {
        self.a > self.b
    }
}

impl<T: PartialOrd> Contains<T> for ClosedInterval<T> {
    #[inline]
    fn contains(&self, x: T) -> bool {
        self.a <= x && x <= self.b
    }
}

impl<T: Float> Absorb<T> for ClosedInterval<T> {
    type Output = Self;

    #[inline]
    fn absorb(self, x: T) -> Self {
        ClosedInterval {
            a: T::min(x, self.a),
            b: T::max(x, self.b),
        }
    }
}

impl<T: Float> Absorb<Self> for ClosedInterval<T> {
    type Output = Self;

    #[inline]
    fn absorb(self, other: Self) -> Self {
        if other.is_empty() {
            self
        } else {
            ClosedInterval {
                a: T::min(self.a, other.a),
                b: T::max(self.b, other.b),
            }
        }
    }
}

impl<T: Float> Intersect<Self> for ClosedInterval<T> {
    type Output = Self;

    #[inline]
    fn intersect(self, interval: Self) -> Self {
        ClosedInterval {
            a: T::max(self.a, interval.a),
            b: T::min(self.b, interval.b),
        }
    }

    #[inline]
    fn intersects(self, interval: Self) -> bool {
        !(self.b < interval.a || self.a > interval.b)
    }
}

impl<T: Float> Centroid<Option<T>> for ClosedInterval<T> {
    fn centroid(self) -> Option<T> {
        if self.is_empty() {
            None
        } else {
            Some(T::from(0.5).unwrap() * (self.a + self.b))
        }
    }
}

// -------------------------------------------------------------------------------------

/// Interval from a to b that doesn't contain the endpoints,
/// where a < b on the floating point line.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub struct OpenInterval<T> {
    /// The lower limit of the interval
    pub a: T,
    /// The upper limit of the interval
    pub b: T,
}

impl<T: PartialOrd + Copy> OpenInterval<T> {
    pub fn new(a: T, b: T) -> OpenInterval<T> {
        assert!(a < b); // we shouldn't be creating empty intervals this way
        OpenInterval { a, b }
    }

    pub fn endpoint(&self, which: EndPoint) -> T {
        match which {
            EndPoint::Lower => self.a,
            EndPoint::Upper => self.b,
        }
    }
}

impl<T: PartialOrd + Sub<Output = T> + Copy> OpenInterval<T> {
    pub fn length(&self) -> T {
        assert!(self.a <= self.b);
        self.b - self.a
    }
}

impl<T: Float> Default for OpenInterval<T> {
    /// Create an empty interval.
    /// The choice of endpoints here is arbitrary as long as b < a.
    fn default() -> Self {
        OpenInterval::empty()
    }
}

impl<T: Float> Empty for OpenInterval<T> {
    #[inline]
    fn empty() -> Self {
        OpenInterval {
            a: T::infinity(),
            b: T::neg_infinity(),
        }
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.a >= self.b
    }
}

impl<T: PartialOrd> Contains<T> for OpenInterval<T> {
    #[inline]
    fn contains(&self, x: T) -> bool {
        self.a < x && x < self.b
    }
}

impl<T: Float> Absorb<Self> for OpenInterval<T> {
    type Output = Self;

    #[inline]
    fn absorb(self, other: Self) -> Self {
        if other.is_empty() {
            self
        } else {
            OpenInterval {
                a: T::min(self.a, other.a),
                b: T::max(self.b, other.b),
            }
        }
    }
}

impl<T: Float> Intersect<Self> for OpenInterval<T> {
    type Output = Self;

    #[inline]
    fn intersect(self, other: Self) -> Self {
        OpenInterval {
            a: T::max(self.a, other.a),
            b: T::min(self.b, other.b),
        }
    }

    #[inline]
    fn intersects(self, other: Self) -> bool {
        self.contains(other.a) || self.contains(other.b)
    }
}