Rearrange rope.rs a little
This commit is contained in:
parent
511f56872a
commit
1164922ffe
|
|
@ -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<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.
|
||||
pub fn total_bytes(&self) -> usize {
|
||||
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`.
|
||||
///
|
||||
/// 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<Rope>) -> Rc<Rope> {
|
||||
pub fn rebalance(self: Rc<Rope>) -> Rc<Rope> {
|
||||
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<Self>) -> NodeIterator {
|
||||
NodeIterator::new(self)
|
||||
}
|
||||
|
||||
/// Returns an iterator over the chars of the text
|
||||
pub fn iter_chars(self: &Rc<Self>) -> 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<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> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue