diff --git a/range_bounds_set/src/range_bounds_set.rs b/range_bounds_set/src/range_bounds_set.rs index 02cf6a5..5bcd913 100644 --- a/range_bounds_set/src/range_bounds_set.rs +++ b/range_bounds_set/src/range_bounds_set.rs @@ -4,7 +4,7 @@ use std::ops::{Bound, RangeBounds}; use derive_new::new; -use crate::bound_ext::{EndBoundWithOrd, StartBoundWithOrd}; +use crate::specific_bounds::{EndBound, StartBound}; type Id = u128; @@ -14,9 +14,9 @@ pub struct RangeBoundsSet { #[new(default)] ranges: HashMap, #[new(default)] - starts: BTreeSet>, + starts: BTreeSet>, #[new(default)] - ends: BTreeSet>, + ends: BTreeSet>, phantom_data: PhantomData, #[new(default)] @@ -26,7 +26,7 @@ pub struct RangeBoundsSet { impl RangeBoundsSet where T: RangeBounds, - I: Ord, + I: Ord + Clone, { //returns Err(()) if the inserting the given range overlaps another range //coalesces ranges if they touch @@ -37,7 +37,11 @@ where pub fn raw_insert(&mut self, range: T) {} pub fn overlapping(&self, range: T) { - self.ends.range(range); + let end_bounds_range = ( + EndBound::from(range.start_bound().cloned()), + EndBound::from(range.end_bound().cloned()), + ); + self.ends.range(end_bounds_range); } pub fn get(&self, point: &I) {} @@ -66,4 +70,17 @@ mod tests { // script to streamline the manual input process. The script // essentially enumerates all of the generated cases and asks me // to input the expected + // + // + // + // + // + // + // + // + // + // + // Rusts Bounds types are overly ambiguous in RangeBouns, it + // should return StartBound and EndBound so that you can + // implement Ord on them } diff --git a/range_bounds_set/src/specific_bounds.rs b/range_bounds_set/src/specific_bounds.rs index c179f85..0d8299c 100644 --- a/range_bounds_set/src/specific_bounds.rs +++ b/range_bounds_set/src/specific_bounds.rs @@ -1,57 +1,8 @@ use std::cmp::Ordering; +use std::ops::{Bound, RangeBounds}; use crate::bound_ext::BoundExt; -pub enum StartBound { - Included(T), - Excluded(T), - Unbounded, -} - -impl PartialEq for StartBound -where - T: PartialEq, -{ - fn eq(&self, other: &Self) -> bool { - match (self.inner(), other.inner()) { - (Some(start1), Some(start2)) => start1 == start2, - (None, None) => true, - _ => false, - } - } -} - -impl PartialOrd for StartBound -where - T: PartialOrd, -{ - fn partial_cmp(&self, other: &Self) -> Option { - match (self.inner(), other.inner()) { - (Some(start1), Some(start2)) => start1.partial_cmp(start2), - (None, Some(_)) => Some(Ordering::Less), - (Some(_), None) => Some(Ordering::Greater), - (None, None) => Some(Ordering::Equal), - } - } -} - -impl BoundExt for StartBound { - fn inner(&self) -> Option<&T> { - match self { - StartBound::Included(inner) => Some(inner), - StartBound::Excluded(inner) => Some(inner), - StartBound::Unbounded => None, - } - } - - fn is_unbounded(&self) -> bool { - match self { - StartBound::Unbounded => true, - _ => false, - } - } -} - pub enum EndBound { Included(T), Excluded(T), @@ -71,12 +22,15 @@ where } } +impl Eq for EndBound where T: PartialEq {} + impl PartialOrd for EndBound where T: PartialOrd, { fn partial_cmp(&self, other: &Self) -> Option { match (self.inner(), other.inner()) { + //todo fix meh (Some(start1), Some(start2)) => start1.partial_cmp(start2), (None, Some(_)) => Some(Ordering::Less), (Some(_), None) => Some(Ordering::Greater), @@ -85,6 +39,24 @@ where } } +impl Ord for EndBound +where + T: PartialOrd, +{ + fn cmp(&self, other: &Self) -> Ordering { + self.partial_cmp(other).unwrap() + } +} + +impl RangeBounds> for (EndBound, EndBound) { + fn start_bound(&self) -> Bound<&EndBound> { + self.0 + } + fn end_bound(&self) -> Bound<&EndBound> { + self.1 + } +} + impl BoundExt for EndBound { fn inner(&self) -> Option<&T> { match self { @@ -101,3 +73,13 @@ impl BoundExt for EndBound { } } } + +impl From> for EndBound { + fn from(value: Bound) -> Self { + match value { + Bound::Included(item) => EndBound::Included(item), + Bound::Excluded(item) => EndBound::Excluded(item), + Bound::Unbounded => Self::Unbounded, + } + } +}