Skip to content

Instantly share code, notes, and snippets.

@Gnosnay
Created July 4, 2020 18:10
Show Gist options
  • Select an option

  • Save Gnosnay/b846f5910c6b66e20c3eeb8b9893a8ed to your computer and use it in GitHub Desktop.

Select an option

Save Gnosnay/b846f5910c6b66e20c3eeb8b9893a8ed to your computer and use it in GitHub Desktop.
check whether two range have common part
use std::cmp::Ordering;
use std::ops::Range;
pub fn has_common_part<T: Ord>(a: Range<T>, b: Range<T>) -> bool {
match a.start.cmp(&b.start) {
Ordering::Equal => true,
Ordering::Less => if let Ordering::Greater = a.end.cmp(&b.start) { true } else { false }
Ordering::Greater => if let Ordering::Less = a.start.cmp(&b.end) { true } else { false }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn common_test() {
assert_eq!(has_common_part(0..3, 1..2), true);
assert_eq!(has_common_part(1..3, 0..3), true);
assert_eq!(has_common_part(0..3, 3..4), false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment