about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndrew Poelstra <apoelstra@wpsoftware.net>2014-06-30 13:05:05 -0700
committerAndrew Poelstra <apoelstra@wpsoftware.net>2014-07-02 12:36:02 -0700
commit7a7ae993ce694bf75a11632b394916e055a4d8ec (patch)
treed98130c7d7839d812ba7e9af82d9ef3048e598a3
parenta698b81ebfe02a614f9b41e68c6b604597a81229 (diff)
downloadrust-7a7ae993ce694bf75a11632b394916e055a4d8ec.tar.gz
rust-7a7ae993ce694bf75a11632b394916e055a4d8ec.zip
collections::bitv: correct use of Vec<T>::grow
The argument passed to Vec::grow is the number of elements to grow
the vector by, not the target number of elements. The old `Bitv`
code did the wrong thing, allocating more memory than it needed to.
-rw-r--r--src/libcollections/bitv.rs13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs
index b480b88b4d4..18e3390dff5 100644
--- a/src/libcollections/bitv.rs
+++ b/src/libcollections/bitv.rs
@@ -515,9 +515,10 @@ impl BitvSet {
     /// Grows the vector to be able to store bits with indices `[0, size - 1]`
     fn grow(&mut self, size: uint) {
         let &BitvSet(ref mut bitv) = self;
+        let old_size = bitv.storage.len();
         let size = (size + uint::BITS - 1) / uint::BITS;
-        if bitv.storage.len() < size {
-            bitv.storage.grow(size, &0);
+        if old_size < size {
+            bitv.storage.grow(size - old_size, &0);
         }
     }
 
@@ -1253,14 +1254,22 @@ mod tests {
 
     #[test]
     fn test_bitv_set_basic() {
+        // calculate nbits with uint::BITS granularity
+        fn calc_nbits(bits: uint) -> uint {
+            uint::BITS * ((bits + uint::BITS - 1) / uint::BITS)
+        }
+
         let mut b = BitvSet::new();
+        assert_eq!(b.capacity(), calc_nbits(0));
         assert!(b.insert(3));
+        assert_eq!(b.capacity(), calc_nbits(3));
         assert!(!b.insert(3));
         assert!(b.contains(&3));
         assert!(b.insert(4));
         assert!(!b.insert(4));
         assert!(b.contains(&3));
         assert!(b.insert(400));
+        assert_eq!(b.capacity(), calc_nbits(400));
         assert!(!b.insert(400));
         assert!(b.contains(&400));
         assert_eq!(b.len(), 3);