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
use crate::{
    BoundedRange, BoundedSet, LowerBoundedRange, LowerBoundedSet, PiecewiseUnboundedSet,
    RangeIntersection, RangeUnion, Rangetools, UnboundedRange, UnboundedSet, UpperBoundedRange,
    UpperBoundedSet,
};

impl<T, R> RangeIntersection<R, BoundedRange<T>> for UnboundedSet<T>
where
    R: Rangetools<Inner = BoundedRange<T>>,
    T: Copy + Ord,
{
    type Output = BoundedSet<T>;
    fn intersection(self, other: R) -> Self::Output {
        RangeIntersection::intersection(other.to_inner().to_set(), self)
    }
}

impl<T, R> RangeIntersection<R, BoundedSet<T>> for UnboundedSet<T>
where
    R: Rangetools<Inner = BoundedSet<T>>,
    T: Copy + Ord,
{
    type Output = BoundedSet<T>;
    fn intersection(self, other: R) -> Self::Output {
        RangeIntersection::intersection(other.to_inner(), self)
    }
}

impl<T, R> RangeIntersection<R, LowerBoundedRange<T>> for UnboundedSet<T>
where
    R: Rangetools<Inner = LowerBoundedRange<T>>,
    T: Copy + Ord,
{
    type Output = LowerBoundedSet<T>;
    fn intersection(self, other: R) -> Self::Output {
        RangeIntersection::intersection(other.to_inner().to_set(), self)
    }
}

impl<T, R> RangeIntersection<R, LowerBoundedSet<T>> for UnboundedSet<T>
where
    R: Rangetools<Inner = LowerBoundedSet<T>>,
    T: Copy + Ord,
{
    type Output = LowerBoundedSet<T>;
    fn intersection(self, other: R) -> Self::Output {
        RangeIntersection::intersection(other.to_inner(), self)
    }
}

impl<T, R> RangeIntersection<R, UpperBoundedRange<T>> for UnboundedSet<T>
where
    R: Rangetools<Inner = UpperBoundedRange<T>>,
    T: Copy + Ord,
{
    type Output = UpperBoundedSet<T>;
    fn intersection(self, other: R) -> Self::Output {
        RangeIntersection::intersection(other.to_inner().to_set(), self)
    }
}

impl<T, R> RangeIntersection<R, UpperBoundedSet<T>> for UnboundedSet<T>
where
    R: Rangetools<Inner = UpperBoundedSet<T>>,
    T: Copy + Ord,
{
    type Output = UpperBoundedSet<T>;
    fn intersection(self, other: R) -> Self::Output {
        RangeIntersection::intersection(other.to_inner(), self)
    }
}

impl<T, R> RangeIntersection<R, UnboundedRange> for UnboundedSet<T>
where
    R: Rangetools<Inner = UnboundedRange>,
{
    type Output = UnboundedSet<T>;
    fn intersection(self, _: R) -> Self::Output {
        self
    }
}

impl<T, R> RangeIntersection<R, UnboundedSet<T>> for UnboundedSet<T>
where
    R: Rangetools<Inner = UnboundedSet<T>>,
    T: Copy + Ord,
{
    type Output = UnboundedSet<T>;
    fn intersection(self, other: R) -> Self::Output {
        match (self, other.to_inner()) {
            (UnboundedSet::Full, other) => other,
            (s, UnboundedSet::Full) => s,
            (
                s,
                UnboundedSet::Piecewise(PiecewiseUnboundedSet {
                    upper_bounded_range: u2,
                    ranges: r2,
                    lower_bounded_range: l2,
                }),
            ) => {
                let a = RangeIntersection::intersection(s.clone(), u2);
                let b = RangeIntersection::intersection(s.clone(), l2);
                let c = RangeIntersection::intersection(s, r2);
                RangeUnion::union(a, RangeUnion::union(b, c))
            }
        }
    }
}