Skip to content

Miscellaneous container doc/code improvements #5400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/libstd/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,61 @@ pub struct Deque<T> {
}

impl<T> Container for Deque<T> {
/// Return the number of elements in the deque
pure fn len(&self) -> uint { self.nelts }

/// Return true if the deque contains no elements
pure fn is_empty(&self) -> bool { self.len() == 0 }
}

impl<T> Mutable for Deque<T> {
/// Clear the deque, removing all values.
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.lo = 0;
self.hi = 0;
}
}

pub impl<T> Deque<T> {
/// Create an empty Deque
static pure fn new() -> Deque<T> {
Deque{nelts: 0, lo: 0, hi: 0,
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) }

/// 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) }

/// 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 {
let idx = (self.lo + (i as uint)) % self.elts.len();
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 {
let mut result = self.elts[self.lo].swap_unwrap();
self.lo = (self.lo + 1u) % self.elts.len();
self.nelts -= 1u;
result
}

/// Remove and return the last element in the deque
///
/// Fails if the deque is empty
fn pop_back(&mut self) -> T {
if self.hi == 0u {
self.hi = self.elts.len() - 1u;
Expand All @@ -66,6 +87,7 @@ pub impl<T> Deque<T> {
result
}

/// Prepend an element to the deque
fn add_front(&mut self, t: T) {
let oldlo = self.lo;
if self.lo == 0u {
Expand All @@ -80,6 +102,7 @@ pub impl<T> Deque<T> {
self.nelts += 1u;
}

/// Append an element to the deque
fn add_back(&mut self, t: T) {
if self.lo == self.hi && self.nelts != 0u {
self.elts = grow(self.nelts, self.lo, self.elts);
Expand Down
15 changes: 6 additions & 9 deletions src/libstd/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
child: &mut Option<~TreeNode<K, V>>) {
// *could* be done without recursion, but it won't borrow check
do child.mutate |mut child| {
if child.right.is_some() {
heir_swap(node, &mut child.right);
for child.each_mut |x| {
if x.right.is_some() {
heir_swap(node, &mut x.right);
} else {
node.key <-> child.key;
node.value <-> child.value;
node.key <-> x.key;
node.value <-> x.value;
}
child
}
}

Expand Down Expand Up @@ -689,7 +688,7 @@ fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
save.level -= 1;

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);
Expand Down Expand Up @@ -988,8 +987,6 @@ mod test_treemap {
let m = m;
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() == (&x2, &y2));
fail_unless!(map_next(&mut a).unwrap() == (&x3, &y3));
Expand Down