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
mod merge;
pub use merge::merge_neighbours_with_same_class;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Interval<A, C> {
Lower { below: A, class: C },
Range { from: A, below: A, class: C },
Upper { from: A, class: C },
Infinite { class: C },
}
impl<A, C> Interval<A, C>
where
A: PartialOrd + Copy,
C: Copy,
{
pub fn lower(below: A, class: C) -> Self {
Interval::Lower { below, class }
}
pub fn range(from: A, below: A, class: C) -> Self {
Interval::Range { from, below, class }
}
pub fn upper(from: A, class: C) -> Self {
Interval::Upper { from, class }
}
pub fn matches(&self, value: A) -> bool {
match self {
Interval::Lower { below, .. } => value < *below,
Interval::Range { from, below, .. } => value >= *from && value < *below,
Interval::Upper { from, .. } => value >= *from,
Interval::Infinite { .. } => true,
}
}
pub fn class(&self) -> &C {
match self {
Interval::Lower { class, .. } => class,
Interval::Range { class, .. } => class,
Interval::Upper { class, .. } => class,
Interval::Infinite { class } => class,
}
}
}