From ac0532b882dd3ee2d9ef6b41013ce619e72c58f8 Mon Sep 17 00:00:00 2001 From: ripytide Date: Sun, 2 Apr 2023 21:25:50 +0100 Subject: [PATCH] more impls --- Cargo.toml | 2 +- src/helpers.rs | 5 ++-- src/range_bounds_map.rs | 57 ++++++++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f10a43d..a0be100 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ serde = {version = "1.0.148", features = ["derive"]} itertools = "0.10.5" labels = "0.0.2" pretty_assertions = "1.3.0" -btree_monstousity = "0.0.4" +btree_monstousity = {version ="0.0.4", features = ["btree_drain_filter"]} [dev-dependencies] ordered-float = "3.4.0" diff --git a/src/helpers.rs b/src/helpers.rs index 537e996..81b8139 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -24,7 +24,6 @@ use labels::{tested, trivial}; use crate::bound_ord::BoundOrd; -//todo why is pub(crate) needed? pub(crate) fn cmp_range_bounds_with_bound_ord( range_bounds: &A, bound_ord: BoundOrd<&B>, @@ -118,7 +117,7 @@ where } #[trivial] -fn contains_bound_ord(range_bounds: &A, bound_ord: BoundOrd<&I>) -> bool +pub(crate) fn contains_bound_ord(range_bounds: &A, bound_ord: BoundOrd<&I>) -> bool where A: RangeBounds, I: Ord, @@ -221,7 +220,7 @@ where } #[tested] -fn overlaps(a: &A, b: &B) -> bool +pub fn overlaps(a: &A, b: &B) -> bool where A: RangeBounds, B: RangeBounds, diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index 83f1c6f..b585972 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -32,7 +32,10 @@ use serde::ser::SerializeMap; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::bound_ord::BoundOrd; -use crate::helpers::{cmp_range_bounds_with_bound_ord, is_valid_range_bounds}; +use crate::helpers::{ + cmp_range_bounds_with_bound_ord, contains_bound_ord, is_valid_range_bounds, + overlaps, +}; use crate::TryFromBounds; /// An ordered map of non-overlapping [`RangeBounds`] based on [`BTreeMap`]. @@ -364,13 +367,7 @@ where return Err(OverlapError); } - let double_comp = |inner_range_bounds: &K, new_range_bounds: &K| { - let retult = BoundOrd::start(new_range_bounds.start_bound()) - .cmp(&BoundOrd::start(inner_range_bounds.start_bound())); - retult - }; - - self.inner.insert(range_bounds, value, double_comp); + self.inner.insert(range_bounds, value, double_comp()); return Ok(()); } @@ -542,7 +539,7 @@ where /// ``` #[trivial] pub fn get_entry_at_point(&self, point: &I) -> Option<(&K, &V)> { - self.inner.get_key_value(comp_start(Bound::Included(point))) + self.inner.get_key_value(comp_start(Bound::Included(point))) } /// Returns an iterator over every (`RangeBounds`, `Value`) entry @@ -566,10 +563,10 @@ where /// assert_eq!(iter.next(), Some((&(8..100), &false))); /// assert_eq!(iter.next(), None); /// ``` - //#[trivial] - //pub fn iter(&self) -> impl DoubleEndedIterator { - //todo!() - //} + #[trivial] + pub fn iter(&self) -> impl DoubleEndedIterator { + self.inner.iter() + } /// Removes every (`RangeBounds`, `Value`) entry in the map which /// overlaps the given `RangeBounds` and returns them in @@ -602,16 +599,20 @@ where /// /// assert_eq!(map.iter().collect::>(), [(&(8..100), &false)]); /// ``` - //#[tested] - //pub fn remove_overlapping( - //&mut self, - //range_bounds: Q, - //) -> impl DoubleEndedIterator - //where - //Q: RangeBounds, - //{ - //todo!() - //} + #[tested] + pub fn remove_overlapping<'a, Q>( + &'a mut self, + range_bounds: Q, + ) -> impl Iterator + '_ + where + Q: RangeBounds + 'a, + { + //optimisation, switch to BTreeMap::drain if it ever gets + //implemented + return self.inner.drain_filter(move |inner_range_bounds, _| { + overlaps(inner_range_bounds, &range_bounds) + }); + } /// Cuts a given `RangeBounds` out of the map and returns an /// iterator of the full or partial `RangeBounds` that were cut in @@ -1676,3 +1677,13 @@ where ) } } +fn double_comp() -> impl FnMut(&K, &K) -> Ordering +where + K: RangeBounds, + I: Ord, +{ + |inner_range_bounds: &K, new_range_bounds: &K| { + BoundOrd::start(new_range_bounds.start_bound()) + .cmp(&BoundOrd::start(inner_range_bounds.start_bound())) + } +}