From 77de84b4f629b63c784903ed7cc08c024b10496b Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 27 May 2013 11:47:38 -0700 Subject: [PATCH] extra: Add reserve and reserve_at_least to extra::deque As called for in #4994 --- src/libextra/deque.rs | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/libextra/deque.rs b/src/libextra/deque.rs index 371b8ce94e7d0..ad3b6ab3df402 100644 --- a/src/libextra/deque.rs +++ b/src/libextra/deque.rs @@ -125,6 +125,31 @@ pub impl Deque { 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 @@ -149,6 +174,7 @@ mod tests { use super::*; use core::cmp::Eq; use core::kinds::Copy; + use core::vec::capacity; #[test] fn test_simple() { @@ -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); + } + }