helpers is looking so neat a tidy now
This commit is contained in:
parent
023a5dbd77
commit
591373f99e
12
Cargo.lock
generated
12
Cargo.lock
generated
@ -55,16 +55,6 @@ dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "labels"
|
||||
version = "0.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ef0d115162498c588b6406a026d2ba545e955720f0ff4230d6b380afe7dd7fd4"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.15"
|
||||
@ -127,9 +117,7 @@ name = "range_bounds_map"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"btree_monstousity",
|
||||
"either",
|
||||
"itertools",
|
||||
"labels",
|
||||
"ordered-float",
|
||||
"pretty_assertions",
|
||||
"serde",
|
||||
|
@ -16,12 +16,10 @@ keywords = ["data-structures", "map", "data", "library"]
|
||||
categories = ["data-structures"]
|
||||
|
||||
[dependencies]
|
||||
either = "1.8.0"
|
||||
serde = {version = "1.0.148", features = ["derive"]}
|
||||
itertools = "0.10.5"
|
||||
labels = "0.0.2"
|
||||
pretty_assertions = "1.3.0"
|
||||
btree_monstousity = {version ="0.0.4", features = ["btree_drain_filter"]}
|
||||
|
||||
[dev-dependencies]
|
||||
ordered-float = "3.4.0"
|
||||
pretty_assertions = "1.3.0"
|
||||
|
108
src/helpers.rs
108
src/helpers.rs
@ -18,9 +18,7 @@ along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::ops::{Bound, RangeBounds};
|
||||
|
||||
use labels::{tested, trivial};
|
||||
use std::ops::Bound;
|
||||
|
||||
use crate::bound_ord::BoundOrd;
|
||||
use crate::range_bounds_map::NiceRange;
|
||||
@ -54,7 +52,6 @@ enum Config {
|
||||
RightContainsLeft,
|
||||
}
|
||||
|
||||
#[tested]
|
||||
fn config<I, A, B>(a: A, b: B) -> Config
|
||||
where
|
||||
A: NiceRange<I>,
|
||||
@ -95,7 +92,6 @@ enum SortedConfig<I> {
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[trivial]
|
||||
fn sorted_config<I, A, B>(a: A, b: B) -> SortedConfig<I>
|
||||
where
|
||||
A: NiceRange<I>,
|
||||
@ -115,30 +111,25 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[trivial]
|
||||
pub(crate) fn contains_bound_ord<I, A>(
|
||||
range_bounds: A,
|
||||
bound_ord: BoundOrd<I>,
|
||||
) -> bool
|
||||
pub(crate) fn contains_bound_ord<I, A>(range: A, bound_ord: BoundOrd<I>) -> bool
|
||||
where
|
||||
A: NiceRange<I>,
|
||||
I: Ord,
|
||||
{
|
||||
let start_bound_ord = BoundOrd::start(range_bounds.start());
|
||||
let end_bound_ord = BoundOrd::end(range_bounds.end());
|
||||
let start_bound_ord = BoundOrd::start(range.start());
|
||||
let end_bound_ord = BoundOrd::end(range.end());
|
||||
|
||||
return bound_ord >= start_bound_ord && bound_ord <= end_bound_ord;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CutResult<I> {
|
||||
pub before_cut: Option<(Bound<I>, Bound<I>)>,
|
||||
pub inside_cut: Option<(Bound<I>, Bound<I>)>,
|
||||
pub after_cut: Option<(Bound<I>, Bound<I>)>,
|
||||
pub(crate) before_cut: Option<(Bound<I>, Bound<I>)>,
|
||||
pub(crate) inside_cut: Option<(Bound<I>, Bound<I>)>,
|
||||
pub(crate) after_cut: Option<(Bound<I>, Bound<I>)>,
|
||||
}
|
||||
|
||||
#[tested]
|
||||
pub(crate) fn cut_range_bounds<I, B, C>(base: B, cut: C) -> CutResult<I>
|
||||
pub(crate) fn cut_range<I, B, C>(base: B, cut: C) -> CutResult<I>
|
||||
where
|
||||
B: NiceRange<I>,
|
||||
C: NiceRange<I>,
|
||||
@ -185,25 +176,18 @@ where
|
||||
|
||||
//only return valid range_bounds
|
||||
return CutResult {
|
||||
before_cut: result
|
||||
.before_cut
|
||||
.filter(|x| is_valid_range_bounds::<(Bound<&I>, Bound<&I>), I>(x)),
|
||||
inside_cut: result
|
||||
.inside_cut
|
||||
.filter(|x| is_valid_range_bounds::<(Bound<&I>, Bound<&I>), I>(x)),
|
||||
after_cut: result
|
||||
.after_cut
|
||||
.filter(|x| is_valid_range_bounds::<(Bound<&I>, Bound<&I>), I>(x)),
|
||||
before_cut: result.before_cut.filter(|x| is_valid_range(*x)),
|
||||
inside_cut: result.inside_cut.filter(|x| is_valid_range(*x)),
|
||||
after_cut: result.after_cut.filter(|x| is_valid_range(*x)),
|
||||
};
|
||||
}
|
||||
|
||||
#[trivial]
|
||||
pub fn is_valid_range_bounds<Q, I>(range_bounds: &Q) -> bool
|
||||
pub(crate) fn is_valid_range<I, K>(range: K) -> bool
|
||||
where
|
||||
Q: RangeBounds<I>,
|
||||
I: std::cmp::PartialOrd,
|
||||
I: Ord,
|
||||
K: NiceRange<I>,
|
||||
{
|
||||
match (range_bounds.start_bound(), range_bounds.end_bound()) {
|
||||
match (range.start(), range.end()) {
|
||||
(Bound::Included(start), Bound::Included(end)) => start <= end,
|
||||
(Bound::Included(start), Bound::Excluded(end)) => start < end,
|
||||
(Bound::Excluded(start), Bound::Included(end)) => start < end,
|
||||
@ -212,8 +196,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[tested]
|
||||
pub fn overlaps<I, A, B>(a: A, b: B) -> bool
|
||||
pub(crate) fn overlaps<I, A, B>(a: A, b: B) -> bool
|
||||
where
|
||||
A: NiceRange<I>,
|
||||
B: NiceRange<I>,
|
||||
@ -222,11 +205,10 @@ where
|
||||
!matches!(sorted_config(a, b), SortedConfig::NonOverlapping(_, _))
|
||||
}
|
||||
|
||||
#[tested]
|
||||
pub(crate) fn touches<I, A, B>(a: &A, b: &B) -> bool
|
||||
pub(crate) fn touches<I, A, B>(a: A, b: B) -> bool
|
||||
where
|
||||
A: RangeBounds<I>,
|
||||
B: RangeBounds<I>,
|
||||
A: NiceRange<I>,
|
||||
B: NiceRange<I>,
|
||||
I: Ord,
|
||||
{
|
||||
match sorted_config(a, b) {
|
||||
@ -243,7 +225,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[trivial]
|
||||
pub(crate) fn flip_bound<I>(bound: Bound<I>) -> Bound<I> {
|
||||
match bound {
|
||||
Bound::Included(point) => Bound::Excluded(point),
|
||||
@ -251,54 +232,3 @@ pub(crate) fn flip_bound<I>(bound: Bound<I>) -> Bound<I> {
|
||||
Bound::Unbounded => Bound::Unbounded,
|
||||
}
|
||||
}
|
||||
|
||||
//assumes the bound overlaps the range_bounds
|
||||
pub(crate) fn split_off_right_section<I, K>(
|
||||
range_bounds: K,
|
||||
with_bound: Bound<I>,
|
||||
) -> (Option<Result<K, TryFromBoundsError>>, (Bound<I>, Bound<I>))
|
||||
where
|
||||
I: Ord,
|
||||
K: NiceRange<I> + TryFromBounds<I>,
|
||||
{
|
||||
let (start_bound, end_bound) =
|
||||
(range_bounds.start_bound(), range_bounds.end_bound());
|
||||
|
||||
let keeping_section = (with_bound, end_bound.cloned());
|
||||
|
||||
let returning_section = (start_bound.cloned(), flip_bound(with_bound));
|
||||
|
||||
if is_valid_range_bounds(&returning_section) {
|
||||
return (
|
||||
Some(K::try_from_bounds(returning_section.0, returning_section.1)),
|
||||
keeping_section,
|
||||
);
|
||||
} else {
|
||||
return (None, keeping_section);
|
||||
}
|
||||
}
|
||||
//assumes the bound overlaps the range_bounds
|
||||
pub(crate) fn split_off_left_section<I, K>(
|
||||
with_bound: Bound<I>,
|
||||
range_bounds: &K,
|
||||
) -> (Option<Result<K, TryFromBoundsError>>, (Bound<I>, Bound<I>))
|
||||
where
|
||||
I: Clone + Ord,
|
||||
K: RangeBounds<I> + TryFromBounds<I>,
|
||||
{
|
||||
let (start_bound, end_bound) =
|
||||
(range_bounds.start_bound(), range_bounds.end_bound());
|
||||
|
||||
let keeping_section = (start_bound.cloned(), with_bound);
|
||||
|
||||
let returning_section = (flip_bound(with_bound), end_bound.cloned());
|
||||
|
||||
if is_valid_range_bounds(&returning_section) {
|
||||
return (
|
||||
Some(K::try_from_bounds(returning_section.0, returning_section.1)),
|
||||
keeping_section,
|
||||
);
|
||||
} else {
|
||||
return (None, keeping_section);
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ use std::ops::{Bound, RangeBounds};
|
||||
use btree_monstousity::btree_map::SearchBoundCustom;
|
||||
use btree_monstousity::BTreeMap;
|
||||
use itertools::Itertools;
|
||||
use labels::{parent_tested, tested, trivial};
|
||||
use serde::de::{MapAccess, Visitor};
|
||||
use serde::ser::SerializeMap;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
@ -290,7 +289,6 @@ where
|
||||
/// let map: RangeBoundsMap<u8, Range<u8>, bool> =
|
||||
/// RangeBoundsMap::new();
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn new() -> Self {
|
||||
RangeBoundsMap {
|
||||
inner: BTreeMap::new(),
|
||||
@ -310,7 +308,6 @@ where
|
||||
/// map.insert_strict(0..1, false).unwrap();
|
||||
/// assert_eq!(map.len(), 1);
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn len(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
@ -328,7 +325,6 @@ where
|
||||
/// map.insert_strict(0..1, false).unwrap();
|
||||
/// assert_eq!(map.is_empty(), false);
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.inner.is_empty()
|
||||
}
|
||||
@ -357,7 +353,6 @@ where
|
||||
/// assert_eq!(map.overlaps(&(4..=5)), true);
|
||||
/// assert_eq!(map.overlaps(&(4..6)), true);
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn overlaps<Q>(&self, range_bounds: Q) -> bool
|
||||
where
|
||||
Q: RangeBounds<I>,
|
||||
@ -431,7 +426,6 @@ where
|
||||
/// assert_eq!(map.get_at_point(&4), Some(&true));
|
||||
/// assert_eq!(map.get_at_point(&101), None);
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn get_at_point(&self, point: I) -> Option<&V> {
|
||||
self.get_entry_at_point(point).map(|(key, value)| value)
|
||||
}
|
||||
@ -454,7 +448,6 @@ where
|
||||
/// assert_eq!(map.contains_point(&4), true);
|
||||
/// assert_eq!(map.contains_point(&101), false);
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn contains_point(&self, point: &I) -> bool {
|
||||
self.get_entry_at_point(point).is_some()
|
||||
}
|
||||
@ -498,7 +491,6 @@ where
|
||||
/// assert_eq!(map.get_entry_at_point(&4), Some((&(4..8), &true)));
|
||||
/// assert_eq!(map.get_entry_at_point(&101), None);
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn get_entry_at_point(&self, point: I) -> Option<(&K, &V)> {
|
||||
self.inner.get_key_value(comp_start(Bound::Included(point)))
|
||||
}
|
||||
@ -524,7 +516,6 @@ where
|
||||
/// assert_eq!(iter.next(), Some((&(8..100), &false)));
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (&K, &V)> {
|
||||
self.inner.iter()
|
||||
}
|
||||
@ -923,7 +914,6 @@ where
|
||||
/// assert_eq!(map.contains_range_bounds(&(2..6)), false);
|
||||
/// assert_eq!(map.contains_range_bounds(&(6..50)), true);
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn contains_range_bounds<Q>(&self, range_bounds: Q) -> bool
|
||||
where
|
||||
Q: RangeBounds<I> + Clone,
|
||||
@ -1191,7 +1181,6 @@ where
|
||||
/// [(&(2..4), &false), (&(4..6), &true), (&(6..8), &false)]
|
||||
/// );
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn insert_overwrite(
|
||||
&mut self,
|
||||
range_bounds: K,
|
||||
@ -1216,7 +1205,6 @@ where
|
||||
///
|
||||
/// assert_eq!(map.first_entry(), Some((&(1..4), &false)));
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn first_entry(&self) -> Option<(&K, &V)> {
|
||||
todo!()
|
||||
}
|
||||
@ -1239,7 +1227,6 @@ where
|
||||
/// map.last_entry(),
|
||||
/// Some((&(8..100), &false))
|
||||
/// );
|
||||
#[trivial]
|
||||
pub fn last_entry(&self) -> Option<(&K, &V)> {
|
||||
todo!()
|
||||
}
|
||||
@ -1287,7 +1274,6 @@ where
|
||||
/// [(6..8, true), (10..16, true)],
|
||||
/// );
|
||||
/// ```
|
||||
#[trivial]
|
||||
pub fn split_off(
|
||||
&mut self,
|
||||
start_bound: Bound<I>,
|
||||
|
Loading…
x
Reference in New Issue
Block a user