From 46a0812ad03559178dc3b2f16dab9c6b66b4eae1 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 12 Jan 2024 23:31:55 -0800 Subject: Introduce new helper functions for creating ropes. --- src/rope.rs | 59 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) (limited to 'src/rope.rs') diff --git a/src/rope.rs b/src/rope.rs index 1eda553..3d72833 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -87,15 +87,21 @@ impl Rope { })) } - fn branch(children: Rc<[Rope]>) -> Rope { - let mut len = 0; - let mut lines = 0; - for c in children.iter() { - len += c.len(); - lines += c.lines(); - } + fn two(x: Rope, y: Rope) -> Rope { + let len = x.len() + y.len(); + let lines = x.lines() + y.lines(); Rope(Node::Branch(Branch { - children, + children: Rc::new([x, y]), + len, + lines, + })) + } + + fn three(x: Rope, y: Rope, z: Rope) -> Rope { + let len = x.len() + y.len() + z.len(); + let lines = x.lines() + y.lines() + z.lines(); + Rope(Node::Branch(Branch { + children: Rc::new([x, y, z]), len, lines, })) @@ -134,28 +140,21 @@ impl Rope { ) { Insert::Node(new_child) => { if b.children.len() == 2 { - return Insert::Node(Rope::branch(Rc::new([ - b.children[0].clone(), - new_child, - ]))); + return Insert::Node(Rope::two(b.children[0].clone(), new_child)); } - return Insert::Node(Rope::branch(Rc::new([ + return Insert::Node(Rope::three( b.children[0].clone(), b.children[1].clone(), new_child, - ]))); + )); } Insert::Split(child1, child2) => { if b.children.len() == 2 { - return Insert::Node(Rope::branch(Rc::new([ - b.children[0].clone(), - child1, - child2, - ]))); + return Insert::Node(Rope::three(b.children[0].clone(), child1, child2)); } return Insert::Split( - Rope::branch(Rc::new([b.children[0].clone(), b.children[1].clone()])), - Rope::branch(Rc::new([child1, child2])), + Rope::two(b.children[0].clone(), b.children[1].clone()), + Rope::two(child1, child2), ); } } @@ -166,25 +165,21 @@ impl Rope { match self.concat_height(self_height, &b.children[0], other_height - 1) { Insert::Node(new_child) => { if b.children.len() == 2 { - return Insert::Node(Rope::branch(Rc::new([new_child, b.children[1].clone()]))); + return Insert::Node(Rope::two(new_child, b.children[1].clone())); } - Insert::Node(Rope::branch(Rc::new([ + Insert::Node(Rope::three( new_child, b.children[1].clone(), b.children[2].clone(), - ]))) + )) } Insert::Split(child1, child2) => { if b.children.len() == 2 { - return Insert::Node(Rope::branch(Rc::new([ - child1, - child2, - b.children[1].clone(), - ]))); + return Insert::Node(Rope::three(child1, child2, b.children[1].clone())); } Insert::Split( - Rope::branch(Rc::new([child1, child2])), - Rope::branch(Rc::new([b.children[1].clone(), b.children[2].clone()])), + Rope::two(child1, child2), + Rope::two(b.children[1].clone(), b.children[2].clone()), ) } } @@ -206,7 +201,7 @@ impl Rope { } match self.concat_height(self.height(), other, other.height()) { Insert::Node(r) => r, - Insert::Split(l, r) => Rope::branch(Rc::new([l, r])), + Insert::Split(l, r) => Rope::two(l, r), } } -- cgit v1.3.1