aboutsummaryrefslogtreecommitdiffstats
path: root/src/rope.rs
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-01-12 23:31:55 -0800
committerRose Hogenson <rosehogenson@posteo.net>2024-01-12 23:31:55 -0800
commit46a0812ad03559178dc3b2f16dab9c6b66b4eae1 (patch)
treede7727a64fad3a3d3f114b08e3defdf0e0da51a3 /src/rope.rs
parentdfed842bcafd2d2df462a79cbd072ed443155a45 (diff)
downloadeditor-46a0812ad03559178dc3b2f16dab9c6b66b4eae1.tar.zst
Introduce new helper functions for creating ropes.
Diffstat (limited to 'src/rope.rs')
-rw-r--r--src/rope.rs59
1 files changed, 27 insertions, 32 deletions
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),
}
}