From 87451d6cb6216f24188b1d17f04aa52682924cab Mon Sep 17 00:00:00 2001 From: timofey Date: Sun, 15 Oct 2023 13:37:36 +0000 Subject: [PATCH] `balanced_bound` --- src/flow/binary/bounds.rs | 1 + src/rstd/atomic_object.rs | 2 +- src/rstd/collections/tree.rs | 24 ++++++++-- src/rstd/collections/tree/context.rs | 70 +++++++++++++++++----------- 4 files changed, 65 insertions(+), 32 deletions(-) diff --git a/src/flow/binary/bounds.rs b/src/flow/binary/bounds.rs index 8a6df96..dfeba0d 100644 --- a/src/flow/binary/bounds.rs +++ b/src/flow/binary/bounds.rs @@ -6,6 +6,7 @@ pub struct Bounds { r: Option, } +#[derive(Debug)] pub enum BoundsError { BoundsViolated { l: A, r: A }, OverflowL(A), diff --git a/src/rstd/atomic_object.rs b/src/rstd/atomic_object.rs index 5c01a24..174990b 100644 --- a/src/rstd/atomic_object.rs +++ b/src/rstd/atomic_object.rs @@ -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 { atomic: A, } diff --git a/src/rstd/collections/tree.rs b/src/rstd/collections/tree.rs index 33fa0d3..aded9d2 100644 --- a/src/rstd/collections/tree.rs +++ b/src/rstd/collections/tree.rs @@ -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, DefaultComparator, - WrappedExtra, + BbError>, >; 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(); + } + } } diff --git a/src/rstd/collections/tree/context.rs b/src/rstd/collections/tree/context.rs index 6578dd6..837e6fd 100644 --- a/src/rstd/collections/tree/context.rs +++ b/src/rstd/collections/tree/context.rs @@ -212,37 +212,37 @@ impl< } } -#[derive(Debug)] -pub enum WrappedExtra { - Wrapped(W), - Extra(E), -} +// #[derive(Debug)] +// pub enum WrappedExtra { +// Wrapped(W), +// Extra(E), +// } -impl From> for TreeContextError> { - fn from(value: WrappedExtra) -> Self { - Self::Extra(value) - } -} +// impl From> for TreeContextError> { +// fn from(value: WrappedExtra) -> Self { +// Self::Extra(value) +// } +// } -impl From for TreeContextError> { - fn from(value: BalancingError) -> Self { - WrappedExtra::Wrapped(value).into() - } -} +// impl From for TreeContextError> { +// fn from(value: BalancingError) -> Self { +// WrappedExtra::Wrapped(value).into() +// } +// } -impl From> for TreeContextError, E>> { - fn from(value: BoundsError) -> Self { - WrappedExtra::Wrapped(value).into() - } -} +// impl From> for TreeContextError, E>> { +// fn from(value: BoundsError) -> Self { +// WrappedExtra::Wrapped(value).into() +// } +// } impl< 'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, - E: 'a + Send, - > BinaryTreesUnbalanced<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra> + E: 'a + Send + From, + > 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(&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, - E: 'a + Send, - > BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra, E>> + E: 'a + Send + From>, + > BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, E> { fn bounds_error(&self, error: BoundsError) -> BTWrap<'a, Self, T> { - Self::fail(error.into()) + Self::fail(TreeContextError::Extra(error.into())) + } +} + +#[derive(Debug)] +pub enum BbError { + Balancing(BalancingError), + Bounds(BoundsError), +} + +impl From for BbError { + fn from(value: BalancingError) -> Self { + Self::Balancing(value) + } +} + +impl From> for BbError { + fn from(value: BoundsError) -> Self { + Self::Bounds(value) } }