Merge pull request #46 from gzsombor/helper-functions

Add intersection/translate and size to the interval type
This commit is contained in:
ripytide
2023-12-06 18:00:10 +00:00
committed by GitHub
2 changed files with 74 additions and 4 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "discrete_range_map"
version = "0.6.0"
version = "0.6.1"
authors = ["James Forster <james.forsterer@gmail.com>"]
edition = "2021"
description = """
+73 -3
View File
@@ -1486,7 +1486,7 @@ pub trait InclusiveRange<I> {
self.start() <= self.end()
}
///requires that self comes before other and they don't overlap
/// requires that self comes before other and they don't overlap
fn touches_ordered(&self, other: &Self) -> bool
where
I: PointType,
@@ -1494,7 +1494,7 @@ pub trait InclusiveRange<I> {
self.end() == other.start().down().unwrap()
}
///requires that self comes before other
/// requires that self comes before other
fn overlaps_ordered(&self, other: &Self) -> bool
where
I: PointType,
@@ -1502,7 +1502,47 @@ pub trait InclusiveRange<I> {
self.contains(other.start()) || self.contains(other.end())
}
///requires that self comes before other
/// Intersect the range with the other one, and return Some if the intersection is not empty.
fn intersect(&self, other: &Self) -> Option<Self>
where
I: PointType,
Self: From<InclusiveInterval<I>>,
{
let intersect_start = I::max(self.start(), other.start());
let intersect_end = I::min(self.end(), other.end());
if intersect_start <= intersect_end {
Some(Self::from(InclusiveInterval {
start: intersect_start,
end: intersect_end,
}))
} else {
None
}
}
/// Move the entire range by the given amount.
fn translate(&self, delta: I) -> Self
where
I: PointType,
I: core::ops::Add<Output = I>,
Self: From<InclusiveInterval<I>>,
{
Self::from(InclusiveInterval {
start: self.start() + delta,
end: self.end() + delta,
})
}
/// The amount between the start and the end points of the range.
fn size(&self) -> I
where
I: PointType,
I: core::ops::Sub<Output = I>,
{
(self.end() - self.start()).up().unwrap()
}
/// requires that self comes before other
fn merge_ordered(&self, other: &Self) -> Self
where
Self: From<InclusiveInterval<I>>,
@@ -2272,6 +2312,36 @@ mod tests {
}
}
#[test]
fn test_intersection() {
let input = InclusiveInterval { start: 5, end: 10 };
assert_eq!(
input.intersect(&InclusiveInterval { start: 8, end: 13 }),
Some(InclusiveInterval { start: 8, end: 10 })
);
assert_eq!(
input.intersect(&InclusiveInterval { start: 10, end: 13 }),
Some(InclusiveInterval { start: 10, end: 10 })
);
assert_eq!(
input.intersect(&InclusiveInterval { start: 11, end: 13 }),
None
);
}
#[test]
fn test_translate() {
let input = InclusiveInterval { start: 5, end: 10 };
assert_eq!(input.translate(3), InclusiveInterval { start: 8, end: 13 });
assert_eq!(input.translate(-2), InclusiveInterval { start: 3, end: 8 });
}
#[test]
fn test_size() {
assert_eq!(InclusiveInterval { start: 5, end: 10 }.size(), 6);
assert_eq!(InclusiveInterval { start: 6, end: 6 }.size(), 1);
}
// Test Helper Functions
//======================
fn all_non_overlapping_test_bound_entries()