Rearrange rope.rs a little

This commit is contained in:
Matthew Gordon 2024-10-19 21:22:02 -03:00
parent 511f56872a
commit 1164922ffe
1 changed files with 27 additions and 23 deletions

View File

@ -43,21 +43,6 @@ impl Rope {
Rc::new(Rope::Leaf { text }) Rc::new(Rope::Leaf { text })
} }
/// Concatenate two Ropes without rebalancing.
///
/// Combines to ropes by creating a new parent node and adding `left` and
/// `right` as children. If `right` is [None] then the resulting node will
/// only have one child.
fn join(left: Rc<Rope>, right: Option<Rc<Rope>>) -> Rc<Self> {
Rc::new(Rope::Branch {
bytes_weight: left.total_bytes(),
chars_weight: left.total_chars(),
lines_weight: left.total_lines(),
left,
right,
})
}
/// Return the total number of bytes in the text. /// Return the total number of bytes in the text.
pub fn total_bytes(&self) -> usize { pub fn total_bytes(&self) -> usize {
match self { match self {
@ -109,6 +94,10 @@ impl Rope {
} }
} }
pub fn concat(first: Rc<Rope>, second: Rc<Rope>) -> Rc<Rope> {
Rope::join(first, Some(second)).rebalance()
}
/// Split the rope in two at character `index`. /// Split the rope in two at character `index`.
/// ///
/// The result is two Ropes—one containing the first 'i' characters of the /// The result is two Ropes—one containing the first 'i' characters of the
@ -152,14 +141,14 @@ impl Rope {
} }
} }
fn is_balanced(&self) -> bool { pub fn is_balanced(&self) -> bool {
match self { match self {
Rope::Branch { bytes_weight, .. } => fibbonacci(self.depth() + 2) <= *bytes_weight, Rope::Branch { bytes_weight, .. } => fibbonacci(self.depth() + 2) <= *bytes_weight,
Rope::Leaf { .. } => true, Rope::Leaf { .. } => true,
} }
} }
fn rebalance(self: Rc<Rope>) -> Rc<Rope> { pub fn rebalance(self: Rc<Rope>) -> Rc<Rope> {
if self.is_balanced() { if self.is_balanced() {
return self; return self;
} }
@ -167,7 +156,7 @@ impl Rope {
merge(&leaf_nodes) merge(&leaf_nodes)
} }
/// Number of steps between this node and it's most distance descendant /// Number of steps between this node and its most distance descendant
/// ///
/// Leaf nodes have a depth of 0 and each branch node has a depth one /// Leaf nodes have a depth of 0 and each branch node has a depth one
/// greater than the depth of deepest child. /// greater than the depth of deepest child.
@ -190,15 +179,30 @@ impl Rope {
} }
} }
/// Returns an iterater over the leaf nodes
fn iter_nodes(self: Rc<Self>) -> NodeIterator {
NodeIterator::new(self)
}
/// Returns an iterator over the chars of the text /// Returns an iterator over the chars of the text
pub fn iter_chars(self: &Rc<Self>) -> CharIterator { pub fn iter_chars(self: &Rc<Self>) -> CharIterator {
CharIterator::new(self) CharIterator::new(self)
} }
/// Concatenate two Ropes without rebalancing.
///
/// Combines to ropes by creating a new parent node and adding `left` and
/// `right` as children. If `right` is [None] then the resulting node will
/// only have one child.
fn join(left: Rc<Rope>, right: Option<Rc<Rope>>) -> Rc<Self> {
Rc::new(Rope::Branch {
bytes_weight: left.total_bytes(),
chars_weight: left.total_chars(),
lines_weight: left.total_lines(),
left,
right,
})
}
/// Returns an iterater over the leaf nodes
fn iter_nodes(self: Rc<Self>) -> NodeIterator {
NodeIterator::new(self)
}
} }
fn merge(leaf_nodes: &[Rc<Rope>]) -> Rc<Rope> { fn merge(leaf_nodes: &[Rc<Rope>]) -> Rc<Rope> {