From 711221e4d85fb1fe77b4dba6bde11a7fa005b184 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 16 Jun 2023 09:40:35 +0000 Subject: [PATCH] `flow::binary::avl` --- src/flow/binary.rs | 80 +---------------------------- src/flow/binary/avl.rs | 80 +++++++++++++++++++++++++++++ src/flow/binary/bounds/bound.rs | 5 +- src/mrds/trees/avl.rs | 5 +- src/rstd/collections/avl/context.rs | 8 +-- 5 files changed, 94 insertions(+), 84 deletions(-) create mode 100644 src/flow/binary/avl.rs diff --git a/src/flow/binary.rs b/src/flow/binary.rs index 431d283..91eecb7 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -1,3 +1,4 @@ +pub mod avl; pub mod bounds; use crate::flow::comparator::*; @@ -125,82 +126,3 @@ pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> { }) } } - -pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> { - fn height(&self, tree: &Self::Tree) -> u64; - - fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T>; - - 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)), - } - } - - fn join_key_unbalanced( - &self, - tl: Self::Tree, - key: Self::Key, - tr: Self::Tree, - ) -> BTWrap<'a, Self, Self::Node>; - - fn join_key_balanced_tree( - &self, - tl: Self::Tree, - key: Self::Key, - tr: Self::Tree, - ) -> BTWrap<'a, Self, Self::Tree> { - self.clone() - .tree_bind(self.clone().join_key_balanced(tl, key, tr)) - } - - fn join_key_balanced( - self, - tl: Self::Tree, - key: Self::Key, - tr: Self::Tree, - ) -> BTWrap<'a, Self, Self::Node> { - let (hl, hr) = (self.height(&tl), self.height(&tr)); - match (hl.saturating_sub(hr), hr.saturating_sub(hl)) { - (0, 0) | (0, 1) | (1, 0) => self.join_key_unbalanced(tl, key, tr), - (0, _) => Self::bind(self.assume_node(&tr), move |nr| { - let (trl, trr, kr) = self.split(&nr); - let (rlh, rrh) = (self.height(&trl), self.height(&trr)); - if rlh > rrh { - Self::bind(self.assume_node(&trl), move |nrl| { - let (trll, trlr, krl) = self.split(&nrl); - Self::T::bind2( - self.join_key_balanced_tree(tl, key, trll), - self.join_key_balanced_tree(trlr, kr, trr), - |ti, to| self.join_key_balanced(ti, krl, to), - ) - }) - } else { - Self::bind(self.join_key_balanced_tree(tl, key, trl), |t| { - self.join_key_balanced(t, kr, trr) - }) - } - }), - (_, 0) => Self::bind(self.assume_node(&tl), move |nl| { - let (tll, tlr, kl) = self.split(&nl); - let (hll, hlr) = (self.height(&tll), self.height(&tlr)); - if hll < hlr { - Self::bind(self.assume_node(&tlr), move |nlr| { - let (tlrl, tlrr, klr) = self.split(&nlr); - Self::T::bind2( - self.join_key_balanced_tree(tll, kl, tlrl), - self.join_key_balanced_tree(tlrr, key, tr), - |to, ti| self.join_key_balanced(to, klr, ti), - ) - }) - } else { - Self::bind(self.join_key_balanced_tree(tlr, key, tr), |t| { - self.join_key_balanced(tll, kl, t) - }) - } - }), - (_, _) => unreachable!(), - } - } -} diff --git a/src/flow/binary/avl.rs b/src/flow/binary/avl.rs new file mode 100644 index 0000000..1924563 --- /dev/null +++ b/src/flow/binary/avl.rs @@ -0,0 +1,80 @@ +use super::*; + +pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> { + fn height(&self, tree: &Self::Tree) -> u64; + + fn leaf_height_error(&self, height: u64) -> BTWrap<'a, Self, T>; + + 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)), + } + } + + fn join_key_unbalanced( + &self, + tl: Self::Tree, + key: Self::Key, + tr: Self::Tree, + ) -> BTWrap<'a, Self, Self::Node>; + + fn join_key_balanced_tree( + &self, + tl: Self::Tree, + key: Self::Key, + tr: Self::Tree, + ) -> BTWrap<'a, Self, Self::Tree> { + self.clone() + .tree_bind(self.clone().join_key_balanced(tl, key, tr)) + } + + fn join_key_balanced( + self, + tl: Self::Tree, + key: Self::Key, + tr: Self::Tree, + ) -> BTWrap<'a, Self, Self::Node> { + let (hl, hr) = (self.height(&tl), self.height(&tr)); + match (hl.saturating_sub(hr), hr.saturating_sub(hl)) { + (0, 0) | (0, 1) | (1, 0) => self.join_key_unbalanced(tl, key, tr), + (0, _) => Self::bind(self.assume_node(&tr), move |nr| { + let (trl, trr, kr) = self.split(&nr); + let (rlh, rrh) = (self.height(&trl), self.height(&trr)); + if rlh > rrh { + Self::bind(self.assume_node(&trl), move |nrl| { + let (trll, trlr, krl) = self.split(&nrl); + Self::T::bind2( + self.join_key_balanced_tree(tl, key, trll), + self.join_key_balanced_tree(trlr, kr, trr), + |ti, to| self.join_key_balanced(ti, krl, to), + ) + }) + } else { + Self::bind(self.join_key_balanced_tree(tl, key, trl), |t| { + self.join_key_balanced(t, kr, trr) + }) + } + }), + (_, 0) => Self::bind(self.assume_node(&tl), move |nl| { + let (tll, tlr, kl) = self.split(&nl); + let (hll, hlr) = (self.height(&tll), self.height(&tlr)); + if hll < hlr { + Self::bind(self.assume_node(&tlr), move |nlr| { + let (tlrl, tlrr, klr) = self.split(&nlr); + Self::T::bind2( + self.join_key_balanced_tree(tll, kl, tlrl), + self.join_key_balanced_tree(tlrr, key, tr), + |to, ti| self.join_key_balanced(to, klr, ti), + ) + }) + } else { + Self::bind(self.join_key_balanced_tree(tlr, key, tr), |t| { + self.join_key_balanced(tll, kl, t) + }) + } + }), + (_, _) => unreachable!(), + } + } +} diff --git a/src/flow/binary/bounds/bound.rs b/src/flow/binary/bounds/bound.rs index 5ae76fe..8eef341 100644 --- a/src/flow/binary/bounds/bound.rs +++ b/src/flow/binary/bounds/bound.rs @@ -1,4 +1,7 @@ -use crate::{flow::binary::*, func::context::*}; +use crate::{ + flow::binary::{avl::*, *}, + func::context::*, +}; use super::*; diff --git a/src/mrds/trees/avl.rs b/src/mrds/trees/avl.rs index 1afcf5f..d717083 100644 --- a/src/mrds/trees/avl.rs +++ b/src/mrds/trees/avl.rs @@ -1,6 +1,9 @@ use std::{fmt::Display, marker::PhantomData, rc::Rc}; -use crate::flow::{binary::*, comparator::*}; +use crate::flow::{ + binary::{avl::*, *}, + comparator::*, +}; use crate::func::{context::*, *}; #[derive(Clone)] diff --git a/src/rstd/collections/avl/context.rs b/src/rstd/collections/avl/context.rs index 27ea2d7..95c180c 100644 --- a/src/rstd/collections/avl/context.rs +++ b/src/rstd/collections/avl/context.rs @@ -1,6 +1,8 @@ -use crate::flow::{binary::*, comparator::*}; -use crate::func::context::*; -use crate::func::*; +use crate::flow::{ + binary::{avl::BinaryTreesAvl, *}, + comparator::*, +}; +use crate::func::{context::*, *}; use crate::rstd::fallible::*; use super::{bounds::*, *};