@@ -31,18 +31,12 @@ pub struct IndexedMap<K: Hash + Ord, V> {
3131impl < K : Clone + Hash + Ord , V > IndexedMap < K , V > {
3232 /// Constructs a new, empty map
3333 pub fn new ( ) -> Self {
34- Self {
35- map : new_hash_map ( ) ,
36- keys : Vec :: new ( ) ,
37- }
34+ Self { map : new_hash_map ( ) , keys : Vec :: new ( ) }
3835 }
3936
4037 /// Constructs a new, empty map with the given capacity pre-allocated
4138 pub fn with_capacity ( capacity : usize ) -> Self {
42- Self {
43- map : hash_map_with_capacity ( capacity) ,
44- keys : Vec :: with_capacity ( capacity) ,
45- }
39+ Self { map : hash_map_with_capacity ( capacity) , keys : Vec :: with_capacity ( capacity) }
4640 }
4741
4842 #[ inline( always) ]
@@ -71,7 +65,8 @@ impl<K: Clone + Hash + Ord, V> IndexedMap<K, V> {
7165 pub fn remove ( & mut self , key : & K ) -> Option < V > {
7266 let ret = self . map . remove ( key) ;
7367 if let Some ( _) = ret {
74- let idx = self . keys . iter ( ) . position ( |k| k == key) . expect ( "map and keys must be consistent" ) ;
68+ let idx =
69+ self . keys . iter ( ) . position ( |k| k == key) . expect ( "map and keys must be consistent" ) ;
7570 self . keys . remove ( idx) ;
7671 }
7772 ret
@@ -91,18 +86,11 @@ impl<K: Clone + Hash + Ord, V> IndexedMap<K, V> {
9186 pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V > {
9287 match self . map . entry ( key. clone ( ) ) {
9388 hash_map:: Entry :: Vacant ( entry) => {
94- Entry :: Vacant ( VacantEntry {
95- underlying_entry : entry,
96- key,
97- keys : & mut self . keys ,
98- } )
89+ Entry :: Vacant ( VacantEntry { underlying_entry : entry, key, keys : & mut self . keys } )
9990 } ,
10091 hash_map:: Entry :: Occupied ( entry) => {
101- Entry :: Occupied ( OccupiedEntry {
102- underlying_entry : entry,
103- keys : & mut self . keys ,
104- } )
105- }
92+ Entry :: Occupied ( OccupiedEntry { underlying_entry : entry, keys : & mut self . keys } )
93+ } ,
10694 }
10795 }
10896
@@ -128,18 +116,19 @@ impl<K: Clone + Hash + Ord, V> IndexedMap<K, V> {
128116 let start = match range. start_bound ( ) {
129117 Bound :: Unbounded => 0 ,
130118 Bound :: Included ( key) => self . keys . binary_search ( key) . unwrap_or_else ( |index| index) ,
131- Bound :: Excluded ( key) => self . keys . binary_search ( key) . map ( |index| index +1 ) . unwrap_or_else ( |index| index) ,
119+ Bound :: Excluded ( key) => {
120+ self . keys . binary_search ( key) . map ( |index| index + 1 ) . unwrap_or_else ( |index| index)
121+ } ,
132122 } ;
133123 let end = match range. end_bound ( ) {
134124 Bound :: Unbounded => self . keys . len ( ) ,
135- Bound :: Included ( key) => self . keys . binary_search ( key) . map ( |index| index +1 ) . unwrap_or_else ( |index| index) ,
125+ Bound :: Included ( key) => {
126+ self . keys . binary_search ( key) . map ( |index| index + 1 ) . unwrap_or_else ( |index| index)
127+ } ,
136128 Bound :: Excluded ( key) => self . keys . binary_search ( key) . unwrap_or_else ( |index| index) ,
137129 } ;
138130
139- Range {
140- inner_range : self . keys [ start..end] . iter ( ) ,
141- map : & self . map ,
142- }
131+ Range { inner_range : self . keys [ start..end] . iter ( ) , map : & self . map }
143132 }
144133
145134 /// Returns the number of `key`/`value` pairs in the map
@@ -169,9 +158,9 @@ pub struct Range<'a, K: Hash + Ord, V> {
169158impl < ' a , K : Hash + Ord , V : ' a > Iterator for Range < ' a , K , V > {
170159 type Item = ( & ' a K , & ' a V ) ;
171160 fn next ( & mut self ) -> Option < ( & ' a K , & ' a V ) > {
172- self . inner_range . next ( ) . map ( |k| {
173- ( k , self . map . get ( k ) . expect ( "map and keys must be consistent" ) )
174- } )
161+ self . inner_range
162+ . next ( )
163+ . map ( |k| ( k , self . map . get ( k ) . expect ( "map and keys must be consistent" ) ) )
175164 }
176165}
177166
@@ -215,7 +204,8 @@ impl<'a, K: Hash + Ord, V> OccupiedEntry<'a, K, V> {
215204 /// Remove the value at the position described by this entry.
216205 pub fn remove_entry ( self ) -> ( K , V ) {
217206 let res = self . underlying_entry . remove_entry ( ) ;
218- let idx = self . keys . iter ( ) . position ( |k| k == & res. 0 ) . expect ( "map and keys must be consistent" ) ;
207+ let idx =
208+ self . keys . iter ( ) . position ( |k| k == & res. 0 ) . expect ( "map and keys must be consistent" ) ;
219209 self . keys . remove ( idx) ;
220210 res
221211 }
0 commit comments