From 3a56cbd5c8bcf18099a5b9f190a40d9060a875ca Mon Sep 17 00:00:00 2001 From: Zsombor Gegesy Date: Wed, 6 Dec 2023 00:00:36 +0100 Subject: [PATCH 1/2] Add intersection/translate and size to the interval type --- src/discrete_range_map.rs | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/discrete_range_map.rs b/src/discrete_range_map.rs index 0293d60..4d039cd 100644 --- a/src/discrete_range_map.rs +++ b/src/discrete_range_map.rs @@ -1502,6 +1502,44 @@ pub trait InclusiveRange { self.contains(other.start()) || self.contains(other.end()) } + // Intersect the range with the other one, and return Some if the intersection is not empty. + fn intersect(&self, other: &Self) -> Option + where + I: PointType, + Self: From>, + { + 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 + } + } + + fn translate(&self, delta: I) -> Self + where + I: PointType, + I: core::ops::Add, + Self: From>, + { + Self::from(InclusiveInterval { + start: self.start() + delta, + end: self.end() + delta, + }) + } + + fn size(&self) -> I + where + I: PointType, + I: core::ops::Sub, + { + (self.end() - self.start()).up().unwrap() + } + ///requires that self comes before other fn merge_ordered(&self, other: &Self) -> Self where @@ -2272,6 +2310,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() From 886449283da666591d07d822586efd0ecd7c0a3a Mon Sep 17 00:00:00 2001 From: ripytide Date: Wed, 6 Dec 2023 17:57:59 +0000 Subject: [PATCH 2/2] minor touchups --- Cargo.toml | 2 +- src/discrete_range_map.rs | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7dd6a33..a4b06ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "discrete_range_map" -version = "0.6.0" +version = "0.6.1" authors = ["James Forster "] edition = "2021" description = """ diff --git a/src/discrete_range_map.rs b/src/discrete_range_map.rs index 4d039cd..852dd37 100644 --- a/src/discrete_range_map.rs +++ b/src/discrete_range_map.rs @@ -1486,7 +1486,7 @@ pub trait InclusiveRange { 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 { 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,7 @@ pub trait InclusiveRange { self.contains(other.start()) || self.contains(other.end()) } - // Intersect the range with the other one, and return Some if the intersection is not empty. + /// Intersect the range with the other one, and return Some if the intersection is not empty. fn intersect(&self, other: &Self) -> Option where I: PointType, @@ -1520,6 +1520,7 @@ pub trait InclusiveRange { } } + /// Move the entire range by the given amount. fn translate(&self, delta: I) -> Self where I: PointType, @@ -1532,6 +1533,7 @@ pub trait InclusiveRange { }) } + /// The amount between the start and the end points of the range. fn size(&self) -> I where I: PointType, @@ -1540,7 +1542,7 @@ pub trait InclusiveRange { (self.end() - self.start()).up().unwrap() } - ///requires that self comes before other + /// requires that self comes before other fn merge_ordered(&self, other: &Self) -> Self where Self: From>,