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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use core::convert::TryInto;
use num_traits::float::FloatCore;
use ordered_float::NotNan;

//Convenience function to create a Range.
#[inline(always)]
pub fn range<T>(start: T, end: T) -> Range<T> {
    Range { start, end }
}

///A 1D range. Internally represented as start and end. (as opposed to a start and length)
///This means that subdivision does not result in any floating point calculations.
///The start value must be <= the end value.
///There is no protection against "degenerate" Ranges where start>end.
///Behavior of any of the functions with degenrate Ranges is unspecified.
///
///
///A point is consindered inside of a range if the point is in [start,end), a semi-open interval.
///
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[must_use]
pub struct Range<T> {
    pub start: T,
    pub end: T,
}

impl<T> Range<T> {
    #[inline(always)]
    #[must_use]
    pub fn new(start: T, end: T) -> Range<T> {
        Range { start, end }
    }
}


impl<'a,B> From<&'a mut [B;2]> for &'a mut Range<B>{
    #[inline(always)]
    fn from(a:&'a mut [B;2])->Self{
        unsafe{&mut *(a as *mut _ as *mut _)}
    }
}
impl<'a,B> From<&'a [B;2]> for &'a Range<B>{
    #[inline(always)]
    fn from(a:&'a [B;2])->Self{
        unsafe{&*(a as *const _ as *const _)}
    }
}


impl<B> From<[B;2]> for Range<B>{
    #[inline(always)]
    fn from(a:[B;2])->Self{
        let [a,b]=a;
        Range{start:a,end:b}
    }
}

impl<B> Into<[B;2]> for Range<B>{
    #[inline(always)]
    fn into(self)->[B;2]{
        [self.start,self.end]
    }
}

impl<B> AsRef<[B;2]> for Range<B>{
    #[inline(always)]
    fn as_ref(& self)->&[B;2]{
        unsafe{&*(self as *const _ as *const _)}     
    }
}

impl<B> AsMut<[B;2]> for Range<B>{
    #[inline(always)]
    fn as_mut(&mut self)->&mut [B;2]{
        unsafe{&mut *(self as *mut _ as *mut _)}     
    }
}




impl<T: Copy + Ord + core::ops::Sub<Output = T> + num_traits::sign::Signed> Range<T> {
    #[inline(always)]
    pub fn distance_to_point(&self, pos: T) -> Option<T> {
        if self.contains(pos) {
            None
        } else {
            Some((pos - self.start).abs().min((pos - self.end).abs()))
        }
    }

    ///Positive if point is to the right of the range.
    ///Negative if point is to the left of range.
    #[inline(always)]
    pub fn difference_to_point(&self, pos: T) -> Option<T> {
        match self.contains_ext(pos) {
            core::cmp::Ordering::Less => Some(pos - self.start),
            core::cmp::Ordering::Greater => Some(pos - self.end),
            core::cmp::Ordering::Equal => None,
        }
    }
}
impl<T: Copy + PartialOrd> Range<T> {
    ///Like contains() but returns Ord.
    ///If the pos is stricly less than the range.start, return less.
    ///If the pos is greater of equal to the range.end, return greater.
    ///else, return equal.
    #[inline(always)]
    pub fn contains_ext(&self, pos: T) -> core::cmp::Ordering {
        if pos < self.start {
            core::cmp::Ordering::Less
        } else if pos >= self.end {
            core::cmp::Ordering::Greater
        } else {
            core::cmp::Ordering::Equal
        }
    }

    ///Returns true if the point is inside of the range or on top of.
    #[inline(always)]
    pub fn contains(&self, pos: T) -> bool {
        self.contains_ext(pos) == core::cmp::Ordering::Equal
        //self.start <= pos && pos < self.end
    }

    ///Subdivides the range.
    ///No floating point calculations are done.
    #[inline(always)]
    pub fn subdivide(&self, divider: T) -> (Range<T>, Range<T>) {
        debug_assert!(self.start <= divider);
        debug_assert!(divider < self.end);

        let l = Range {
            start: self.start,
            end: divider,
        };
        let r = Range {
            start: divider,
            end: self.end,
        };
        (l, r)
    }

    #[must_use]
    #[inline(always)]
    pub fn is_valid(&self) -> bool {
        self.start <= self.end
    }

    #[inline(always)]
    pub fn grow_to_fit(&mut self, b: &Range<T>) {
        let a = self;
        if b.start < a.start {
            a.start = b.start;
        }
        if b.end > a.end {
            a.end = b.end;
        }
    }

    ///Returns true if self contains the specified range.
    #[inline(always)]
    pub fn contains_range(&self, val: &Range<T>) -> bool {
        self.start <= val.start && val.end <= self.end
    }

    ///Returns true if two ranges intersect.
    #[inline(always)]
    pub fn intersects(&self, val: &Range<T>) -> bool {
        self.contains(val.start) || val.contains(self.start)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_range() {
        let a = Range::new(0, 5);
        let b = Range::new(0, 5);
        assert!(a.contains_range(&b))
    }
}
impl<T: Copy + core::ops::Sub<Output = T>> Range<T> {
    #[inline(always)]
    pub fn distance(&self) -> T {
        self.end - self.start
    }
}

impl<T: Copy + core::ops::Sub<Output = T> + core::ops::Add<Output = T>> Range<T> {
    #[inline(always)]
    pub fn grow(&mut self, radius: T) -> &mut Self {
        self.end = self.end + radius;
        self.start = self.start - radius;
        self
    }
}

impl<N: FloatCore> AsRef<Range<N>> for Range<NotNan<N>> {
    #[inline(always)]
    fn as_ref(&self) -> &Range<N> {
        unsafe { &*((self as *const Self) as *const Range<N>) }
    }
}

impl<S: Copy> Range<S> {
    
    #[inline(always)]
    pub fn inner_as<B:'static+Copy>(&self) -> Range<B> where S: num_traits::AsPrimitive<B>{
        Range {
            start: self.start.as_(),
            end: self.end.as_(),
        }
    }


    #[inline(always)]
    pub fn inner_into<A>(&self) -> Range<A> where S:Into<A> {
        let start = self.start.into();
        let end = self.end.into();
        Range { start, end }
    }


    #[inline(always)]
    pub fn inner_try_into<A>(&self) -> Result<Range<A>, S::Error> where S: TryInto<A> {
        let start = self.start.try_into();
        let end = self.end.try_into();
        match (start, end) {
            (Ok(start), Ok(end)) => Ok(Range { start, end }),
            (Ok(_), Err(e)) => Err(e),
            (Err(e), Ok(_)) => Err(e),
            (Err(e1), Err(_)) => Err(e1),
        }
    }
}

impl<T: Copy + core::ops::Sub<Output = T> + core::ops::Add<Output = T>> Range<T> {
    ///Create a range from a point and radius.
    #[inline(always)]
    pub fn from_point(point: T, radius: T) -> Range<T> {
        Range {
            start: point - radius,
            end: point + radius,
        }
    }
}

impl<T: Copy + Ord> Range<T> {
    ///Creates a range that represents the intersection range.
    #[inline(always)]
    pub fn get_intersection(&self, val: &Range<T>) -> Option<Range<T>> {
        let a = self.start.max(val.start);
        let b = self.end.min(val.end);
        if a > b {
            None
        } else {
            Some(Range { start: a, end: b })
        }
    }
}