From b0aaa173bdd2137d7cdd7aa55f5aa7d7222b4c03 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Wed, 27 Dec 2023 08:45:36 -0800 Subject: Remove unnecessary length field. --- src/rope.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/rope.rs b/src/rope.rs index f26457c..320cd14 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -8,7 +8,6 @@ use std::rc::Rc; const MAX_NODE_SIZE: usize = 4; struct Branch { - len: usize, left: Rc, right: Rc, } @@ -19,13 +18,6 @@ enum Node { } impl Node { - fn len(&self) -> usize { - return match self { - Node::Leaf(v) => v.len(), - Node::Branch(b) => b.len, - }; - } - fn write(&self, out: &mut Vec) { match self { Node::Leaf(v) => { @@ -37,6 +29,13 @@ impl Node { } } } + + fn empty(&self) -> bool { + return match self { + Node::Leaf(v) => v.len() == 0, + _ => false, + }; + } } impl Display for Node { @@ -52,14 +51,13 @@ pub struct Rope(Rc); impl Rope { fn concat(self, Rope(other): Rope) -> Rope { let Rope(me) = self; - if me.len() == 0 { + if me.empty() { return Rope(other); } - if other.len() == 0 { + if other.empty() { return Rope(me); } return Rope(Rc::new(Node::Branch(Branch { - len: me.len() + other.len(), left: me, right: other, }))); -- cgit v1.3.1