diff --git a/src/flow/binary/bounds/bound.rs b/src/flow/binary/bounds/bound.rs index a92fdbf..7fc5620 100644 --- a/src/flow/binary/bounds/bound.rs +++ b/src/flow/binary/bounds/bound.rs @@ -1,3 +1,4 @@ +use crate::flow::binary::avl::*; use crate::flow::binary::*; use crate::func::context::*; @@ -9,6 +10,14 @@ pub struct Bound { bounds: Bounds, } +impl std::ops::Deref for Bound { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.bound + } +} + pub struct BoundNode2 { boundsl: Bounds, boundsr: Bounds, @@ -19,11 +28,17 @@ pub struct BoundNode2 { #[derive(Clone)] pub struct BoundTrees(BT); +impl BoundTrees { + pub fn new(bt: BT) -> Self { + Self(bt) + } +} + impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees { type T = BT::T; } -trait BinaryTreesBindable<'a>: BinaryTrees<'a> { +pub trait BinaryTreesBindable<'a>: BinaryTrees<'a> { fn bounds_error(&self, error: BoundsError) -> BTWrap<'a, Self, T>; fn bounds_bind( @@ -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 +{ + 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) + } +} diff --git a/src/mrds/trees/avl.rs b/src/mrds/trees/avl.rs index f1d017a..90fc8f3 100644 --- a/src/mrds/trees/avl.rs +++ b/src/mrds/trees/avl.rs @@ -1,7 +1,7 @@ use std::{fmt::Display, marker::PhantomData, rc::Rc}; use crate::flow::{ - binary::{avl::*, *}, + binary::{avl::*, bounds::bound::BinaryTreesBindable, *}, comparator::*, }; use crate::func::{context::*, *}; @@ -197,8 +197,16 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesTryJoin<'a> for AvlTs { } } +impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesBindable<'a> for AvlTs { + fn bounds_error(&self, _error: bounds::BoundsError) -> BTWrap<'a, Self, T> { + panic!("bounds violated"); + } +} + #[cfg(test)] mod tests { + use crate::flow::binary::bounds::bound::*; + use super::*; #[test] @@ -221,4 +229,25 @@ mod tests { } // 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); + } }