22//! pairs is independent of the hash values of the keys.
33
44mod core;
5+ mod slice;
56
7+ pub use self :: slice:: Slice ;
68pub use crate :: mutable_keys:: MutableKeys ;
79
810#[ cfg( feature = "rayon" ) ]
@@ -15,13 +17,14 @@ use ::core::hash::{BuildHasher, Hash, Hasher};
1517use :: core:: iter:: FusedIterator ;
1618use :: core:: ops:: { Index , IndexMut , RangeBounds } ;
1719use :: core:: slice:: { Iter as SliceIter , IterMut as SliceIterMut } ;
20+ use alloc:: boxed:: Box ;
1821
1922#[ cfg( feature = "std" ) ]
2023use std:: collections:: hash_map:: RandomState ;
2124
2225use self :: core:: IndexMapCore ;
2326use crate :: equivalent:: Equivalent ;
24- use crate :: util:: third;
27+ use crate :: util:: { third, try_simplify_range } ;
2528use crate :: { Bucket , Entries , HashValue } ;
2629
2730pub use self :: core:: { Entry , OccupiedEntry , VacantEntry } ;
@@ -758,6 +761,27 @@ where
758761}
759762
760763impl < K , V , S > IndexMap < K , V , S > {
764+ /// Returns a slice of all the key-value pairs in the map.
765+ ///
766+ /// Computes in **O(1)** time.
767+ pub fn as_slice ( & self ) -> & Slice < K , V > {
768+ Slice :: from_slice ( self . as_entries ( ) )
769+ }
770+
771+ /// Returns a mutable slice of all the key-value pairs in the map.
772+ ///
773+ /// Computes in **O(1)** time.
774+ pub fn as_mut_slice ( & mut self ) -> & mut Slice < K , V > {
775+ Slice :: from_mut_slice ( self . as_entries_mut ( ) )
776+ }
777+
778+ /// Converts into a boxed slice of all the key-value pairs in the map.
779+ ///
780+ /// Note that this will drop the inner hash table and any excess capacity.
781+ pub fn into_boxed_slice ( self ) -> Box < Slice < K , V > > {
782+ Slice :: from_boxed ( self . into_entries ( ) . into_boxed_slice ( ) )
783+ }
784+
761785 /// Get a key-value pair by index
762786 ///
763787 /// Valid indices are *0 <= index < self.len()*
@@ -776,6 +800,28 @@ impl<K, V, S> IndexMap<K, V, S> {
776800 self . as_entries_mut ( ) . get_mut ( index) . map ( Bucket :: ref_mut)
777801 }
778802
803+ /// Returns a slice of key-value pairs in the given range of indices.
804+ ///
805+ /// Valid indices are *0 <= index < self.len()*
806+ ///
807+ /// Computes in **O(1)** time.
808+ pub fn get_range < R : RangeBounds < usize > > ( & self , range : R ) -> Option < & Slice < K , V > > {
809+ let entries = self . as_entries ( ) ;
810+ let range = try_simplify_range ( range, entries. len ( ) ) ?;
811+ entries. get ( range) . map ( Slice :: from_slice)
812+ }
813+
814+ /// Returns a mutable slice of key-value pairs in the given range of indices.
815+ ///
816+ /// Valid indices are *0 <= index < self.len()*
817+ ///
818+ /// Computes in **O(1)** time.
819+ pub fn get_range_mut < R : RangeBounds < usize > > ( & mut self , range : R ) -> Option < & mut Slice < K , V > > {
820+ let entries = self . as_entries_mut ( ) ;
821+ let range = try_simplify_range ( range, entries. len ( ) ) ?;
822+ entries. get_mut ( range) . map ( Slice :: from_mut_slice)
823+ }
824+
779825 /// Get the first key-value pair
780826 ///
781827 /// Computes in **O(1)** time.
@@ -846,7 +892,7 @@ impl<K, V, S> IndexMap<K, V, S> {
846892/// [`keys`]: struct.IndexMap.html#method.keys
847893/// [`IndexMap`]: struct.IndexMap.html
848894pub struct Keys < ' a , K , V > {
849- pub ( crate ) iter : SliceIter < ' a , Bucket < K , V > > ,
895+ iter : SliceIter < ' a , Bucket < K , V > > ,
850896}
851897
852898impl < ' a , K , V > Iterator for Keys < ' a , K , V > {
@@ -1045,6 +1091,13 @@ pub struct Iter<'a, K, V> {
10451091 iter : SliceIter < ' a , Bucket < K , V > > ,
10461092}
10471093
1094+ impl < ' a , K , V > Iter < ' a , K , V > {
1095+ /// Returns a slice of the remaining entries in the iterator.
1096+ pub fn as_slice ( & self ) -> & ' a Slice < K , V > {
1097+ Slice :: from_slice ( self . iter . as_slice ( ) )
1098+ }
1099+ }
1100+
10481101impl < ' a , K , V > Iterator for Iter < ' a , K , V > {
10491102 type Item = ( & ' a K , & ' a V ) ;
10501103
@@ -1089,6 +1142,15 @@ pub struct IterMut<'a, K, V> {
10891142 iter : SliceIterMut < ' a , Bucket < K , V > > ,
10901143}
10911144
1145+ impl < ' a , K , V > IterMut < ' a , K , V > {
1146+ /// Returns a slice of the remaining entries in the iterator.
1147+ ///
1148+ /// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
1149+ pub fn into_slice ( self ) -> & ' a mut Slice < K , V > {
1150+ Slice :: from_mut_slice ( self . iter . into_slice ( ) )
1151+ }
1152+ }
1153+
10921154impl < ' a , K , V > Iterator for IterMut < ' a , K , V > {
10931155 type Item = ( & ' a K , & ' a mut V ) ;
10941156
@@ -1122,7 +1184,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
11221184/// [`into_iter`]: struct.IndexMap.html#method.into_iter
11231185/// [`IndexMap`]: struct.IndexMap.html
11241186pub struct IntoIter < K , V > {
1125- pub ( crate ) iter : vec:: IntoIter < Bucket < K , V > > ,
1187+ iter : vec:: IntoIter < Bucket < K , V > > ,
11261188}
11271189
11281190impl < K , V > Iterator for IntoIter < K , V > {
0 commit comments