more refactoring of things and reverting the startwrapper AGAIN lol.

This commit is contained in:
ripytide
2023-04-01 15:55:34 +01:00
parent 8d2609baf9
commit c3850983de
5 changed files with 52 additions and 116 deletions
+39 -36
View File
@@ -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<T> PartialOrd for BoundOrd<T> where T: Ord {}
impl<T> PartialOrd for BoundOrd<T>
where
T: Ord,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T> PartialEq for BoundOrd<T>
where
@@ -126,23 +133,19 @@ impl<T> Eq for BoundOrd<T> 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<T>(
left: &T,
right: &T,
priority: bool,
) -> Option<Ordering>
fn cmp_with_priority<T>(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<T> From<BoundOrd<T>> for Bound<T> {
-1
View File
@@ -228,7 +228,6 @@ along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
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::{
+11 -12
View File
@@ -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<I, K, V>
where
I: PartialOrd,
I: Ord,
{
inner: BTreeMap<StartRangeBoundsOrdWrapper<I, K>, V>,
starts: BTreeMap<BoundOrd<I>, (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<I>,
Q: RangeBounds<I>,
{
@@ -2178,7 +2177,7 @@ impl<I, K, V> Iterator for IntoIter<I, K, V> {
impl<I, K, V> Default for RangeBoundsMap<I, K, V>
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<I>,
B: RangeBounds<I>,
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<I>,
B: RangeBounds<I>,
I: PartialOrd,
I: Ord,
{
let ae = expand(a);
let be = expand(b);
@@ -2338,7 +2337,7 @@ where
fn contains_bound_ord<I, A>(range_bounds: &A, bound_ord: BoundOrd<&I>) -> bool
where
A: RangeBounds<I>,
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<I>,
C: RangeBounds<I>,
I: PartialOrd + Clone,
I: Ord + Clone,
{
let base_all @ (base_start, base_end) = (
base_range_bounds.start_bound(),
@@ -2442,7 +2441,7 @@ fn overlaps<I, A, B>(a: &A, b: &B) -> bool
where
A: RangeBounds<I>,
B: RangeBounds<I>,
I: PartialOrd,
I: Ord,
{
!matches!(sorted_config(a, b), SortedConfig::NonOverlapping(_, _))
}
@@ -2452,7 +2451,7 @@ fn touches<I, A, B>(a: &A, b: &B) -> bool
where
A: RangeBounds<I>,
B: RangeBounds<I>,
I: PartialOrd,
I: Ord,
{
match sorted_config(a, b) {
SortedConfig::NonOverlapping(a, b) => match (a.1, b.0) {
+2 -2
View File
@@ -112,7 +112,7 @@ use crate::{
#[derive(Debug, Clone)]
pub struct RangeBoundsSet<I, K>
where
I: PartialOrd,
I: Ord,
{
map: RangeBoundsMap<I, K, ()>,
}
@@ -1411,7 +1411,7 @@ impl<I, K> Iterator for IntoIter<I, K> {
impl<I, K> Default for RangeBoundsSet<I, K>
where
I: PartialOrd,
I: Ord,
{
#[trivial]
fn default() -> Self {
-65
View File
@@ -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 <https://www.gnu.org/licenses/>.
*/
use std::cmp::Ordering;
use std::marker::PhantomData;
use std::ops::RangeBounds;
use crate::bound_ord::BoundOrd;
#[derive(Debug, Clone)]
pub struct StartRangeBoundsOrdWrapper<I, K> {
phantom: PhantomData<I>,
inner: K,
}
impl<I, K> Ord for StartRangeBoundsOrdWrapper<I, K>
where
I: Ord,
K: RangeBounds<I>,
{
fn cmp(&self, other: &Self) -> Ordering {
BoundOrd::start(self.inner.start_bound())
.cmp(&BoundOrd::start(other.inner.start_bound()))
}
}
impl<I, K> PartialOrd for StartRangeBoundsOrdWrapper<I, K>
where
I: Ord,
K: RangeBounds<I>,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(&other))
}
}
impl<I, K> PartialEq for StartRangeBoundsOrdWrapper<I, K>
where
I: Ord,
K: RangeBounds<I>,
{
fn eq(&self, other: &Self) -> bool {
self.cmp(&other).is_eq()
}
}
impl<I, K> Eq for StartRangeBoundsOrdWrapper<I, K>
where
I: Ord,
K: RangeBounds<I>,
{
}