From c3850983decaf64816838da8d212922c8ad9bce6 Mon Sep 17 00:00:00 2001 From: ripytide Date: Sat, 1 Apr 2023 15:55:34 +0100 Subject: [PATCH] more refactoring of things and reverting the startwrapper AGAIN lol. --- src/bound_ord.rs | 75 ++++++++++++++------------- src/lib.rs | 1 - src/range_bounds_map.rs | 23 ++++---- src/range_bounds_set.rs | 4 +- src/start_range_bounds_ord_wrapper.rs | 65 ----------------------- 5 files changed, 52 insertions(+), 116 deletions(-) delete mode 100644 src/start_range_bounds_ord_wrapper.rs diff --git a/src/bound_ord.rs b/src/bound_ord.rs index a284cf0..91a035a 100644 --- a/src/bound_ord.rs +++ b/src/bound_ord.rs @@ -75,42 +75,49 @@ where { #[rustfmt::skip] #[tested] - fn cmp(&self, other: &Self) ->Ordering { + fn cmp(&self, other: &Self) -> Ordering { match (self, other) { - (BoundOrd::Included(start1), BoundOrd::Included(start2)) => start1.partial_cmp(start2), - (BoundOrd::Included(start1), BoundOrd::StartExcluded(start2)) => partial_cmp_with_priority(start1, start2, true), - (BoundOrd::Included(start1), BoundOrd::EndExcluded(start2)) => partial_cmp_with_priority(start1, start2, false), - (BoundOrd::Included(_), BoundOrd::EndUnbounded) => Some(Ordering::Less), - (BoundOrd::Included(_), BoundOrd::StartUnbounded) => Some(Ordering::Greater), + (BoundOrd::Included(start1), BoundOrd::Included(start2)) => start1.cmp(start2), + (BoundOrd::Included(start1), BoundOrd::StartExcluded(start2)) => cmp_with_priority(start1, start2, true), + (BoundOrd::Included(start1), BoundOrd::EndExcluded(start2)) => cmp_with_priority(start1, start2, false), + (BoundOrd::Included(_), BoundOrd::EndUnbounded) => Ordering::Less, + (BoundOrd::Included(_), BoundOrd::StartUnbounded) => Ordering::Greater, - (BoundOrd::StartExcluded(start1), BoundOrd::StartExcluded(start2)) => start1.partial_cmp(start2), - (BoundOrd::StartExcluded(start1), BoundOrd::Included(start2)) => partial_cmp_with_priority(start1, start2, false), - (BoundOrd::StartExcluded(start1), BoundOrd::EndExcluded(start2)) => partial_cmp_with_priority(start1, start2, false), - (BoundOrd::StartExcluded(_), BoundOrd::StartUnbounded) => Some(Ordering::Greater), - (BoundOrd::StartExcluded(_), BoundOrd::EndUnbounded) => Some(Ordering::Less), + (BoundOrd::StartExcluded(start1), BoundOrd::StartExcluded(start2)) => start1.cmp(start2), + (BoundOrd::StartExcluded(start1), BoundOrd::Included(start2)) => cmp_with_priority(start1, start2, false), + (BoundOrd::StartExcluded(start1), BoundOrd::EndExcluded(start2)) => cmp_with_priority(start1, start2, false), + (BoundOrd::StartExcluded(_), BoundOrd::StartUnbounded) => Ordering::Greater, + (BoundOrd::StartExcluded(_), BoundOrd::EndUnbounded) => Ordering::Less, - (BoundOrd::StartUnbounded, BoundOrd::Included(_)) => Some(Ordering::Less), - (BoundOrd::StartUnbounded, BoundOrd::StartExcluded(_)) => Some(Ordering::Less), - (BoundOrd::StartUnbounded, BoundOrd::EndExcluded(_)) => Some(Ordering::Less), - (BoundOrd::StartUnbounded, BoundOrd::StartUnbounded) => Some(Ordering::Equal), - (BoundOrd::StartUnbounded, BoundOrd::EndUnbounded) => Some(Ordering::Less), + (BoundOrd::StartUnbounded, BoundOrd::Included(_)) => Ordering::Less, + (BoundOrd::StartUnbounded, BoundOrd::StartExcluded(_)) => Ordering::Less, + (BoundOrd::StartUnbounded, BoundOrd::EndExcluded(_)) => Ordering::Less, + (BoundOrd::StartUnbounded, BoundOrd::StartUnbounded) => Ordering::Equal, + (BoundOrd::StartUnbounded, BoundOrd::EndUnbounded) => Ordering::Less, - (BoundOrd::EndExcluded(start1), BoundOrd::EndExcluded(start2)) => start1.partial_cmp(start2), - (BoundOrd::EndExcluded(start1), BoundOrd::Included(start2)) => partial_cmp_with_priority(start1, start2, true), - (BoundOrd::EndExcluded(start1), BoundOrd::StartExcluded(start2)) => partial_cmp_with_priority(start1, start2, true), - (BoundOrd::EndExcluded(_), BoundOrd::StartUnbounded) => Some(Ordering::Greater), - (BoundOrd::EndExcluded(_), BoundOrd::EndUnbounded) => Some(Ordering::Less), + (BoundOrd::EndExcluded(start1), BoundOrd::EndExcluded(start2)) => start1.cmp(start2), + (BoundOrd::EndExcluded(start1), BoundOrd::Included(start2)) => cmp_with_priority(start1, start2, true), + (BoundOrd::EndExcluded(start1), BoundOrd::StartExcluded(start2)) => cmp_with_priority(start1, start2, true), + (BoundOrd::EndExcluded(_), BoundOrd::StartUnbounded) => Ordering::Greater, + (BoundOrd::EndExcluded(_), BoundOrd::EndUnbounded) => Ordering::Less, - (BoundOrd::EndUnbounded, BoundOrd::Included(_)) => Some(Ordering::Greater), - (BoundOrd::EndUnbounded, BoundOrd::StartExcluded(_)) => Some(Ordering::Greater), - (BoundOrd::EndUnbounded, BoundOrd::EndExcluded(_)) => Some(Ordering::Greater), - (BoundOrd::EndUnbounded, BoundOrd::EndUnbounded) => Some(Ordering::Equal), - (BoundOrd::EndUnbounded, BoundOrd::StartUnbounded) => Some(Ordering::Greater), + (BoundOrd::EndUnbounded, BoundOrd::Included(_)) => Ordering::Greater, + (BoundOrd::EndUnbounded, BoundOrd::StartExcluded(_)) => Ordering::Greater, + (BoundOrd::EndUnbounded, BoundOrd::EndExcluded(_)) => Ordering::Greater, + (BoundOrd::EndUnbounded, BoundOrd::EndUnbounded) => Ordering::Equal, + (BoundOrd::EndUnbounded, BoundOrd::StartUnbounded) => Ordering::Greater, } } } -impl PartialOrd for BoundOrd where T: Ord {} +impl PartialOrd for BoundOrd +where + T: Ord, +{ + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} impl PartialEq for BoundOrd where @@ -126,23 +133,19 @@ impl Eq for BoundOrd where T: Ord {} /// If they are equal say the item with priority is larger /// where false means left has priority and true means right. #[parent_tested] -fn partial_cmp_with_priority( - left: &T, - right: &T, - priority: bool, -) -> Option +fn cmp_with_priority(left: &T, right: &T, priority: bool) -> Ordering where - T: PartialOrd, + T: Ord, { - let result = left.partial_cmp(right)?; + let result = left.cmp(right); - Some(match result { + match result { Ordering::Equal => match priority { false => Ordering::Greater, true => Ordering::Less, }, x => x, - }) + } } impl From> for Bound { diff --git a/src/lib.rs b/src/lib.rs index bfb10af..507e14a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -228,7 +228,6 @@ along with range_bounds_map. If not, see . pub(crate) mod bound_ord; pub mod range_bounds_map; pub mod range_bounds_set; -pub(crate) mod start_range_bounds_ord_wrapper; pub mod try_from_bounds; pub use crate::range_bounds_map::{ diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index 8458df2..3e99291 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -32,7 +32,6 @@ use serde::ser::SerializeMap; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::bound_ord::BoundOrd; -use crate::start_range_bounds_ord_wrapper::StartRangeBoundsOrdWrapper; use crate::TryFromBounds; /// An ordered map of non-overlapping [`RangeBounds`] based on [`BTreeMap`]. @@ -127,12 +126,12 @@ use crate::TryFromBounds; /// /// [`RangeBounds`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html /// [`BTreeMap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct RangeBoundsMap where - I: PartialOrd, + I: Ord, { - inner: BTreeMap, V>, + starts: BTreeMap, (K, V)>, } /// An error type to represent a [`RangeBounds`] overlapping another @@ -2126,7 +2125,7 @@ pub struct Overlapping<'a, I, K, V, Q> { } impl<'a, I, K, V, Q> Iterator for Overlapping<'a, I, K, V, Q> where - I: PartialOrd, + I: Ord, K: RangeBounds, Q: RangeBounds, { @@ -2178,7 +2177,7 @@ impl Iterator for IntoIter { impl Default for RangeBoundsMap where - I: PartialOrd, + I: Ord, { #[trivial] fn default() -> Self { @@ -2275,7 +2274,7 @@ fn config<'a, I, A, B>(a: &'a A, b: &'a B) -> Config where A: RangeBounds, B: RangeBounds, - I: PartialOrd, + I: Ord, { let (a_start, a_end) = expand(a); let (b_start, b_end) = expand(b); @@ -2319,7 +2318,7 @@ fn sorted_config<'a, I, A, B>(a: &'a A, b: &'a B) -> SortedConfig<&'a I> where A: RangeBounds, B: RangeBounds, - I: PartialOrd, + I: Ord, { let ae = expand(a); let be = expand(b); @@ -2338,7 +2337,7 @@ where fn contains_bound_ord(range_bounds: &A, bound_ord: BoundOrd<&I>) -> bool where A: RangeBounds, - I: PartialOrd, + I: Ord, { let start_bound_ord = BoundOrd::start(range_bounds.start_bound()); let end_bound_ord = BoundOrd::end(range_bounds.end_bound()); @@ -2361,7 +2360,7 @@ fn cut_range_bounds<'a, I, B, C>( where B: RangeBounds, C: RangeBounds, - I: PartialOrd + Clone, + I: Ord + Clone, { let base_all @ (base_start, base_end) = ( base_range_bounds.start_bound(), @@ -2442,7 +2441,7 @@ fn overlaps(a: &A, b: &B) -> bool where A: RangeBounds, B: RangeBounds, - I: PartialOrd, + I: Ord, { !matches!(sorted_config(a, b), SortedConfig::NonOverlapping(_, _)) } @@ -2452,7 +2451,7 @@ fn touches(a: &A, b: &B) -> bool where A: RangeBounds, B: RangeBounds, - I: PartialOrd, + I: Ord, { match sorted_config(a, b) { SortedConfig::NonOverlapping(a, b) => match (a.1, b.0) { diff --git a/src/range_bounds_set.rs b/src/range_bounds_set.rs index 972667e..22e305e 100644 --- a/src/range_bounds_set.rs +++ b/src/range_bounds_set.rs @@ -112,7 +112,7 @@ use crate::{ #[derive(Debug, Clone)] pub struct RangeBoundsSet where - I: PartialOrd, + I: Ord, { map: RangeBoundsMap, } @@ -1411,7 +1411,7 @@ impl Iterator for IntoIter { impl Default for RangeBoundsSet where - I: PartialOrd, + I: Ord, { #[trivial] fn default() -> Self { diff --git a/src/start_range_bounds_ord_wrapper.rs b/src/start_range_bounds_ord_wrapper.rs deleted file mode 100644 index 77aaf61..0000000 --- a/src/start_range_bounds_ord_wrapper.rs +++ /dev/null @@ -1,65 +0,0 @@ -/* -Copyright 2022 James Forster - -This file is part of range_bounds_map. - -range_bounds_map is free software: you can redistribute it and/or -modify it under the terms of the GNU Affero General Public License as -published by the Free Software Foundation, either version 3 of the -License, or (at your option) any later version. - -range_bounds_map is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with range_bounds_map. If not, see . -*/ - -use std::cmp::Ordering; -use std::marker::PhantomData; -use std::ops::RangeBounds; - -use crate::bound_ord::BoundOrd; - -#[derive(Debug, Clone)] -pub struct StartRangeBoundsOrdWrapper { - phantom: PhantomData, - inner: K, -} - -impl Ord for StartRangeBoundsOrdWrapper -where - I: Ord, - K: RangeBounds, -{ - fn cmp(&self, other: &Self) -> Ordering { - BoundOrd::start(self.inner.start_bound()) - .cmp(&BoundOrd::start(other.inner.start_bound())) - } -} -impl PartialOrd for StartRangeBoundsOrdWrapper -where - I: Ord, - K: RangeBounds, -{ - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(&other)) - } -} -impl PartialEq for StartRangeBoundsOrdWrapper -where - I: Ord, - K: RangeBounds, -{ - fn eq(&self, other: &Self) -> bool { - self.cmp(&other).is_eq() - } -} -impl Eq for StartRangeBoundsOrdWrapper -where - I: Ord, - K: RangeBounds, -{ -}