Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(indexmap): added truncate #526

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `SortedLinkedListView`, the `!Sized` version of `SortedLinkedList`.
- Added implementation of `Borrow` and `BorrowMut` for `String` and `Vec`.
- Added `Deque::{swap, swap_unchecked, swap_remove_front, swap_remove_back}`.
- Added `truncate` to `IndexMap`

### Changed

Expand Down
27 changes: 27 additions & 0 deletions src/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,33 @@ where
self.core.retain_in_order(move |k, v| f(k, v));
}

/// Shortens the map, keeping the first len elements and dropping the rest.
///
/// If len is greater than the map’s current length, this has no effect.
///
/// Computes in *O*(1) time (average).
///
/// # Examples
///
/// ```
/// use heapless::FnvIndexMap;
///
/// let mut map = FnvIndexMap::<_, _, 8>::new();
/// map.insert(1, "a").unwrap();
/// map.insert(2, "b").unwrap();
/// map.insert(3, "c").unwrap();
/// map.truncate(2);
/// assert_eq!(map.len(), 2);
///
/// let mut iter = map.iter();
/// assert_eq!(iter.next(), Some((&1, &"a")));
/// assert_eq!(iter.next(), Some((&2, &"b")));
/// assert_eq!(iter.next(), None);
/// ```
pub fn truncate(&mut self, len: usize) {
self.core.entries.truncate(len);
}

/* Private API */
/// Return probe (indices) and position (entries)
fn find<Q>(&self, key: &Q) -> Option<(usize, usize)>
Expand Down
Loading