diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 5edd3d0b780be..f70325995ae9d 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -619,9 +619,9 @@ impl Vec { /// # Example /// /// ``` - /// let mut vec: Vec = vec![1]; + /// let mut vec: Vec = Vec::new(); /// vec.reserve(10); - /// assert!(vec.capacity() >= 11); + /// assert!(vec.capacity() >= 10); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn reserve(&mut self, additional: uint) { @@ -656,9 +656,9 @@ impl Vec { /// # Example /// /// ``` - /// let mut vec: Vec = vec![1]; + /// let mut vec: Vec = Vec::new(); /// vec.reserve_exact(10); - /// assert!(vec.capacity() >= 11); + /// assert!(vec.capacity() >= 10); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn reserve_exact(&mut self, additional: uint) { @@ -670,6 +670,54 @@ impl Vec { } } + /// Reserves the minimum capacity for an element to be inserted at `index` in the given + /// `Vec`. The collection may reserve more space to avoid frequent reallocations. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. + /// + /// # Example + /// + /// ``` + /// let mut vec: Vec = Vec::new(); + /// vec.reserve_index(10); + /// assert!(vec.capacity() >= 11); + /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve_index(&mut self, index: uint) { + let len = self.len(); + if index >= len { + self.reserve(index - len + 1); + } + } + + /// Reserves the minimum capacity for an element to be inserted at `index` in the + /// given `Vec`. Does nothing if the capacity is already sufficient. + /// + /// Note that the allocator may give the collection more space than it requests. Therefore + /// capacity can not be relied upon to be precisely minimal. Prefer `reserve_index` if future + /// insertions are expected. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. + /// + /// # Example + /// + /// ``` + /// let mut vec: Vec = Vec::new(); + /// vec.reserve_index_exact(10); + /// assert!(vec.capacity() >= 11); + /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve_index_exact(&mut self, index: uint) { + let len = self.len(); + if index >= len { + self.reserve_exact(index - len + 1); + } + } + /// Shrinks the capacity of the vector as much as possible. It will drop /// down as close as possible to the length but the allocator may still /// inform the vector that there is space for a few more elements. @@ -1792,6 +1840,72 @@ mod tests { assert!(v.capacity() >= 33) } + #[test] + fn test_reserve_exact() { + let mut v = Vec::new(); + assert_eq!(v.capacity(), 0); + + v.reserve_exact(2); + assert!(v.capacity() >= 2); + + for i in range(0i, 16) { + v.push(i); + } + + assert!(v.capacity() >= 16); + v.reserve_exact(16); + assert!(v.capacity() >= 32); + + v.push(16); + + v.reserve_exact(16); + assert!(v.capacity() >= 33) + } + + #[test] + fn test_reserve_index() { + let mut v = Vec::new(); + assert_eq!(v.capacity(), 0); + + v.reserve_index(2); + assert!(v.capacity() >= 3); + + for i in range(0i, 16) { + v.push(i); + } + + assert!(v.capacity() >= 16); + v.reserve_index(16); + assert!(v.capacity() >= 17); + + v.push(16); + + v.reserve_index(32); + assert!(v.capacity() >= 33) + } + + #[test] + fn test_reserve_index_exact() { + let mut v = Vec::new(); + assert_eq!(v.capacity(), 0); + + v.reserve_index_exact(2); + assert!(v.capacity() >= 3); + + for i in range(0i, 16) { + v.push(i); + } + + assert!(v.capacity() >= 16); + v.reserve_index_exact(16); + assert!(v.capacity() >= 17); + + v.push(16); + + v.reserve_index_exact(32); + assert!(v.capacity() >= 33) + } + #[test] fn test_extend() { let mut v = Vec::new(); diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 36e66ed27f3c9..d4485c06974d9 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -115,6 +115,105 @@ impl VecMap { VecMap { v: Vec::with_capacity(capacity) } } + /// Returns the number of elements the `VecMap` can hold without + /// reallocating. + /// + /// # Example + /// + /// ``` + /// use std::collections::VecMap; + /// let map: VecMap = VecMap::with_capacity(10); + /// assert_eq!(map.capacity(), 10); + /// ``` + #[inline] + #[stable] + pub fn capacity(&self) -> uint { + self.v.capacity() + } + + /// Reserves capacity for at least `additional` more elements to be inserted in the given + /// `VecMap`. The collection may reserve more space to avoid frequent reallocations. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. + /// + /// # Example + /// + /// ``` + /// use std::collections::VecMap; + /// let mut map: VecMap = VecMap::new(); + /// map.reserve(10); + /// assert!(map.capacity() >= 10); + /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve(&mut self, additional: uint) { + self.v.reserve(additional); + } + + /// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the + /// `VecMap`. The collection may reserve more space to avoid frequent reallocations. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. + /// + /// # Example + /// + /// ``` + /// use std::collections::VecMap; + /// let mut map: VecMap = VecMap::new(); + /// map.reserve_exact(10); + /// assert!(map.capacity() >= 10); + /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve_exact(&mut self, additional: uint) { + self.v.reserve_exact(additional); + } + + /// Reserves the minimum capacity for an element to be inserted at `index` in the given + /// `VecMap`. The collection may reserve more space to avoid frequent reallocations. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. + /// + /// # Example + /// + /// ``` + /// use std::collections::VecMap; + /// let mut map: VecMap = VecMap::new(); + /// map.reserve_index(10); + /// assert!(map.capacity() >= 11); + /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve_index(&mut self, index: uint) { + self.v.reserve_index(index); + } + + /// Reserves the minimum capacity for an element to be inserted at `index` in the + /// given `VecMap`. Does nothing if the capacity is already sufficient. + /// + /// Note that the allocator may give the collection more space than it requests. Therefore + /// capacity can not be relied upon to be precisely minimal. Prefer `reserve_index` if future + /// insertions are expected. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. + /// + /// # Example + /// + /// ``` + /// use std::collections::VecMap; + /// let mut map: VecMap = VecMap::new(); + /// map.reserve_index_exact(10); + /// assert!(map.capacity() >= 11); + /// ``` + pub fn reserve_index_exact(&mut self, index: uint) { + self.v.reserve_index_exact(index); + } + /// Returns an iterator visiting all keys in ascending order by the keys. /// The iterator's element type is `uint`. #[unstable = "matches collection reform specification, waiting for dust to settle"] @@ -698,6 +797,94 @@ mod test_map { assert_eq!(m.remove(&1), None); } + #[test] + fn test_reserve() { + let mut map = VecMap::new(); + assert_eq!(map.capacity(), 0); + + map.reserve(2); + assert!(map.capacity() >= 2); + + for i in range(0u, 16) { + map.insert(i, "a"); + } + + assert!(map.capacity() >= 16); + map.reserve(16); + assert!(map.capacity() >= 32); + + map.insert(16,"b"); + + map.reserve(16); + assert!(map.capacity() >= 33) + } + + #[test] + fn test_reserve_exact() { + let mut map = VecMap::new(); + assert_eq!(map.capacity(), 0); + + map.reserve_exact(2); + assert!(map.capacity() >= 2); + + for i in range(0u, 16) { + map.insert(i, "a"); + } + + assert!(map.capacity() >= 16); + map.reserve_exact(16); + assert!(map.capacity() >= 32); + + map.insert(16,"b"); + + map.reserve_exact(16); + assert!(map.capacity() >= 33) + } + + #[test] + fn test_reserve_index() { + let mut map = VecMap::new(); + assert_eq!(map.capacity(), 0); + + map.reserve_index(2); + assert!(map.capacity() >= 3); + + for i in range(0u, 16) { + map.insert(i, "a"); + } + + assert!(map.capacity() >= 16); + map.reserve_index(16); + assert!(map.capacity() >= 17); + + map.insert(16,"b"); + + map.reserve_index(32); + assert!(map.capacity() >= 33) + } + + #[test] + fn test_reserve_index_exact() { + let mut map = VecMap::new(); + assert_eq!(map.capacity(), 0); + + map.reserve_index_exact(2); + assert!(map.capacity() >= 3); + + for i in range(0u, 16) { + map.insert(i, "a"); + } + + assert!(map.capacity() >= 16); + map.reserve_index_exact(16); + assert!(map.capacity() >= 17); + + map.insert(16,"b"); + + map.reserve_index_exact(32); + assert!(map.capacity() >= 33) + } + #[test] fn test_keys() { let mut map = VecMap::new(); @@ -1046,3 +1233,4 @@ mod bench { |m, i| { m.get(&i); }); } } +