Skip to content

Commit

Permalink
Add shrink_to
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Mar 29, 2022
1 parent 42fab99 commit 94d1197
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 3 additions & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
`MutableKeys::get_index_mut2` to access the former behavior.

- `IterMut` and `ValuesMut` now implement `Debug`.


- The new `IndexMap::shrink_to` and `IndexSet::shrink_to` methods shrink
the capacity with a lower bound.

- 1.8.1

Expand Down
9 changes: 8 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,14 @@ where
///
/// Computes in **O(n)** time.
pub fn shrink_to_fit(&mut self) {
self.core.shrink_to_fit();
self.core.shrink_to(0);
}

/// Shrink the capacity of the map with a lower limit.
///
/// Computes in **O(n)** time.
pub fn shrink_to(&mut self, min_capacity: usize) {
self.core.shrink_to(min_capacity);
}

fn hash<Q: ?Sized + Hash>(&self, key: &Q) -> HashValue {
Expand Down
8 changes: 4 additions & 4 deletions src/map/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ impl<K, V> IndexMapCore<K, V> {
self.entries.reserve_exact(additional);
}

/// Shrink the capacity of the map as much as possible.
pub(crate) fn shrink_to_fit(&mut self) {
self.indices.shrink_to(0, get_hash(&self.entries));
self.entries.shrink_to_fit();
/// Shrink the capacity of the map with a lower bound
pub(crate) fn shrink_to(&mut self, min_capacity: usize) {
self.indices.shrink_to(min_capacity, get_hash(&self.entries));
self.entries.shrink_to(min_capacity);
}

/// Remove the last key-value pair
Expand Down
7 changes: 7 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ where
self.map.shrink_to_fit();
}

/// Shrink the capacity of the set with a lower limit.
///
/// Computes in **O(n)** time.
pub fn shrink_to(&mut self, min_capacity: usize) {
self.map.shrink_to(min_capacity);
}

/// Insert the value into the set.
///
/// If an equivalent item already exists in the set, it returns
Expand Down

0 comments on commit 94d1197

Please sign in to comment.