@@ -14,7 +14,7 @@ use std::{
1414
1515use crate :: {
1616 fingerprint:: Fingerprint ,
17- stable_hasher:: { HashStable , StableHasher , ToStableHashKey } ,
17+ stable_hasher:: { HashStable , StableHasher , StableOrd , ToStableHashKey } ,
1818} ;
1919
2020/// `UnordItems` is the order-less version of `Iterator`. It only contains methods
@@ -158,6 +158,7 @@ pub struct UnordSet<V: Eq + Hash> {
158158}
159159
160160impl < V : Eq + Hash > Default for UnordSet < V > {
161+ #[ inline]
161162 fn default ( ) -> Self {
162163 Self { inner : FxHashSet :: default ( ) }
163164 }
@@ -207,6 +208,46 @@ impl<V: Eq + Hash> UnordSet<V> {
207208 UnordItems ( self . inner . into_iter ( ) )
208209 }
209210
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+
210251 // We can safely extend this UnordSet from a set of unordered values because that
211252 // won't expose the internal ordering anywhere.
212253 #[ inline]
@@ -221,12 +262,14 @@ impl<V: Eq + Hash> UnordSet<V> {
221262}
222263
223264impl < V : Hash + Eq > Extend < V > for UnordSet < V > {
265+ #[ inline]
224266 fn extend < T : IntoIterator < Item = V > > ( & mut self , iter : T ) {
225267 self . inner . extend ( iter)
226268 }
227269}
228270
229271impl < V : Hash + Eq > FromIterator < V > for UnordSet < V > {
272+ #[ inline]
230273 fn from_iter < T : IntoIterator < Item = V > > ( iter : T ) -> Self {
231274 UnordSet { inner : FxHashSet :: from_iter ( iter) }
232275 }
@@ -254,24 +297,28 @@ pub struct UnordMap<K: Eq + Hash, V> {
254297}
255298
256299impl < K : Eq + Hash , V > Default for UnordMap < K , V > {
300+ #[ inline]
257301 fn default ( ) -> Self {
258302 Self { inner : FxHashMap :: default ( ) }
259303 }
260304}
261305
262306impl < K : Hash + Eq , V > Extend < ( K , V ) > for UnordMap < K , V > {
307+ #[ inline]
263308 fn extend < T : IntoIterator < Item = ( K , V ) > > ( & mut self , iter : T ) {
264309 self . inner . extend ( iter)
265310 }
266311}
267312
268313impl < K : Hash + Eq , V > FromIterator < ( K , V ) > for UnordMap < K , V > {
314+ #[ inline]
269315 fn from_iter < T : IntoIterator < Item = ( K , V ) > > ( iter : T ) -> Self {
270316 UnordMap { inner : FxHashMap :: from_iter ( iter) }
271317 }
272318}
273319
274320impl < K : Hash + Eq , V , I : Iterator < Item = ( K , V ) > > From < UnordItems < ( K , V ) , I > > for UnordMap < K , V > {
321+ #[ inline]
275322 fn from ( items : UnordItems < ( K , V ) , I > ) -> Self {
276323 UnordMap { inner : FxHashMap :: from_iter ( items. 0 ) }
277324 }
@@ -351,30 +398,56 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
351398 self . inner . extend ( items. 0 )
352399 }
353400
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 ) >
355403 where
356404 K : ToStableHashKey < HCX > ,
357405 {
358406 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+
360413 items
361414 }
362415
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 ) >
364428 where
365429 K : ToStableHashKey < HCX > ,
366430 {
367431 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+ }
369437 items
370438 }
371439
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 >
373442 where
374443 K : ToStableHashKey < HCX > ,
375444 {
376445 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+ }
378451 items. into_iter ( ) . map ( |( _, v) | v)
379452 }
380453}
0 commit comments