bound tests

This commit is contained in:
AF 2023-06-16 16:21:19 +00:00
parent cc48cc1d24
commit 20e441be04
2 changed files with 65 additions and 2 deletions

View File

@ -1,3 +1,4 @@
use crate::flow::binary::avl::*;
use crate::flow::binary::*; use crate::flow::binary::*;
use crate::func::context::*; use crate::func::context::*;
@ -9,6 +10,14 @@ pub struct Bound<A, T> {
bounds: Bounds<A>, bounds: Bounds<A>,
} }
impl<A, T> std::ops::Deref for Bound<A, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.bound
}
}
pub struct BoundNode2<A, T> { pub struct BoundNode2<A, T> {
boundsl: Bounds<A>, boundsl: Bounds<A>,
boundsr: Bounds<A>, boundsr: Bounds<A>,
@ -19,11 +28,17 @@ pub struct BoundNode2<A, T> {
#[derive(Clone)] #[derive(Clone)]
pub struct BoundTrees<BT>(BT); pub struct BoundTrees<BT>(BT);
impl<BT> BoundTrees<BT> {
pub fn new(bt: BT) -> Self {
Self(bt)
}
}
impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees<BT> { impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees<BT> {
type T = BT::T; type T = BT::T;
} }
trait BinaryTreesBindable<'a>: BinaryTrees<'a> { pub trait BinaryTreesBindable<'a>: BinaryTrees<'a> {
fn bounds_error<T: 'a>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T>; fn bounds_error<T: 'a>(&self, error: BoundsError<Self::Key>) -> BTWrap<'a, Self, T>;
fn bounds_bind<A: 'a, B: 'a>( fn bounds_bind<A: 'a, B: 'a>(
@ -185,3 +200,22 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTryJoin<'a>> BinaryTreesTryJoi
) )
} }
} }
impl<
'a,
BT: BinaryTreesBindable<'a>
+ BinaryTreesTreeOf<'a>
+ BinaryTreesEmpty<'a>
+ BinaryTreesHeight<'a>
+ BinaryTreesTryJoin<'a>,
> BinaryTreesMutable<'a> for BoundTrees<BT>
{
fn join_key(
self,
tl: Self::Tree,
key: Self::Key,
tr: Self::Tree,
) -> BTWrap<'a, Self, Self::Node> {
self.join_key_balanced(tl, key, tr)
}
}

View File

@ -1,7 +1,7 @@
use std::{fmt::Display, marker::PhantomData, rc::Rc}; use std::{fmt::Display, marker::PhantomData, rc::Rc};
use crate::flow::{ use crate::flow::{
binary::{avl::*, *}, binary::{avl::*, bounds::bound::BinaryTreesBindable, *},
comparator::*, comparator::*,
}; };
use crate::func::{context::*, *}; use crate::func::{context::*, *};
@ -197,8 +197,16 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesTryJoin<'a> for AvlTs<A> {
} }
} }
impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesBindable<'a> for AvlTs<A> {
fn bounds_error<T: 'a>(&self, _error: bounds::BoundsError<Self::Key>) -> BTWrap<'a, Self, T> {
panic!("bounds violated");
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::flow::binary::bounds::bound::*;
use super::*; use super::*;
#[test] #[test]
@ -221,4 +229,25 @@ mod tests {
} }
// assert!(false); // assert!(false);
} }
#[test]
fn test_with_bounds() {
let trees = BoundTrees::new(AvlTs::new());
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);
assert!(tree.balanced());
// println!("{} {}", tree.height, tree);
}
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);
assert!(tree.balanced());
// println!("{} {}", tree.height, tree);
}
// assert!(false);
}
} }