From d990fb1431cb1cc4002bfa0d4d817133625d8231 Mon Sep 17 00:00:00 2001
From: timofey <tim@ongoteam.yaconnect.com>
Date: Thu, 31 Aug 2023 23:42:57 +0000
Subject: [PATCH] `TreesHeight`/`TreesHeightError`

---
 src/flow/binary.rs                   | 14 +++++----
 src/flow/binary/avl.rs               |  4 +--
 src/flow/binary/balancing.rs         | 32 +++++++++++----------
 src/flow/binary/bound.rs             | 42 ++++++++++++++-------------
 src/mrds/trees/heighted.rs           | 20 +++++++------
 src/rstd/collections/tree/context.rs | 43 +++++++++++++++++-----------
 6 files changed, 87 insertions(+), 68 deletions(-)

diff --git a/src/flow/binary.rs b/src/flow/binary.rs
index 989eff7..ffbfd49 100644
--- a/src/flow/binary.rs
+++ b/src/flow/binary.rs
@@ -23,16 +23,17 @@ pub trait BinaryTrees: Clone {
     type Tree: Send;
     type Key: Send + Clone;
     type Comparator: Comparator<Self::Key>;
+
+    fn comparator(&self) -> &Self::Comparator;
+    fn split(&self, node: &Self::Node) -> Split<Self>;
+    fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool;
+    fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference>;
 }
 
 pub trait MonadTrees<'a>: FunctorContext<'a, T = Self::_Tm> + BinaryTrees {
     type _Tm: Monad<'a>;
 
-    fn comparator(&self) -> &Self::Comparator;
-    fn split(&self, node: &Self::Node) -> Split<Self>;
     fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node>;
-    fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool;
-    fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference>;
 }
 
 pub trait BinaryTreesTreeOf<'a>: MonadTrees<'a> {
@@ -146,8 +147,11 @@ impl Display for HeightError {
     }
 }
 
-pub trait BinaryTreesHeight<'a>: MonadTrees<'a> {
+pub trait TreesHeight: BinaryTrees {
     fn height(&self, tree: &Self::Tree) -> u64;
+}
+
+pub trait TreesHeightError<'a>: MonadTrees<'a> + TreesHeight {
     fn height_error<T: 'a + Send>(&self, error: HeightError) -> BTWrap<'a, Self, T>;
 }
 
diff --git a/src/flow/binary/avl.rs b/src/flow/binary/avl.rs
index 97b8871..5c74ea3 100644
--- a/src/flow/binary/avl.rs
+++ b/src/flow/binary/avl.rs
@@ -56,7 +56,7 @@ fn balancing(hl: u64, hr: u64) -> Balancing {
 }
 
 pub trait BinaryTreesAvl<'a>:
-    BinaryTreesHeight<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>
+    TreesHeightError<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>
 {
     fn assume_node(&self, tree: &Self::Tree) -> BTWrap<'a, Self, Self::Node> {
         match self.refer(tree) {
@@ -116,7 +116,7 @@ pub trait BinaryTreesAvl<'a>:
     }
 }
 
-impl<'a, BT: BinaryTreesHeight<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>>
+impl<'a, BT: TreesHeightError<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>>
     BinaryTreesAvl<'a> for BT
 {
 }
diff --git a/src/flow/binary/balancing.rs b/src/flow/binary/balancing.rs
index c36fed7..967d212 100644
--- a/src/flow/binary/balancing.rs
+++ b/src/flow/binary/balancing.rs
@@ -2,7 +2,7 @@ use std::fmt::Display;
 
 use super::{avl::*, bound::*, *};
 
-pub trait BinaryTreesUnbalanced<'a>: BinaryTreesHeight<'a> {
+pub trait BinaryTreesUnbalanced<'a>: TreesHeightError<'a> {
     fn tree_of_with_height(&self, node: Self::Node, height: u64) -> BTWrap<'a, Self, Self::Tree>;
 
     fn balancing_error<T: 'a + Send>(&self, error: BalancingError) -> BTWrap<'a, Self, T>;
@@ -84,16 +84,12 @@ fn matches_height(hl: u64, hr: u64, hp: u64) -> Result<(), BalancingError> {
     }
 }
 
-impl<BT: BinaryTrees> BinaryTrees for BalancedTrees<BT> {
+impl<BT: TreesHeight> BinaryTrees for BalancedTrees<BT> {
     type Node = BT::Node;
     type Reference = (BT::Reference, u64);
     type Tree = BT::Tree;
     type Key = BT::Key;
     type Comparator = BT::Comparator;
-}
-
-impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
-    type _Tm = Self::T;
 
     fn comparator(&self) -> &Self::Comparator {
         self.0.comparator()
@@ -103,6 +99,18 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
         self.0.split(node)
     }
 
+    fn equal(&self, (rl, hl): &Self::Reference, (rr, hr): &Self::Reference) -> bool {
+        hl == hr && self.0.equal(rl, rr)
+    }
+
+    fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
+        Some((self.0.refer(tree)?, self.0.height(tree)))
+    }
+}
+
+impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
+    type _Tm = Self::T;
+
     fn resolve(&self, (reference, hp): &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
         let hp = *hp;
         let ctx = self.0.clone();
@@ -111,14 +119,6 @@ impl<'a, BT: BinaryTreesUnbalanced<'a>> MonadTrees<'a> for BalancedTrees<BT> {
             ctx.balancing_bind(matches_height(hl, hr, hp), |_| Self::pure(node))
         })
     }
-
-    fn equal(&self, (rl, hl): &Self::Reference, (rr, hr): &Self::Reference) -> bool {
-        hl == hr && self.0.equal(rl, rr)
-    }
-
-    fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
-        Some((self.0.refer(tree)?, self.0.height(tree)))
-    }
 }
 
 impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesTreeOf<'a> for BalancedTrees<BT> {
@@ -146,11 +146,13 @@ impl<'a, BT: BinaryTreesUnbalanced<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<
     }
 }
 
-impl<'a, BT: BinaryTreesUnbalanced<'a>> BinaryTreesHeight<'a> for BalancedTrees<BT> {
+impl<BT: TreesHeight> TreesHeight for BalancedTrees<BT> {
     fn height(&self, tree: &Self::Tree) -> u64 {
         self.0.height(tree)
     }
+}
 
+impl<'a, BT: BinaryTreesUnbalanced<'a>> TreesHeightError<'a> for BalancedTrees<BT> {
     fn height_error<T: 'a + Send>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
         self.0.height_error(error)
     }
diff --git a/src/flow/binary/bound.rs b/src/flow/binary/bound.rs
index feb3696..e3f86b1 100644
--- a/src/flow/binary/bound.rs
+++ b/src/flow/binary/bound.rs
@@ -64,10 +64,6 @@ impl<BT: BinaryTrees> BinaryTrees for BoundTrees<BT> {
     type Tree = Bound<Self::Key, BT::Tree>;
     type Key = BT::Key;
     type Comparator = BT::Comparator;
-}
-
-impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
-    type _Tm = Self::T;
 
     fn comparator(&self) -> &Self::Comparator {
         self.0.comparator()
@@ -88,6 +84,22 @@ impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
         )
     }
 
+    fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool {
+        Bounds::equal(&rl.bounds, &rr.bounds, self.comparator())
+            && self.0.equal(&rl.bound, &rr.bound)
+    }
+
+    fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
+        Some(Bound {
+            bound: self.0.refer(&tree.bound)?,
+            bounds: tree.bounds.clone(),
+        })
+    }
+}
+
+impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
+    type _Tm = Self::T;
+
     fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
         let ctx = self.0.clone();
         let bounds = reference.bounds.clone();
@@ -106,18 +118,6 @@ impl<'a, BT: BinaryTreesBindable<'a>> MonadTrees<'a> for BoundTrees<BT> {
             )
         })
     }
-
-    fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool {
-        Bounds::equal(&rl.bounds, &rr.bounds, self.comparator())
-            && self.0.equal(&rl.bound, &rr.bound)
-    }
-
-    fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
-        Some(Bound {
-            bound: self.0.refer(&tree.bound)?,
-            bounds: tree.bounds.clone(),
-        })
-    }
 }
 
 impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesTreeOf<'a>> BinaryTreesTreeOf<'a>
@@ -166,13 +166,15 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a
     }
 }
 
-impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesHeight<'a>> BinaryTreesHeight<'a>
-    for BoundTrees<BT>
-{
+impl<BT: TreesHeight> TreesHeight for BoundTrees<BT> {
     fn height(&self, tree: &Self::Tree) -> u64 {
         self.0.height(&tree.bound)
     }
+}
 
+impl<'a, BT: BinaryTreesBindable<'a> + TreesHeightError<'a>> TreesHeightError<'a>
+    for BoundTrees<BT>
+{
     fn height_error<T: 'a + Send>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
         self.0.height_error(error)
     }
@@ -207,7 +209,7 @@ impl<
         BT: BinaryTreesBindable<'a>
             + BinaryTreesTreeOf<'a>
             + BinaryTreesEmpty<'a>
-            + BinaryTreesHeight<'a>
+            + TreesHeightError<'a>
             + BinaryTreesTryJoin<'a>,
     > BinaryTreesMutable<'a> for BoundTrees<BT>
 {
diff --git a/src/mrds/trees/heighted.rs b/src/mrds/trees/heighted.rs
index a22d126..bce7986 100644
--- a/src/mrds/trees/heighted.rs
+++ b/src/mrds/trees/heighted.rs
@@ -92,10 +92,6 @@ impl<A: Send + Sync + Ord + Clone> BinaryTrees for Trees<A> {
     type Tree = Tree<A>;
     type Key = A;
     type Comparator = DefaultComparator;
-}
-
-impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
-    type _Tm = Self::T;
 
     fn comparator(&self) -> &Self::Comparator {
         &DefaultComparator
@@ -105,10 +101,6 @@ impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
         (node.l.clone(), node.r.clone(), node.key.clone())
     }
 
-    fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
-        reference.node.as_ref().clone()
-    }
-
     fn equal(&self, _rhs: &Self::Reference, _lhs: &Self::Reference) -> bool {
         false
     }
@@ -118,11 +110,21 @@ impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
     }
 }
 
-impl<'a, A: 'a + Send + Sync + Ord + Clone> BinaryTreesHeight<'a> for Trees<A> {
+impl<'a, A: 'a + Send + Sync + Ord + Clone> MonadTrees<'a> for Trees<A> {
+    type _Tm = Self::T;
+
+    fn resolve(&self, reference: &Self::Reference) -> BTWrap<'a, Self, Self::Node> {
+        reference.node.as_ref().clone()
+    }
+}
+
+impl<A: Send + Sync + Ord + Clone> TreesHeight for Trees<A> {
     fn height(&self, tree: &Self::Tree) -> u64 {
         tree.height
     }
+}
 
+impl<'a, A: 'a + Send + Sync + Ord + Clone> TreesHeightError<'a> for Trees<A> {
     fn height_error<T: 'a + Send>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
         panic!("{error}")
     }
diff --git a/src/rstd/collections/tree/context.rs b/src/rstd/collections/tree/context.rs
index c2a5b6b..d7653bd 100644
--- a/src/rstd/collections/tree/context.rs
+++ b/src/rstd/collections/tree/context.rs
@@ -82,6 +82,22 @@ impl<
     type Tree = Tree<'a, Ctx, A>;
     type Key = A;
     type Comparator = C;
+
+    fn comparator(&self) -> &Self::Comparator {
+        self.0 .0.as_ref()
+    }
+
+    fn split(&self, node: &Self::Node) -> crate::flow::binary::Split<Self> {
+        (node.l.clone(), node.r.clone(), node.key.clone())
+    }
+
+    fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool {
+        rl == rr
+    }
+
+    fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
+        tree.node.as_ref().cloned()
+    }
 }
 
 impl<
@@ -94,14 +110,6 @@ impl<
 {
     type _Tm = Self::T;
 
-    fn comparator(&self) -> &Self::Comparator {
-        self.0 .0.as_ref()
-    }
-
-    fn split(&self, node: &Self::Node) -> crate::flow::binary::Split<Self> {
-        (node.l.clone(), node.r.clone(), node.key.clone())
-    }
-
     fn resolve(
         &self,
         reference: &Self::Reference,
@@ -112,14 +120,6 @@ impl<
                 .map_err(TreeContextError::Resolution)
         }))
     }
-
-    fn equal(&self, rl: &Self::Reference, rr: &Self::Reference) -> bool {
-        rl == rr
-    }
-
-    fn refer(&self, tree: &Self::Tree) -> Option<Self::Reference> {
-        tree.node.as_ref().cloned()
-    }
 }
 
 impl<
@@ -155,12 +155,21 @@ impl<
         A: Mentionable<'a, Ctx> + Clone,
         C: 'a + Comparator<A>,
         E: 'a + Send,
-    > BinaryTreesHeight<'a> for TreeContext2<'a, Ctx, A, C, E>
+    > TreesHeight for TreeContext2<'a, Ctx, A, C, E>
 {
     fn height(&self, tree: &Self::Tree) -> u64 {
         tree.height
     }
+}
 
+impl<
+        'a,
+        Ctx: Context<'a>,
+        A: Mentionable<'a, Ctx> + Clone,
+        C: 'a + Comparator<A>,
+        E: 'a + Send,
+    > TreesHeightError<'a> for TreeContext2<'a, Ctx, A, C, E>
+{
     fn height_error<T: 'a + Send>(&self, error: HeightError) -> BTWrap<'a, Self, T> {
         Self::fail(error.into())
     }