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
use crate::set::traits::Set;
use num::Num;

/// A one-dimensional interval with a start and an end.
/// Whether or not the start and end elements are included in the interval
/// depends on the actual implementor type
pub trait Interval<T>: Set<T>
where
    T: Num + Copy, {
    fn from_boundaries(start: T, end_inclusive: T) -> Self;

    fn get_start(&self) -> T;

    fn get_end(&self) -> T;

    fn length(&self) -> T;

    fn get_start_if_nonempty(&self) -> Option<T> {
        if self.is_empty() {
            None
        } else {
            Some(self.get_start())
        }
    }

    fn get_end_if_nonempty(&self) -> Option<T> {
        if self.is_empty() {
            None
        } else {
            Some(self.get_end())
        }
    }
}

pub trait Coalesce<T>: Sized {
    fn coalesce_with(&self, other: &T) -> Option<Self>;
}

/// implementors are container types that should be able to coalesce the
/// contained intervals
pub trait CoalesceIntervals<I: Interval<E>, E: Num + Copy>: Sized {
    fn to_coalesced_intervals(&self) -> Vec<I>;

    fn coalesce_intervals_inplace(&mut self);

    fn into_coalesced(mut self) -> Self {
        self.coalesce_intervals_inplace();
        self
    }
}

pub trait Topology {
    fn is_open(&self) -> bool;
    fn is_closed(&self) -> bool;
}