From d9c21f639df741c39aa0aa4812d60b5a31f8d7f8 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 16 Jun 2023 13:23:01 +0000 Subject: [PATCH] `BinaryTreesHeight::height_r` --- src/flow/binary.rs | 2 +- src/flow/binary/bounds/bound.rs | 4 ++++ src/mrds/trees/avl.rs | 22 ++++++++++++---------- src/rstd/collections/avl/binary.rs | 12 ++++++++---- src/rstd/collections/avl/bounds.rs | 4 ++++ src/rstd/collections/avl/context.rs | 4 ++++ 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/flow/binary.rs b/src/flow/binary.rs index 31a15fb..8f3bc7f 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -133,7 +133,7 @@ pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> { pub trait BinaryTreesHeight<'a>: BinaryTrees<'a> { fn height(&self, tree: &Self::Tree) -> u64; - + fn height_r(&self, reference: &Self::Reference) -> u64; fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T>; } diff --git a/src/flow/binary/bounds/bound.rs b/src/flow/binary/bounds/bound.rs index 8848fe2..89cad47 100644 --- a/src/flow/binary/bounds/bound.rs +++ b/src/flow/binary/bounds/bound.rs @@ -155,6 +155,10 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesHeight<'a>> BinaryTreesHeight< self.0.height(&tree.bound) } + fn height_r(&self, reference: &Self::Reference) -> u64 { + self.0.height_r(&reference.bound) + } + fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T> { self.0.leaf_height_error(height) } diff --git a/src/mrds/trees/avl.rs b/src/mrds/trees/avl.rs index ed17069..9a1eca9 100644 --- a/src/mrds/trees/avl.rs +++ b/src/mrds/trees/avl.rs @@ -29,12 +29,14 @@ impl AvlN { struct AvlR { node: Rc>, + height: u64, } impl Clone for AvlR { fn clone(&self) -> Self { Self { node: self.node.clone(), + height: self.height, } } } @@ -45,15 +47,8 @@ impl Display for AvlR { } } -#[cfg(test)] -impl AvlR { - fn balanced(&self) -> bool { - self.node.balanced() - } -} - struct AvlT { - reference: Option>, + reference: Option>>, height: u64, } @@ -135,7 +130,10 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTrees<'a> for AvlTs { } fn refer(&self, tree: &Self::Tree) -> Option { - tree.reference.clone() + Some(AvlR { + node: tree.reference.clone()?, + height: tree.height, + }) } } @@ -145,7 +143,7 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesTreeOf<'a> for AvlTs { height: std::cmp::max(node.l.height, node.r.height) .checked_add(1) .unwrap(), - reference: Some(AvlR { node: node.into() }), + reference: Some(node.into()), } } } @@ -183,6 +181,10 @@ impl<'a, A: 'a + PartialOrd + Clone> BinaryTreesHeight<'a> for AvlTs { tree.height } + fn height_r(&self, reference: &Self::Reference) -> u64 { + reference.height + } + fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T> { panic!("leaf height error: {height}.") } diff --git a/src/rstd/collections/avl/binary.rs b/src/rstd/collections/avl/binary.rs index 3ddcd00..19ea17a 100644 --- a/src/rstd/collections/avl/binary.rs +++ b/src/rstd/collections/avl/binary.rs @@ -4,12 +4,12 @@ use super::*; pub struct AvlReference<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> { node: Point<'a, Ctx, AvlNode<'a, Ctx, A>>, - parent_height: u64, + height: u64, } impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> PartialEq for AvlReference<'a, Ctx, A> { fn eq(&self, other: &Self) -> bool { - self.node == other.node && self.parent_height == other.parent_height + self.node == other.node && self.height == other.height } } @@ -67,7 +67,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AvlTree<'a, Ctx, A> { Nullable::Null(_) => None, Nullable::NotNull(ref point) => Some(AvlReference { node: point.clone(), - parent_height: self.height, + height: self.height, }), } } @@ -75,7 +75,7 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AvlTree<'a, Ctx, A> { impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AvlReference<'a, Ctx, A> { pub fn resolve(&self) -> Resolution<'a, Ctx, AvlNode<'a, Ctx, A>> { - let parent_height = self.parent_height; + let parent_height = self.height; Ctx::fmap(self.node.resolve(), move |resolved| { let node = resolved?; node.matches_height(parent_height) @@ -83,6 +83,10 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> AvlReference<'a, Ctx, A> { Ok(node) }) } + + pub fn height(&self) -> u64 { + self.height + } } impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Clone for AvlTree<'a, Ctx, A> { diff --git a/src/rstd/collections/avl/bounds.rs b/src/rstd/collections/avl/bounds.rs index 341b11c..915e5e6 100644 --- a/src/rstd/collections/avl/bounds.rs +++ b/src/rstd/collections/avl/bounds.rs @@ -183,4 +183,8 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone> BoundReference<'a, C pub fn equal(rl: &Self, rr: &Self, comparator: &impl Comparator) -> bool { rl.reference == rr.reference && Bounds::equal(&rl.bounds, &rr.bounds, comparator) } + + pub fn height(&self) -> u64 { + self.reference.height() + } } diff --git a/src/rstd/collections/avl/context.rs b/src/rstd/collections/avl/context.rs index eb2afb9..3d25987 100644 --- a/src/rstd/collections/avl/context.rs +++ b/src/rstd/collections/avl/context.rs @@ -108,6 +108,10 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone, C: 'a + Comparator u64 { + reference.height() + } + fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T> { as Fail<_>>::fail(ResolutionError::Parse(BoundError::Avl( AvlError::LeafHeight(height),