nanalogue_core/utils/intersects.rs
1//! Intersects trait for testing if an interval intersects with self
2//! Used for geometric and interval intersection testing
3
4use std::ops::Range;
5
6/// Implements test if an interval intersects with self
7pub trait Intersects<T> {
8 /// see if interval intersects with self
9 fn intersects(&self, val: &T) -> bool;
10}
11
12impl Intersects<Range<u64>> for Range<u64> {
13 /// Check if two ranges intersect (overlap)
14 ///
15 /// ```
16 /// use nanalogue_core::Intersects;
17 /// assert!((0..3).intersects(&(0..1)));
18 /// assert!(!(0..3).intersects(&(5..7)));
19 /// assert!(!(0..3).intersects(&(1..1)));
20 /// assert!((1..3).intersects(&(0..2)));
21 /// ```
22 fn intersects(&self, val: &Range<u64>) -> bool {
23 (self.start < val.end && self.end > val.start) && !(self.is_empty() || val.is_empty())
24 }
25}