@@ -23,7 +23,7 @@ use std::collections::hash_map::RandomState;
2323
2424use self :: core:: IndexMapCore ;
2525use crate :: equivalent:: Equivalent ;
26- use crate :: util:: third;
26+ use crate :: util:: { third, try_simplify_range } ;
2727use crate :: { Bucket , Entries , HashValue } ;
2828
2929pub use self :: core:: { Entry , OccupiedEntry , VacantEntry } ;
@@ -760,6 +760,20 @@ where
760760}
761761
762762impl < K , V , S > IndexMap < K , V , S > {
763+ /// Returns a slice of all the key-value pairs in the map.
764+ ///
765+ /// Computes in **O(1)** time.
766+ pub fn as_slice ( & self ) -> & Slice < K , V > {
767+ Slice :: from_slice ( self . as_entries ( ) )
768+ }
769+
770+ /// Returns a mutable slice of all the key-value pairs in the map.
771+ ///
772+ /// Computes in **O(1)** time.
773+ pub fn as_mut_slice ( & mut self ) -> & mut Slice < K , V > {
774+ Slice :: from_mut_slice ( self . as_entries_mut ( ) )
775+ }
776+
763777 /// Get a key-value pair by index
764778 ///
765779 /// Valid indices are *0 <= index < self.len()*
@@ -778,6 +792,28 @@ impl<K, V, S> IndexMap<K, V, S> {
778792 self . as_entries_mut ( ) . get_mut ( index) . map ( Bucket :: ref_mut)
779793 }
780794
795+ /// Returns a slice of key-value pairs in the given range of indices.
796+ ///
797+ /// Valid indices are *0 <= index < self.len()*
798+ ///
799+ /// Computes in **O(1)** time.
800+ pub fn get_range < R : RangeBounds < usize > > ( & self , range : R ) -> Option < & Slice < K , V > > {
801+ let entries = self . as_entries ( ) ;
802+ let range = try_simplify_range ( range, entries. len ( ) ) ?;
803+ entries. get ( range) . map ( Slice :: from_slice)
804+ }
805+
806+ /// Returns a mutable slice of key-value pairs in the given range of indices.
807+ ///
808+ /// Valid indices are *0 <= index < self.len()*
809+ ///
810+ /// Computes in **O(1)** time.
811+ pub fn get_range_mut < R : RangeBounds < usize > > ( & mut self , range : R ) -> Option < & mut Slice < K , V > > {
812+ let entries = self . as_entries_mut ( ) ;
813+ let range = try_simplify_range ( range, entries. len ( ) ) ?;
814+ entries. get_mut ( range) . map ( Slice :: from_mut_slice)
815+ }
816+
781817 /// Get the first key-value pair
782818 ///
783819 /// Computes in **O(1)** time.
@@ -1047,6 +1083,13 @@ pub struct Iter<'a, K, V> {
10471083 iter : SliceIter < ' a , Bucket < K , V > > ,
10481084}
10491085
1086+ impl < ' a , K , V > Iter < ' a , K , V > {
1087+ /// Returns a slice of the remaining entries in the iterator.
1088+ pub fn as_slice ( & self ) -> & ' a Slice < K , V > {
1089+ Slice :: from_slice ( self . iter . as_slice ( ) )
1090+ }
1091+ }
1092+
10501093impl < ' a , K , V > Iterator for Iter < ' a , K , V > {
10511094 type Item = ( & ' a K , & ' a V ) ;
10521095
@@ -1091,6 +1134,15 @@ pub struct IterMut<'a, K, V> {
10911134 iter : SliceIterMut < ' a , Bucket < K , V > > ,
10921135}
10931136
1137+ impl < ' a , K , V > IterMut < ' a , K , V > {
1138+ /// Returns a slice of the remaining entries in the iterator.
1139+ ///
1140+ /// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
1141+ pub fn into_slice ( self ) -> & ' a mut Slice < K , V > {
1142+ Slice :: from_mut_slice ( self . iter . into_slice ( ) )
1143+ }
1144+ }
1145+
10941146impl < ' a , K , V > Iterator for IterMut < ' a , K , V > {
10951147 type Item = ( & ' a K , & ' a mut V ) ;
10961148
0 commit comments