@@ -8,13 +8,12 @@ pub use crate::mutable_keys::MutableKeys;
8
8
#[ cfg( feature = "rayon" ) ]
9
9
pub use crate :: rayon:: map as rayon;
10
10
11
- use crate :: vec :: { self , Vec } ;
11
+ use crate :: EntryVec ;
12
12
use :: core:: cmp:: Ordering ;
13
13
use :: core:: fmt;
14
14
use :: core:: hash:: { BuildHasher , Hash , Hasher } ;
15
15
use :: core:: iter:: FromIterator ;
16
16
use :: core:: ops:: { Index , IndexMut , RangeFull } ;
17
- use :: core:: slice:: { Iter as SliceIter , IterMut as SliceIterMut } ;
18
17
19
18
#[ cfg( has_std) ]
20
19
use std:: collections:: hash_map:: RandomState ;
@@ -101,23 +100,23 @@ impl<K, V, S> Entries for IndexMap<K, V, S> {
101
100
type Entry = Bucket < K , V > ;
102
101
103
102
#[ inline]
104
- fn into_entries ( self ) -> Vec < Self :: Entry > {
103
+ fn into_entries ( self ) -> EntryVec < Self :: Entry > {
105
104
self . core . into_entries ( )
106
105
}
107
106
108
107
#[ inline]
109
- fn as_entries ( & self ) -> & [ Self :: Entry ] {
108
+ fn as_entries ( & self ) -> & EntryVec < Self :: Entry > {
110
109
self . core . as_entries ( )
111
110
}
112
111
113
112
#[ inline]
114
- fn as_entries_mut ( & mut self ) -> & mut [ Self :: Entry ] {
113
+ fn as_entries_mut ( & mut self ) -> & mut EntryVec < Self :: Entry > {
115
114
self . core . as_entries_mut ( )
116
115
}
117
116
118
117
fn with_entries < F > ( & mut self , f : F )
119
118
where
120
- F : FnOnce ( & mut [ Self :: Entry ] ) ,
119
+ F : FnOnce ( & mut EntryVec < Self :: Entry > ) ,
121
120
{
122
121
self . core . with_entries ( f) ;
123
122
}
@@ -618,6 +617,8 @@ where
618
617
K : Ord ,
619
618
{
620
619
self . with_entries ( |entries| {
620
+ #[ cfg( feature = "amortize" ) ]
621
+ let entries = entries. make_contiguous ( ) ;
621
622
entries. sort_by ( |a, b| Ord :: cmp ( & a. key , & b. key ) ) ;
622
623
} ) ;
623
624
}
@@ -635,6 +636,8 @@ where
635
636
F : FnMut ( & K , & V , & K , & V ) -> Ordering ,
636
637
{
637
638
self . with_entries ( move |entries| {
639
+ #[ cfg( feature = "amortize" ) ]
640
+ let entries = entries. make_contiguous ( ) ;
638
641
entries. sort_by ( move |a, b| cmp ( & a. key , & a. value , & b. key , & b. value ) ) ;
639
642
} ) ;
640
643
}
@@ -648,7 +651,11 @@ where
648
651
F : FnMut ( & K , & V , & K , & V ) -> Ordering ,
649
652
{
650
653
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
+ }
652
659
IntoIter {
653
660
iter : entries. into_iter ( ) ,
654
661
}
@@ -724,7 +731,7 @@ impl<K, V, S> IndexMap<K, V, S> {
724
731
/// [`keys`]: struct.IndexMap.html#method.keys
725
732
/// [`IndexMap`]: struct.IndexMap.html
726
733
pub 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 ,
728
735
}
729
736
730
737
impl < ' 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> {
768
775
/// [`values`]: struct.IndexMap.html#method.values
769
776
/// [`IndexMap`]: struct.IndexMap.html
770
777
pub struct Values < ' a , K , V > {
771
- iter : SliceIter < ' a , Bucket < K , V > > ,
778
+ iter : < & ' a EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
772
779
}
773
780
774
781
impl < ' 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> {
812
819
/// [`values_mut`]: struct.IndexMap.html#method.values_mut
813
820
/// [`IndexMap`]: struct.IndexMap.html
814
821
pub struct ValuesMut < ' a , K , V > {
815
- iter : SliceIterMut < ' a , Bucket < K , V > > ,
822
+ iter : < & ' a mut EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
816
823
}
817
824
818
825
impl < ' a , K , V > Iterator for ValuesMut < ' a , K , V > {
@@ -841,7 +848,7 @@ impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> {
841
848
/// [`iter`]: struct.IndexMap.html#method.iter
842
849
/// [`IndexMap`]: struct.IndexMap.html
843
850
pub struct Iter < ' a , K , V > {
844
- iter : SliceIter < ' a , Bucket < K , V > > ,
851
+ iter : < & ' a EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
845
852
}
846
853
847
854
impl < ' 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> {
885
892
/// [`iter_mut`]: struct.IndexMap.html#method.iter_mut
886
893
/// [`IndexMap`]: struct.IndexMap.html
887
894
pub struct IterMut < ' a , K , V > {
888
- iter : SliceIterMut < ' a , Bucket < K , V > > ,
895
+ iter : < & ' a mut EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
889
896
}
890
897
891
898
impl < ' a , K , V > Iterator for IterMut < ' a , K , V > {
@@ -914,7 +921,7 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
914
921
/// [`into_iter`]: struct.IndexMap.html#method.into_iter
915
922
/// [`IndexMap`]: struct.IndexMap.html
916
923
pub struct IntoIter < K , V > {
917
- pub ( crate ) iter : vec :: IntoIter < Bucket < K , V > > ,
924
+ pub ( crate ) iter : < EntryVec < Bucket < K , V > > as IntoIterator > :: IntoIter ,
918
925
}
919
926
920
927
impl < K , V > Iterator for IntoIter < K , V > {
@@ -936,6 +943,11 @@ impl<K, V> ExactSizeIterator for IntoIter<K, V> {
936
943
}
937
944
938
945
impl < 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" ) ) ]
939
951
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
940
952
let iter = self . iter . as_slice ( ) . iter ( ) . map ( Bucket :: refs) ;
941
953
f. debug_list ( ) . entries ( iter) . finish ( )
@@ -950,7 +962,10 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
950
962
/// [`drain`]: struct.IndexMap.html#method.drain
951
963
/// [`IndexMap`]: struct.IndexMap.html
952
964
pub 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 > > ,
954
969
}
955
970
956
971
impl < ' a , K , V > Iterator for Drain < ' a , K , V > {
@@ -1307,7 +1322,9 @@ mod tests {
1307
1322
assert_eq ! ( map. get( & i) , Some ( & ( i * i) ) ) ;
1308
1323
map. shrink_to_fit ( ) ;
1309
1324
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
+ }
1311
1328
assert_eq ! ( map. get( & i) , Some ( & ( i * i) ) ) ;
1312
1329
}
1313
1330
}
0 commit comments