HeightError

This commit is contained in:
AF 2023-06-18 12:59:23 +00:00
parent a1d836ed54
commit ee169f8bf4
5 changed files with 27 additions and 21 deletions

View File

@ -3,6 +3,8 @@ pub mod balancing;
pub mod bound;
pub mod bounds;
use std::fmt::Display;
use crate::flow::comparator::*;
use crate::func::{context::*, *};
@ -132,10 +134,26 @@ pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> {
}
}
#[derive(Debug)]
pub enum HeightError {
LeafHeight(u64),
NodeHeight,
}
impl Display for HeightError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NodeHeight => write!(f, "invalid node height: 0."),
Self::LeafHeight(height) => {
write!(f, "invalid leaf height: {height}!=0.")
}
}
}
}
pub trait BinaryTreesHeight<'a>: BinaryTrees<'a> {
fn height(&self, tree: &Self::Tree) -> u64;
fn leaf_height_error<T: 'a>(&self, height: u64) -> BTWrap<'a, Self, T>;
fn node_height_error<T: 'a>(&self) -> BTWrap<'a, Self, T>;
fn height_error<T: 'a>(&self, error: HeightError) -> BTWrap<'a, Self, T>;
}
pub trait BinaryTreesTryJoin<'a>: BinaryTrees<'a> {

View File

@ -6,7 +6,7 @@ pub trait BinaryTreesAvl<'a>:
fn assume_node(&self, tree: &Self::Tree) -> BTWrap<'a, Self, Self::Node> {
match self.refer(tree) {
Some(reference) => self.resolve(&reference),
None => self.leaf_height_error(self.height(tree)),
None => self.height_error(HeightError::LeafHeight(self.height(tree))),
}
}

View File

@ -160,12 +160,8 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesHeight<'a> for BalancedTrees<
self.0.height(tree)
}
fn leaf_height_error<T: 'a>(&self, height: u64) -> BTWrap<'a, Self, T> {
self.0.leaf_height_error(height)
}
fn node_height_error<T: 'a>(&self) -> BTWrap<'a, Self, T> {
self.0.node_height_error()
fn height_error<T: 'a>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
self.0.height_error(error)
}
}

View File

@ -169,12 +169,8 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesHeight<'a>> BinaryTreesHeight<
self.0.height(&tree.bound)
}
fn leaf_height_error<T: 'a>(&self, height: u64) -> BTWrap<'a, Self, T> {
self.0.leaf_height_error(height)
}
fn node_height_error<T: 'a>(&self) -> BTWrap<'a, Self, T> {
self.0.node_height_error()
fn height_error<T: 'a>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
self.0.height_error(error)
}
}

View File

@ -119,12 +119,8 @@ impl<'a, A: 'a + Ord + Clone> BinaryTreesHeight<'a> for Trees<A> {
tree.height
}
fn leaf_height_error<T: 'a>(&self, height: u64) -> BTWrap<'a, Self, T> {
panic!("leaf height error: {height}.")
}
fn node_height_error<T: 'a>(&self) -> BTWrap<'a, Self, T> {
panic!("node with height 0.")
fn height_error<T: 'a>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
panic!("{error}")
}
}