Day03 *finally* done.
This commit is contained in:
parent
bd5d5ae901
commit
bb245b8711
12 changed files with 1002 additions and 56 deletions
|
@ -1,50 +1,53 @@
|
|||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
/// range that includes the start, but excludes the end.
|
||||
pub struct Range<T>
|
||||
where
|
||||
T: PartialOrd + Ord + PartialEq + Copy,
|
||||
{
|
||||
start: T,
|
||||
end: T,
|
||||
// #[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
// /// range that includes the start, but excludes the end.
|
||||
// pub struct Range<T>
|
||||
// where
|
||||
// T: PartialOrd + Ord + PartialEq + Copy,
|
||||
// {
|
||||
// start: T,
|
||||
// end: T,
|
||||
// }
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
pub trait RangeIntersection {
|
||||
fn any_overlap(&self, other: &Self) -> bool;
|
||||
fn calc_intersection(&self, other: &Self) -> Option<Self>
|
||||
where
|
||||
Self: std::marker::Sized;
|
||||
fn complete_overlap(&self, other: &Self) -> bool;
|
||||
}
|
||||
|
||||
impl<T> Range<T>
|
||||
impl<T> RangeIntersection for Range<T>
|
||||
where
|
||||
T: PartialOrd + Ord + PartialEq + Copy,
|
||||
{
|
||||
pub fn new(start: T, end: T) -> Self {
|
||||
Self {
|
||||
start: start.min(end),
|
||||
end: end.max(start),
|
||||
}
|
||||
}
|
||||
|
||||
/// calcs whether self and other overlap at all. symettric.
|
||||
pub fn any_overlap(&self, other: &Self) -> bool {
|
||||
self.start < other.end && self.end > other.start
|
||||
fn any_overlap(&self, other: &Self) -> bool {
|
||||
if self.is_empty() || other.is_empty() {
|
||||
false
|
||||
} else {
|
||||
self.start < other.end && self.end > other.start
|
||||
}
|
||||
}
|
||||
|
||||
/// calculates the range that is part of both ranges.
|
||||
/// Returns None if the ranges do not overlap.
|
||||
pub fn calc_intersection(&self, other: &Self) -> Option<Self> {
|
||||
fn calc_intersection(&self, other: &Self) -> Option<Self> {
|
||||
if self.any_overlap(other) {
|
||||
Some(
|
||||
Self::new(
|
||||
self.start.max(other.start), self.end.min(other.end)
|
||||
)
|
||||
)
|
||||
Some(self.start.max(other.start)..self.end.min(other.end))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///calcs whether self completely contains other
|
||||
pub fn complete_overlap(&self, other: &Self) -> bool {
|
||||
self.start <= other.start && self.end >= other.end
|
||||
}
|
||||
|
||||
pub fn in_range(&self, other: T) -> bool {
|
||||
self.start <= other && other < self.end
|
||||
fn complete_overlap(&self, other: &Self) -> bool {
|
||||
if self.is_empty() || other.is_empty() {
|
||||
false
|
||||
} else {
|
||||
self.start <= other.start && self.end >= other.end
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,31 +57,31 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn calc_intersection() {
|
||||
let a = Range::new(1, 5);
|
||||
let b = Range::new(3, 9);
|
||||
let c = Range::new(4, 6);
|
||||
let d = Range::new(6, 8);
|
||||
let e = Range::new(1, 4);
|
||||
assert_eq!(a.calc_intersection(&b), Some(Range::new(3,5)));
|
||||
assert_eq!(b.calc_intersection(&a), Some(Range::new(3,5)));
|
||||
let a = 1..5;
|
||||
let b = 3..9;
|
||||
let c = 4..6;
|
||||
let d = 6..8;
|
||||
let e = 1..4;
|
||||
assert_eq!(a.calc_intersection(&b), Some(3..5));
|
||||
assert_eq!(b.calc_intersection(&a), Some(3..5));
|
||||
|
||||
assert_eq!(a.calc_intersection(&d), None);
|
||||
assert_eq!(d.calc_intersection(&a), None);
|
||||
|
||||
assert_eq!(c.calc_intersection(&b), Some(c));
|
||||
assert_eq!(b.calc_intersection(&c), Some(c));
|
||||
assert_eq!(c.calc_intersection(&b), Some(c.clone()));
|
||||
assert_eq!(b.calc_intersection(&c), Some(c.clone()));
|
||||
|
||||
assert_eq!(e.calc_intersection(&b), Some(Range::new(3,4)));
|
||||
assert_eq!(b.calc_intersection(&e), Some(Range::new(3,4)));
|
||||
assert_eq!(e.calc_intersection(&b), Some(3..4));
|
||||
assert_eq!(b.calc_intersection(&e), Some(3..4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_any_overlap() {
|
||||
let a = Range::new(1, 5);
|
||||
let b = Range::new(3, 9);
|
||||
let c = Range::new(4, 6);
|
||||
let d = Range::new(6, 8);
|
||||
let e = Range::new(1, 4);
|
||||
let a = 1..5;
|
||||
let b = 3..9;
|
||||
let c = 4..6;
|
||||
let d = 6..8;
|
||||
let e = 1..4;
|
||||
assert!(a.any_overlap(&b));
|
||||
assert!(b.any_overlap(&a));
|
||||
|
||||
|
@ -96,11 +99,11 @@ mod tests {
|
|||
}
|
||||
#[test]
|
||||
fn test_complete_overlap() {
|
||||
let a = Range::new(1, 5);
|
||||
let b = Range::new(3, 9);
|
||||
let c = Range::new(4, 6);
|
||||
let d = Range::new(6, 8);
|
||||
let e = Range::new(1, 4);
|
||||
let a = 1..5;
|
||||
let b = 3..9;
|
||||
let c = 4..6;
|
||||
let d = 6..8;
|
||||
let e = 1..4;
|
||||
assert!(a.complete_overlap(&a));
|
||||
|
||||
assert!(a.complete_overlap(&e));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue