AVL refactor

This commit is contained in:
AF 2023-06-20 07:23:05 +00:00
parent de26092abc
commit 479d011123
2 changed files with 65 additions and 28 deletions

View File

@ -29,9 +29,11 @@ def main():
),
reverse=True,
)
t = 0
for entry in all_entries:
# print(*entry["metrics"])
print(entry["name"], entry["metrics"]["halstead"])
if entry["kind"] == "unit":
t += entry["metrics"]["loc"]["ploc"]
print(t)
main()

View File

@ -1,5 +1,45 @@
use super::*;
trait IntoTree<'a, Trees: BinaryTrees<'a>> {
fn into_tree(self, trees: &Trees) -> BTWrap<'a, Trees, Trees::Tree>;
}
struct T<T>(T);
impl<'a, Trees: BinaryTrees<'a>> IntoTree<'a, Trees> for T<Trees::Tree> {
fn into_tree(self, _trees: &Trees) -> BTWrap<'a, Trees, Trees::Tree> {
Trees::pure(self.0)
}
}
impl<'a, Trees: BinaryTreesAvl<'a>, L: IntoTree<'a, Trees>, R: IntoTree<'a, Trees>>
IntoTree<'a, Trees> for (L, Trees::Key, R)
{
fn into_tree(self, trees: &Trees) -> BTWrap<'a, Trees, Trees::Tree> {
let trees = trees.clone();
Trees::T::bind2(
self.0.into_tree(&trees),
self.2.into_tree(&trees),
move |tl, tr| trees.join_key_balanced_tree(tl, self.1, tr),
)
}
}
trait BinaryTreesAvlExt<'a>: BinaryTreesAvl<'a> {
fn make_node(
self,
itl: impl IntoTree<'a, Self>,
key: Self::Key,
itr: impl IntoTree<'a, Self>,
) -> BTWrap<'a, Self, Self::Node> {
Self::T::bind2(itl.into_tree(&self), itr.into_tree(&self), |tl, tr| {
self.join_key_balanced(tl, key, tr)
})
}
}
impl<'a, Trees: BinaryTreesAvl<'a>> BinaryTreesAvlExt<'a> for Trees {}
pub trait BinaryTreesAvl<'a>:
BinaryTreesHeight<'a> + BinaryTreesTreeOf<'a> + BinaryTreesTryJoin<'a>
{
@ -10,6 +50,17 @@ pub trait BinaryTreesAvl<'a>:
}
}
fn assume_bind<T>(
self,
tree: &Self::Tree,
f: impl 'a + FnOnce(Self, Self::Tree, Self::Key, Self::Tree) -> BTWrap<'a, Self, T>,
) -> BTWrap<'a, Self, T> {
Self::bind(self.assume_node(tree), move |node| {
let (tl, tr, key) = self.split(&node);
f(self, tl, key, tr)
})
}
fn join_key_balanced_tree(
&self,
tl: Self::Tree,
@ -29,40 +80,24 @@ pub trait BinaryTreesAvl<'a>:
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.try_join(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));
(0, _) => self.assume_bind(&tr, move |ctx, trl, kr, trr| {
let (rlh, rrh) = (ctx.height(&trl), ctx.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),
)
ctx.assume_bind(&trl, move |ctx, trll, krl, trlr| {
ctx.make_node((T(tl), key, T(trll)), krl, (T(trlr), kr, T(trr)))
})
} else {
Self::bind(self.join_key_balanced_tree(tl, key, trl), |t| {
self.join_key_balanced(t, kr, trr)
})
ctx.make_node((T(tl), key, T(trl)), kr, T(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));
(_, 0) => self.assume_bind(&tl, move |ctx, tll, kl, tlr| {
let (hll, hlr) = (ctx.height(&tll), ctx.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),
)
ctx.assume_bind(&tlr, move |ctx, tlrl, klr, tlrr| {
ctx.make_node((T(tll), kl, T(tlrl)), klr, (T(tlrr), key, T(tr)))
})
} else {
Self::bind(self.join_key_balanced_tree(tlr, key, tr), |t| {
self.join_key_balanced(tll, kl, t)
})
ctx.make_node(T(tll), kl, (T(tlr), key, T(tr)))
}
}),
(_, _) => unreachable!(),