Skip to content

Commit

Permalink
auto merge of #6769 : catamorphism/rust/issue-4994, r=thestinger
Browse files Browse the repository at this point in the history
r? @thestinger As called for in #4994
  • Loading branch information
bors committed May 28, 2013
2 parents f7f36a8 + 77de84b commit fe7f528
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/libextra/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ pub impl<T> Deque<T> {
self.hi = (self.hi + 1u) % self.elts.len();
self.nelts += 1u;
}

/// Reserve capacity for exactly `n` elements in the given deque,
/// doing nothing if `self`'s capacity is already equal to or greater
/// than the requested capacity
///
/// # Arguments
///
/// * n - The number of elements to reserve space for
fn reserve(&mut self, n: uint) {
vec::reserve(&mut self.elts, n);
}

/// Reserve capacity for at least `n` elements in the given deque,
/// over-allocating in case the caller needs to reserve additional
/// space.
///
/// Do nothing if `self`'s capacity is already equal to or greater
/// than the requested capacity.
///
/// # Arguments
///
/// * n - The number of elements to reserve space for
fn reserve_at_least(&mut self, n: uint) {
vec::reserve_at_least(&mut self.elts, n);
}
}

/// Grow is only called on full elts, so nelts is also len(elts), unlike
Expand All @@ -149,6 +174,7 @@ mod tests {
use super::*;
use core::cmp::Eq;
use core::kinds::Copy;
use core::vec::capacity;

#[test]
fn test_simple() {
Expand Down Expand Up @@ -328,4 +354,29 @@ mod tests {
}

}

#[test]
fn test_reserve() {
let mut d = Deque::new();
d.add_back(0u64);
d.reserve(50);
assert_eq!(capacity(&mut d.elts), 50);
let mut d = Deque::new();
d.add_back(0u32);
d.reserve(50);
assert_eq!(capacity(&mut d.elts), 50);
}

#[test]
fn test_reserve_at_least() {
let mut d = Deque::new();
d.add_back(0u64);
d.reserve_at_least(50);
assert_eq!(capacity(&mut d.elts), 64);
let mut d = Deque::new();
d.add_back(0u32);
d.reserve_at_least(50);
assert_eq!(capacity(&mut d.elts), 64);
}

}

0 comments on commit fe7f528

Please sign in to comment.