From 354c2363c8104cf34fd14e0ff79aceb14dc0a7ea Mon Sep 17 00:00:00 2001 From: timofey Date: Tue, 30 May 2023 12:53:28 +0000 Subject: [PATCH] `BinaryTrees` func shortcuts --- src/flow/binary.rs | 85 ++++++++++++++++++----------- src/rstd/collections/avl/context.rs | 4 +- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/flow/binary.rs b/src/flow/binary.rs index 71469f2..f55ae88 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -31,6 +31,31 @@ pub trait BinaryTrees<'a>: 'a + Clone { fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>; fn equal(&self, rhs: &Self::Reference, lhs: &Self::Reference) -> bool; fn refer(&self, tree: &Self::Tree) -> Option; + + fn bind( + fa: BTWrap<'a, Self, A>, + f: impl 'a + FnOnce(A) -> BTWrap<'a, Self, B>, + ) -> BTWrap<'a, Self, B> { + ::bind(fa, f) + } + + fn pure(a: A) -> BTWrap<'a, Self, A> { + ::pure(a) + } + + fn fmap( + fa: BTWrap<'a, Self, A>, + f: impl 'a + FnOnce(A) -> B, + ) -> BTWrap<'a, Self, B> { + ::fmap(fa, f) + } + + fn fail(e: E) -> BTWrap<'a, Self, A> + where + Self::T: Fail<'a, E>, + { + >::fail(e) + } } pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> { @@ -49,15 +74,15 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> { key: KeyRc<'a, Self>, tr: Self::Tree, ) -> BTWrap<'a, Self, Self::Tree> { - ::bind(self.clone().join_key(tl, key, tr), move |n| self.tree_of(n)) + Self::bind(self.clone().join_key(tl, key, tr), move |n| self.tree_of(n)) } fn join(self, tl: Self::Tree, tr: Self::Tree) -> BTWrap<'a, Self, Self::Tree> { let Some(rl) = self.refer(&tl) else { - return ::pure(tr) + return Self::pure(tr) }; let Some(rr) = self.refer(&tr) else { - return ::pure(tl) + return Self::pure(tl) }; Self::T::bind2(self.resolve(&rl), self.resolve(&rr), move |nl, nr| { let (tll, tlr, kl) = self.split(&nl); @@ -65,11 +90,11 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> { let ft = self.clone().join(tlr, trl); let ft = { let ctx = self.clone(); - ::bind(ft, move |t| ctx.join_key_tree(tll, kl, t)) + Self::bind(ft, move |t| ctx.join_key_tree(tll, kl, t)) }; let ft = { let ctx = self.clone(); - ::bind(ft, move |t| ctx.join_key_tree(t, kr, trr)) + Self::bind(ft, move |t| ctx.join_key_tree(t, kr, trr)) }; ft }) @@ -88,17 +113,13 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> { ) -> BTWrap<'a, Self, KeySplit<'a, Self>> { let (tl, tr, k) = self.split(&node); match self.comparator().pick_smaller(&key, &k) { - Comparison::L => { - ::bind(self.clone().split_key(tl, key), move |(tll, tlr)| { - ::fmap(self.join_key_tree(tlr, k, tr), |t| (tll, t)) - }) - } - Comparison::E => ::pure((tl, tr)), - Comparison::R => { - ::bind(self.clone().split_key(tr, key), move |(trl, trr)| { - ::fmap(self.join_key_tree(tl, k, trl), |t| (t, trr)) - }) - } + Comparison::L => Self::bind(self.clone().split_key(tl, key), move |(tll, tlr)| { + Self::fmap(self.join_key_tree(tlr, k, tr), |t| (tll, t)) + }), + Comparison::E => Self::pure((tl, tr)), + Comparison::R => Self::bind(self.clone().split_key(tr, key), move |(trl, trr)| { + Self::fmap(self.join_key_tree(tl, k, trl), |t| (t, trr)) + }), } } @@ -108,7 +129,7 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> { key: KeyRc<'a, Self>, ) -> BTWrap<'a, Self, KeySplit<'a, Self>> { match self.refer(&tree) { - Some(reference) => ::bind(self.resolve(&reference), |node| { + Some(reference) => Self::bind(self.resolve(&reference), |node| { self.split_key_node(node, key) }), None => self.split_key_empty(tree, key), @@ -116,17 +137,17 @@ pub trait BinaryTreesMutable<'a>: BinaryTrees<'a> { } fn add(self, tree: Self::Tree, key: KeyRc<'a, Self>) -> BTWrap<'a, Self, Self::Node> { - ::bind(self.clone().split_key(tree, key.clone()), |(tl, tr)| { + Self::bind(self.clone().split_key(tree, key.clone()), |(tl, tr)| { self.join_key(tl, key, tr) }) } fn add_tree(self, tree: Self::Tree, key: KeyRc<'a, Self>) -> BTWrap<'a, Self, Self::Tree> { - ::bind(self.clone().add(tree, key), move |n| self.tree_of(n)) + Self::bind(self.clone().add(tree, key), move |n| self.tree_of(n)) } fn remove(self, tree: Self::Tree, key: KeyRc<'a, Self>) -> BTWrap<'a, Self, Self::Tree> { - ::bind(self.clone().split_key(tree, key.clone()), |(tl, tr)| { + Self::bind(self.clone().split_key(tree, key.clone()), |(tl, tr)| { self.join(tl, tr) }) } @@ -157,7 +178,7 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> { key: KeyRc<'a, Self>, tr: Self::Tree, ) -> BTWrap<'a, Self, Self::Tree> { - ::bind(self.clone().join_key_balanced(tl, key, tr), move |node| { + Self::bind(self.clone().join_key_balanced(tl, key, tr), move |node| { self.tree_of(node) }) } @@ -171,11 +192,11 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> { let (lh, rh) = (self.height(&tl), self.height(&tr)); match (lh.saturating_sub(rh), rh.saturating_sub(lh)) { (0, 0) | (0, 1) | (1, 0) => self.join_key_unbalanced(tl, key, tr), - (0, _) => ::bind(self.assume_node(&tr), move |nr| { + (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 { - ::bind(self.assume_node(&trl), move |nrl| { + Self::bind(self.assume_node(&trl), move |nrl| { let (trll, trlr, krl) = self.split(&nrl); Self::T::bind2( self.clone().join_key_balanced_tree(tl, key, trll), @@ -184,17 +205,16 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> { ) }) } else { - ::bind( - self.clone().join_key_balanced_tree(tl, key, trl), - |t| self.join_key_balanced(t, kr, trr), - ) + Self::bind(self.clone().join_key_balanced_tree(tl, key, trl), |t| { + self.join_key_balanced(t, kr, trr) + }) } }), - (_, 0) => ::bind(self.assume_node(&tl), move |nl| { + (_, 0) => Self::bind(self.assume_node(&tl), move |nl| { let (tll, tlr, kl) = self.split(&nl); let (llh, lrh) = (self.height(&tll), self.height(&tlr)); if llh < lrh { - ::bind(self.assume_node(&tlr), move |nlr| { + Self::bind(self.assume_node(&tlr), move |nlr| { let (tlrl, tlrr, klr) = self.split(&nlr); Self::T::bind2( self.clone().join_key_balanced_tree(tll, kl, tlrl), @@ -203,10 +223,9 @@ pub trait BinaryTreesAvl<'a>: BinaryTrees<'a> { ) }) } else { - ::bind( - self.clone().join_key_balanced_tree(tlr, key, tr), - |t| self.join_key_balanced(tll, kl, t), - ) + Self::bind(self.clone().join_key_balanced_tree(tlr, key, tr), |t| { + self.join_key_balanced(tll, kl, t) + }) } }), (_, _) => unreachable!(), diff --git a/src/rstd/collections/avl/context.rs b/src/rstd/collections/avl/context.rs index e1e4a6d..c4f89b2 100644 --- a/src/rstd/collections/avl/context.rs +++ b/src/rstd/collections/avl/context.rs @@ -41,8 +41,8 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>, C: 'a + Comparator> Binar fn tree_of(&self, node: Self::Node) -> BTWrap<'a, Self, Self::Tree> { match node.into_tree() { - Ok(tree) => ::pure(tree), - Err(e) => >::fail(ResolutionError::Parse(e)), + Ok(tree) => Self::pure(tree), + Err(e) => Self::fail(ResolutionError::Parse(e)), } }