Skip to content

Commit

Permalink
OrderedLots::truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Oct 4, 2024
1 parent 5e6a958 commit a3dde14
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- `OrderedLots::clear()` removes all entries from the collection.
- `OrderedLots::truncate()` truncates the collection's length.

## v0.3.1 (2023-12-02)

Expand Down
12 changes: 12 additions & 0 deletions src/ordered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ impl<T> OrderedLots<T> {
self.order.clear();
}

/// Sets this collection's length to `new_len` if it is less than or equal
/// to the current length.
pub fn truncate(&mut self, new_len: usize) {
if new_len == 0 {
self.clear();
} else if new_len <= self.order.len() {
for to_remove in self.order.drain(new_len..) {
self.slots.remove(to_remove);
}
}
}

/// Orders the elements in this collection leveraging the standard library's
/// sorting implementation. See [`slice::sort()`] for more information.
#[inline]
Expand Down

0 comments on commit a3dde14

Please sign in to comment.