@@ -14,7 +14,7 @@ use std::{
14
14
15
15
use crate :: {
16
16
fingerprint:: Fingerprint ,
17
- stable_hasher:: { HashStable , StableHasher , ToStableHashKey } ,
17
+ stable_hasher:: { HashStable , StableHasher , StableOrd , ToStableHashKey } ,
18
18
} ;
19
19
20
20
/// `UnordItems` is the order-less version of `Iterator`. It only contains methods
@@ -158,6 +158,7 @@ pub struct UnordSet<V: Eq + Hash> {
158
158
}
159
159
160
160
impl < V : Eq + Hash > Default for UnordSet < V > {
161
+ #[ inline]
161
162
fn default ( ) -> Self {
162
163
Self { inner : FxHashSet :: default ( ) }
163
164
}
@@ -207,6 +208,46 @@ impl<V: Eq + Hash> UnordSet<V> {
207
208
UnordItems ( self . inner . into_iter ( ) )
208
209
}
209
210
211
+ #[ inline]
212
+ pub fn to_sorted < HCX > ( & self , hcx : & HCX , cache_sort_key : bool ) -> Vec < & V >
213
+ where
214
+ V : ToStableHashKey < HCX > ,
215
+ {
216
+ let mut items: Vec < & V > = self . inner . iter ( ) . collect ( ) ;
217
+ if cache_sort_key {
218
+ items. sort_by_cached_key ( |k| k. to_stable_hash_key ( hcx) ) ;
219
+ } else {
220
+ items. sort_unstable_by_key ( |k| k. to_stable_hash_key ( hcx) ) ;
221
+ }
222
+
223
+ items
224
+ }
225
+
226
+ #[ inline]
227
+ pub fn to_sorted_stable_ord ( & self ) -> Vec < V >
228
+ where
229
+ V : Ord + StableOrd + Copy ,
230
+ {
231
+ let mut items: Vec < V > = self . inner . iter ( ) . copied ( ) . collect ( ) ;
232
+ items. sort_unstable ( ) ;
233
+ items
234
+ }
235
+
236
+ #[ inline]
237
+ pub fn into_sorted < HCX > ( self , hcx : & HCX , cache_sort_key : bool ) -> Vec < V >
238
+ where
239
+ V : ToStableHashKey < HCX > ,
240
+ {
241
+ let mut items: Vec < V > = self . inner . into_iter ( ) . collect ( ) ;
242
+ if cache_sort_key {
243
+ items. sort_by_cached_key ( |k| k. to_stable_hash_key ( hcx) ) ;
244
+ } else {
245
+ items. sort_unstable_by_key ( |k| k. to_stable_hash_key ( hcx) ) ;
246
+ }
247
+
248
+ items
249
+ }
250
+
210
251
// We can safely extend this UnordSet from a set of unordered values because that
211
252
// won't expose the internal ordering anywhere.
212
253
#[ inline]
@@ -221,12 +262,14 @@ impl<V: Eq + Hash> UnordSet<V> {
221
262
}
222
263
223
264
impl < V : Hash + Eq > Extend < V > for UnordSet < V > {
265
+ #[ inline]
224
266
fn extend < T : IntoIterator < Item = V > > ( & mut self , iter : T ) {
225
267
self . inner . extend ( iter)
226
268
}
227
269
}
228
270
229
271
impl < V : Hash + Eq > FromIterator < V > for UnordSet < V > {
272
+ #[ inline]
230
273
fn from_iter < T : IntoIterator < Item = V > > ( iter : T ) -> Self {
231
274
UnordSet { inner : FxHashSet :: from_iter ( iter) }
232
275
}
@@ -254,24 +297,28 @@ pub struct UnordMap<K: Eq + Hash, V> {
254
297
}
255
298
256
299
impl < K : Eq + Hash , V > Default for UnordMap < K , V > {
300
+ #[ inline]
257
301
fn default ( ) -> Self {
258
302
Self { inner : FxHashMap :: default ( ) }
259
303
}
260
304
}
261
305
262
306
impl < K : Hash + Eq , V > Extend < ( K , V ) > for UnordMap < K , V > {
307
+ #[ inline]
263
308
fn extend < T : IntoIterator < Item = ( K , V ) > > ( & mut self , iter : T ) {
264
309
self . inner . extend ( iter)
265
310
}
266
311
}
267
312
268
313
impl < K : Hash + Eq , V > FromIterator < ( K , V ) > for UnordMap < K , V > {
314
+ #[ inline]
269
315
fn from_iter < T : IntoIterator < Item = ( K , V ) > > ( iter : T ) -> Self {
270
316
UnordMap { inner : FxHashMap :: from_iter ( iter) }
271
317
}
272
318
}
273
319
274
320
impl < K : Hash + Eq , V , I : Iterator < Item = ( K , V ) > > From < UnordItems < ( K , V ) , I > > for UnordMap < K , V > {
321
+ #[ inline]
275
322
fn from ( items : UnordItems < ( K , V ) , I > ) -> Self {
276
323
UnordMap { inner : FxHashMap :: from_iter ( items. 0 ) }
277
324
}
@@ -351,30 +398,56 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
351
398
self . inner . extend ( items. 0 )
352
399
}
353
400
354
- pub fn to_sorted < HCX > ( & self , hcx : & HCX ) -> Vec < ( & K , & V ) >
401
+ #[ inline]
402
+ pub fn to_sorted < HCX > ( & self , hcx : & HCX , cache_sort_key : bool ) -> Vec < ( & K , & V ) >
355
403
where
356
404
K : ToStableHashKey < HCX > ,
357
405
{
358
406
let mut items: Vec < ( & K , & V ) > = self . inner . iter ( ) . collect ( ) ;
359
- items. sort_by_cached_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
407
+ if cache_sort_key {
408
+ items. sort_by_cached_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
409
+ } else {
410
+ items. sort_unstable_by_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
411
+ }
412
+
360
413
items
361
414
}
362
415
363
- pub fn into_sorted < HCX > ( self , hcx : & HCX ) -> Vec < ( K , V ) >
416
+ #[ inline]
417
+ pub fn to_sorted_stable_ord ( & self ) -> Vec < ( K , & V ) >
418
+ where
419
+ K : Ord + StableOrd + Copy ,
420
+ {
421
+ let mut items: Vec < ( K , & V ) > = self . inner . iter ( ) . map ( |( & k, v) | ( k, v) ) . collect ( ) ;
422
+ items. sort_unstable_by_key ( |& ( k, _) | k) ;
423
+ items
424
+ }
425
+
426
+ #[ inline]
427
+ pub fn into_sorted < HCX > ( self , hcx : & HCX , cache_sort_key : bool ) -> Vec < ( K , V ) >
364
428
where
365
429
K : ToStableHashKey < HCX > ,
366
430
{
367
431
let mut items: Vec < ( K , V ) > = self . inner . into_iter ( ) . collect ( ) ;
368
- items. sort_by_cached_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
432
+ if cache_sort_key {
433
+ items. sort_by_cached_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
434
+ } else {
435
+ items. sort_unstable_by_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
436
+ }
369
437
items
370
438
}
371
439
372
- pub fn values_sorted < HCX > ( & self , hcx : & HCX ) -> impl Iterator < Item = & V >
440
+ #[ inline]
441
+ pub fn values_sorted < HCX > ( & self , hcx : & HCX , cache_sort_key : bool ) -> impl Iterator < Item = & V >
373
442
where
374
443
K : ToStableHashKey < HCX > ,
375
444
{
376
445
let mut items: Vec < ( & K , & V ) > = self . inner . iter ( ) . collect ( ) ;
377
- items. sort_by_cached_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
446
+ if cache_sort_key {
447
+ items. sort_by_cached_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
448
+ } else {
449
+ items. sort_unstable_by_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
450
+ }
378
451
items. into_iter ( ) . map ( |( _, v) | v)
379
452
}
380
453
}
0 commit comments