Created
July 4, 2020 18:10
-
-
Save Gnosnay/b846f5910c6b66e20c3eeb8b9893a8ed to your computer and use it in GitHub Desktop.
check whether two range have common part
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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