@@ -23,7 +23,7 @@ use std::collections::hash_map::RandomState;
23
23
24
24
use self :: core:: IndexMapCore ;
25
25
use crate :: equivalent:: Equivalent ;
26
- use crate :: util:: third;
26
+ use crate :: util:: { third, try_simplify_range } ;
27
27
use crate :: { Bucket , Entries , HashValue } ;
28
28
29
29
pub use self :: core:: { Entry , OccupiedEntry , VacantEntry } ;
@@ -760,6 +760,20 @@ where
760
760
}
761
761
762
762
impl < 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
+
763
777
/// Get a key-value pair by index
764
778
///
765
779
/// Valid indices are *0 <= index < self.len()*
@@ -778,6 +792,28 @@ impl<K, V, S> IndexMap<K, V, S> {
778
792
self . as_entries_mut ( ) . get_mut ( index) . map ( Bucket :: ref_mut)
779
793
}
780
794
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
+
781
817
/// Get the first key-value pair
782
818
///
783
819
/// Computes in **O(1)** time.
@@ -1047,6 +1083,13 @@ pub struct Iter<'a, K, V> {
1047
1083
iter : SliceIter < ' a , Bucket < K , V > > ,
1048
1084
}
1049
1085
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
+
1050
1093
impl < ' a , K , V > Iterator for Iter < ' a , K , V > {
1051
1094
type Item = ( & ' a K , & ' a V ) ;
1052
1095
@@ -1091,6 +1134,15 @@ pub struct IterMut<'a, K, V> {
1091
1134
iter : SliceIterMut < ' a , Bucket < K , V > > ,
1092
1135
}
1093
1136
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
+
1094
1146
impl < ' a , K , V > Iterator for IterMut < ' a , K , V > {
1095
1147
type Item = ( & ' a K , & ' a mut V ) ;
1096
1148
0 commit comments