@@ -8,13 +8,12 @@ pub use crate::mutable_keys::MutableKeys;
88#[ cfg( feature = "rayon" ) ]
99pub use crate :: rayon:: map as rayon;
1010
11- use crate :: vec :: { self , Vec } ;
11+ use crate :: EntryVec ;
1212use :: core:: cmp:: Ordering ;
1313use :: core:: fmt;
1414use :: core:: hash:: { BuildHasher , Hash , Hasher } ;
1515use :: core:: iter:: FromIterator ;
1616use :: core:: ops:: { Index , IndexMut , RangeFull } ;
17- use :: core:: slice:: { Iter as SliceIter , IterMut as SliceIterMut } ;
1817
1918#[ cfg( has_std) ]
2019use std:: collections:: hash_map:: RandomState ;
@@ -101,23 +100,23 @@ impl<K, V, S> Entries for IndexMap<K, V, S> {
101100 type Entry = Bucket < K , V > ;
102101
103102 #[ inline]
104- fn into_entries ( self ) -> Vec < Self :: Entry > {
103+ fn into_entries ( self ) -> EntryVec < Self :: Entry > {
105104 self . core . into_entries ( )
106105 }
107106
108107 #[ inline]
109- fn as_entries ( & self ) -> & [ Self :: Entry ] {
108+ fn as_entries ( & self ) -> & EntryVec < Self :: Entry > {
110109 self . core . as_entries ( )
111110 }
112111
113112 #[ inline]
114- fn as_entries_mut ( & mut self ) -> & mut [ Self :: Entry ] {
113+ fn as_entries_mut ( & mut self ) -> & mut EntryVec < Self :: Entry > {
115114 self . core . as_entries_mut ( )
116115 }
117116
118117 fn with_entries < F > ( & mut self , f : F )
119118 where
120- F : FnOnce ( & mut [ Self :: Entry ] ) ,
119+ F : FnOnce ( & mut EntryVec < Self :: Entry > ) ,
121120 {
122121 self . core . with_entries ( f) ;
123122 }
@@ -618,6 +617,8 @@ where
618617 K : Ord ,
619618 {
620619 self . with_entries ( |entries| {
620+ #[ cfg( feature = "amortize" ) ]
621+ let entries = entries. make_contiguous ( ) ;
621622 entries. sort_by ( |a, b| Ord :: cmp ( & a. key , & b. key ) ) ;
622623 } ) ;
623624 }
@@ -635,6 +636,8 @@ where
635636 F : FnMut ( & K , & V , & K , & V ) -> Ordering ,
636637 {
637638 self . with_entries ( move |entries| {
639+ #[ cfg( feature = "amortize" ) ]
640+ let entries = entries. make_contiguous ( ) ;
638641 entries. sort_by ( move |a, b| cmp ( & a. key , & a. value , & b. key , & b. value ) ) ;
639642 } ) ;
640643 }
@@ -648,7 +651,11 @@ where
648651 F : FnMut ( & K , & V , & K , & V ) -> Ordering ,
649652 {
650653 let mut entries = self . into_entries ( ) ;
651- entries. sort_by ( move |a, b| cmp ( & a. key , & a. value , & b. key , & b. value ) ) ;
654+ {
655+ #[ cfg( feature = "amortize" ) ]
656+ let entries = entries. make_contiguous ( ) ;
657+ entries. sort_by ( move |a, b| cmp ( & a. key , & a. value , & b. key , & b. value ) ) ;
658+ }
652659 IntoIter {
653660 iter : entries. into_iter ( ) ,
654661 }
@@ -724,7 +731,7 @@ impl<K, V, S> IndexMap<K, V, S> {
724731/// [`keys`]: struct.IndexMap.html#method.keys
725732/// [`IndexMap`]: struct.IndexMap.html
726733pub struct Keys < ' a , K , V > {
727- pub ( crate ) iter : SliceIter < ' a , Bucket < K , V > > ,
734+ pub ( crate ) iter : < & ' a EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
728735}
729736
730737impl < ' a , K , V > Iterator for Keys < ' a , K , V > {
@@ -768,7 +775,7 @@ impl<'a, K: fmt::Debug, V> fmt::Debug for Keys<'a, K, V> {
768775/// [`values`]: struct.IndexMap.html#method.values
769776/// [`IndexMap`]: struct.IndexMap.html
770777pub struct Values < ' a , K , V > {
771- iter : SliceIter < ' a , Bucket < K , V > > ,
778+ iter : < & ' a EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
772779}
773780
774781impl < ' a , K , V > Iterator for Values < ' a , K , V > {
@@ -812,7 +819,7 @@ impl<'a, K, V: fmt::Debug> fmt::Debug for Values<'a, K, V> {
812819/// [`values_mut`]: struct.IndexMap.html#method.values_mut
813820/// [`IndexMap`]: struct.IndexMap.html
814821pub struct ValuesMut < ' a , K , V > {
815- iter : SliceIterMut < ' a , Bucket < K , V > > ,
822+ iter : < & ' a mut EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
816823}
817824
818825impl < ' a , K , V > Iterator for ValuesMut < ' a , K , V > {
@@ -841,7 +848,7 @@ impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> {
841848/// [`iter`]: struct.IndexMap.html#method.iter
842849/// [`IndexMap`]: struct.IndexMap.html
843850pub struct Iter < ' a , K , V > {
844- iter : SliceIter < ' a , Bucket < K , V > > ,
851+ iter : < & ' a EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
845852}
846853
847854impl < ' a , K , V > Iterator for Iter < ' a , K , V > {
@@ -885,7 +892,7 @@ impl<'a, K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'a, K, V> {
885892/// [`iter_mut`]: struct.IndexMap.html#method.iter_mut
886893/// [`IndexMap`]: struct.IndexMap.html
887894pub struct IterMut < ' a , K , V > {
888- iter : SliceIterMut < ' a , Bucket < K , V > > ,
895+ iter : < & ' a mut EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
889896}
890897
891898impl < ' a , K , V > Iterator for IterMut < ' a , K , V > {
@@ -914,7 +921,7 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
914921/// [`into_iter`]: struct.IndexMap.html#method.into_iter
915922/// [`IndexMap`]: struct.IndexMap.html
916923pub struct IntoIter < K , V > {
917- pub ( crate ) iter : vec :: IntoIter < Bucket < K , V > > ,
924+ pub ( crate ) iter : < EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
918925}
919926
920927impl < K , V > Iterator for IntoIter < K , V > {
@@ -936,6 +943,11 @@ impl<K, V> ExactSizeIterator for IntoIter<K, V> {
936943}
937944
938945impl < K : fmt:: Debug , V : fmt:: Debug > fmt:: Debug for IntoIter < K , V > {
946+ #[ cfg( feature = "amortize" ) ]
947+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
948+ f. debug_struct ( "IntoIter" ) . finish ( )
949+ }
950+ #[ cfg( not( feature = "amortize" ) ) ]
939951 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
940952 let iter = self . iter . as_slice ( ) . iter ( ) . map ( Bucket :: refs) ;
941953 f. debug_list ( ) . entries ( iter) . finish ( )
@@ -950,7 +962,10 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
950962/// [`drain`]: struct.IndexMap.html#method.drain
951963/// [`IndexMap`]: struct.IndexMap.html
952964pub struct Drain < ' a , K , V > {
953- pub ( crate ) iter : vec:: Drain < ' a , Bucket < K , V > > ,
965+ #[ cfg( not( feature = "amortize" ) ) ]
966+ pub ( crate ) iter : crate :: vec:: Drain < ' a , Bucket < K , V > > ,
967+ #[ cfg( feature = "amortize" ) ]
968+ pub ( crate ) iter : atone:: vc:: Drain < ' a , Bucket < K , V > > ,
954969}
955970
956971impl < ' a , K , V > Iterator for Drain < ' a , K , V > {
@@ -1307,7 +1322,9 @@ mod tests {
13071322 assert_eq ! ( map. get( & i) , Some ( & ( i * i) ) ) ;
13081323 map. shrink_to_fit ( ) ;
13091324 assert_eq ! ( map. len( ) , i + 1 ) ;
1310- assert_eq ! ( map. capacity( ) , i + 1 ) ;
1325+ if !cfg ! ( feature = "amortize" ) {
1326+ assert_eq ! ( map. capacity( ) , i + 1 ) ;
1327+ }
13111328 assert_eq ! ( map. get( & i) , Some ( & ( i * i) ) ) ;
13121329 }
13131330 }
0 commit comments