added discrete_bounds and refactored some utils functions

This commit is contained in:
ripytide
2023-04-20 15:08:52 +01:00
parent e5739cedf7
commit f1a588eb9c
5 changed files with 20 additions and 34 deletions
-2
View File
@@ -232,11 +232,9 @@ pub mod discrete_bounds;
pub mod range_bounds_map;
pub mod range_bounds_set;
pub mod try_from_bounds;
pub use crate::range_bounds_map::{
OverlapError, OverlapOrTryFromBoundsError, RangeBoundsMap,
TryFromBoundsError,
};
pub use crate::range_bounds_set::RangeBoundsSet;
pub use crate::try_from_bounds::TryFromBounds;
+7 -7
View File
@@ -33,10 +33,10 @@ use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::bound_ord::DiscreteBoundOrd;
use crate::discrete_bounds::DiscreteBounds;
use crate::utils::{
cmp_range_with_bound_ord, cut_range, flip_bound, is_valid_range, overlaps,
cmp_range_with_discrete_bound_ord, cut_range, flip_bound, is_valid_range, overlaps,
};
use crate::TryFromBounds;
/// An ordered map of non-overlapping ranges based on [`BTreeMap`].
///
@@ -756,7 +756,7 @@ where
>
where
Q: NiceRange<I> + 'a,
K: TryFromBounds<I>,
K: TryFrom<DiscreteBounds<I>>,
V: Clone,
{
invalid_range_panic(range);
@@ -1679,7 +1679,7 @@ where
K: NiceRange<I>,
{
move |inner_range: &K| {
cmp_range_with_bound_ord(*inner_range, DiscreteBoundOrd::start(start))
cmp_range_with_discrete_bound_ord(*inner_range, DiscreteBoundOrd::start(start))
}
}
fn overlapping_end_comp<I, K>(end: Bound<I>) -> impl FnMut(&K) -> Ordering
@@ -1688,7 +1688,7 @@ where
K: NiceRange<I>,
{
move |inner_range: &K| {
cmp_range_with_bound_ord(*inner_range, DiscreteBoundOrd::end(end))
cmp_range_with_discrete_bound_ord(*inner_range, DiscreteBoundOrd::end(end))
}
}
fn touching_start_comp<I, K>(start: Bound<I>) -> impl FnMut(&K) -> Ordering
@@ -1751,8 +1751,8 @@ where
/// already implement RangeBounds and Copy on your type then this will
/// also be implemted.
pub trait NiceRange<I>: Copy {
fn start(&self) -> Bound<I>;
fn end(&self) -> Bound<I>;
fn start(&self) -> DiscreteBoundOrd<I>;
fn end(&self) -> DiscreteBoundOrd<I>;
}
impl<K, I> NiceRange<I> for K
where
+1 -1
View File
@@ -8,7 +8,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::range_bounds_map::{IntoIter as RangeBoundsMapIntoIter, NiceRange};
use crate::{
OverlapError, OverlapOrTryFromBoundsError, RangeBoundsMap, TryFromBounds,
OverlapError, OverlapOrTryFromBoundsError, RangeBoundsMap,
TryFromBoundsError,
};
+1 -18
View File
@@ -1,6 +1,6 @@
use std::ops::{Bound, RangeBounds};
use crate::{TryFromBounds, TryFromBoundsError};
use crate::{TryFromBoundsError};
pub type AnyRange = (Bound<i8>, Bound<i8>);
@@ -57,20 +57,3 @@ impl RangeBounds<i8> for InExRange {
Bound::Excluded(&self.end)
}
}
impl TryFromBounds<i8> for InExRange {
fn try_from_bounds(
start_bound: Bound<i8>,
end_bound: Bound<i8>,
) -> Result<Self, crate::TryFromBoundsError>
where
Self: Sized,
{
match (start_bound, end_bound) {
(Bound::Included(start), Bound::Excluded(end)) => {
Ok(InExRange { start, end })
}
_ => Err(TryFromBoundsError),
}
}
}
+11 -6
View File
@@ -23,17 +23,17 @@ use std::ops::Bound;
use crate::bound_ord::DiscreteBoundOrd;
use crate::range_bounds_map::NiceRange;
pub(crate) fn cmp_range_with_bound_ord<A, B>(
pub(crate) fn cmp_range_with_discrete_bound_ord<A, B>(
range: A,
bound_ord: DiscreteBoundOrd<B>,
discrete_bound_ord: DiscreteBoundOrd<B>,
) -> Ordering
where
A: NiceRange<B>,
B: Ord,
{
if bound_ord < DiscreteBoundOrd::start(range.start()) {
if discrete_bound_ord < range.start() {
Ordering::Less
} else if bound_ord > DiscreteBoundOrd::end(range.end()) {
} else if discrete_bound_ord > range.end() {
Ordering::Greater
} else {
Ordering::Equal
@@ -56,7 +56,9 @@ where
B: NiceRange<I>,
I: Ord,
{
match DiscreteBoundOrd::start(a.start()) < DiscreteBoundOrd::start(b.start()) {
match DiscreteBoundOrd::start(a.start())
< DiscreteBoundOrd::start(b.start())
{
true => {
match (
contains_bound_ord(a, DiscreteBoundOrd::start(b.start())),
@@ -110,7 +112,10 @@ where
}
}
pub(crate) fn contains_bound_ord<I, A>(range: A, bound_ord: DiscreteBoundOrd<I>) -> bool
pub(crate) fn contains_bound_ord<I, A>(
range: A,
bound_ord: DiscreteBoundOrd<I>,
) -> bool
where
A: NiceRange<I>,
I: Ord,