diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index b69568a..e81d49f 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -1225,6 +1225,51 @@ where return Ok(()); } + + /// Returns the first (`RangeBounds`, `Value`) pair in the map, if + /// any. + /// + /// # Examples + /// ``` + /// use range_bounds_map::RangeBoundsMap; + /// + /// let range_bounds_map = RangeBoundsMap::try_from([ + /// (1..4, false), + /// (4..8, true), + /// (8..100, false), + /// ]) + /// .unwrap(); + /// + /// assert_eq!( + /// range_bounds_map.first_entry(), + /// Some((&(1..4), &false)) + /// ); + /// ``` + pub fn first_entry(&self) -> Option<(&K, &V)> { + self.iter().next() + } + + /// Returns the last (`RangeBounds`, `Value`) pair in the map, if + /// any. + /// + /// # Examples + /// ``` + /// use range_bounds_map::RangeBoundsMap; + /// + /// let range_bounds_map = RangeBoundsMap::try_from([ + /// (1..4, false), + /// (4..8, true), + /// (8..100, false), + /// ]) + /// .unwrap(); + /// + /// assert_eq!( + /// range_bounds_map.last_entry(), + /// Some((&(8..100), &false)) + /// ); + pub fn last_entry(&self) -> Option<(&K, &V)> { + self.iter().next_back() + } } impl TryFrom<[(K, V); N]> for RangeBoundsMap diff --git a/src/range_bounds_set.rs b/src/range_bounds_set.rs index a1c5fc6..ae9170c 100644 --- a/src/range_bounds_set.rs +++ b/src/range_bounds_set.rs @@ -104,7 +104,7 @@ use crate::{ /// ``` /// /// [`RangeBounds`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html -#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] pub struct RangeBoundsSet where I: PartialOrd, @@ -618,6 +618,36 @@ where { self.map.overwrite(range_bounds, ()) } + + /// Returns the first `RangeBounds` in the set, if any. + /// + /// # Examples + /// ``` + /// use range_bounds_map::RangeBoundsSet; + /// + /// let range_bounds_set = + /// RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap(); + /// + /// assert_eq!(range_bounds_set.first(), Some(&(1..4))); + /// ``` + pub fn first(&self) -> Option<&K> { + self.map.first_entry().map(|(key, _)| key) + } + + /// Returns the last `RangeBounds` in the set, if any. + /// + /// # Examples + /// ``` + /// use range_bounds_map::RangeBoundsSet; + /// + /// let range_bounds_set = + /// RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap(); + /// + /// assert_eq!(range_bounds_set.last(), Some(&(8..100))); + /// ``` + pub fn last(&self) -> Option<&K> { + self.map.last_entry().map(|(key, _)| key) + } } impl TryFrom<[K; N]> for RangeBoundsSet @@ -652,3 +682,15 @@ where return Ok(range_bounds_set); } } + +impl Default for RangeBoundsSet +where + I: PartialOrd, +{ + #[trivial] + fn default() -> Self { + RangeBoundsSet { + map: RangeBoundsMap::default(), + } + } +} diff --git a/todo.md b/todo.md index 6926570..d970e15 100644 --- a/todo.md +++ b/todo.md @@ -3,11 +3,10 @@ - try to fix all the uses of cloned() in the library - make a StartBoundWrapper that uses StartBound to implement ord and use that instead of storing the startbound twice -- text flow all comments # features -- RangeMap, RangeSet, RangeInclusiveMap... types for signature +- make specifc RangeMap, RangeSet, RangeInclusiveMap... types for signature simplification - add coalesce if same-value otherwise overwrite) function to make finally make range_bounds_map a superset of rangemap