2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/*!
|
|
|
|
* High-level text containers.
|
|
|
|
*
|
|
|
|
* Ropes are a high-level representation of text that offers
|
|
|
|
* much better performance than strings for common operations,
|
|
|
|
* and generally reduce memory allocations and copies, while only
|
|
|
|
* entailing a small degradation of less common operations.
|
|
|
|
*
|
|
|
|
* More precisely, where a string is represented as a memory buffer,
|
|
|
|
* a rope is a tree structure whose leaves are slices of immutable
|
|
|
|
* strings. Therefore, concatenation, appending, prepending, substrings,
|
|
|
|
* etc. are operations that require only trivial tree manipulation,
|
|
|
|
* generally without having to copy memory. In addition, the tree
|
|
|
|
* structure of ropes makes them suitable as a form of index to speed-up
|
|
|
|
* access to Unicode characters by index in long chunks of text.
|
|
|
|
*
|
|
|
|
* The following operations are algorithmically faster in ropes:
|
|
|
|
*
|
|
|
|
* * extracting a subrope is logarithmic (linear in strings);
|
|
|
|
* * appending/prepending is near-constant time (linear in strings);
|
|
|
|
* * concatenation is near-constant time (linear in strings);
|
|
|
|
* * char length is constant-time (linear in strings);
|
|
|
|
* * access to a character by index is logarithmic (linear in strings);
|
|
|
|
*/
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::option;
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
use core::uint;
|
|
|
|
use core::vec;
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// The type of ropes.
|
2012-10-01 16:01:42 -05:00
|
|
|
pub type Rope = node::Root;
|
2011-11-04 12:38:42 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Creating a rope
|
|
|
|
*/
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create an empty rope
|
2012-12-23 16:38:01 -06:00
|
|
|
pub pure fn empty() -> Rope {
|
2012-09-02 19:32:06 -05:00
|
|
|
return node::Empty;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Adopt a string as a rope.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * str - A valid string.
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A rope representing the same string as `str`. Depending of the length
|
|
|
|
* of `str`, this rope may be empty, flat or complex.
|
|
|
|
*
|
|
|
|
* # Performance notes
|
|
|
|
*
|
|
|
|
* * this operation does not copy the string;
|
|
|
|
* * the function runs in linear time.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn of_str(str: @~str) -> Rope {
|
2012-08-01 19:30:05 -05:00
|
|
|
return of_substr(str, 0u, str::len(*str));
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* As `of_str` but for a substring.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
* * byte_offset - The offset of `str` at which the rope starts.
|
|
|
|
* * byte_len - The number of bytes of `str` to use.
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A rope representing the same string as `str::substr(str, byte_offset,
|
|
|
|
* byte_len)`. Depending on `byte_len`, this rope may be empty, flat or
|
|
|
|
* complex.
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
*
|
|
|
|
* This operation does not copy the substring.
|
|
|
|
*
|
|
|
|
* # Safety notes
|
|
|
|
*
|
|
|
|
* * this function does _not_ check the validity of the substring;
|
|
|
|
* * this function fails if `byte_offset` or `byte_len` do not match `str`.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn of_substr(str: @~str, byte_offset: uint, byte_len: uint) -> Rope {
|
2012-09-02 19:32:06 -05:00
|
|
|
if byte_len == 0u { return node::Empty; }
|
2013-02-11 21:26:38 -06:00
|
|
|
if byte_offset + byte_len > str::len(*str) { fail!(); }
|
2012-09-02 19:32:06 -05:00
|
|
|
return node::Content(node::of_substr(str, byte_offset, byte_len));
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Adding things to a rope
|
|
|
|
*/
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Add one char to the end of the rope
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
*
|
|
|
|
* * this function executes in near-constant time
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn append_char(rope: Rope, char: char) -> Rope {
|
2012-08-01 19:30:05 -05:00
|
|
|
return append_str(rope, @str::from_chars(~[char]));
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Add one string to the end of the rope
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
*
|
|
|
|
* * this function executes in near-linear time
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn append_str(rope: Rope, str: @~str) -> Rope {
|
2012-08-01 19:30:05 -05:00
|
|
|
return append_rope(rope, of_str(str))
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Add one char to the beginning of the rope
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
* * this function executes in near-constant time
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn prepend_char(rope: Rope, char: char) -> Rope {
|
2012-08-01 19:30:05 -05:00
|
|
|
return prepend_str(rope, @str::from_chars(~[char]));
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Add one string to the beginning of the rope
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
* * this function executes in near-linear time
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn prepend_str(rope: Rope, str: @~str) -> Rope {
|
2012-08-01 19:30:05 -05:00
|
|
|
return append_rope(of_str(str), rope)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Concatenate two ropes
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn append_rope(left: Rope, right: Rope) -> Rope {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (left) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return right,
|
|
|
|
node::Content(left_content) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (right) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return left,
|
|
|
|
node::Content(right_content) => {
|
|
|
|
return node::Content(node::concat2(left_content, right_content));
|
2012-08-03 21:59:04 -05:00
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Concatenate many ropes.
|
|
|
|
*
|
|
|
|
* If the ropes are balanced initially and have the same height, the resulting
|
|
|
|
* rope remains balanced. However, this function does not take any further
|
|
|
|
* measure to ensure that the result is balanced.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn concat(v: ~[Rope]) -> Rope {
|
2012-03-26 20:35:18 -05:00
|
|
|
//Copy `v` into a mut vector
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut len = vec::len(v);
|
2012-09-02 19:32:06 -05:00
|
|
|
if len == 0u { return node::Empty; }
|
2013-02-12 17:34:48 -06:00
|
|
|
let mut ropes = vec::from_elem(len, v[0]);
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(1u, len) |i| {
|
2011-11-06 10:27:08 -06:00
|
|
|
ropes[i] = v[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Merge progresively
|
|
|
|
while len > 1u {
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, len/2u) |i| {
|
2011-11-06 10:27:08 -06:00
|
|
|
ropes[i] = append_rope(ropes[2u*i], ropes[2u*i+1u]);
|
|
|
|
}
|
|
|
|
if len%2u != 0u {
|
|
|
|
ropes[len/2u] = ropes[len - 1u];
|
|
|
|
len = len/2u + 1u;
|
|
|
|
} else {
|
|
|
|
len = len/2u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Return final rope
|
2012-08-01 19:30:05 -05:00
|
|
|
return ropes[0];
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Keeping ropes healthy
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Balance a rope.
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A copy of the rope in which small nodes have been grouped in memory,
|
|
|
|
* and with a reduced height.
|
|
|
|
*
|
|
|
|
* If you perform numerous rope concatenations, it is generally a good idea
|
|
|
|
* to rebalance your rope at some point, before using it for other purposes.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn bal(rope:Rope) -> Rope {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return rope,
|
|
|
|
node::Content(x) => match (node::bal(x)) {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => rope,
|
2012-09-02 19:32:06 -05:00
|
|
|
option::Some(y) => node::Content(y)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Transforming ropes
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Extract a subrope from a rope.
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
*
|
|
|
|
* * on a balanced rope, this operation takes algorithmic time;
|
|
|
|
* * this operation does not involve any copying
|
|
|
|
*
|
|
|
|
* # Safety note
|
|
|
|
*
|
|
|
|
* * this function fails if char_offset/char_len do not represent
|
|
|
|
* valid positions in rope
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn sub_chars(rope: Rope, char_offset: uint, char_len: uint) -> Rope {
|
2012-09-02 19:32:06 -05:00
|
|
|
if char_len == 0u { return node::Empty; }
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2013-02-11 21:26:38 -06:00
|
|
|
node::Empty => fail!(),
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Content(node) => if char_len > node::char_len(node) {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!()
|
2012-08-03 21:59:04 -05:00
|
|
|
} else {
|
2012-09-02 19:32:06 -05:00
|
|
|
return node::Content(node::sub_chars(node, char_offset, char_len))
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Extract a subrope from a rope.
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
*
|
|
|
|
* * on a balanced rope, this operation takes algorithmic time;
|
|
|
|
* * this operation does not involve any copying
|
|
|
|
*
|
|
|
|
* # Safety note
|
|
|
|
*
|
|
|
|
* * this function fails if byte_offset/byte_len do not represent
|
|
|
|
* valid positions in rope
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn sub_bytes(rope: Rope, byte_offset: uint, byte_len: uint) -> Rope {
|
2012-09-02 19:32:06 -05:00
|
|
|
if byte_len == 0u { return node::Empty; }
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2013-02-11 21:26:38 -06:00
|
|
|
node::Empty => fail!(),
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Content(node) =>if byte_len > node::byte_len(node) {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!()
|
2012-08-03 21:59:04 -05:00
|
|
|
} else {
|
2012-09-02 19:32:06 -05:00
|
|
|
return node::Content(node::sub_bytes(node, byte_offset, byte_len))
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Comparing ropes
|
|
|
|
*/
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Compare two ropes by Unicode lexicographical order.
|
|
|
|
*
|
|
|
|
* This function compares only the contents of the rope, not their structure.
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A negative value if `left < right`, 0 if eq(left, right) or a positive
|
|
|
|
* value if `left > right`
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn cmp(left: Rope, right: Rope) -> int {
|
2012-08-06 14:34:08 -05:00
|
|
|
match ((left, right)) {
|
2012-09-02 19:32:06 -05:00
|
|
|
(node::Empty, node::Empty) => return 0,
|
|
|
|
(node::Empty, _) => return -1,
|
|
|
|
(_, node::Empty) => return 1,
|
|
|
|
(node::Content(a), node::Content(b)) => {
|
2012-08-01 19:30:05 -05:00
|
|
|
return node::cmp(a, b);
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Returns `true` if both ropes have the same content (regardless of
|
|
|
|
* their structure), `false` otherwise
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn eq(left: Rope, right: Rope) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return cmp(left, right) == 0;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * left - an arbitrary rope
|
|
|
|
* * right - an arbitrary rope
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` if `left <= right` in lexicographical order (regardless of their
|
|
|
|
* structure), `false` otherwise
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn le(left: Rope, right: Rope) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return cmp(left, right) <= 0;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * left - an arbitrary rope
|
|
|
|
* * right - an arbitrary rope
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` if `left < right` in lexicographical order (regardless of their
|
|
|
|
* structure), `false` otherwise
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn lt(left: Rope, right: Rope) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return cmp(left, right) < 0;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * left - an arbitrary rope
|
|
|
|
* * right - an arbitrary rope
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` if `left >= right` in lexicographical order (regardless of their
|
|
|
|
* structure), `false` otherwise
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn ge(left: Rope, right: Rope) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return cmp(left, right) >= 0;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * left - an arbitrary rope
|
|
|
|
* * right - an arbitrary rope
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` if `left > right` in lexicographical order (regardless of their
|
|
|
|
* structure), `false` otherwise
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn gt(left: Rope, right: Rope) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return cmp(left, right) > 0;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Iterating
|
|
|
|
*/
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Loop through a rope, char by char
|
|
|
|
*
|
|
|
|
* While other mechanisms are available, this is generally the best manner
|
|
|
|
* of looping through the contents of a rope char by char. If you prefer a
|
|
|
|
* loop that iterates through the contents string by string (e.g. to print
|
|
|
|
* the contents of the rope or output it to the system), however,
|
|
|
|
* you should rather use `traverse_components`.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * rope - A rope to traverse. It may be empty.
|
|
|
|
* * it - A block to execute with each consecutive character of the rope.
|
|
|
|
* Return `true` to continue, `false` to stop.
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` If execution proceeded correctly, `false` if it was interrupted,
|
|
|
|
* that is if `it` returned `false` at any point.
|
|
|
|
*/
|
2012-10-03 14:21:48 -05:00
|
|
|
pub fn loop_chars(rope: Rope, it: fn(c: char) -> bool) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return true,
|
|
|
|
node::Content(x) => return node::loop_chars(x, it)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
2011-11-05 05:12:31 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Loop through a rope, char by char, until the end.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
* * rope - A rope to traverse. It may be empty
|
|
|
|
* * it - A block to execute with each consecutive character of the rope.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn iter_chars(rope: Rope, it: fn(char)) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do loop_chars(rope) |x| {
|
2011-11-04 12:38:42 -05:00
|
|
|
it(x);
|
2012-03-26 05:39:20 -05:00
|
|
|
true
|
2011-12-29 14:03:39 -06:00
|
|
|
};
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Loop through a rope, string by string
|
|
|
|
*
|
|
|
|
* While other mechanisms are available, this is generally the best manner of
|
|
|
|
* looping through the contents of a rope string by string, which may be
|
|
|
|
* useful e.g. to print strings as you see them (without having to copy their
|
|
|
|
* contents into a new string), to send them to then network, to write them to
|
|
|
|
* a file, etc.. If you prefer a loop that iterates through the contents
|
|
|
|
* char by char (e.g. to search for a char), however, you should rather
|
|
|
|
* use `traverse`.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * rope - A rope to traverse. It may be empty
|
|
|
|
* * it - A block to execute with each consecutive string component of the
|
|
|
|
* rope. Return `true` to continue, `false` to stop
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `true` If execution proceeded correctly, `false` if it was interrupted,
|
|
|
|
* that is if `it` returned `false` at any point.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn loop_leaves(rope: Rope, it: fn(node::Leaf) -> bool) -> bool{
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return true,
|
|
|
|
node::Content(x) => return node::loop_leaves(x, it)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub mod iterator {
|
|
|
|
pub mod leaf {
|
2013-01-08 21:37:25 -06:00
|
|
|
use rope::{Rope, node};
|
|
|
|
|
|
|
|
use core::prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2012-10-01 17:26:23 -05:00
|
|
|
pub fn start(rope: Rope) -> node::leaf_iterator::T {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return node::leaf_iterator::empty(),
|
|
|
|
node::Content(x) => return node::leaf_iterator::start(x)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-01 17:26:23 -05:00
|
|
|
pub fn next(it: &node::leaf_iterator::T) -> Option<node::Leaf> {
|
2012-08-01 19:30:05 -05:00
|
|
|
return node::leaf_iterator::next(it);
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-01 16:01:42 -05:00
|
|
|
pub mod char {
|
2013-01-08 21:37:25 -06:00
|
|
|
use rope::{Rope, node};
|
|
|
|
|
|
|
|
use core::prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2012-10-01 17:26:23 -05:00
|
|
|
pub fn start(rope: Rope) -> node::char_iterator::T {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return node::char_iterator::empty(),
|
|
|
|
node::Content(x) => return node::char_iterator::start(x)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-01 17:26:23 -05:00
|
|
|
pub fn next(it: &node::char_iterator::T) -> Option<char> {
|
2012-08-01 19:30:05 -05:00
|
|
|
return node::char_iterator::next(it)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Rope properties
|
|
|
|
*/
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Returns the height of the rope.
|
|
|
|
*
|
|
|
|
* The height of the rope is a bound on the number of operations which
|
|
|
|
* must be performed during a character access before finding the leaf in
|
|
|
|
* which a character is contained.
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
*
|
|
|
|
* Constant time.
|
|
|
|
*/
|
2012-12-23 16:38:01 -06:00
|
|
|
pub pure fn height(rope: Rope) -> uint {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return 0u,
|
|
|
|
node::Content(x) => return node::height(x)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The number of character in the rope
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
*
|
|
|
|
* Constant time.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub pure fn char_len(rope: Rope) -> uint {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return 0u,
|
|
|
|
node::Content(x) => return node::char_len(x)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The number of bytes in the rope
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
*
|
|
|
|
* Constant time.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub pure fn byte_len(rope: Rope) -> uint {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return 0u,
|
|
|
|
node::Content(x) => return node::byte_len(x)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The character at position `pos`
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * pos - A position in the rope
|
|
|
|
*
|
|
|
|
* # Safety notes
|
|
|
|
*
|
|
|
|
* The function will fail if `pos` is not a valid position in the rope.
|
|
|
|
*
|
|
|
|
* # Performance note
|
|
|
|
*
|
|
|
|
* This function executes in a time proportional to the height of the
|
|
|
|
* rope + the (bounded) length of the largest leaf.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn char_at(rope: Rope, pos: uint) -> char {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (rope) {
|
2013-02-11 21:26:38 -06:00
|
|
|
node::Empty => fail!(),
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Content(x) => return node::char_at(x, pos)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Implementation
|
|
|
|
*/
|
2012-12-23 16:41:37 -06:00
|
|
|
pub mod node {
|
|
|
|
use rope::node;
|
|
|
|
|
|
|
|
use core::cast;
|
|
|
|
use core::char;
|
|
|
|
use core::option;
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
use core::uint;
|
|
|
|
use core::vec;
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Implementation of type `rope`
|
2012-10-01 16:01:42 -05:00
|
|
|
pub enum Root {
|
2012-07-04 16:53:12 -05:00
|
|
|
/// An empty rope
|
2012-09-02 19:32:06 -05:00
|
|
|
Empty,
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A non-empty rope
|
2012-09-02 19:32:06 -05:00
|
|
|
Content(@Node),
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* A text component in a rope.
|
|
|
|
*
|
|
|
|
* This is actually a slice in a rope, so as to ensure maximal sharing.
|
|
|
|
*
|
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * byte_offset = The number of bytes skippen in `content`
|
|
|
|
* * byte_len - The number of bytes of `content` to use
|
|
|
|
* * char_len - The number of chars in the leaf.
|
|
|
|
* * content - Contents of the leaf.
|
|
|
|
*
|
|
|
|
* Note that we can have `char_len < str::char_len(content)`, if
|
|
|
|
* this leaf is only a subset of the string. Also note that the
|
|
|
|
* string can be shared between several ropes, e.g. for indexing
|
|
|
|
* purposes.
|
|
|
|
*/
|
2013-01-22 10:44:24 -06:00
|
|
|
pub struct Leaf {
|
2012-03-07 20:17:30 -06:00
|
|
|
byte_offset: uint,
|
2013-01-22 10:44:24 -06:00
|
|
|
byte_len: uint,
|
|
|
|
char_len: uint,
|
|
|
|
content: @~str,
|
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* A node obtained from the concatenation of two other nodes
|
|
|
|
*
|
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * left - The node containing the beginning of the text.
|
|
|
|
* * right - The node containing the end of the text.
|
|
|
|
* * char_len - The number of chars contained in all leaves of this node.
|
|
|
|
* * byte_len - The number of bytes in the subrope.
|
|
|
|
*
|
|
|
|
* Used to pre-allocate the correct amount of storage for
|
|
|
|
* serialization.
|
|
|
|
*
|
|
|
|
* * height - Height of the subrope.
|
|
|
|
*
|
|
|
|
* Used for rebalancing and to allocate stacks for traversals.
|
|
|
|
*/
|
2013-01-22 10:44:24 -06:00
|
|
|
pub struct Concat {
|
2012-07-05 17:58:25 -05:00
|
|
|
//FIXME (#2744): Perhaps a `vec` instead of `left`/`right`
|
2013-01-22 10:44:24 -06:00
|
|
|
left: @Node,
|
|
|
|
right: @Node,
|
2011-11-04 12:38:42 -05:00
|
|
|
char_len: uint,
|
|
|
|
byte_len: uint,
|
2013-01-22 10:44:24 -06:00
|
|
|
height: uint,
|
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub enum Node {
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A leaf consisting in a `str`
|
2012-09-02 19:32:06 -05:00
|
|
|
Leaf(Leaf),
|
2012-07-04 16:53:12 -05:00
|
|
|
/// The concatenation of two ropes
|
2012-09-02 19:32:06 -05:00
|
|
|
Concat(Concat),
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The maximal number of chars that _should_ be permitted in a single node
|
|
|
|
*
|
|
|
|
* This is not a strict value
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub const hint_max_leaf_char_len: uint = 256u;
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The maximal height that _should_ be permitted in a tree.
|
|
|
|
*
|
|
|
|
* This is not a strict value
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub const hint_max_node_height: uint = 16u;
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Adopt a string as a node.
|
|
|
|
*
|
|
|
|
* If the string is longer than `max_leaf_char_len`, it is
|
|
|
|
* logically split between as many leaves as necessary. Regardless,
|
|
|
|
* the string itself is not copied.
|
|
|
|
*
|
|
|
|
* Performance note: The complexity of this function is linear in
|
|
|
|
* the length of `str`.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn of_str(str: @~str) -> @Node {
|
2012-08-01 19:30:05 -05:00
|
|
|
return of_substr(str, 0u, str::len(*str));
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Adopt a slice of a string as a node.
|
|
|
|
*
|
|
|
|
* If the slice is longer than `max_leaf_char_len`, it is logically split
|
|
|
|
* between as many leaves as necessary. Regardless, the string itself
|
|
|
|
* is not copied
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * byte_start - The byte offset where the slice of `str` starts.
|
|
|
|
* * byte_len - The number of bytes from `str` to use.
|
|
|
|
*
|
|
|
|
* # Safety note
|
|
|
|
*
|
|
|
|
* Behavior is undefined if `byte_start` or `byte_len` do not represent
|
|
|
|
* valid positions in `str`
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn of_substr(str: @~str, byte_start: uint, byte_len: uint) -> @Node {
|
2012-08-01 19:30:05 -05:00
|
|
|
return of_substr_unsafer(str, byte_start, byte_len,
|
2012-02-23 09:59:30 -06:00
|
|
|
str::count_chars(*str, byte_start, byte_len));
|
2011-11-06 06:09:24 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Adopt a slice of a string as a node.
|
|
|
|
*
|
|
|
|
* If the slice is longer than `max_leaf_char_len`, it is logically split
|
|
|
|
* between as many leaves as necessary. Regardless, the string itself
|
|
|
|
* is not copied
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * byte_start - The byte offset where the slice of `str` starts.
|
|
|
|
* * byte_len - The number of bytes from `str` to use.
|
|
|
|
* * char_len - The number of chars in `str` in the interval
|
|
|
|
* [byte_start, byte_start+byte_len)
|
|
|
|
*
|
|
|
|
* # Safety notes
|
|
|
|
*
|
|
|
|
* * Behavior is undefined if `byte_start` or `byte_len` do not represent
|
|
|
|
* valid positions in `str`
|
|
|
|
* * Behavior is undefined if `char_len` does not accurately represent the
|
|
|
|
* number of chars between byte_start and byte_start+byte_len
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn of_substr_unsafer(str: @~str, byte_start: uint, byte_len: uint,
|
|
|
|
char_len: uint) -> @Node {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!((byte_start + byte_len <= str::len(*str)));
|
2013-01-22 10:44:24 -06:00
|
|
|
let candidate = @Leaf(Leaf {
|
|
|
|
byte_offset: byte_start,
|
|
|
|
byte_len: byte_len,
|
|
|
|
char_len: char_len,
|
|
|
|
content: str,
|
|
|
|
});
|
2011-11-04 12:38:42 -05:00
|
|
|
if char_len <= hint_max_leaf_char_len {
|
2012-08-01 19:30:05 -05:00
|
|
|
return candidate;
|
2011-11-04 12:38:42 -05:00
|
|
|
} else {
|
|
|
|
//Firstly, split `str` in slices of hint_max_leaf_char_len
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut leaves = uint::div_ceil(char_len, hint_max_leaf_char_len);
|
2011-11-04 12:38:42 -05:00
|
|
|
//Number of leaves
|
2013-02-12 17:34:48 -06:00
|
|
|
let mut nodes = vec::from_elem(leaves, candidate);
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i = 0u;
|
|
|
|
let mut offset = byte_start;
|
2011-11-04 12:38:42 -05:00
|
|
|
let first_leaf_char_len =
|
|
|
|
if char_len%hint_max_leaf_char_len == 0u {
|
|
|
|
hint_max_leaf_char_len
|
|
|
|
} else {
|
|
|
|
char_len%hint_max_leaf_char_len
|
|
|
|
};
|
|
|
|
while i < leaves {
|
|
|
|
let chunk_char_len: uint =
|
|
|
|
if i == 0u { first_leaf_char_len }
|
|
|
|
else { hint_max_leaf_char_len };
|
|
|
|
let chunk_byte_len =
|
2012-02-23 09:59:30 -06:00
|
|
|
str::count_bytes(*str, offset, chunk_char_len);
|
2013-01-22 10:44:24 -06:00
|
|
|
nodes[i] = @Leaf(Leaf {
|
2011-11-04 12:38:42 -05:00
|
|
|
byte_offset: offset,
|
2013-01-22 10:44:24 -06:00
|
|
|
byte_len: chunk_byte_len,
|
|
|
|
char_len: chunk_char_len,
|
|
|
|
content: str,
|
2011-11-04 12:38:42 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
offset += chunk_byte_len;
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Then, build a tree from these slices by collapsing them
|
|
|
|
while leaves > 1u {
|
|
|
|
i = 0u;
|
|
|
|
while i < leaves - 1u {//Concat nodes 0 with 1, 2 with 3 etc.
|
|
|
|
nodes[i/2u] = concat2(nodes[i], nodes[i + 1u]);
|
|
|
|
i += 2u;
|
|
|
|
}
|
|
|
|
if i == leaves - 1u {
|
|
|
|
//And don't forget the last node if it is in even position
|
|
|
|
nodes[i/2u] = nodes[i];
|
|
|
|
}
|
|
|
|
leaves = uint::div_ceil(leaves, 2u);
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return nodes[0u];
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub pure fn byte_len(node: @Node) -> uint {
|
2012-07-05 17:58:25 -05:00
|
|
|
//FIXME (#2744): Could we do this without the pattern-matching?
|
2012-08-06 14:34:08 -05:00
|
|
|
match (*node) {
|
2013-01-22 10:44:24 -06:00
|
|
|
Leaf(y) => y.byte_len,
|
|
|
|
Concat(ref y) => y.byte_len
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub pure fn char_len(node: @Node) -> uint {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (*node) {
|
2013-01-22 10:44:24 -06:00
|
|
|
Leaf(y) => y.char_len,
|
|
|
|
Concat(ref y) => y.char_len
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Concatenate a forest of nodes into one tree.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * forest - The forest. This vector is progressively rewritten during
|
|
|
|
* execution and should be discarded as meaningless afterwards.
|
|
|
|
*/
|
2013-01-29 20:30:22 -06:00
|
|
|
pub fn tree_from_forest_destructive(forest: &mut [@Node]) -> @Node {
|
2012-05-24 15:35:57 -05:00
|
|
|
let mut i;
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut len = vec::len(forest);
|
2011-11-04 12:38:42 -05:00
|
|
|
while len > 1u {
|
|
|
|
i = 0u;
|
|
|
|
while i < len - 1u {//Concat nodes 0 with 1, 2 with 3 etc.
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut left = forest[i];
|
|
|
|
let mut right = forest[i+1u];
|
2011-11-04 12:38:42 -05:00
|
|
|
let left_len = char_len(left);
|
|
|
|
let right_len= char_len(right);
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut left_height= height(left);
|
|
|
|
let mut right_height=height(right);
|
2011-11-06 06:09:24 -06:00
|
|
|
if left_len + right_len > hint_max_leaf_char_len {
|
2011-11-04 12:38:42 -05:00
|
|
|
if left_len <= hint_max_leaf_char_len {
|
|
|
|
left = flatten(left);
|
2011-11-06 06:09:24 -06:00
|
|
|
left_height = height(left);
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
if right_len <= hint_max_leaf_char_len {
|
|
|
|
right = flatten(right);
|
2011-11-06 06:09:24 -06:00
|
|
|
right_height = height(right);
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
2011-11-06 06:09:24 -06:00
|
|
|
if left_height >= hint_max_node_height {
|
|
|
|
left = of_substr_unsafer(@serialize_node(left),
|
|
|
|
0u,byte_len(left),
|
|
|
|
left_len);
|
|
|
|
}
|
|
|
|
if right_height >= hint_max_node_height {
|
|
|
|
right = of_substr_unsafer(@serialize_node(right),
|
|
|
|
0u,byte_len(right),
|
|
|
|
right_len);
|
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
forest[i/2u] = concat2(left, right);
|
|
|
|
i += 2u;
|
|
|
|
}
|
|
|
|
if i == len - 1u {
|
|
|
|
//And don't forget the last node if it is in even position
|
|
|
|
forest[i/2u] = forest[i];
|
|
|
|
}
|
|
|
|
len = uint::div_ceil(len, 2u);
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return forest[0];
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
pub fn serialize_node(node: @Node) -> ~str {
|
|
|
|
unsafe {
|
2013-02-12 17:34:48 -06:00
|
|
|
let mut buf = vec::from_elem(byte_len(node), 0);
|
2013-01-23 13:43:58 -06:00
|
|
|
let mut offset = 0u;//Current position in the buffer
|
|
|
|
let it = leaf_iterator::start(node);
|
|
|
|
loop {
|
|
|
|
match (leaf_iterator::next(&it)) {
|
|
|
|
option::None => break,
|
|
|
|
option::Some(x) => {
|
|
|
|
//FIXME (#2744): Replace with memcpy or something similar
|
|
|
|
let mut local_buf: ~[u8] =
|
|
|
|
cast::reinterpret_cast(&*x.content);
|
|
|
|
let mut i = x.byte_offset;
|
|
|
|
while i < x.byte_len {
|
|
|
|
buf[offset] = local_buf[i];
|
|
|
|
offset += 1u;
|
|
|
|
i += 1u;
|
|
|
|
}
|
2013-02-15 01:30:30 -06:00
|
|
|
cast::forget(local_buf);
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
2013-02-15 01:30:30 -06:00
|
|
|
return cast::transmute(buf);
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Replace a subtree by a single leaf with the same contents.
|
|
|
|
*
|
|
|
|
* * Performance note
|
|
|
|
*
|
|
|
|
* This function executes in linear time.
|
|
|
|
*/
|
2013-01-23 13:43:58 -06:00
|
|
|
pub fn flatten(node: @Node) -> @Node {
|
|
|
|
unsafe {
|
|
|
|
match (*node) {
|
2013-01-22 10:44:24 -06:00
|
|
|
Leaf(_) => node,
|
|
|
|
Concat(ref x) => {
|
|
|
|
@Leaf(Leaf {
|
|
|
|
byte_offset: 0u,
|
|
|
|
byte_len: x.byte_len,
|
|
|
|
char_len: x.char_len,
|
|
|
|
content: @serialize_node(node),
|
|
|
|
})
|
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Balance a node.
|
|
|
|
*
|
|
|
|
* # Algorithm
|
|
|
|
*
|
|
|
|
* * if the node height is smaller than `hint_max_node_height`, do nothing
|
|
|
|
* * otherwise, gather all leaves as a forest, rebuild a balanced node,
|
|
|
|
* concatenating small leaves along the way
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
2012-08-20 14:23:37 -05:00
|
|
|
* * `option::None` if no transformation happened
|
2013-01-25 13:29:51 -06:00
|
|
|
* * `option::Some(x)` otherwise, in which case `x` has the same contents
|
2012-07-04 16:53:12 -05:00
|
|
|
* as `node` bot lower height and/or fragmentation.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn bal(node: @Node) -> Option<@Node> {
|
2012-08-20 14:23:37 -05:00
|
|
|
if height(node) < hint_max_node_height { return option::None; }
|
2011-11-06 10:33:39 -06:00
|
|
|
//1. Gather all leaves as a forest
|
2012-09-21 20:43:30 -05:00
|
|
|
let mut forest = ~[];
|
2011-11-06 10:33:39 -06:00
|
|
|
let it = leaf_iterator::start(node);
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-08-29 17:06:49 -05:00
|
|
|
match (leaf_iterator::next(&it)) {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => break,
|
2012-09-26 19:33:34 -05:00
|
|
|
option::Some(x) => forest.push(@Leaf(x))
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
2011-11-06 10:33:39 -06:00
|
|
|
//2. Rebuild tree from forest
|
|
|
|
let root = @*tree_from_forest_destructive(forest);
|
2012-08-20 14:23:37 -05:00
|
|
|
return option::Some(root);
|
2011-11-06 10:33:39 -06:00
|
|
|
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Compute the subnode of a node.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * node - A node
|
|
|
|
* * byte_offset - A byte offset in `node`
|
|
|
|
* * byte_len - The number of bytes to return
|
|
|
|
*
|
|
|
|
* # Performance notes
|
|
|
|
*
|
|
|
|
* * this function performs no copying;
|
|
|
|
* * this function executes in a time proportional to the height of `node`
|
|
|
|
*
|
|
|
|
* # Safety notes
|
|
|
|
*
|
|
|
|
* This function fails if `byte_offset` or `byte_len` do not represent
|
|
|
|
* valid positions in `node`.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn sub_bytes(node: @Node, byte_offset: uint,
|
|
|
|
byte_len: uint) -> @Node {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut node = node;
|
|
|
|
let mut byte_offset = byte_offset;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2011-11-04 12:38:42 -05:00
|
|
|
if byte_offset == 0u && byte_len == node::byte_len(node) {
|
2012-08-01 19:30:05 -05:00
|
|
|
return node;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match (*node) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Leaf(x) => {
|
2011-11-04 12:38:42 -05:00
|
|
|
let char_len =
|
2012-02-23 09:59:30 -06:00
|
|
|
str::count_chars(*x.content, byte_offset, byte_len);
|
2013-01-22 10:44:24 -06:00
|
|
|
return @Leaf(Leaf {
|
|
|
|
byte_offset: byte_offset,
|
|
|
|
byte_len: byte_len,
|
|
|
|
char_len: char_len,
|
|
|
|
content: x.content,
|
|
|
|
});
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
2012-09-28 02:22:18 -05:00
|
|
|
node::Concat(ref x) => {
|
2011-11-04 12:38:42 -05:00
|
|
|
let left_len: uint = node::byte_len(x.left);
|
|
|
|
if byte_offset <= left_len {
|
|
|
|
if byte_offset + byte_len <= left_len {
|
2011-11-06 10:33:39 -06:00
|
|
|
//Case 1: Everything fits in x.left, tail-call
|
2011-11-04 12:38:42 -05:00
|
|
|
node = x.left;
|
|
|
|
} else {
|
|
|
|
//Case 2: A (non-empty, possibly full) suffix
|
|
|
|
//of x.left and a (non-empty, possibly full) prefix
|
|
|
|
//of x.right
|
|
|
|
let left_result =
|
|
|
|
sub_bytes(x.left, byte_offset, left_len);
|
|
|
|
let right_result =
|
|
|
|
sub_bytes(x.right, 0u, left_len - byte_offset);
|
2012-08-01 19:30:05 -05:00
|
|
|
return concat2(left_result, right_result);
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Case 3: Everything fits in x.right
|
|
|
|
byte_offset -= left_len;
|
|
|
|
node = x.right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Compute the subnode of a node.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * node - A node
|
|
|
|
* * char_offset - A char offset in `node`
|
|
|
|
* * char_len - The number of chars to return
|
|
|
|
*
|
|
|
|
* # Performance notes
|
|
|
|
*
|
|
|
|
* * this function performs no copying;
|
|
|
|
* * this function executes in a time proportional to the height of `node`
|
|
|
|
*
|
|
|
|
* # Safety notes
|
|
|
|
*
|
|
|
|
* This function fails if `char_offset` or `char_len` do not represent
|
|
|
|
* valid positions in `node`.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn sub_chars(node: @Node, char_offset: uint,
|
|
|
|
char_len: uint) -> @Node {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut node = node;
|
|
|
|
let mut char_offset = char_offset;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (*node) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Leaf(x) => {
|
2011-11-06 10:33:39 -06:00
|
|
|
if char_offset == 0u && char_len == x.char_len {
|
2012-08-01 19:30:05 -05:00
|
|
|
return node;
|
2011-11-06 10:33:39 -06:00
|
|
|
}
|
|
|
|
let byte_offset =
|
2012-02-23 09:59:30 -06:00
|
|
|
str::count_bytes(*x.content, 0u, char_offset);
|
2011-11-06 10:33:39 -06:00
|
|
|
let byte_len =
|
2012-02-23 09:59:30 -06:00
|
|
|
str::count_bytes(*x.content, byte_offset, char_len);
|
2013-01-22 10:44:24 -06:00
|
|
|
return @Leaf(Leaf {
|
|
|
|
byte_offset: byte_offset,
|
|
|
|
byte_len: byte_len,
|
|
|
|
char_len: char_len,
|
|
|
|
content: x.content,
|
|
|
|
});
|
2011-11-06 10:33:39 -06:00
|
|
|
}
|
2012-09-28 02:22:18 -05:00
|
|
|
node::Concat(ref x) => {
|
2012-08-01 19:30:05 -05:00
|
|
|
if char_offset == 0u && char_len == x.char_len {return node;}
|
2011-11-06 10:33:39 -06:00
|
|
|
let left_len : uint = node::char_len(x.left);
|
|
|
|
if char_offset <= left_len {
|
|
|
|
if char_offset + char_len <= left_len {
|
|
|
|
//Case 1: Everything fits in x.left, tail call
|
|
|
|
node = x.left;
|
|
|
|
} else {
|
|
|
|
//Case 2: A (non-empty, possibly full) suffix
|
|
|
|
//of x.left and a (non-empty, possibly full) prefix
|
|
|
|
//of x.right
|
|
|
|
let left_result =
|
|
|
|
sub_chars(x.left, char_offset, left_len);
|
|
|
|
let right_result =
|
|
|
|
sub_chars(x.right, 0u, left_len - char_offset);
|
2012-08-01 19:30:05 -05:00
|
|
|
return concat2(left_result, right_result);
|
2011-11-06 10:33:39 -06:00
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
} else {
|
2011-11-06 10:33:39 -06:00
|
|
|
//Case 3: Everything fits in x.right, tail call
|
|
|
|
node = x.right;
|
|
|
|
char_offset -= left_len;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
2011-11-06 10:33:39 -06:00
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn concat2(left: @Node, right: @Node) -> @Node {
|
2013-01-22 10:44:24 -06:00
|
|
|
@Concat(Concat {
|
|
|
|
left: left,
|
|
|
|
right: right,
|
|
|
|
char_len: char_len(left) + char_len(right),
|
|
|
|
byte_len: byte_len(left) + byte_len(right),
|
|
|
|
height: uint::max(height(left), height(right)) + 1u,
|
|
|
|
})
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-12-23 16:38:01 -06:00
|
|
|
pub pure fn height(node: @Node) -> uint {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (*node) {
|
2013-01-22 10:44:24 -06:00
|
|
|
Leaf(_) => 0u,
|
|
|
|
Concat(ref x) => x.height,
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn cmp(a: @Node, b: @Node) -> int {
|
2011-11-04 12:38:42 -05:00
|
|
|
let ita = char_iterator::start(a);
|
|
|
|
let itb = char_iterator::start(b);
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut result = 0;
|
2011-11-04 12:38:42 -05:00
|
|
|
while result == 0 {
|
2012-08-29 17:06:49 -05:00
|
|
|
match ((char_iterator::next(&ita), char_iterator::next(&itb))) {
|
2012-08-20 14:23:37 -05:00
|
|
|
(option::None, option::None) => break,
|
|
|
|
(option::Some(chara), option::Some(charb)) => {
|
2011-11-04 12:38:42 -05:00
|
|
|
result = char::cmp(chara, charb);
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
(option::Some(_), _) => {
|
2011-11-04 12:38:42 -05:00
|
|
|
result = 1;
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
(_, option::Some(_)) => {
|
2011-11-04 12:38:42 -05:00
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return result;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-10-03 14:21:48 -05:00
|
|
|
pub fn loop_chars(node: @Node, it: fn(c: char) -> bool) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return loop_leaves(node,|leaf| {
|
2012-03-26 05:39:20 -05:00
|
|
|
str::all_between(*leaf.content,
|
|
|
|
leaf.byte_offset,
|
|
|
|
leaf.byte_len, it)
|
2012-06-30 18:19:07 -05:00
|
|
|
});
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Loop through a node, leaf by leaf
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * rope - A node to traverse.
|
|
|
|
* * it - A block to execute with each consecutive leaf of the node.
|
|
|
|
* Return `true` to continue, `false` to stop
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* `true` If execution proceeded correctly, `false` if it was interrupted,
|
|
|
|
* that is if `it` returned `false` at any point.
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn loop_leaves(node: @Node, it: fn(Leaf) -> bool) -> bool{
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut current = node;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (*current) {
|
2012-09-02 19:32:06 -05:00
|
|
|
Leaf(x) => return it(x),
|
2012-09-28 02:22:18 -05:00
|
|
|
Concat(ref x) => if loop_leaves(x.left, it) { //non tail call
|
2012-08-03 21:59:04 -05:00
|
|
|
current = x.right; //tail call
|
|
|
|
} else {
|
|
|
|
return false;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * pos - A position in the rope
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The character at position `pos`
|
|
|
|
*
|
|
|
|
* # Safety notes
|
|
|
|
*
|
|
|
|
* The function will fail if `pos` is not a valid position in the rope.
|
|
|
|
*
|
|
|
|
* Performance note: This function executes in a time
|
|
|
|
* proportional to the height of the rope + the (bounded)
|
|
|
|
* length of the largest leaf.
|
|
|
|
*/
|
2012-12-23 16:38:01 -06:00
|
|
|
pub pure fn char_at(node: @Node, pos: uint) -> char {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut node = node;
|
|
|
|
let mut pos = pos;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *node {
|
2012-09-02 19:32:06 -05:00
|
|
|
Leaf(x) => return str::char_at(*x.content, pos),
|
2013-01-22 10:44:24 -06:00
|
|
|
Concat(Concat {left, right, _}) => {
|
2011-11-15 06:31:44 -06:00
|
|
|
let left_len = char_len(left);
|
|
|
|
node = if left_len > pos { left }
|
|
|
|
else { pos -= left_len; right };
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-11-15 06:31:44 -06:00
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub mod leaf_iterator {
|
2013-01-08 21:37:25 -06:00
|
|
|
use rope::node::{Concat, Leaf, Node, height};
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::option;
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::vec;
|
|
|
|
|
2013-01-22 10:44:24 -06:00
|
|
|
pub struct T {
|
2013-01-29 21:14:23 -06:00
|
|
|
mut stack: ~[@Node],
|
2013-01-22 10:44:24 -06:00
|
|
|
mut stackpos: int,
|
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn empty() -> T {
|
2013-01-29 20:13:14 -06:00
|
|
|
let mut stack : ~[@Node] = ~[];
|
2013-01-22 10:44:24 -06:00
|
|
|
T { stack: stack, stackpos: -1 }
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn start(node: @Node) -> T {
|
2013-01-29 22:23:35 -06:00
|
|
|
let stack = vec::from_elem(height(node)+1u, node);
|
2013-01-22 10:44:24 -06:00
|
|
|
T {
|
|
|
|
stack: stack,
|
|
|
|
stackpos: 0,
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn next(it: &T) -> Option<Leaf> {
|
2012-08-20 14:23:37 -05:00
|
|
|
if it.stackpos < 0 { return option::None; }
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2011-11-04 12:38:42 -05:00
|
|
|
let current = it.stack[it.stackpos];
|
|
|
|
it.stackpos -= 1;
|
2012-08-06 14:34:08 -05:00
|
|
|
match (*current) {
|
2012-09-28 02:22:18 -05:00
|
|
|
Concat(ref x) => {
|
2011-11-04 12:38:42 -05:00
|
|
|
it.stackpos += 1;
|
|
|
|
it.stack[it.stackpos] = x.right;
|
|
|
|
it.stackpos += 1;
|
|
|
|
it.stack[it.stackpos] = x.left;
|
|
|
|
}
|
2012-09-02 19:32:06 -05:00
|
|
|
Leaf(x) => return option::Some(x)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub mod char_iterator {
|
2013-01-08 21:37:25 -06:00
|
|
|
use rope::node::{Leaf, Node};
|
2012-12-23 16:41:37 -06:00
|
|
|
use rope::node::leaf_iterator;
|
|
|
|
|
|
|
|
use core::option;
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
|
2013-01-22 10:44:24 -06:00
|
|
|
pub struct T {
|
2012-09-02 19:32:06 -05:00
|
|
|
leaf_iterator: leaf_iterator::T,
|
|
|
|
mut leaf: Option<Leaf>,
|
2013-01-22 10:44:24 -06:00
|
|
|
mut leaf_byte_pos: uint,
|
|
|
|
}
|
2011-11-04 12:38:42 -05:00
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn start(node: @Node) -> T {
|
2013-01-22 10:44:24 -06:00
|
|
|
T {
|
2011-11-04 12:38:42 -05:00
|
|
|
leaf_iterator: leaf_iterator::start(node),
|
2013-01-22 10:44:24 -06:00
|
|
|
leaf: option::None,
|
|
|
|
leaf_byte_pos: 0u,
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn empty() -> T {
|
2013-01-22 10:44:24 -06:00
|
|
|
T {
|
2011-11-04 12:38:42 -05:00
|
|
|
leaf_iterator: leaf_iterator::empty(),
|
2013-01-22 10:44:24 -06:00
|
|
|
leaf: option::None,
|
|
|
|
leaf_byte_pos: 0u,
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn next(it: &T) -> Option<char> {
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (get_current_or_next_leaf(it)) {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => return option::None,
|
|
|
|
option::Some(_) => {
|
2011-11-04 12:38:42 -05:00
|
|
|
let next_char = get_next_char_in_leaf(it);
|
2012-08-06 14:34:08 -05:00
|
|
|
match (next_char) {
|
2012-09-07 17:32:04 -05:00
|
|
|
option::None => loop,
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(_) => return next_char
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn get_current_or_next_leaf(it: &T) -> Option<Leaf> {
|
2012-08-29 17:06:49 -05:00
|
|
|
match ((*it).leaf) {
|
|
|
|
option::Some(_) => return (*it).leaf,
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => {
|
2012-08-29 17:06:49 -05:00
|
|
|
let next = leaf_iterator::next(&((*it).leaf_iterator));
|
2012-08-06 14:34:08 -05:00
|
|
|
match (next) {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => return option::None,
|
|
|
|
option::Some(_) => {
|
2012-08-29 17:06:49 -05:00
|
|
|
(*it).leaf = next;
|
|
|
|
(*it).leaf_byte_pos = 0u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return next;
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn get_next_char_in_leaf(it: &T) -> Option<char> {
|
2012-08-29 17:06:49 -05:00
|
|
|
match copy (*it).leaf {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => return option::None,
|
|
|
|
option::Some(aleaf) => {
|
2012-08-29 17:06:49 -05:00
|
|
|
if (*it).leaf_byte_pos >= aleaf.byte_len {
|
2011-11-04 12:38:42 -05:00
|
|
|
//We are actually past the end of the leaf
|
2012-08-29 17:06:49 -05:00
|
|
|
(*it).leaf = option::None;
|
2012-08-20 14:23:37 -05:00
|
|
|
return option::None
|
2011-11-04 12:38:42 -05:00
|
|
|
} else {
|
2012-11-26 22:05:19 -06:00
|
|
|
let range =
|
2012-01-14 18:05:07 -06:00
|
|
|
str::char_range_at(*aleaf.content,
|
2012-08-29 17:06:49 -05:00
|
|
|
(*it).leaf_byte_pos + aleaf.byte_offset);
|
2012-11-26 22:05:19 -06:00
|
|
|
let ch = range.ch;
|
|
|
|
let next = range.next;
|
2012-08-29 17:06:49 -05:00
|
|
|
(*it).leaf_byte_pos = next - aleaf.byte_offset;
|
2012-08-20 14:23:37 -05:00
|
|
|
return option::Some(ch)
|
2011-11-04 12:38:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-01-08 21:37:25 -06:00
|
|
|
use rope::*;
|
2012-12-27 20:24:18 -06:00
|
|
|
|
|
|
|
use core::option;
|
|
|
|
use core::str;
|
2012-12-28 14:46:08 -06:00
|
|
|
use core::uint;
|
2012-12-27 20:24:18 -06:00
|
|
|
use core::vec;
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
//Utility function, used for sanity check
|
2012-09-02 19:32:06 -05:00
|
|
|
fn rope_to_string(r: Rope) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (r) {
|
2012-09-02 19:32:06 -05:00
|
|
|
node::Empty => return ~"",
|
|
|
|
node::Content(x) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
let str = @mut ~"";
|
2013-01-23 13:43:58 -06:00
|
|
|
fn aux(str: @mut ~str, node: @node::Node) {
|
|
|
|
unsafe {
|
|
|
|
match (*node) {
|
|
|
|
node::Leaf(x) => {
|
|
|
|
*str += str::slice(
|
|
|
|
*x.content, x.byte_offset,
|
|
|
|
x.byte_offset + x.byte_len);
|
|
|
|
}
|
|
|
|
node::Concat(ref x) => {
|
|
|
|
aux(str, x.left);
|
|
|
|
aux(str, x.right);
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
aux(str, x);
|
2012-08-01 19:30:05 -05:00
|
|
|
return *str
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trivial() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(char_len(empty()) == 0u);
|
|
|
|
fail_unless!(byte_len(empty()) == 0u);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn of_string1() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let sample = @~"0123456789ABCDE";
|
2012-01-17 21:05:07 -06:00
|
|
|
let r = of_str(sample);
|
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(char_len(r) == str::char_len(*sample));
|
|
|
|
fail_unless!(rope_to_string(r) == *sample);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn of_string2() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let buf = @ mut ~"1234567890";
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0;
|
2013-01-11 23:01:42 -06:00
|
|
|
while i < 10 {
|
|
|
|
let a = *buf;
|
|
|
|
let b = *buf;
|
|
|
|
*buf = a + b;
|
|
|
|
i+=1;
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
let sample = @*buf;
|
|
|
|
let r = of_str(sample);
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(char_len(r) == str::char_len(*sample));
|
|
|
|
fail_unless!(rope_to_string(r) == *sample);
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut string_iter = 0u;
|
2012-02-23 03:44:04 -06:00
|
|
|
let string_len = str::len(*sample);
|
2012-01-17 21:05:07 -06:00
|
|
|
let rope_iter = iterator::char::start(r);
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut equal = true;
|
2012-01-17 21:05:07 -06:00
|
|
|
while equal {
|
2012-08-29 17:06:49 -05:00
|
|
|
match (node::char_iterator::next(&rope_iter)) {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => {
|
2012-01-17 21:05:07 -06:00
|
|
|
if string_iter < string_len {
|
|
|
|
equal = false;
|
|
|
|
} break; }
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(c) => {
|
2012-11-26 22:05:19 -06:00
|
|
|
let range = str::char_range_at(*sample, string_iter);
|
|
|
|
string_iter = range.next;
|
|
|
|
if range.ch != c { equal = false; break; }
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(equal);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn iter1() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let buf = @ mut ~"1234567890";
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0;
|
2013-01-11 23:01:42 -06:00
|
|
|
while i < 10 {
|
|
|
|
let a = *buf;
|
|
|
|
let b = *buf;
|
|
|
|
*buf = a + b;
|
|
|
|
i+=1;
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
let sample = @*buf;
|
|
|
|
let r = of_str(sample);
|
|
|
|
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut len = 0u;
|
2012-01-17 21:05:07 -06:00
|
|
|
let it = iterator::char::start(r);
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-08-29 17:06:49 -05:00
|
|
|
match (node::char_iterator::next(&it)) {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => break,
|
|
|
|
option::Some(_) => len += 1u
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(len == str::char_len(*sample));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bal1() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let init = @~"1234567890";
|
2012-07-12 17:03:33 -05:00
|
|
|
let buf = @mut * init;
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0;
|
2013-01-11 23:01:42 -06:00
|
|
|
while i < 8 {
|
|
|
|
let a = *buf;
|
|
|
|
let b = *buf;
|
|
|
|
*buf = a + b;
|
|
|
|
i+=1;
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
let sample = @*buf;
|
|
|
|
let r1 = of_str(sample);
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut r2 = of_str(init);
|
2012-01-17 21:05:07 -06:00
|
|
|
i = 0;
|
|
|
|
while i < 8 { r2 = append_rope(r2, r2); i+= 1;}
|
|
|
|
|
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(eq(r1, r2));
|
2012-01-17 21:05:07 -06:00
|
|
|
let r3 = bal(r2);
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(char_len(r1) == char_len(r3));
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(eq(r1, r3));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-04-18 19:02:00 -05:00
|
|
|
#[ignore]
|
2012-01-17 21:05:07 -06:00
|
|
|
fn char_at1() {
|
|
|
|
//Generate a large rope
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut r = of_str(@~"123456789");
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, 10u) |_i| {
|
2012-01-17 21:05:07 -06:00
|
|
|
r = append_rope(r, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Copy it in the slowest possible way
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut r2 = empty();
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, char_len(r)) |i| {
|
2012-01-17 21:05:07 -06:00
|
|
|
r2 = append_char(r2, char_at(r, i));
|
|
|
|
}
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(eq(r, r2));
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut r3 = empty();
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, char_len(r)) |i| {
|
2012-01-17 21:05:07 -06:00
|
|
|
r3 = prepend_char(r3, char_at(r, char_len(r) - i - 1u));
|
|
|
|
}
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(eq(r, r3));
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
//Additional sanity checks
|
|
|
|
let balr = bal(r);
|
|
|
|
let bal2 = bal(r2);
|
|
|
|
let bal3 = bal(r3);
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(eq(r, balr));
|
|
|
|
fail_unless!(eq(r, bal2));
|
|
|
|
fail_unless!(eq(r, bal3));
|
|
|
|
fail_unless!(eq(r2, r3));
|
|
|
|
fail_unless!(eq(bal2, bal3));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn concat1() {
|
|
|
|
//Generate a reasonable rope
|
2012-07-14 00:57:48 -05:00
|
|
|
let chunk = of_str(@~"123456789");
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut r = empty();
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, 10u) |_i| {
|
2012-01-17 21:05:07 -06:00
|
|
|
r = append_rope(r, chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Same rope, obtained with rope::concat
|
2012-03-12 17:52:30 -05:00
|
|
|
let r2 = concat(vec::from_elem(10u, chunk));
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(eq(r, r2));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-01-30 22:27:16 -06:00
|
|
|
}
|