BalancingError::Height

This commit is contained in:
AF 2023-06-18 13:03:29 +00:00
parent ee169f8bf4
commit 4f702288dd
2 changed files with 11 additions and 9 deletions

View File

@ -39,8 +39,7 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BalancedTrees<BT> {
#[derive(Debug)] #[derive(Debug)]
pub enum BalancingError { pub enum BalancingError {
LeafHeight(u64), Height(HeightError),
NodeHeight,
Balance(u64, u64), Balance(u64, u64),
HeightOverflow, HeightOverflow,
HeightMismatch { children: (u64, u64), parent: u64 }, HeightMismatch { children: (u64, u64), parent: u64 },
@ -49,10 +48,7 @@ pub enum BalancingError {
impl Display for BalancingError { impl Display for BalancingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::NodeHeight => write!(f, "invalid AVL non-leaf height: 0."), Self::Height(height_error) => write!(f, "invalid AVL non-leaf height: {height_error}"),
Self::LeafHeight(height) => {
write!(f, "invalid AVL leaf height: {height}!=0.")
}
Self::Balance(hl, hr) => write!(f, "unbalanced AVL node: {hl} {hr}."), Self::Balance(hl, hr) => write!(f, "unbalanced AVL node: {hl} {hr}."),
Self::HeightOverflow => write!(f, "AVL tree height overflow"), Self::HeightOverflow => write!(f, "AVL tree height overflow"),
Self::HeightMismatch { children, parent } => { Self::HeightMismatch { children, parent } => {

View File

@ -3,7 +3,7 @@ pub mod context;
use std::{error::Error, fmt::Display, rc::Rc}; use std::{error::Error, fmt::Display, rc::Rc};
use crate::flow::binary::balancing::*; use crate::flow::binary::{balancing::*, *};
use crate::rcore::*; use crate::rcore::*;
use crate::rstd::{ use crate::rstd::{
atomic::{au64::*, *}, atomic::{au64::*, *},
@ -37,6 +37,12 @@ impl<E> From<BalancingError> for AvlError<E> {
} }
} }
impl<E> From<HeightError> for AvlError<E> {
fn from(value: HeightError) -> Self {
BalancingError::Height(value).into()
}
}
impl<E: Display> Display for AvlError<E> { impl<E: Display> Display for AvlError<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
@ -173,10 +179,10 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for AvlTreeFact
let height = u64::a_deserialize(deserializer)?; let height = u64::a_deserialize(deserializer)?;
if let Nullable::Null(_) = node { if let Nullable::Null(_) = node {
if height != 0 { if height != 0 {
Err(BalancingError::LeafHeight(height))? Err(HeightError::LeafHeight(height))?
} }
} else if height == 0 { } else if height == 0 {
Err(BalancingError::NodeHeight)? Err(HeightError::NodeHeight)?
} }
Ok(AvlTree { node, height }) Ok(AvlTree { node, height })
} }