2
2
//! pairs is independent of the hash values of the keys.
3
3
4
4
mod core;
5
+ mod slice;
5
6
7
+ pub use self :: slice:: Slice ;
6
8
pub use crate :: mutable_keys:: MutableKeys ;
7
9
8
10
#[ cfg( feature = "rayon" ) ]
@@ -15,13 +17,14 @@ use ::core::hash::{BuildHasher, Hash, Hasher};
15
17
use :: core:: iter:: FusedIterator ;
16
18
use :: core:: ops:: { Index , IndexMut , RangeBounds } ;
17
19
use :: core:: slice:: { Iter as SliceIter , IterMut as SliceIterMut } ;
20
+ use alloc:: boxed:: Box ;
18
21
19
22
#[ cfg( feature = "std" ) ]
20
23
use std:: collections:: hash_map:: RandomState ;
21
24
22
25
use self :: core:: IndexMapCore ;
23
26
use crate :: equivalent:: Equivalent ;
24
- use crate :: util:: third;
27
+ use crate :: util:: { third, try_simplify_range } ;
25
28
use crate :: { Bucket , Entries , HashValue } ;
26
29
27
30
pub use self :: core:: { Entry , OccupiedEntry , VacantEntry } ;
@@ -758,6 +761,27 @@ where
758
761
}
759
762
760
763
impl < 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
+
761
785
/// Get a key-value pair by index
762
786
///
763
787
/// Valid indices are *0 <= index < self.len()*
@@ -776,6 +800,28 @@ impl<K, V, S> IndexMap<K, V, S> {
776
800
self . as_entries_mut ( ) . get_mut ( index) . map ( Bucket :: ref_mut)
777
801
}
778
802
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
+
779
825
/// Get the first key-value pair
780
826
///
781
827
/// Computes in **O(1)** time.
@@ -846,7 +892,7 @@ impl<K, V, S> IndexMap<K, V, S> {
846
892
/// [`keys`]: struct.IndexMap.html#method.keys
847
893
/// [`IndexMap`]: struct.IndexMap.html
848
894
pub struct Keys < ' a , K , V > {
849
- pub ( crate ) iter : SliceIter < ' a , Bucket < K , V > > ,
895
+ iter : SliceIter < ' a , Bucket < K , V > > ,
850
896
}
851
897
852
898
impl < ' a , K , V > Iterator for Keys < ' a , K , V > {
@@ -1045,6 +1091,13 @@ pub struct Iter<'a, K, V> {
1045
1091
iter : SliceIter < ' a , Bucket < K , V > > ,
1046
1092
}
1047
1093
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
+
1048
1101
impl < ' a , K , V > Iterator for Iter < ' a , K , V > {
1049
1102
type Item = ( & ' a K , & ' a V ) ;
1050
1103
@@ -1089,6 +1142,15 @@ pub struct IterMut<'a, K, V> {
1089
1142
iter : SliceIterMut < ' a , Bucket < K , V > > ,
1090
1143
}
1091
1144
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
+
1092
1154
impl < ' a , K , V > Iterator for IterMut < ' a , K , V > {
1093
1155
type Item = ( & ' a K , & ' a mut V ) ;
1094
1156
@@ -1122,7 +1184,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
1122
1184
/// [`into_iter`]: struct.IndexMap.html#method.into_iter
1123
1185
/// [`IndexMap`]: struct.IndexMap.html
1124
1186
pub struct IntoIter < K , V > {
1125
- pub ( crate ) iter : vec:: IntoIter < Bucket < K , V > > ,
1187
+ iter : vec:: IntoIter < Bucket < K , V > > ,
1126
1188
}
1127
1189
1128
1190
impl < K , V > Iterator for IntoIter < K , V > {
0 commit comments