more improvements: No More Cloning!!!

This commit is contained in:
ripytide
2023-04-01 18:26:19 +01:00
parent ac28f2ed94
commit ff1a8079f9
4 changed files with 35 additions and 19 deletions
+12
View File
@@ -67,6 +67,18 @@ impl<T> BoundOrd<T> {
Bound::Unbounded => BoundOrd::EndUnbounded,
}
}
#[trivial]
pub fn as_ref(&self) -> BoundOrd<&T> {
//I can't believe this is neccessary but apparently so
match self {
BoundOrd::Included(x) => BoundOrd::Included(x),
BoundOrd::StartExcluded(x) => BoundOrd::StartExcluded(x),
BoundOrd::StartUnbounded => BoundOrd::StartUnbounded,
BoundOrd::EndExcluded(x) => BoundOrd::EndExcluded(x),
BoundOrd::EndUnbounded => BoundOrd::EndUnbounded,
}
}
}
impl<T> Ord for BoundOrd<T>
@@ -23,16 +23,13 @@ use std::ops::RangeBounds;
use crate::bound_ord::BoundOrd;
pub enum CustomRangeBoundsOrdWrapper<I, K> {
//non real
BoundOrd(BoundOrd<I>),
//real
RangeBounds(K),
}
impl<I, K> Ord for CustomRangeBoundsOrdWrapper<I, K>
where
I: Ord + Clone,
I: Ord,
K: RangeBounds<I>,
{
fn cmp(&self, other: &Self) -> Ordering {
@@ -40,15 +37,21 @@ where
(
CustomRangeBoundsOrdWrapper::RangeBounds(range_bounds),
CustomRangeBoundsOrdWrapper::BoundOrd(bound_ord),
) => cmp_range_bounds_with_bound_ord(range_bounds, bound_ord),
) => cmp_range_bounds_with_bound_ord(
range_bounds,
bound_ord.as_ref(),
),
(
CustomRangeBoundsOrdWrapper::BoundOrd(bound_ord),
CustomRangeBoundsOrdWrapper::RangeBounds(range_bounds),
) => cmp_range_bounds_with_bound_ord(range_bounds, bound_ord)
.reverse(),
) => cmp_range_bounds_with_bound_ord(
range_bounds,
bound_ord.as_ref(),
)
.reverse(),
_ => {
panic!(
"You cannot compare a Non-Real CustomOrdWrapper with another Non-Real CustomOrdWrapper!"
"Must have ONE of each real RangeBounds and non-real BoundOrd!"
);
}
}
@@ -57,18 +60,20 @@ where
fn cmp_range_bounds_with_bound_ord<I, K>(
range_bounds: &K,
bound_ord: &BoundOrd<I>,
bound_ord: BoundOrd<&I>,
) -> Ordering
where
I: Ord + Clone,
I: Ord,
K: RangeBounds<I>,
{
let start_bound_ord = BoundOrd::start(range_bounds.start_bound().cloned());
let end_bound_ord = BoundOrd::end(range_bounds.end_bound().cloned());
//optimisation remove cloning here and all trait bounds that are
//reliant on this
let start_bound_ord = BoundOrd::start(range_bounds.start_bound());
let end_bound_ord = BoundOrd::end(range_bounds.end_bound());
if bound_ord < &start_bound_ord {
if bound_ord < start_bound_ord {
Ordering::Greater
} else if bound_ord > &end_bound_ord {
} else if bound_ord > end_bound_ord {
Ordering::Less
} else {
Ordering::Equal
@@ -77,7 +82,7 @@ where
impl<I, K> PartialOrd for CustomRangeBoundsOrdWrapper<I, K>
where
I: Ord + Clone,
I: Ord,
K: RangeBounds<I>,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
@@ -87,14 +92,14 @@ where
impl<I, K> Eq for CustomRangeBoundsOrdWrapper<I, K>
where
I: Ord + Clone,
I: Ord,
K: RangeBounds<I>,
{
}
impl<I, K> PartialEq for CustomRangeBoundsOrdWrapper<I, K>
where
I: Ord + Clone,
I: Ord,
K: RangeBounds<I>,
{
fn eq(&self, other: &Self) -> bool {
+1 -1
View File
@@ -226,7 +226,7 @@ along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
#![allow(clippy::tabs_in_doc_comments)]
#![allow(clippy::needless_return)]
pub(crate) mod bound_ord;
pub(crate) mod custom_ord_wrapper;
pub(crate) mod custom_range_bounds_ord_wrapper;
pub mod range_bounds_map;
pub mod range_bounds_set;
pub mod try_from_bounds;
-1
View File
@@ -33,7 +33,6 @@ use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::bound_ord::BoundOrd;
use crate::custom_ord_wrapper::CustomOrdWrapper;
use crate::TryFromBounds;
/// An ordered map of non-overlapping [`RangeBounds`] based on [`BTreeMap`].