diff --git a/src/core/rope.rs b/src/core/rope.rs index 3f9e5fb..1b313f9 100644 --- a/src/core/rope.rs +++ b/src/core/rope.rs @@ -43,21 +43,6 @@ impl Rope { 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, right: Option>) -> Rc { - 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. pub fn total_bytes(&self) -> usize { match self { @@ -109,6 +94,10 @@ impl Rope { } } + pub fn concat(first: Rc, second: Rc) -> Rc { + Rope::join(first, Some(second)).rebalance() + } + /// Split the rope in two at character `index`. /// /// 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 { Rope::Branch { bytes_weight, .. } => fibbonacci(self.depth() + 2) <= *bytes_weight, Rope::Leaf { .. } => true, } } - fn rebalance(self: Rc) -> Rc { + pub fn rebalance(self: Rc) -> Rc { if self.is_balanced() { return self; } @@ -167,7 +156,7 @@ impl Rope { 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 /// 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) -> NodeIterator { - NodeIterator::new(self) - } - /// Returns an iterator over the chars of the text pub fn iter_chars(self: &Rc) -> CharIterator { 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, right: Option>) -> Rc { + 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) -> NodeIterator { + NodeIterator::new(self) + } } fn merge(leaf_nodes: &[Rc]) -> Rc {