added first() and last() functions

This commit is contained in:
ripytide
2022-12-10 16:29:14 +00:00
parent 33e4cfac0b
commit 6b57f6d66f
3 changed files with 89 additions and 3 deletions
+45
View File
@@ -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<const N: usize, I, K, V> TryFrom<[(K, V); N]> for RangeBoundsMap<I, K, V>
+43 -1
View File
@@ -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<I, K>
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<const N: usize, I, K> TryFrom<[K; N]> for RangeBoundsSet<I, K>
@@ -652,3 +682,15 @@ where
return Ok(range_bounds_set);
}
}
impl<I, K> Default for RangeBoundsSet<I, K>
where
I: PartialOrd,
{
#[trivial]
fn default() -> Self {
RangeBoundsSet {
map: RangeBoundsMap::default(),
}
}
}
+1 -2
View File
@@ -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