From 03b4a4764b10b1cd1d8f7a7333ec8790978b2535 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 16 Jun 2023 08:53:13 +0000 Subject: [PATCH] `BinaryTreesEmpty` --- src/flow/binary.rs | 16 +++++++------ src/flow/binary/avl.rs | 22 +++++++++--------- src/flow/binary/bounds/bound.rs | 35 +++++++++++++++++++++++++++++ src/rstd/collections/avl/context.rs | 24 +++++++++++--------- 4 files changed, 70 insertions(+), 27 deletions(-) diff --git a/src/flow/binary.rs b/src/flow/binary.rs index adde5ff..d3a18cb 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -34,9 +34,17 @@ pub trait BinaryTrees<'a>: FunctorContext<'a, T = Self::_Tm> + Clone { } } -pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> { +pub trait BinaryTreesEmpty<'a>: BinaryTrees<'a> { fn empty(&self) -> Self::Tree; + fn split_key_empty( + &self, + tree: Self::Tree, + key: Self::Key, + ) -> BTWrap<'a, Self, KeySplit<'a, Self>>; +} + +pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> { fn join_key( self, tl: Self::Tree, @@ -76,12 +84,6 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> { }) } - fn split_key_empty( - &self, - tree: Self::Tree, - key: Self::Key, - ) -> BTWrap<'a, Self, KeySplit<'a, Self>>; - fn split_key_node( self, node: Self::Node, diff --git a/src/flow/binary/avl.rs b/src/flow/binary/avl.rs index 0670839..c51b841 100644 --- a/src/flow/binary/avl.rs +++ b/src/flow/binary/avl.rs @@ -146,7 +146,7 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTrees<'a> for AvlTs { } } -impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesMutable<'a> for AvlTs { +impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesEmpty<'a> for AvlTs { fn empty(&self) -> Self::Tree { AvlT { reference: None, @@ -154,15 +154,6 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesMutable<'a> for AvlTs { } } - 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) - } - fn split_key_empty( &self, _tree: Self::Tree, @@ -172,6 +163,17 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesMutable<'a> for AvlTs { } } +impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesMutable<'a> for AvlTs { + 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) + } +} + impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesAvl<'a> for AvlTs { fn height(&self, tree: &Self::Tree) -> u64 { tree.height diff --git a/src/flow/binary/bounds/bound.rs b/src/flow/binary/bounds/bound.rs index 958402a..5c71d95 100644 --- a/src/flow/binary/bounds/bound.rs +++ b/src/flow/binary/bounds/bound.rs @@ -94,3 +94,38 @@ impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees { }) } } + +impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a> + for BoundTrees +{ + fn empty(&self) -> Self::Tree { + Bound { + bound: self.0.empty(), + bounds: Bounds::unbound(), + } + } + + fn split_key_empty( + &self, + tree: Self::Tree, + key: Self::Key, + ) -> BTWrap<'a, Self, KeySplit<'a, Self>> { + match tree.bounds.split(&key, self.comparator()) { + Ok((boundsl, boundsr)) => { + Self::bind(self.0.split_key_empty(tree.bound, key), |(tl, tr)| { + Self::pure(( + Bound { + bound: tl, + bounds: boundsl, + }, + Bound { + bound: tr, + bounds: boundsr, + }, + )) + }) + } + Err(e) => self.0.bounds_error(e), + } + } +} diff --git a/src/rstd/collections/avl/context.rs b/src/rstd/collections/avl/context.rs index c8d3b17..27ea2d7 100644 --- a/src/rstd/collections/avl/context.rs +++ b/src/rstd/collections/avl/context.rs @@ -67,21 +67,12 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator> - BinaryTreesMutable<'a> for BoundContext<'a, Ctx, A, C> + BinaryTreesEmpty<'a> for BoundContext<'a, Ctx, A, C> { fn empty(&self) -> Self::Tree { BoundTree::empty(self.factory.clone()) } - 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) - } - fn split_key_empty( &self, tree: Self::Tree, @@ -91,6 +82,19 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator> + BinaryTreesMutable<'a> for BoundContext<'a, Ctx, A, C> +{ + 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) + } +} + impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator> BinaryTreesAvl<'a> for BoundContext<'a, Ctx, A, C> {