mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-29 21:40:27 +00:00
Merge pull request #526 from pamburus/feature/indexmap/truncate
feat(indexmap): added `truncate`
This commit is contained in:
commit
0c6728cc53
@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
- Added `Deque::{swap, swap_unchecked, swap_remove_front, swap_remove_back}`.
|
||||
- Make `String::from_utf8_unchecked` const.
|
||||
- Implemented `PartialEq` and `Eq` for `Deque`.
|
||||
- Added `truncate` to `IndexMap`.
|
||||
|
||||
### Changed
|
||||
|
||||
|
@ -1169,6 +1169,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(3, "a").unwrap();
|
||||
/// map.insert(2, "b").unwrap();
|
||||
/// map.insert(1, "c").unwrap();
|
||||
/// map.truncate(2);
|
||||
/// assert_eq!(map.len(), 2);
|
||||
///
|
||||
/// let mut iter = map.iter();
|
||||
/// assert_eq!(iter.next(), Some((&3, &"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)>
|
||||
|
Loading…
x
Reference in New Issue
Block a user