Skip to content

Commit 190f544

Browse files
committed
Add shrink_to
1 parent 9f17c85 commit 190f544

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

RELEASES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
`MutableKeys::get_index_mut2` to access the former behavior.
1616

1717
- `IterMut` and `ValuesMut` now implement `Debug`.
18-
18+
19+
- The new `IndexMap::shrink_to` and `IndexSet::shrink_to` methods shrink
20+
the capacity with a lower bound.
1921

2022
- 1.8.0
2123

src/map.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,14 @@ where
332332
///
333333
/// Computes in **O(n)** time.
334334
pub fn shrink_to_fit(&mut self) {
335-
self.core.shrink_to_fit();
335+
self.core.shrink_to(0);
336+
}
337+
338+
/// Shrink the capacity of the map with a lower limit.
339+
///
340+
/// Computes in **O(n)** time.
341+
pub fn shrink_to(&mut self, min_capacity: usize) {
342+
self.core.shrink_to(min_capacity);
336343
}
337344

338345
fn hash<Q: ?Sized + Hash>(&self, key: &Q) -> HashValue {

src/map/core.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ impl<K, V> IndexMapCore<K, V> {
201201
self.entries.reserve_exact(additional);
202202
}
203203

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

210210
/// Remove the last key-value pair

src/set.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ where
268268
self.map.shrink_to_fit();
269269
}
270270

271+
/// Shrink the capacity of the set with a lower limit.
272+
///
273+
/// Computes in **O(n)** time.
274+
pub fn shrink_to(&mut self, min_capacity: usize) {
275+
self.map.shrink_to(min_capacity);
276+
}
277+
271278
/// Insert the value into the set.
272279
///
273280
/// If an equivalent item already exists in the set, it returns

0 commit comments

Comments
 (0)