balanced_bound
All checks were successful
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.65) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.

This commit is contained in:
AF 2023-10-15 13:37:36 +00:00
parent 89af9d1084
commit 87451d6cb6
4 changed files with 65 additions and 32 deletions

View File

@ -6,6 +6,7 @@ pub struct Bounds<A> {
r: Option<A>,
}
#[derive(Debug)]
pub enum BoundsError<A> {
BoundsViolated { l: A, r: A },
OverflowL(A),

View File

@ -7,7 +7,7 @@ use crate::atomic::*;
use super::*;
/// Generic implementation of a [Mentionable] for [Atomic]s.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct AtomicObject<A> {
atomic: A,
}

View File

@ -234,15 +234,13 @@ impl<'a, Ctx: Context<'a>, A: MentionableBase<'a> + Clone> Clone for Tree<'a, Ct
#[cfg(test)]
mod tests {
use std::convert::Infallible;
use crate::{
flow::{
binary::{balancing::*, *},
binary::{balancing::*, bound::BoundTrees, *},
comparator::*,
},
rstd::atomic_object::*,
testing::TestContextPlain,
testing::*,
};
use super::{context::*, *};
@ -252,7 +250,7 @@ mod tests {
TestContextPlain,
AtomicObject<u64>,
DefaultComparator,
WrappedExtra<BalancingError, Infallible>,
BbError<AtomicObject<u64>>,
>;
fn new_trees() -> Trees {
@ -320,4 +318,20 @@ mod tests {
let reference2 = trees.refer(&singular_keyed(&trees, 2)).unwrap();
assert!(!trees.equal(&reference1, &reference2));
}
#[test]
fn balanced_bound() {
let trees = BoundTrees::new(BalancedTrees::new(new_trees()));
let mut tree = trees.empty();
for i in [
8, 3, 10, 17, 0, 13, 6, 1, 11, 5, 4, 7, 18, 14, 15, 9, 2, 19, 16, 12,
] {
tree = trees.clone().add_tree(tree, i.into()).unwrap();
}
for i in [
2, 9, 4, 7, 8, 10, 17, 1, 13, 15, 18, 12, 5, 0, 3, 6, 16, 19, 14, 11,
] {
tree = trees.clone().remove(tree, i.into()).unwrap();
}
}
}

View File

@ -212,37 +212,37 @@ impl<
}
}
#[derive(Debug)]
pub enum WrappedExtra<W, E> {
Wrapped(W),
Extra(E),
}
// #[derive(Debug)]
// pub enum WrappedExtra<W, E> {
// Wrapped(W),
// Extra(E),
// }
impl<R, E, W> From<WrappedExtra<W, E>> for TreeContextError<R, WrappedExtra<W, E>> {
fn from(value: WrappedExtra<W, E>) -> Self {
Self::Extra(value)
}
}
// impl<R, E, W> From<WrappedExtra<W, E>> for TreeContextError<R, WrappedExtra<W, E>> {
// fn from(value: WrappedExtra<W, E>) -> Self {
// Self::Extra(value)
// }
// }
impl<R, E> From<BalancingError> for TreeContextError<R, WrappedExtra<BalancingError, E>> {
fn from(value: BalancingError) -> Self {
WrappedExtra::Wrapped(value).into()
}
}
// impl<R, E> From<BalancingError> for TreeContextError<R, WrappedExtra<BalancingError, E>> {
// fn from(value: BalancingError) -> Self {
// WrappedExtra::Wrapped(value).into()
// }
// }
impl<R, A, E> From<BoundsError<A>> for TreeContextError<R, WrappedExtra<BoundsError<A>, E>> {
fn from(value: BoundsError<A>) -> Self {
WrappedExtra::Wrapped(value).into()
}
}
// impl<R, A, E> From<BoundsError<A>> for TreeContextError<R, WrappedExtra<BoundsError<A>, E>> {
// fn from(value: BoundsError<A>) -> Self {
// WrappedExtra::Wrapped(value).into()
// }
// }
impl<
'a,
Ctx: Context<'a>,
A: Mentionable<'a, Ctx> + Clone,
C: 'a + Comparator<A>,
E: 'a + Send,
> BinaryTreesUnbalanced<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra<BalancingError, E>>
E: 'a + Send + From<BalancingError>,
> BinaryTreesUnbalanced<'a> for TreeContext2<'a, Ctx, A, C, E>
{
fn tree_of_with_height(&self, node: Self::Node, height: u64) -> BTWrap<'a, Self, Self::Tree> {
let node = Nullable::from(node);
@ -250,7 +250,7 @@ impl<
}
fn balancing_error<T: 'a + Send>(&self, error: BalancingError) -> BTWrap<'a, Self, T> {
Self::fail(error.into())
Self::fail(TreeContextError::Extra(error.into()))
}
fn node_heights(&self, node: &Self::Node) -> (u64, u64) {
@ -263,10 +263,28 @@ impl<
Ctx: Context<'a>,
A: Mentionable<'a, Ctx> + Clone,
C: 'a + Comparator<A>,
E: 'a + Send,
> BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra<BoundsError<A>, E>>
E: 'a + Send + From<BoundsError<A>>,
> BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, E>
{
fn bounds_error<T: 'a + Send>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T> {
Self::fail(error.into())
Self::fail(TreeContextError::Extra(error.into()))
}
}
#[derive(Debug)]
pub enum BbError<A> {
Balancing(BalancingError),
Bounds(BoundsError<A>),
}
impl<A> From<BalancingError> for BbError<A> {
fn from(value: BalancingError) -> Self {
Self::Balancing(value)
}
}
impl<A> From<BoundsError<A>> for BbError<A> {
fn from(value: BoundsError<A>) -> Self {
Self::Bounds(value)
}
}