Fixed bug in Rope::new_from_str().

The stragglers appended at the end were being appended at the top of
the tree, making it very unbalanced.  Using proper appending now.
This commit is contained in:
Nathan Vegdahl 2015-02-19 22:52:21 -08:00
parent 2df1302723
commit fe0a7e48ca

View File

@ -93,15 +93,10 @@ impl Rope {
} }
else { else {
while rope_stack.len() > 1 { while rope_stack.len() > 1 {
let right = Box::new(rope_stack.pop().unwrap()); let right = rope_stack.pop().unwrap();
let left = Box::new(rope_stack.pop().unwrap()); let mut left = rope_stack.pop().unwrap();
let h = max(left.tree_height, right.tree_height) + 1; left.append_right(right);
let gc = left.grapheme_count_ + right.grapheme_count_; rope_stack.push(left);
rope_stack.push(Rope {
data: RopeData::Branch(left, right),
grapheme_count_: gc,
tree_height: h,
});
} }
rope_stack.pop().unwrap() rope_stack.pop().unwrap()
}; };
@ -1367,9 +1362,9 @@ mod tests {
#[test] #[test]
fn rebalance_1() { fn rebalance_1() {
let mut rope1 = Rope::new_from_str(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE]).unwrap().as_slice()); let mut rope1 = Rope::new_from_str(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE]).unwrap().as_slice());
rope1.insert_text_at_grapheme_index(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 1024]).unwrap().as_slice(), MAX_NODE_SIZE); rope1.insert_text_at_grapheme_index(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 63]).unwrap().as_slice(), MAX_NODE_SIZE);
let mut rope2 = Rope::new_from_str(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 1025]).unwrap().as_slice()); let mut rope2 = Rope::new_from_str(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 64]).unwrap().as_slice());
assert_eq!(rope1.tree_height, rope2.tree_height); assert_eq!(rope1.tree_height, rope2.tree_height);
} }
@ -1378,9 +1373,9 @@ mod tests {
#[test] #[test]
fn rebalance_2() { fn rebalance_2() {
let mut rope1 = Rope::new_from_str(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 2]).unwrap().as_slice()); let mut rope1 = Rope::new_from_str(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 2]).unwrap().as_slice());
rope1.insert_text_at_grapheme_index(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 64]).unwrap().as_slice(), MAX_NODE_SIZE); rope1.insert_text_at_grapheme_index(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 63]).unwrap().as_slice(), MAX_NODE_SIZE);
let mut rope2 = Rope::new_from_str(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 66]).unwrap().as_slice()); let mut rope2 = Rope::new_from_str(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 65]).unwrap().as_slice());
//let mut f1 = BufferedWriter::new(File::create(&Path::new("yar1.gv")).unwrap()); //let mut f1 = BufferedWriter::new(File::create(&Path::new("yar1.gv")).unwrap());
//let mut f2 = BufferedWriter::new(File::create(&Path::new("yar2.gv")).unwrap()); //let mut f2 = BufferedWriter::new(File::create(&Path::new("yar2.gv")).unwrap());