tangled
alpha
login
or
join now
jonas.tngl.sh
/
advent-of-code
0
fork
atom
Advent of Code solutions in Rust
0
fork
atom
overview
issues
pulls
pipelines
feat: add `try_union` function to `RangeSet` trait
jonas.tngl.sh
3 months ago
820277bf
57f74dfc
+44
1 changed file
expand all
collapse all
unified
split
aoc_utils
src
range.rs
+44
aoc_utils/src/range.rs
···
2
2
3
3
/// Extension trait for set operations on range types
4
4
pub trait RangeSet: Sized {
5
5
+
fn try_union(&self, other: &Self) -> Option<Self>;
5
6
fn intersection(&self, other: &Self) -> Option<Self>;
6
7
}
7
8
···
9
10
where
10
11
T: Copy + PartialOrd + Ord,
11
12
{
13
13
+
fn try_union(&self, other: &Self) -> Option<Self> {
14
14
+
if self.contains(other.start()) {
15
15
+
Some(*self.start()..=*self.end().max(other.end()))
16
16
+
} else if other.contains(self.start()) {
17
17
+
Some(*other.start()..=*self.end().max(other.end()))
18
18
+
} else {
19
19
+
None
20
20
+
}
21
21
+
}
22
22
+
12
23
fn intersection(&self, other: &Self) -> Option<Self> {
13
24
let range = (*self.start().max(other.start()))..=(*self.end().min(other.end()));
14
25
(!range.is_empty()).then_some(range)
···
18
29
#[cfg(test)]
19
30
mod tests {
20
31
use super::*;
32
32
+
33
33
+
#[test]
34
34
+
fn inclusive_range_try_union() {
35
35
+
// 1 2 3 4 5 6 7 8
36
36
+
// ---------
37
37
+
// -----------
38
38
+
assert_eq!(RangeSet::try_union(&(1..=5), &(3..=8)), Some(1..=8));
39
39
+
40
40
+
// 1 2 3 4 5 6 7 8
41
41
+
// -----------
42
42
+
// ---------
43
43
+
assert_eq!(RangeSet::try_union(&(3..=8), &(1..=5)), Some(1..=8));
44
44
+
45
45
+
// 1 2 3 4 5 6 7 8
46
46
+
// ---------------
47
47
+
// -----
48
48
+
assert_eq!(RangeSet::try_union(&(1..=8), &(3..=5)), Some(1..=8));
49
49
+
50
50
+
// 1 2 3 4 5 6 7 8
51
51
+
// -----
52
52
+
// ---------------
53
53
+
assert_eq!(RangeSet::try_union(&(3..=5), &(1..=8)), Some(1..=8));
54
54
+
55
55
+
// 1 2 3 4 5 6 7 8
56
56
+
// -----
57
57
+
// -------
58
58
+
assert_eq!(RangeSet::try_union(&(1..=3), &(5..=8)), None);
59
59
+
60
60
+
// 1 2 3 4 5 6 7 8
61
61
+
// -------
62
62
+
// -----
63
63
+
assert_eq!(RangeSet::try_union(&(5..=8), &(1..=3)), None);
64
64
+
}
21
65
22
66
#[test]
23
67
fn inclusive_range_intersection() {