auto merge of #5400 : thestinger/rust/misc, r=brson
This commit is contained in:
commit
5a77a10488
2 changed files with 30 additions and 10 deletions
|
@ -22,13 +22,17 @@ pub struct Deque<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Container for Deque<T> {
|
impl<T> Container for Deque<T> {
|
||||||
|
/// Return the number of elements in the deque
|
||||||
pure fn len(&self) -> uint { self.nelts }
|
pure fn len(&self) -> uint { self.nelts }
|
||||||
|
|
||||||
|
/// Return true if the deque contains no elements
|
||||||
pure fn is_empty(&self) -> bool { self.len() == 0 }
|
pure fn is_empty(&self) -> bool { self.len() == 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Mutable for Deque<T> {
|
impl<T> Mutable for Deque<T> {
|
||||||
|
/// Clear the deque, removing all values.
|
||||||
fn clear(&mut self) {
|
fn clear(&mut self) {
|
||||||
for vec::each_mut(self.elts) |x| { *x = None }
|
for self.elts.each_mut |x| { *x = None }
|
||||||
self.nelts = 0;
|
self.nelts = 0;
|
||||||
self.lo = 0;
|
self.lo = 0;
|
||||||
self.hi = 0;
|
self.hi = 0;
|
||||||
|
@ -36,19 +40,33 @@ impl<T> Mutable for Deque<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub impl<T> Deque<T> {
|
pub impl<T> Deque<T> {
|
||||||
|
/// Create an empty Deque
|
||||||
static pure fn new() -> Deque<T> {
|
static pure fn new() -> Deque<T> {
|
||||||
Deque{nelts: 0, lo: 0, hi: 0,
|
Deque{nelts: 0, lo: 0, hi: 0,
|
||||||
elts: vec::from_fn(initial_capacity, |_| None)}
|
elts: vec::from_fn(initial_capacity, |_| None)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return a reference to the first element in the deque
|
||||||
|
///
|
||||||
|
/// Fails if the deque is empty
|
||||||
fn peek_front(&self) -> &self/T { get(self.elts, self.lo) }
|
fn peek_front(&self) -> &self/T { get(self.elts, self.lo) }
|
||||||
|
|
||||||
|
/// Return a reference to the last element in the deque
|
||||||
|
///
|
||||||
|
/// Fails if the deque is empty
|
||||||
fn peek_back(&self) -> &self/T { get(self.elts, self.hi - 1u) }
|
fn peek_back(&self) -> &self/T { get(self.elts, self.hi - 1u) }
|
||||||
|
|
||||||
|
/// Retrieve an element in the deque by index
|
||||||
|
///
|
||||||
|
/// Fails if there is no element with the given index
|
||||||
fn get(&self, i: int) -> &self/T {
|
fn get(&self, i: int) -> &self/T {
|
||||||
let idx = (self.lo + (i as uint)) % self.elts.len();
|
let idx = (self.lo + (i as uint)) % self.elts.len();
|
||||||
get(self.elts, idx)
|
get(self.elts, idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove and return the first element in the deque
|
||||||
|
///
|
||||||
|
/// Fails if the deque is empty
|
||||||
fn pop_front(&mut self) -> T {
|
fn pop_front(&mut self) -> T {
|
||||||
let mut result = self.elts[self.lo].swap_unwrap();
|
let mut result = self.elts[self.lo].swap_unwrap();
|
||||||
self.lo = (self.lo + 1u) % self.elts.len();
|
self.lo = (self.lo + 1u) % self.elts.len();
|
||||||
|
@ -56,6 +74,9 @@ pub impl<T> Deque<T> {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove and return the last element in the deque
|
||||||
|
///
|
||||||
|
/// Fails if the deque is empty
|
||||||
fn pop_back(&mut self) -> T {
|
fn pop_back(&mut self) -> T {
|
||||||
if self.hi == 0u {
|
if self.hi == 0u {
|
||||||
self.hi = self.elts.len() - 1u;
|
self.hi = self.elts.len() - 1u;
|
||||||
|
@ -66,6 +87,7 @@ pub impl<T> Deque<T> {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Prepend an element to the deque
|
||||||
fn add_front(&mut self, t: T) {
|
fn add_front(&mut self, t: T) {
|
||||||
let oldlo = self.lo;
|
let oldlo = self.lo;
|
||||||
if self.lo == 0u {
|
if self.lo == 0u {
|
||||||
|
@ -80,6 +102,7 @@ pub impl<T> Deque<T> {
|
||||||
self.nelts += 1u;
|
self.nelts += 1u;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Append an element to the deque
|
||||||
fn add_back(&mut self, t: T) {
|
fn add_back(&mut self, t: T) {
|
||||||
if self.lo == self.hi && self.nelts != 0u {
|
if self.lo == self.hi && self.nelts != 0u {
|
||||||
self.elts = grow(self.nelts, self.lo, self.elts);
|
self.elts = grow(self.nelts, self.lo, self.elts);
|
||||||
|
|
|
@ -636,14 +636,13 @@ fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
|
||||||
fn heir_swap<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>,
|
fn heir_swap<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>,
|
||||||
child: &mut Option<~TreeNode<K, V>>) {
|
child: &mut Option<~TreeNode<K, V>>) {
|
||||||
// *could* be done without recursion, but it won't borrow check
|
// *could* be done without recursion, but it won't borrow check
|
||||||
do child.mutate |mut child| {
|
for child.each_mut |x| {
|
||||||
if child.right.is_some() {
|
if x.right.is_some() {
|
||||||
heir_swap(node, &mut child.right);
|
heir_swap(node, &mut x.right);
|
||||||
} else {
|
} else {
|
||||||
node.key <-> child.key;
|
node.key <-> x.key;
|
||||||
node.value <-> child.value;
|
node.value <-> x.value;
|
||||||
}
|
}
|
||||||
child
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -689,7 +688,7 @@ fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
|
||||||
save.level -= 1;
|
save.level -= 1;
|
||||||
|
|
||||||
if right_level > save.level {
|
if right_level > save.level {
|
||||||
do save.right.mutate |mut x| { x.level = save.level; x }
|
for save.right.each_mut |x| { x.level = save.level }
|
||||||
}
|
}
|
||||||
|
|
||||||
skew(save);
|
skew(save);
|
||||||
|
@ -988,8 +987,6 @@ mod test_treemap {
|
||||||
let m = m;
|
let m = m;
|
||||||
let mut a = m.iter();
|
let mut a = m.iter();
|
||||||
|
|
||||||
// FIXME: #4492 (ICE): iter.get() == Some((&x1, &y1))
|
|
||||||
|
|
||||||
fail_unless!(map_next(&mut a).unwrap() == (&x1, &y1));
|
fail_unless!(map_next(&mut a).unwrap() == (&x1, &y1));
|
||||||
fail_unless!(map_next(&mut a).unwrap() == (&x2, &y2));
|
fail_unless!(map_next(&mut a).unwrap() == (&x2, &y2));
|
||||||
fail_unless!(map_next(&mut a).unwrap() == (&x3, &y3));
|
fail_unless!(map_next(&mut a).unwrap() == (&x3, &y3));
|
||||||
|
|
Loading…
Reference in a new issue