Skip to content

Commit 8b06fd2

Browse files
committed
---
yaml --- r: 161723 b: refs/heads/master c: b82624b h: refs/heads/master i: 161721: 45a1312 161719: e3d7a71 v: v3
1 parent 7b81fc9 commit 8b06fd2

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 72c96badd277f1090d7a70fdca37b547b6ba000f
2+
refs/heads/master: b82624bf205e83555d7764d9f849fbfd30df0083
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cafe2966770ff377aad6dd9fd808e68055587c58
55
refs/heads/try: 0f0d21c1eb5c7be04d323e0b06faf252ad790af6

trunk/src/libstd/collections/hash/map.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,13 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
587587
self.resize_policy.usable_capacity(self.table.capacity())
588588
}
589589

590+
/// Reserves capacity for at least `additional` more elements to be inserted
591+
/// in the `HashMap`. The collection may reserve more space to avoid
592+
/// frequent reallocations.
590593
///
591-
/// This function has no effect on the operational semantics of the
592-
/// hashtable, only on performance.
594+
/// # Panics
595+
///
596+
/// Panics if the new allocation size overflows `uint`.
593597
///
594598
/// # Example
595599
///
@@ -598,11 +602,18 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
598602
/// let mut map: HashMap<&str, int> = HashMap::new();
599603
/// map.reserve(10);
600604
/// ```
601-
pub fn reserve(&mut self, new_minimum_capacity: uint) {
602-
let cap = max(INITIAL_CAPACITY, new_minimum_capacity).next_power_of_two();
605+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
606+
pub fn reserve(&mut self, additional: uint) {
607+
let new_size = self.len().checked_add(additional).expect("capacity overflow");
608+
let min_cap = self.resize_policy.min_capacity(new_size);
609+
610+
// An invalid value shouldn't make us run out of space. This includes
611+
// an overflow check.
612+
assert!(new_size <= min_cap);
603613

604-
if self.table.capacity() < cap {
605-
self.resize(cap);
614+
if self.table.capacity() < min_cap {
615+
let new_capacity = max(min_cap.next_power_of_two(), INITIAL_CAPACITY);
616+
self.resize(new_capacity);
606617
}
607618
}
608619

0 commit comments

Comments
 (0)