From 59bacfda0c56423c8fb2cdd7b8f6926cd5034e9c Mon Sep 17 00:00:00 2001 From: Pavel Ivanov Date: Sat, 28 Dec 2024 09:43:17 +0100 Subject: [PATCH 1/3] feat(indexmap): added `truncate` --- CHANGELOG.md | 1 + src/indexmap.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6de5dab8..21eba6ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/indexmap.rs b/src/indexmap.rs index c0afe2b7..173998a7 100644 --- a/src/indexmap.rs +++ b/src/indexmap.rs @@ -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(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(&self, key: &Q) -> Option<(usize, usize)> From 455faace035afa89eb9214c7f7b5323049d93a27 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 6 Apr 2025 00:10:08 +0200 Subject: [PATCH 2/3] Fix formatting. --- src/indexmap.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/indexmap.rs b/src/indexmap.rs index 173998a7..8b9685fc 100644 --- a/src/indexmap.rs +++ b/src/indexmap.rs @@ -1169,9 +1169,9 @@ where self.core.retain_in_order(move |k, v| f(k, v)); } - /// Shortens the map, keeping the first len elements and dropping the rest. + /// 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. + /// If `len` is greater than the map's current length, this has no effect. /// /// Computes in *O*(1) time (average). /// From 1e7f5b8d0c0bef179cc1e6032d81fee5b2fa4c1b Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 6 Apr 2025 00:17:38 +0200 Subject: [PATCH 3/3] Make example easier to follow. --- src/indexmap.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/indexmap.rs b/src/indexmap.rs index 8b9685fc..574d4eb0 100644 --- a/src/indexmap.rs +++ b/src/indexmap.rs @@ -1181,14 +1181,14 @@ where /// use heapless::FnvIndexMap; /// /// let mut map = FnvIndexMap::<_, _, 8>::new(); - /// map.insert(1, "a").unwrap(); + /// map.insert(3, "a").unwrap(); /// map.insert(2, "b").unwrap(); - /// map.insert(3, "c").unwrap(); + /// map.insert(1, "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((&3, &"a"))); /// assert_eq!(iter.next(), Some((&2, &"b"))); /// assert_eq!(iter.next(), None); /// ```