deque: Implement Deque::with_capacity. Lower initial capacity to 8.
We need a reasonably small initial capacity to make Deques faster for the common case.
This commit is contained in:
parent
8a3267672c
commit
0ff5c17cbb
1 changed files with 18 additions and 2 deletions
|
@ -14,7 +14,8 @@ use std::uint;
|
|||
use std::vec;
|
||||
use std::iterator::FromIterator;
|
||||
|
||||
static INITIAL_CAPACITY: uint = 32u; // 2^5
|
||||
static INITIAL_CAPACITY: uint = 8u; // 2^3
|
||||
static MINIMUM_CAPACITY: uint = 2u;
|
||||
|
||||
#[allow(missing_doc)]
|
||||
pub struct Deque<T> {
|
||||
|
@ -43,8 +44,13 @@ impl<T> Mutable for Deque<T> {
|
|||
impl<T> Deque<T> {
|
||||
/// Create an empty Deque
|
||||
pub fn new() -> Deque<T> {
|
||||
Deque::with_capacity(INITIAL_CAPACITY)
|
||||
}
|
||||
|
||||
/// Create an empty Deque with space for at least `n` elements.
|
||||
pub fn with_capacity(n: uint) -> Deque<T> {
|
||||
Deque{nelts: 0, lo: 0,
|
||||
elts: vec::from_fn(INITIAL_CAPACITY, |_| None)}
|
||||
elts: vec::from_fn(uint::max(MINIMUM_CAPACITY, n), |_| None)}
|
||||
}
|
||||
|
||||
/// Return a reference to the first element in the deque
|
||||
|
@ -527,6 +533,16 @@ mod tests {
|
|||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_capacity() {
|
||||
let mut d = Deque::with_capacity(0);
|
||||
d.add_back(1);
|
||||
assert_eq!(d.len(), 1);
|
||||
let mut d = Deque::with_capacity(50);
|
||||
d.add_back(1);
|
||||
assert_eq!(d.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reserve() {
|
||||
let mut d = Deque::new();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue