fn reserve(&mut self, n: uint);
fn reserve_at_least(&mut self, n: uint);
fn capacity(&self) -> uint;
+ fn shrink_to_fit(&mut self);
fn push(&mut self, t: T);
unsafe fn push_fast(&mut self, t: T);
*
* * n - The number of elements to reserve space for
*/
+ #[inline]
fn reserve_at_least(&mut self, n: uint) {
self.reserve(uint::next_power_of_two(n));
}
}
}
+ /// Shrink the capacity of the vector to match the length
+ fn shrink_to_fit(&mut self) {
+ unsafe {
+ let ptr: *mut *mut Vec<()> = cast::transmute(self);
+ let alloc = (**ptr).fill;
+ let size = alloc + sys::size_of::<Vec<()>>();
+ *ptr = realloc_raw(*ptr as *mut c_void, size) as *mut Vec<()>;
+ (**ptr).alloc = alloc;
+ }
+ }
+
/// Append an element to a vector
#[inline]
fn push(&mut self, t: T) {
use sys;
use vec::*;
use cmp::*;
+ use prelude::*;
fn square(n: uint) -> uint { n * n }
}
assert!(cnt == 3);
}
+
+ #[test]
+ fn test_shrink_to_fit() {
+ let mut xs = ~[0, 1, 2, 3];
+ for i in range(4, 100) {
+ xs.push(i)
+ }
+ assert_eq!(xs.capacity(), 128);
+ xs.shrink_to_fit();
+ assert_eq!(xs.capacity(), 100);
+ assert_eq!(xs, range(0, 100).to_owned_vec());
+ }
}
#[cfg(test)]