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
use std::cmp::PartialOrd;
use std::ops::*;

/// Intersection of two ranges from point of the first range
#[derive(Debug)]
pub enum Intersection {
    /// They have no common area
    Empty,
    /// They partially overlap
    Overlap,
    /// One is fully within the other
    Full,
}

impl Intersection {
    /// Test if there is any intersection
    /// '''
    ///     Intersection::Empty => false,
    ///     Intersection::Overlap => true,
    ///     Intersection::Full => true,
    pub fn is_any(&self) -> bool {
        match self {
            Intersection::Empty => false,
            _ => true,
        }
    }
}

/// More precise intersection of two ranges from point of the first range
#[derive(Debug, PartialEq)]
pub enum IntersectionExt {
    /// The self is below the other
    Bellow,
    /// The self is below but overlaping
    BellowOverlap,
    /// The self is within the other
    Within,
    /// The self is same as the other
    Same,
    /// The self is over the other, the other is within the self
    Over,
    /// The self is above but overlaping
    AboveOverlap,
    /// The self is above the other
    Above,
}

impl IntersectionExt {
    /// Get simpler intersection
    pub fn intersection(&self) -> Intersection {
        match self {
            IntersectionExt::Bellow => Intersection::Empty,
            IntersectionExt::BellowOverlap => Intersection::Overlap,
            IntersectionExt::Within => Intersection::Full,
            IntersectionExt::Same => Intersection::Full,
            IntersectionExt::Over => Intersection::Full,
            IntersectionExt::AboveOverlap => Intersection::Overlap,
            IntersectionExt::Above => Intersection::Empty,
        }
    }
    /// Test if there is any intersection
    pub fn is_any(&self) -> bool {
        match self {
            IntersectionExt::Bellow => false,
            IntersectionExt::Above => false,
            _ => true,
        }
    }
}

pub trait Intersect<T: PartialOrd, U: RangeBounds<T>>: RangeBounds<T> {
    /// Test two ranges for an intersection
    fn intersect(&self, other: &U) -> IntersectionExt;
}

impl<T: PartialOrd> Intersect<T, Range<T>> for Range<T> {
    fn intersect(&self, other: &Range<T>) -> IntersectionExt {
        if self.end == other.end {
            if self.start < other.start {
                IntersectionExt::Over
            } else if self.start > other.start {
                IntersectionExt::Within
            } else {
                IntersectionExt::Same
            }
        } else if self.end < other.end {
            if self.end <= other.start {
                IntersectionExt::Bellow
            } else if self.start < other.start {
                IntersectionExt::BellowOverlap
            } else {
                IntersectionExt::Within
            }
        } else if self.start < other.end {
            if self.start <= other.start {
                IntersectionExt::Over
            } else {
                IntersectionExt::AboveOverlap
            }
        } else {
            IntersectionExt::Above
        }
    }
}

impl<T: PartialOrd> Intersect<T, RangeFrom<T>> for Range<T> {
    fn intersect(&self, other: &RangeFrom<T>) -> IntersectionExt {
        if self.end <= other.start {
            IntersectionExt::Bellow
        } else if self.start < other.start {
            IntersectionExt::BellowOverlap
        } else {
            IntersectionExt::Within
        }
    }
}

impl<T: PartialOrd> Intersect<T, RangeFull> for Range<T> {
    fn intersect(&self, _: &RangeFull) -> IntersectionExt {
        IntersectionExt::Within
    }
}

impl<T: PartialOrd> Intersect<T, RangeTo<T>> for Range<T> {
    fn intersect(&self, other: &RangeTo<T>) -> IntersectionExt {
        if self.start >= other.end {
            IntersectionExt::Above
        } else if self.end > other.end {
            IntersectionExt::AboveOverlap
        } else {
            IntersectionExt::Within
        }
    }
}

/*
 * TODO:
use num::Integer;

impl<T: PartialOrd + Copy + Integer> Intersect<T, RangeInclusive<T>> for Range<T> {
    fn intersect(&self, other: &RangeInclusive<T>) -> IntersectionExt {
        let (a_start, a_end) = (self.start, self.end - T::one());
        let (b_start, b_end) = (*other.start(), *other.end());
        if a_end == b_end {
            if a_start < b_start {
                IntersectionExt::Over
            } else if a_start > b_start {
                IntersectionExt::Within
            } else {
                IntersectionExt::Same
            }
        } else if a_end < b_end {
            if a_end <= b_start {
                IntersectionExt::Bellow
            } else if a_start < b_start {
                IntersectionExt::BellowOverlap
            } else {
                IntersectionExt::Within
            }
        } else if a_start < b_end {
            if a_start <= b_start {
                IntersectionExt::Over
            } else {
                IntersectionExt::AboveOverlap
            }
        } else {
            IntersectionExt::Above
        }
    }
}
*/

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

    macro_rules! test {
        ($a: expr) => {
            assert_eq!($a.intersect(&(11..11)), IntersectionExt::Bellow);
            assert_eq!($a.intersect(&(10..11)), IntersectionExt::Bellow);
            assert_eq!($a.intersect(&(9..11)), IntersectionExt::BellowOverlap);
            assert_eq!($a.intersect(&(9..10)), IntersectionExt::Over);
            assert_eq!($a.intersect(&(3..10)), IntersectionExt::Same);
            assert_eq!($a.intersect(&(5..9)), IntersectionExt::Over);
            assert_eq!($a.intersect(&(3..9)), IntersectionExt::Over);
            assert_eq!($a.intersect(&(2..11)), IntersectionExt::Within);
            assert_eq!($a.intersect(&(3..11)), IntersectionExt::Within);
            assert_eq!($a.intersect(&(2..9)), IntersectionExt::AboveOverlap);
            assert_eq!($a.intersect(&(2..3)), IntersectionExt::Above);
            assert_eq!($a.intersect(&(1..2)), IntersectionExt::Above);

            assert_eq!($a.intersect(&(11..)), IntersectionExt::Bellow);
            assert_eq!($a.intersect(&(10..)), IntersectionExt::Bellow);
            assert_eq!($a.intersect(&(9..)), IntersectionExt::BellowOverlap);
            assert_eq!($a.intersect(&(3..)), IntersectionExt::Within);
            assert_eq!($a.intersect(&(2..)), IntersectionExt::Within);

            assert_eq!($a.intersect(&(..)), IntersectionExt::Within);

            assert_eq!($a.intersect(&(..11)), IntersectionExt::Within);
            assert_eq!($a.intersect(&(..10)), IntersectionExt::Within);
            assert_eq!($a.intersect(&(..9)), IntersectionExt::AboveOverlap);
            assert_eq!($a.intersect(&(..3)), IntersectionExt::Above);
            assert_eq!($a.intersect(&(..2)), IntersectionExt::Above);
        };
    }
    #[test]
    pub fn range_test() {
        test!(3..10);

        match (10..22).intersect(&(0..11)) {
            IntersectionExt::Bellow => (),
            IntersectionExt::BellowOverlap => (),
            IntersectionExt::Within => (),
            IntersectionExt::Same => (),
            IntersectionExt::Over => (),
            IntersectionExt::AboveOverlap => (),
            IntersectionExt::Above => (),
        }
    }
}