2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2017-11-16 23:30:08 -08:00
|
|
|
|
|
|
|
pub trait RangeExt<T> {
|
2019-04-11 12:30:29 -07:00
|
|
|
fn range_contains(&self, i: &T) -> bool;
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
2018-12-08 14:29:41 -08:00
|
|
|
impl<T> RangeExt<T> for Range<T>
|
|
|
|
where
|
2019-04-11 15:23:14 -07:00
|
|
|
T: PartialOrd,
|
2018-12-08 14:29:41 -08:00
|
|
|
{
|
2019-04-11 12:30:29 -07:00
|
|
|
fn range_contains(&self, i: &T) -> bool {
|
|
|
|
i >= &self.start && i < &self.end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RangeExt<T> for RangeInclusive<T>
|
|
|
|
where
|
2019-04-11 15:23:14 -07:00
|
|
|
T: PartialOrd,
|
2019-04-11 12:30:29 -07:00
|
|
|
{
|
|
|
|
fn range_contains(&self, i: &T) -> bool {
|
|
|
|
i >= self.start() && i <= self.end()
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2019-04-11 15:23:14 -07:00
|
|
|
fn exclusive() {
|
2019-04-11 12:30:29 -07:00
|
|
|
assert!(!(0..0).range_contains(&0));
|
2019-04-11 15:23:14 -07:00
|
|
|
assert!(!(0..0).range_contains(&0));
|
|
|
|
assert!(!(1..10).range_contains(&0));
|
|
|
|
assert!(!(1..10).range_contains(&10));
|
2019-04-11 12:30:29 -07:00
|
|
|
assert!(!(1..10).range_contains(&0));
|
|
|
|
assert!(!(1..10).range_contains(&10));
|
2019-04-11 15:23:14 -07:00
|
|
|
assert!((0..1).range_contains(&0));
|
|
|
|
assert!((0..1).range_contains(&0));
|
|
|
|
assert!((10..20).range_contains(&15));
|
|
|
|
assert!((10..20).range_contains(&15));
|
2019-04-11 12:30:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-04-11 15:23:14 -07:00
|
|
|
fn inclusive() {
|
2019-04-11 12:30:29 -07:00
|
|
|
assert!(!(0..=10).range_contains(&11));
|
2019-04-11 15:23:14 -07:00
|
|
|
assert!(!(1..=10).range_contains(&0));
|
2019-04-11 12:30:29 -07:00
|
|
|
assert!(!(5..=10).range_contains(&4));
|
2019-04-11 15:23:14 -07:00
|
|
|
assert!((0..=0).range_contains(&0));
|
|
|
|
assert!((0..=1).range_contains(&0));
|
|
|
|
assert!((0..=10).range_contains(&0));
|
|
|
|
assert!((0..=10).range_contains(&10));
|
|
|
|
assert!((0..=10).range_contains(&7));
|
|
|
|
assert!((1..=10).range_contains(&10));
|
|
|
|
assert!((10..=20).range_contains(&15));
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|