Skip to content

Commit

Permalink
std: Change the behavior of reserve for HashMap.
Browse files Browse the repository at this point in the history
HashMap's `reserve` method now takes as an argument the *extra* space
to reserve.

[breaking-change]
  • Loading branch information
pczarn committed Nov 30, 2014
1 parent 72c96ba commit b82624b
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/libstd/collections/hash/map.rs
Expand Up @@ -587,9 +587,13 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
self.resize_policy.usable_capacity(self.table.capacity())
}

/// Reserves capacity for at least `additional` more elements to be inserted
/// in the `HashMap`. The collection may reserve more space to avoid
/// frequent reallocations.
///
/// This function has no effect on the operational semantics of the
/// hashtable, only on performance.
/// # Panics
///
/// Panics if the new allocation size overflows `uint`.
///
/// # Example
///
Expand All @@ -598,11 +602,18 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
/// let mut map: HashMap<&str, int> = HashMap::new();
/// map.reserve(10);
/// ```
pub fn reserve(&mut self, new_minimum_capacity: uint) {
let cap = max(INITIAL_CAPACITY, new_minimum_capacity).next_power_of_two();
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn reserve(&mut self, additional: uint) {
let new_size = self.len().checked_add(additional).expect("capacity overflow");
let min_cap = self.resize_policy.min_capacity(new_size);

// An invalid value shouldn't make us run out of space. This includes
// an overflow check.
assert!(new_size <= min_cap);

if self.table.capacity() < cap {
self.resize(cap);
if self.table.capacity() < min_cap {
let new_capacity = max(min_cap.next_power_of_two(), INITIAL_CAPACITY);
self.resize(new_capacity);
}
}

Expand Down

9 comments on commit b82624b

@bors
Copy link
Contributor

@bors bors commented on b82624b Nov 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Gankro
at pczarn@b82624b

@bors
Copy link
Contributor

@bors bors commented on b82624b Nov 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging pczarn/rust/hash_map-explicit-shrinking = b82624b into auto

@bors
Copy link
Contributor

@bors bors commented on b82624b Nov 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pczarn/rust/hash_map-explicit-shrinking = b82624b merged ok, testing candidate = 680d1fb4

@bors
Copy link
Contributor

@bors bors commented on b82624b Dec 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on b82624b Dec 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Gankro
at pczarn@b82624b

@bors
Copy link
Contributor

@bors bors commented on b82624b Dec 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging pczarn/rust/hash_map-explicit-shrinking = b82624b into auto

@bors
Copy link
Contributor

@bors bors commented on b82624b Dec 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pczarn/rust/hash_map-explicit-shrinking = b82624b merged ok, testing candidate = 207a508

@bors
Copy link
Contributor

@bors bors commented on b82624b Dec 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on b82624b Dec 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 207a508

Please sign in to comment.