balanced_bound
This commit is contained in:
parent
89af9d1084
commit
87451d6cb6
@ -6,6 +6,7 @@ pub struct Bounds<A> {
|
|||||||
r: Option<A>,
|
r: Option<A>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum BoundsError<A> {
|
pub enum BoundsError<A> {
|
||||||
BoundsViolated { l: A, r: A },
|
BoundsViolated { l: A, r: A },
|
||||||
OverflowL(A),
|
OverflowL(A),
|
||||||
|
@ -7,7 +7,7 @@ use crate::atomic::*;
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
/// Generic implementation of a [Mentionable] for [Atomic]s.
|
/// 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> {
|
pub struct AtomicObject<A> {
|
||||||
atomic: A,
|
atomic: A,
|
||||||
}
|
}
|
||||||
|
@ -234,15 +234,13 @@ impl<'a, Ctx: Context<'a>, A: MentionableBase<'a> + Clone> Clone for Tree<'a, Ct
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::convert::Infallible;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
flow::{
|
flow::{
|
||||||
binary::{balancing::*, *},
|
binary::{balancing::*, bound::BoundTrees, *},
|
||||||
comparator::*,
|
comparator::*,
|
||||||
},
|
},
|
||||||
rstd::atomic_object::*,
|
rstd::atomic_object::*,
|
||||||
testing::TestContextPlain,
|
testing::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{context::*, *};
|
use super::{context::*, *};
|
||||||
@ -252,7 +250,7 @@ mod tests {
|
|||||||
TestContextPlain,
|
TestContextPlain,
|
||||||
AtomicObject<u64>,
|
AtomicObject<u64>,
|
||||||
DefaultComparator,
|
DefaultComparator,
|
||||||
WrappedExtra<BalancingError, Infallible>,
|
BbError<AtomicObject<u64>>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
fn new_trees() -> Trees {
|
fn new_trees() -> Trees {
|
||||||
@ -320,4 +318,20 @@ mod tests {
|
|||||||
let reference2 = trees.refer(&singular_keyed(&trees, 2)).unwrap();
|
let reference2 = trees.refer(&singular_keyed(&trees, 2)).unwrap();
|
||||||
assert!(!trees.equal(&reference1, &reference2));
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,37 +212,37 @@ impl<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
// #[derive(Debug)]
|
||||||
pub enum WrappedExtra<W, E> {
|
// pub enum WrappedExtra<W, E> {
|
||||||
Wrapped(W),
|
// Wrapped(W),
|
||||||
Extra(E),
|
// Extra(E),
|
||||||
}
|
// }
|
||||||
|
|
||||||
impl<R, E, W> From<WrappedExtra<W, E>> for TreeContextError<R, WrappedExtra<W, E>> {
|
// impl<R, E, W> From<WrappedExtra<W, E>> for TreeContextError<R, WrappedExtra<W, E>> {
|
||||||
fn from(value: WrappedExtra<W, E>) -> Self {
|
// fn from(value: WrappedExtra<W, E>) -> Self {
|
||||||
Self::Extra(value)
|
// Self::Extra(value)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
impl<R, E> From<BalancingError> for TreeContextError<R, WrappedExtra<BalancingError, E>> {
|
// impl<R, E> From<BalancingError> for TreeContextError<R, WrappedExtra<BalancingError, E>> {
|
||||||
fn from(value: BalancingError) -> Self {
|
// fn from(value: BalancingError) -> Self {
|
||||||
WrappedExtra::Wrapped(value).into()
|
// WrappedExtra::Wrapped(value).into()
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
impl<R, A, E> From<BoundsError<A>> for TreeContextError<R, WrappedExtra<BoundsError<A>, E>> {
|
// impl<R, A, E> From<BoundsError<A>> for TreeContextError<R, WrappedExtra<BoundsError<A>, E>> {
|
||||||
fn from(value: BoundsError<A>) -> Self {
|
// fn from(value: BoundsError<A>) -> Self {
|
||||||
WrappedExtra::Wrapped(value).into()
|
// WrappedExtra::Wrapped(value).into()
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
impl<
|
impl<
|
||||||
'a,
|
'a,
|
||||||
Ctx: Context<'a>,
|
Ctx: Context<'a>,
|
||||||
A: Mentionable<'a, Ctx> + Clone,
|
A: Mentionable<'a, Ctx> + Clone,
|
||||||
C: 'a + Comparator<A>,
|
C: 'a + Comparator<A>,
|
||||||
E: 'a + Send,
|
E: 'a + Send + From<BalancingError>,
|
||||||
> BinaryTreesUnbalanced<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra<BalancingError, E>>
|
> 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> {
|
fn tree_of_with_height(&self, node: Self::Node, height: u64) -> BTWrap<'a, Self, Self::Tree> {
|
||||||
let node = Nullable::from(node);
|
let node = Nullable::from(node);
|
||||||
@ -250,7 +250,7 @@ impl<
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn balancing_error<T: 'a + Send>(&self, error: BalancingError) -> BTWrap<'a, Self, T> {
|
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) {
|
fn node_heights(&self, node: &Self::Node) -> (u64, u64) {
|
||||||
@ -263,10 +263,28 @@ impl<
|
|||||||
Ctx: Context<'a>,
|
Ctx: Context<'a>,
|
||||||
A: Mentionable<'a, Ctx> + Clone,
|
A: Mentionable<'a, Ctx> + Clone,
|
||||||
C: 'a + Comparator<A>,
|
C: 'a + Comparator<A>,
|
||||||
E: 'a + Send,
|
E: 'a + Send + From<BoundsError<A>>,
|
||||||
> BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, WrappedExtra<BoundsError<A>, E>>
|
> BinaryTreesBindable<'a> for TreeContext2<'a, Ctx, A, C, E>
|
||||||
{
|
{
|
||||||
fn bounds_error<T: 'a + Send>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T> {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user