@@ -252,6 +252,13 @@ impl<K, V, S> IndexMap<K, V, S> {
252252 self . core . clear ( ) ;
253253 }
254254
255+ /// Shortens the map, keeping the first `len` elements and dropping the rest.
256+ ///
257+ /// If `len` is greater than the map's current length, this has no effect.
258+ pub fn truncate ( & mut self , len : usize ) {
259+ self . core . truncate ( len) ;
260+ }
261+
255262 /// Clears the `IndexMap` in the given index range, returning those
256263 /// key-value pairs as a drain iterator.
257264 ///
@@ -273,6 +280,23 @@ impl<K, V, S> IndexMap<K, V, S> {
273280 iter : self . core . drain ( range) ,
274281 }
275282 }
283+
284+ /// Splits the collection into two at the given index.
285+ ///
286+ /// Returns a newly allocated map containing the elements in the range
287+ /// `[at, len)`. After the call, the original map will be left containing
288+ /// the elements `[0, at)` with its previous capacity unchanged.
289+ ///
290+ /// ***Panics*** if `at > len`.
291+ pub fn split_off ( & mut self , at : usize ) -> Self
292+ where
293+ S : Clone ,
294+ {
295+ Self {
296+ core : self . core . split_off ( at) ,
297+ hash_builder : self . hash_builder . clone ( ) ,
298+ }
299+ }
276300}
277301
278302impl < K , V , S > IndexMap < K , V , S >
@@ -693,6 +717,22 @@ impl<K, V, S> IndexMap<K, V, S> {
693717 self . as_entries_mut ( ) . get_mut ( index) . map ( Bucket :: muts)
694718 }
695719
720+ pub fn first ( & self ) -> Option < ( & K , & V ) > {
721+ self . as_entries ( ) . first ( ) . map ( Bucket :: refs)
722+ }
723+
724+ pub fn first_mut ( & mut self ) -> Option < ( & mut K , & mut V ) > {
725+ self . as_entries_mut ( ) . first_mut ( ) . map ( Bucket :: muts)
726+ }
727+
728+ pub fn last ( & self ) -> Option < ( & K , & V ) > {
729+ self . as_entries ( ) . last ( ) . map ( Bucket :: refs)
730+ }
731+
732+ pub fn last_mut ( & mut self ) -> Option < ( & mut K , & mut V ) > {
733+ self . as_entries_mut ( ) . last_mut ( ) . map ( Bucket :: muts)
734+ }
735+
696736 /// Remove the key-value pair by index
697737 ///
698738 /// Valid indices are *0 <= index < self.len()*
@@ -718,6 +758,13 @@ impl<K, V, S> IndexMap<K, V, S> {
718758 pub fn shift_remove_index ( & mut self , index : usize ) -> Option < ( K , V ) > {
719759 self . core . shift_remove_index ( index)
720760 }
761+
762+ /// Swaps the position of two key-value pairs in the map.
763+ ///
764+ /// ***Panics*** if `a` or `b` are out of bounds.
765+ pub fn swap_indices ( & mut self , a : usize , b : usize ) {
766+ self . core . swap_indices ( a, b)
767+ }
721768}
722769
723770/// An iterator over the keys of a `IndexMap`.
0 commit comments