@@ -31,18 +31,12 @@ pub struct IndexedMap<K: Hash + Ord, V> {
31
31
impl < K : Clone + Hash + Ord , V > IndexedMap < K , V > {
32
32
/// Constructs a new, empty map
33
33
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 ( ) }
38
35
}
39
36
40
37
/// Constructs a new, empty map with the given capacity pre-allocated
41
38
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) }
46
40
}
47
41
48
42
#[ inline( always) ]
@@ -71,7 +65,8 @@ impl<K: Clone + Hash + Ord, V> IndexedMap<K, V> {
71
65
pub fn remove ( & mut self , key : & K ) -> Option < V > {
72
66
let ret = self . map . remove ( key) ;
73
67
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" ) ;
75
70
self . keys . remove ( idx) ;
76
71
}
77
72
ret
@@ -91,18 +86,11 @@ impl<K: Clone + Hash + Ord, V> IndexedMap<K, V> {
91
86
pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V > {
92
87
match self . map . entry ( key. clone ( ) ) {
93
88
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 } )
99
90
} ,
100
91
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
+ } ,
106
94
}
107
95
}
108
96
@@ -128,18 +116,19 @@ impl<K: Clone + Hash + Ord, V> IndexedMap<K, V> {
128
116
let start = match range. start_bound ( ) {
129
117
Bound :: Unbounded => 0 ,
130
118
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
+ } ,
132
122
} ;
133
123
let end = match range. end_bound ( ) {
134
124
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
+ } ,
136
128
Bound :: Excluded ( key) => self . keys . binary_search ( key) . unwrap_or_else ( |index| index) ,
137
129
} ;
138
130
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 }
143
132
}
144
133
145
134
/// Returns the number of `key`/`value` pairs in the map
@@ -169,9 +158,9 @@ pub struct Range<'a, K: Hash + Ord, V> {
169
158
impl < ' a , K : Hash + Ord , V : ' a > Iterator for Range < ' a , K , V > {
170
159
type Item = ( & ' a K , & ' a V ) ;
171
160
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" ) ) )
175
164
}
176
165
}
177
166
@@ -215,7 +204,8 @@ impl<'a, K: Hash + Ord, V> OccupiedEntry<'a, K, V> {
215
204
/// Remove the value at the position described by this entry.
216
205
pub fn remove_entry ( self ) -> ( K , V ) {
217
206
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" ) ;
219
209
self . keys . remove ( idx) ;
220
210
res
221
211
}
0 commit comments