Skip to content

Commit

Permalink
feat(indexmap): added truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Dec 28, 2024
1 parent 0ebca23 commit fc8a24f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,30 @@ 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);
/// assert_eq!(map.get_index(0), Some((&1, &"a")));
/// assert_eq!(map.get_index(1), Some((&2, &"b")));
/// ```
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

0 comments on commit fc8a24f

Please sign in to comment.