@@ -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,34 @@ impl<K, V, S> IndexMap<K, V, S> {
693717 self . as_entries_mut ( ) . get_mut ( index) . map ( Bucket :: muts)
694718 }
695719
720+ /// Get the first key-value pair
721+ ///
722+ /// Computes in **O(1)** time.
723+ pub fn first ( & self ) -> Option < ( & K , & V ) > {
724+ self . as_entries ( ) . first ( ) . map ( Bucket :: refs)
725+ }
726+
727+ /// Get the first key-value pair, with mutable access to the value
728+ ///
729+ /// Computes in **O(1)** time.
730+ pub fn first_mut ( & mut self ) -> Option < ( & K , & mut V ) > {
731+ self . as_entries_mut ( ) . first_mut ( ) . map ( Bucket :: ref_mut)
732+ }
733+
734+ /// Get the last key-value pair
735+ ///
736+ /// Computes in **O(1)** time.
737+ pub fn last ( & self ) -> Option < ( & K , & V ) > {
738+ self . as_entries ( ) . last ( ) . map ( Bucket :: refs)
739+ }
740+
741+ /// Get the last key-value pair, with mutable access to the value
742+ ///
743+ /// Computes in **O(1)** time.
744+ pub fn last_mut ( & mut self ) -> Option < ( & K , & mut V ) > {
745+ self . as_entries_mut ( ) . last_mut ( ) . map ( Bucket :: ref_mut)
746+ }
747+
696748 /// Remove the key-value pair by index
697749 ///
698750 /// Valid indices are *0 <= index < self.len()*
@@ -718,6 +770,13 @@ impl<K, V, S> IndexMap<K, V, S> {
718770 pub fn shift_remove_index ( & mut self , index : usize ) -> Option < ( K , V ) > {
719771 self . core . shift_remove_index ( index)
720772 }
773+
774+ /// Swaps the position of two key-value pairs in the map.
775+ ///
776+ /// ***Panics*** if `a` or `b` are out of bounds.
777+ pub fn swap_indices ( & mut self , a : usize , b : usize ) {
778+ self . core . swap_indices ( a, b)
779+ }
721780}
722781
723782/// An iterator over the keys of a `IndexMap`.
0 commit comments