diff options
| author | bors <bors@rust-lang.org> | 2014-12-27 04:41:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-12-27 04:41:53 +0000 |
| commit | 18842f89f084c52588fe7cffe07f87bf6e90796a (patch) | |
| tree | e1f0dbd9292f20f1ec689828a8218ca24d50e16c | |
| parent | 3c60bc02ce6de2823d0b837f90d5db0077fce6f7 (diff) | |
| parent | 868acdf7f33c1c0123231a07200fb002cc7509d2 (diff) | |
| download | rust-18842f89f084c52588fe7cffe07f87bf6e90796a.tar.gz rust-18842f89f084c52588fe7cffe07f87bf6e90796a.zip | |
auto merge of #20143 : csouth3/rust/vecmap-reserve, r=Gankro
Implement `reserve_len` and `reserve_len_exact` for `VecMap` in accordance with rust-lang/rfcs#509.
| -rw-r--r-- | src/libcollections/vec_map.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 659c68c42dd..5ebcc736624 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -138,6 +138,52 @@ impl<V> VecMap<V> { self.v.capacity() } + /// Reserves capacity for the given `VecMap` to contain `len` distinct keys. + /// In the case of `VecMap` this means reallocations will not occur as long + /// as all inserted keys are less than `len`. + /// + /// The collection may reserve more space to avoid frequent reallocations. + /// + /// # Examples + /// + /// ``` + /// use std::collections::VecMap; + /// let mut map: VecMap<&str> = VecMap::new(); + /// map.reserve_len(10); + /// assert!(map.capacity() >= 10); + /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve_len(&mut self, len: uint) { + let cur_len = self.v.len(); + if len >= cur_len { + self.v.reserve(len - cur_len); + } + } + + /// Reserves the minimum capacity for the given `VecMap` to contain `len` distinct keys. + /// In the case of `VecMap` this means reallocations will not occur as long as all inserted + /// keys are less than `len`. + /// + /// Note that the allocator may give the collection more space than it requests. + /// Therefore capacity cannot be relied upon to be precisely minimal. Prefer + /// `reserve_len` if future insertions are expected. + /// + /// # Examples + /// + /// ``` + /// use std::collections::VecMap; + /// let mut map: VecMap<&str> = VecMap::new(); + /// map.reserve_len_exact(10); + /// assert!(map.capacity() >= 10); + /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve_len_exact(&mut self, len: uint) { + let cur_len = self.v.len(); + if len >= cur_len { + self.v.reserve_exact(len - cur_len); + } + } + /// 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"] |
