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
use crate::*;

/// # A trait for time overlapping
///
/// Two time windows overlap if the
/// intersection is not empty.
pub trait TimeOverlapping<TW> {
    fn overlaps(&self, rhs: &TW) -> bool;
}


impl<TW1:TimeConvex,TW2:TimeConvex> TimeOverlapping<TW2> for TW1
    where TW2: TimeBounds<TimePoint=TW1::TimePoint>
{
    #[inline]
    fn overlaps(&self, rhs: &TW2) -> bool {
        self.lower_bound() <= rhs.upper_bound() && rhs.lower_bound() <= self.upper_bound()
    }
}


impl<TW:TimeConvex> TimeOverlapping<TimeSet<TW::TimePoint>> for TW
{
    #[inline]
    fn overlaps(&self, rhs: &TimeSet<TW::TimePoint>) -> bool {
        rhs.overlaps(self)
    }
}

impl<T:TimePoint, TW> TimeOverlapping<TW> for TimeSet<T>
    where TW: TimeConvex<TimePoint=T>
{
    #[inline]
    fn overlaps(&self, rhs: &TW) -> bool
    {
        self.0.iter()
            .find(|ts| ts.upper_bound() >= rhs.lower_bound())
            .map(|ts| ts.lower_bound() <= rhs.upper_bound())
            .unwrap_or(false)
    }
}

impl<T:TimePoint> TimeOverlapping<Self> for TimeSet<T>
{
    fn overlaps(&self, rhs: &Self) -> bool {
        // todo: optimise it by using order of inner intervals
        rhs.into_iter().any(|tw| self.overlaps(&tw))
    }
}