simplify Bounds
This commit is contained in:
parent
f9727d3117
commit
d481ec539e
@ -28,6 +28,30 @@ impl<A: Clone> Bounds<A> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn orderedl(
|
||||||
|
l: &Option<A>,
|
||||||
|
r: &A,
|
||||||
|
comparator: &impl Comparator<A>,
|
||||||
|
) -> Result<(), BoundsError<A>> {
|
||||||
|
if let Some(l) = &l {
|
||||||
|
Self::ordered(l, r, comparator)
|
||||||
|
} else {
|
||||||
|
Err(BoundsError::OverflowL(r.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn orderedr(
|
||||||
|
l: &A,
|
||||||
|
r: &Option<A>,
|
||||||
|
comparator: &impl Comparator<A>,
|
||||||
|
) -> Result<(), BoundsError<A>> {
|
||||||
|
if let Some(r) = &r {
|
||||||
|
Self::ordered(l, r, comparator)
|
||||||
|
} else {
|
||||||
|
Err(BoundsError::OverflowR(l.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn new(
|
fn new(
|
||||||
l: Option<A>,
|
l: Option<A>,
|
||||||
r: Option<A>,
|
r: Option<A>,
|
||||||
@ -56,23 +80,15 @@ impl<A: Clone> Bounds<A> {
|
|||||||
key: &A,
|
key: &A,
|
||||||
comparator: &impl Comparator<A>,
|
comparator: &impl Comparator<A>,
|
||||||
) -> Result<Self, BoundsError<A>> {
|
) -> Result<Self, BoundsError<A>> {
|
||||||
if let Some(lr) = &l.r {
|
Self::orderedl(&l.r, key, comparator)?;
|
||||||
Self::ordered(lr, key, comparator)?
|
Self::orderedr(key, &r.l, comparator)?;
|
||||||
} else {
|
|
||||||
Err(BoundsError::OverflowL(key.clone()))?
|
|
||||||
}
|
|
||||||
if let Some(rl) = &r.l {
|
|
||||||
Self::ordered(key, rl, comparator)?
|
|
||||||
} else {
|
|
||||||
Err(BoundsError::OverflowR(key.clone()))?
|
|
||||||
}
|
|
||||||
Self::new(l.l, r.r, comparator)
|
Self::new(l.l, r.r, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn equal_bound(l: &Option<A>, r: &Option<A>, comparator: &impl Comparator<A>) -> bool {
|
fn equal_bound(l: &Option<A>, r: &Option<A>, comparator: &impl Comparator<A>) -> bool {
|
||||||
match (l, r) {
|
match (l, r) {
|
||||||
(None, None) => true,
|
(None, None) => true,
|
||||||
(Some(kl), Some(kr)) => matches!(comparator.compare(kl, kr), Comparison::E),
|
(Some(kl), Some(kr)) => comparator.equal(kl, kr),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,10 @@ pub enum Comparison {
|
|||||||
/// ```
|
/// ```
|
||||||
pub trait Comparator<A> {
|
pub trait Comparator<A> {
|
||||||
fn compare(&self, kl: &A, kr: &A) -> Comparison;
|
fn compare(&self, kl: &A, kr: &A) -> Comparison;
|
||||||
|
|
||||||
|
fn equal(&self, kl: &A, kr: &A) -> bool {
|
||||||
|
matches!(self.compare(kl, kr), Comparison::E)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Implementation of a [Comparator] relying on [`Ord`].
|
/// Implementation of a [Comparator] relying on [`Ord`].
|
||||||
|
Loading…
Reference in New Issue
Block a user