Improved algorithm of Rope::append()
It doesn't use an intermediate conversion to a string anymore, and should execute in O(log N) as long as the right-hand rope is equal length or smaller than the left. It may also be O(log N) for the left-hand rope being smaller, but that depends on whether the rebalance code executes in O(log N) time when the left and right hand side are individually balanced. I haven't analysed it yet, so dunno.
This commit is contained in:
parent
d26c95ab1d
commit
535f219c41
|
@ -200,11 +200,17 @@ impl Rope {
|
||||||
|
|
||||||
/// Appends another rope to the end of this one, consuming the other rope.
|
/// Appends another rope to the end of this one, consuming the other rope.
|
||||||
pub fn append(&mut self, rope: Rope) {
|
pub fn append(&mut self, rope: Rope) {
|
||||||
// TODO: make more efficient. Converting to a string and then
|
if self.tree_height <= rope.tree_height || self.is_leaf() {
|
||||||
// inserting is pretty slow...
|
let mut temp_rope = Box::new(Rope::new());
|
||||||
let s = rope.to_string();
|
mem::swap(self, &mut (*temp_rope));
|
||||||
let gc = self.grapheme_count();
|
self.data = RopeData::Branch(temp_rope, Box::new(rope));
|
||||||
self.insert_text_at_grapheme_index(s.as_slice(), gc);
|
}
|
||||||
|
else if let RopeData::Branch(_, ref mut right) = self.data {
|
||||||
|
right.append(rope);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.update_stats();
|
||||||
|
self.rebalance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Makes a copy of the rope as a string
|
/// Makes a copy of the rope as a string
|
||||||
|
|
Loading…
Reference in New Issue
Block a user