@@ -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 } ;
@@ -703,6 +703,20 @@ where
703703}
704704
705705impl < K , V , S > IndexMap < K , V , S > {
706+ /// Returns a slice of all the key-value pairs in the map.
707+ ///
708+ /// Computes in **O(1)** time.
709+ pub fn as_slice ( & self ) -> & Slice < K , V > {
710+ Slice :: from_slice ( self . as_entries ( ) )
711+ }
712+
713+ /// Returns a mutable slice of all the key-value pairs in the map.
714+ ///
715+ /// Computes in **O(1)** time.
716+ pub fn as_mut_slice ( & mut self ) -> & mut Slice < K , V > {
717+ Slice :: from_mut_slice ( self . as_entries_mut ( ) )
718+ }
719+
706720 /// Get a key-value pair by index
707721 ///
708722 /// Valid indices are *0 <= index < self.len()*
@@ -721,6 +735,28 @@ impl<K, V, S> IndexMap<K, V, S> {
721735 self . as_entries_mut ( ) . get_mut ( index) . map ( Bucket :: muts)
722736 }
723737
738+ /// Returns a slice of key-value pairs in the given range of indices.
739+ ///
740+ /// Valid indices are *0 <= index < self.len()*
741+ ///
742+ /// Computes in **O(1)** time.
743+ pub fn get_range < R : RangeBounds < usize > > ( & self , range : R ) -> Option < & Slice < K , V > > {
744+ let entries = self . as_entries ( ) ;
745+ let range = try_simplify_range ( range, entries. len ( ) ) ?;
746+ entries. get ( range) . map ( Slice :: from_slice)
747+ }
748+
749+ /// Returns a mutable slice of key-value pairs in the given range of indices.
750+ ///
751+ /// Valid indices are *0 <= index < self.len()*
752+ ///
753+ /// Computes in **O(1)** time.
754+ pub fn get_range_mut < R : RangeBounds < usize > > ( & mut self , range : R ) -> Option < & mut Slice < K , V > > {
755+ let entries = self . as_entries_mut ( ) ;
756+ let range = try_simplify_range ( range, entries. len ( ) ) ?;
757+ entries. get_mut ( range) . map ( Slice :: from_mut_slice)
758+ }
759+
724760 /// Get the first key-value pair
725761 ///
726762 /// Computes in **O(1)** time.
@@ -911,6 +947,13 @@ pub struct Iter<'a, K, V> {
911947 iter : SliceIter < ' a , Bucket < K , V > > ,
912948}
913949
950+ impl < ' a , K , V > Iter < ' a , K , V > {
951+ /// Returns a slice of the remaining entries in the iterator.
952+ pub fn as_slice ( & self ) -> & ' a Slice < K , V > {
953+ Slice :: from_slice ( self . iter . as_slice ( ) )
954+ }
955+ }
956+
914957impl < ' a , K , V > Iterator for Iter < ' a , K , V > {
915958 type Item = ( & ' a K , & ' a V ) ;
916959
@@ -955,6 +998,15 @@ pub struct IterMut<'a, K, V> {
955998 iter : SliceIterMut < ' a , Bucket < K , V > > ,
956999}
9571000
1001+ impl < ' a , K , V > IterMut < ' a , K , V > {
1002+ /// Returns a slice of the remaining entries in the iterator.
1003+ ///
1004+ /// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
1005+ pub fn into_slice ( self ) -> & ' a mut Slice < K , V > {
1006+ Slice :: from_mut_slice ( self . iter . into_slice ( ) )
1007+ }
1008+ }
1009+
9581010impl < ' a , K , V > Iterator for IterMut < ' a , K , V > {
9591011 type Item = ( & ' a K , & ' a mut V ) ;
9601012
0 commit comments