@@ -208,21 +208,24 @@ impl<V: Eq + Hash> UnordSet<V> {
208
208
UnordItems ( self . inner . into_iter ( ) )
209
209
}
210
210
211
+ /// Returns the items of this set in stable sort order (as defined by `ToStableHashKey`).
212
+ ///
213
+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
214
+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
215
+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
216
+ /// for `V` is expensive (e.g. a `DefId -> DefPathHash` lookup).
211
217
#[ inline]
212
218
pub fn to_sorted < HCX > ( & self , hcx : & HCX , cache_sort_key : bool ) -> Vec < & V >
213
219
where
214
220
V : ToStableHashKey < HCX > ,
215
221
{
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
222
+ to_sorted_vec ( hcx, self . inner . iter ( ) , cache_sort_key, |& x| x)
224
223
}
225
224
225
+ /// Returns the items of this set in stable sort order (as defined by
226
+ /// `StableOrd`). This method is much more efficient than
227
+ /// `into_sorted` because it does not need to transform keys to their
228
+ /// `ToStableHashKey` equivalent.
226
229
#[ inline]
227
230
pub fn to_sorted_stable_ord ( & self ) -> Vec < V >
228
231
where
@@ -233,19 +236,18 @@ impl<V: Eq + Hash> UnordSet<V> {
233
236
items
234
237
}
235
238
239
+ /// Returns the items of this set in stable sort order (as defined by `ToStableHashKey`).
240
+ ///
241
+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
242
+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
243
+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
244
+ /// for `V` is expensive (e.g. a `DefId -> DefPathHash` lookup).
236
245
#[ inline]
237
246
pub fn into_sorted < HCX > ( self , hcx : & HCX , cache_sort_key : bool ) -> Vec < V >
238
247
where
239
248
V : ToStableHashKey < HCX > ,
240
249
{
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
250
+ to_sorted_vec ( hcx, self . inner . into_iter ( ) , cache_sort_key, |x| x)
249
251
}
250
252
251
253
// We can safely extend this UnordSet from a set of unordered values because that
@@ -398,21 +400,23 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
398
400
self . inner . extend ( items. 0 )
399
401
}
400
402
403
+ /// Returns the entries of this map in stable sort order (as defined by `ToStableHashKey`).
404
+ ///
405
+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
406
+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
407
+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
408
+ /// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
401
409
#[ inline]
402
410
pub fn to_sorted < HCX > ( & self , hcx : & HCX , cache_sort_key : bool ) -> Vec < ( & K , & V ) >
403
411
where
404
412
K : ToStableHashKey < HCX > ,
405
413
{
406
- let mut items: Vec < ( & K , & V ) > = self . inner . iter ( ) . collect ( ) ;
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
-
413
- items
414
+ to_sorted_vec ( hcx, self . inner . iter ( ) , cache_sort_key, |& ( k, _) | k)
414
415
}
415
416
417
+ /// Returns the entries of this map in stable sort order (as defined by `StableOrd`).
418
+ /// This method can be much more efficient than `into_sorted` because it does not need
419
+ /// to transform keys to their `ToStableHashKey` equivalent.
416
420
#[ inline]
417
421
pub fn to_sorted_stable_ord ( & self ) -> Vec < ( K , & V ) >
418
422
where
@@ -423,32 +427,35 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
423
427
items
424
428
}
425
429
430
+ /// Returns the entries of this map in stable sort order (as defined by `ToStableHashKey`).
431
+ ///
432
+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
433
+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
434
+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
435
+ /// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
426
436
#[ inline]
427
437
pub fn into_sorted < HCX > ( self , hcx : & HCX , cache_sort_key : bool ) -> Vec < ( K , V ) >
428
438
where
429
439
K : ToStableHashKey < HCX > ,
430
440
{
431
- let mut items: Vec < ( K , V ) > = self . inner . into_iter ( ) . collect ( ) ;
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
- }
437
- items
441
+ to_sorted_vec ( hcx, self . inner . into_iter ( ) , cache_sort_key, |( k, _) | k)
438
442
}
439
443
444
+ /// Returns the values of this map in stable sort order (as defined by K's
445
+ /// `ToStableHashKey` implementation).
446
+ ///
447
+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
448
+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
449
+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
450
+ /// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
440
451
#[ inline]
441
452
pub fn values_sorted < HCX > ( & self , hcx : & HCX , cache_sort_key : bool ) -> impl Iterator < Item = & V >
442
453
where
443
454
K : ToStableHashKey < HCX > ,
444
455
{
445
- let mut items: Vec < ( & K , & V ) > = self . inner . iter ( ) . collect ( ) ;
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
- }
451
- items. into_iter ( ) . map ( |( _, v) | v)
456
+ to_sorted_vec ( hcx, self . inner . iter ( ) , cache_sort_key, |& ( k, _) | k)
457
+ . into_iter ( )
458
+ . map ( |( _, v) | v)
452
459
}
453
460
}
454
461
@@ -540,6 +547,27 @@ impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordBag<V> {
540
547
}
541
548
}
542
549
550
+ #[ inline]
551
+ fn to_sorted_vec < HCX , T , K , I > (
552
+ hcx : & HCX ,
553
+ iter : I ,
554
+ cache_sort_key : bool ,
555
+ extract_key : fn ( & T ) -> & K ,
556
+ ) -> Vec < T >
557
+ where
558
+ I : Iterator < Item = T > ,
559
+ K : ToStableHashKey < HCX > ,
560
+ {
561
+ let mut items: Vec < T > = iter. collect ( ) ;
562
+ if cache_sort_key {
563
+ items. sort_by_cached_key ( |x| extract_key ( x) . to_stable_hash_key ( hcx) ) ;
564
+ } else {
565
+ items. sort_unstable_by_key ( |x| extract_key ( x) . to_stable_hash_key ( hcx) ) ;
566
+ }
567
+
568
+ items
569
+ }
570
+
543
571
fn hash_iter_order_independent <
544
572
HCX ,
545
573
T : HashStable < HCX > ,
0 commit comments