finish adding TryFrom<[]> to map and set, and finish writing docs for set methods.
This commit is contained in:
parent
539116716a
commit
53b13cbd4f
14
src/lib.rs
14
src/lib.rs
@ -75,20 +75,20 @@ along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
|
||||
//! }
|
||||
//!
|
||||
//! // Next we can create a custom typed RangeBoundsMap
|
||||
//! let mut reservations_map = RangeBoundsMap::new();
|
||||
//! let reservation_map = RangeBoundsMap::try_from([
|
||||
//! (Reservation::Finite(10, 20), "Ferris".to_string()),
|
||||
//! (Reservation::Infinite(20), "Corro".to_string()),
|
||||
//! ]).unwrap();
|
||||
//!
|
||||
//! reservations_map.insert(Reservation::Finite(10, 20), "Ferris".to_string());
|
||||
//! reservations_map.insert(Reservation::Infinite(20), "Corro".to_string());
|
||||
//!
|
||||
//! for (reservation, name) in reservations_map.overlapping(&(16..17)) {
|
||||
//! for (reservation, name) in reservation_map.overlapping(&(16..17)) {
|
||||
//! println!("{name} has reserved {reservation:?} inside the range 16..17");
|
||||
//! }
|
||||
//!
|
||||
//! for (reservation, name) in reservations_map.iter() {
|
||||
//! for (reservation, name) in reservation_map.iter() {
|
||||
//! println!("{name} has reserved {reservation:?}");
|
||||
//! }
|
||||
//!
|
||||
//! assert_eq!(reservations_map.overlaps(&Reservation::Infinite(0)), true);
|
||||
//! assert_eq!(reservation_map.overlaps(&Reservation::Infinite(0)), true);
|
||||
//! ```
|
||||
//!
|
||||
//!
|
||||
|
@ -41,6 +41,10 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.starts.len()
|
||||
}
|
||||
|
||||
//returns Err(()) if the given range overlaps another range
|
||||
//does not coalesce ranges if they touch
|
||||
pub fn insert(&mut self, range_bounds: K, value: V) -> Result<(), ()> {
|
||||
@ -158,6 +162,22 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize, I, K, V> TryFrom<[(K, V); N]> for RangeBoundsMap<I, K, V>
|
||||
where
|
||||
K: RangeBounds<I>,
|
||||
I: Ord + Clone,
|
||||
{
|
||||
type Error = ();
|
||||
fn try_from(pairs: [(K, V); N]) -> Result<Self, Self::Error> {
|
||||
let mut range_bounds_map = RangeBoundsMap::new();
|
||||
for (range_bounds, value) in pairs {
|
||||
range_bounds_map.insert(range_bounds, value)?;
|
||||
}
|
||||
|
||||
return Ok(range_bounds_map);
|
||||
}
|
||||
}
|
||||
|
||||
fn overlaps<I, A, B>(a: &A, b: &B) -> bool
|
||||
where
|
||||
A: RangeBounds<I>,
|
||||
|
@ -27,12 +27,8 @@ use crate::range_bounds_map::RangeBoundsMap;
|
||||
/// ```
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let mut visits = RangeBoundsSet::new();
|
||||
///
|
||||
/// // Add some ranges
|
||||
/// visits.insert(4..8);
|
||||
/// visits.insert(8..18);
|
||||
/// visits.insert(20..100);
|
||||
/// // Make a set with some ranges
|
||||
/// let visits = RangeBoundsSet::try_from([4..8, 8..18, 20..100]).unwrap();
|
||||
///
|
||||
/// // Check if a point is contained in the set
|
||||
/// if visits.contains_point(&0) {
|
||||
@ -101,55 +97,74 @@ where
|
||||
K: RangeBounds<I>,
|
||||
I: Ord + Clone,
|
||||
{
|
||||
/// Makes a new, empty `RangeBoundsSet`
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use std::ops::Range;
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let range_bounds_set: RangeBoundsSet<u8, Range<u8>> = RangeBoundsSet::new();
|
||||
/// ```
|
||||
/// Makes a new, empty `RangeBoundsSet`.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use std::ops::Range;
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let range_bounds_set: RangeBoundsSet<u8, Range<u8>> = RangeBoundsSet::new();
|
||||
/// ```
|
||||
pub fn new() -> Self {
|
||||
RangeBoundsSet {
|
||||
map: RangeBoundsMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of `RangeBounds` in the set.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let mut range_bounds_set = RangeBoundsSet::new();
|
||||
///
|
||||
/// assert_eq!(range_bounds_set.len(), 0);
|
||||
/// range_bounds_set.insert(0..1).unwrap();
|
||||
/// assert_eq!(range_bounds_set.len(), 1);
|
||||
/// ```
|
||||
pub fn len(&self) -> usize {
|
||||
self.map.len()
|
||||
}
|
||||
|
||||
/// Adds a new `RangeBounds` to the set.
|
||||
///
|
||||
/// If the new `RangeBounds` overlaps one or more `RangeBounds`
|
||||
/// already in the set then `Err(())` is returned and the set is
|
||||
/// not updated.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let mut range_bounds_set = RangeBoundsSet::new();
|
||||
///
|
||||
/// range_bounds_set.insert(5..10);
|
||||
/// ```
|
||||
///
|
||||
/// If the new `RangeBounds` overlaps one or more `RangeBounds`
|
||||
/// already in the set then `Err(())` is returned and the set is
|
||||
/// not updated.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let mut range_bounds_set = RangeBoundsSet::new();
|
||||
///
|
||||
/// assert_eq!(range_bounds_set.insert(5..10), Ok(()));
|
||||
/// assert_eq!(range_bounds_set.insert(5..10), Err(()));
|
||||
/// assert_eq!(range_bounds_set.len(), 1);
|
||||
/// ```
|
||||
pub fn insert(&mut self, range_bounds: K) -> Result<(), ()> {
|
||||
self.map.insert(range_bounds, ())
|
||||
}
|
||||
|
||||
/// Returns `true` if the given `RangeBounds` overlaps any of the
|
||||
/// `RangeBounds` in the set.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let mut range_bounds_set = RangeBoundsSet::new();
|
||||
///
|
||||
/// range_bounds_set.insert(5..10);
|
||||
///
|
||||
/// assert_eq!(range_bounds_set.overlaps(4..6), false);
|
||||
///
|
||||
///
|
||||
/// assert_eq!(range_bounds_set.overlaps(4..6), true);
|
||||
/// ```
|
||||
/// `RangeBounds` in the set.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let mut range_bounds_set = RangeBoundsSet::new();
|
||||
///
|
||||
/// range_bounds_set.insert(5..10);
|
||||
///
|
||||
/// assert_eq!(range_bounds_set.overlaps(&(1..=3)), false);
|
||||
/// assert_eq!(range_bounds_set.overlaps(&(4..5)), false);
|
||||
///
|
||||
/// assert_eq!(range_bounds_set.overlaps(&(4..=5)), true);
|
||||
/// assert_eq!(range_bounds_set.overlaps(&(4..6)), true);
|
||||
/// ```
|
||||
pub fn overlaps<Q>(&self, search_range_bounds: &Q) -> bool
|
||||
where
|
||||
Q: RangeBounds<I>,
|
||||
@ -157,6 +172,21 @@ where
|
||||
self.map.overlaps(search_range_bounds)
|
||||
}
|
||||
|
||||
/// Returns an iterator over every `RangeBounds` in the set which
|
||||
/// overlaps the given `search_range_bounds` in ascending order.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let range_bounds_set = RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
||||
///
|
||||
/// let mut overlapping = range_bounds_set.overlapping(&(2..8));
|
||||
///
|
||||
/// assert_eq!(overlapping.next(), Some(&(1..4)));
|
||||
/// assert_eq!(overlapping.next(), Some(&(4..8)));
|
||||
/// assert_eq!(overlapping.next(), None);
|
||||
/// ```
|
||||
pub fn overlapping<Q>(
|
||||
&self,
|
||||
search_range_bounds: &Q,
|
||||
@ -169,15 +199,72 @@ where
|
||||
.map(|(key, _)| key)
|
||||
}
|
||||
|
||||
/// Returns a reference to the `RangeBounds` in the set that
|
||||
/// overlaps the given point, 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.get_at_point(&3), Some(&(1..4)));
|
||||
/// assert_eq!(range_bounds_set.get_at_point(&4), Some(&(4..8)));
|
||||
/// assert_eq!(range_bounds_set.get_at_point(&101), None);
|
||||
/// ```
|
||||
pub fn get_at_point(&self, point: &I) -> Option<&K> {
|
||||
self.map.get_key_value_at_point(point).map(|(key, _)| key)
|
||||
}
|
||||
|
||||
/// Returns `true` if the set contains a `RangeBounds` that
|
||||
/// overlaps a given point, and `false` if not.
|
||||
///
|
||||
/// # 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.contains_point(&3), true);
|
||||
/// assert_eq!(range_bounds_set.contains_point(&4), true);
|
||||
/// assert_eq!(range_bounds_set.contains_point(&101), false);
|
||||
/// ```
|
||||
pub fn contains_point(&self, point: &I) -> bool {
|
||||
self.map.contains_point(point)
|
||||
}
|
||||
|
||||
/// Returns an iterator over every `RangeBouns` in the set in
|
||||
/// ascending order.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use range_bounds_map::RangeBoundsSet;
|
||||
///
|
||||
/// let range_bounds_set = RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
||||
///
|
||||
/// let mut iter = range_bounds_set.overlapping(&(2..8));
|
||||
///
|
||||
/// assert_eq!(iter.next(), Some(&(1..4)));
|
||||
/// assert_eq!(iter.next(), Some(&(4..8)));
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
pub fn iter(&self) -> impl Iterator<Item = &K> {
|
||||
self.map.iter().map(|(key, _)| key)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize, I, K> TryFrom<[K; N]> for RangeBoundsSet<I, K>
|
||||
where
|
||||
K: RangeBounds<I>,
|
||||
I: Ord + Clone,
|
||||
{
|
||||
type Error = ();
|
||||
fn try_from(range_bounds: [K; N]) -> Result<Self, Self::Error> {
|
||||
let mut range_bounds_set = RangeBoundsSet::new();
|
||||
for range_bounds in range_bounds {
|
||||
range_bounds_set.insert(range_bounds)?;
|
||||
}
|
||||
|
||||
return Ok(range_bounds_set);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user