Skip to content

Commit

Permalink
extra: Add reserve and reserve_at_least to extra::deque
Browse files Browse the repository at this point in the history
As called for in #4994
  • Loading branch information
catamorphism committed May 27, 2013
1 parent 3941f78 commit 77de84b
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);
}

}

5 comments on commit 77de84b

@bors
Copy link
Contributor

@bors bors commented on 77de84b May 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from thestinger
at catamorphism@77de84b

@bors
Copy link
Contributor

@bors bors commented on 77de84b May 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging catamorphism/rust/issue-4994 = 77de84b into auto

@bors
Copy link
Contributor

@bors bors commented on 77de84b May 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catamorphism/rust/issue-4994 = 77de84b merged ok, testing candidate = fe7f528

@bors
Copy link
Contributor

@bors bors commented on 77de84b May 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 77de84b May 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = fe7f528

Please sign in to comment.