@@ -1169,6 +1169,33 @@ where
1169
1169
self . core . retain_in_order ( move |k, v| f ( k, v) ) ;
1170
1170
}
1171
1171
1172
+ /// Shortens the map, keeping the first `len` elements and dropping the rest.
1173
+ ///
1174
+ /// If `len` is greater than the map's current length, this has no effect.
1175
+ ///
1176
+ /// Computes in *O*(1) time (average).
1177
+ ///
1178
+ /// # Examples
1179
+ ///
1180
+ /// ```
1181
+ /// use heapless::FnvIndexMap;
1182
+ ///
1183
+ /// let mut map = FnvIndexMap::<_, _, 8>::new();
1184
+ /// map.insert(3, "a").unwrap();
1185
+ /// map.insert(2, "b").unwrap();
1186
+ /// map.insert(1, "c").unwrap();
1187
+ /// map.truncate(2);
1188
+ /// assert_eq!(map.len(), 2);
1189
+ ///
1190
+ /// let mut iter = map.iter();
1191
+ /// assert_eq!(iter.next(), Some((&3, &"a")));
1192
+ /// assert_eq!(iter.next(), Some((&2, &"b")));
1193
+ /// assert_eq!(iter.next(), None);
1194
+ /// ```
1195
+ pub fn truncate ( & mut self , len : usize ) {
1196
+ self . core . entries . truncate ( len) ;
1197
+ }
1198
+
1172
1199
/* Private API */
1173
1200
/// Return probe (indices) and position (entries)
1174
1201
fn find < Q > ( & self , key : & Q ) -> Option < ( usize , usize ) >
0 commit comments