33//! as required by the query system.
44
55use rustc_hash:: { FxHashMap , FxHashSet } ;
6- use smallvec:: SmallVec ;
76use std:: {
8- borrow:: Borrow ,
7+ borrow:: { Borrow , BorrowMut } ,
98 collections:: hash_map:: Entry ,
109 hash:: Hash ,
1110 iter:: { Product , Sum } ,
@@ -135,27 +134,20 @@ impl<'a, T: Copy + 'a, I: Iterator<Item = &'a T>> UnordItems<&'a T, I> {
135134}
136135
137136impl < T , I : Iterator < Item = T > > UnordItems < T , I > {
137+ #[ inline]
138138 pub fn into_sorted < HCX > ( self , hcx : & HCX ) -> Vec < T >
139139 where
140140 T : ToStableHashKey < HCX > ,
141141 {
142- let mut items: Vec < T > = self . 0 . collect ( ) ;
143- items. sort_by_cached_key ( |x| x. to_stable_hash_key ( hcx) ) ;
144- items
142+ self . collect_sorted ( hcx, true )
145143 }
146144
147145 #[ inline]
148146 pub fn into_sorted_stable_ord ( self ) -> Vec < T >
149147 where
150148 T : StableCompare ,
151149 {
152- let mut items: Vec < T > = self . 0 . collect ( ) ;
153- if !T :: CAN_USE_UNSTABLE_SORT {
154- items. sort_by ( T :: stable_cmp) ;
155- } else {
156- items. sort_unstable_by ( T :: stable_cmp)
157- }
158- items
150+ self . collect_stable_ord_by_key ( |x| x)
159151 }
160152
161153 #[ inline]
@@ -164,29 +156,53 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
164156 K : StableCompare ,
165157 C : for < ' a > Fn ( & ' a T ) -> & ' a K ,
166158 {
167- let mut items: Vec < T > = self . 0 . collect ( ) ;
168- if !K :: CAN_USE_UNSTABLE_SORT {
169- items. sort_by ( |a, b| {
170- let a_key = project_to_key ( a) ;
171- let b_key = project_to_key ( b) ;
172- a_key. stable_cmp ( b_key)
173- } ) ;
174- } else {
175- items. sort_unstable_by ( |a, b| {
176- let a_key = project_to_key ( a) ;
177- let b_key = project_to_key ( b) ;
178- a_key. stable_cmp ( b_key)
179- } ) ;
159+ self . collect_stable_ord_by_key ( project_to_key)
160+ }
161+
162+ pub fn collect_sorted < HCX , C > ( self , hcx : & HCX , cache_sort_key : bool ) -> C
163+ where
164+ T : ToStableHashKey < HCX > ,
165+ C : FromIterator < T > + BorrowMut < [ T ] > ,
166+ {
167+ let mut items: C = self . 0 . collect ( ) ;
168+
169+ let slice = items. borrow_mut ( ) ;
170+ if slice. len ( ) > 1 {
171+ if cache_sort_key {
172+ slice. sort_by_cached_key ( |x| x. to_stable_hash_key ( hcx) ) ;
173+ } else {
174+ slice. sort_by_key ( |x| x. to_stable_hash_key ( hcx) ) ;
175+ }
180176 }
177+
181178 items
182179 }
183180
184- pub fn into_sorted_small_vec < HCX , const LEN : usize > ( self , hcx : & HCX ) -> SmallVec < [ T ; LEN ] >
181+ pub fn collect_stable_ord_by_key < K , C , P > ( self , project_to_key : P ) -> C
185182 where
186- T : ToStableHashKey < HCX > ,
183+ K : StableCompare ,
184+ P : for < ' a > Fn ( & ' a T ) -> & ' a K ,
185+ C : FromIterator < T > + BorrowMut < [ T ] > ,
187186 {
188- let mut items: SmallVec < [ T ; LEN ] > = self . 0 . collect ( ) ;
189- items. sort_by_cached_key ( |x| x. to_stable_hash_key ( hcx) ) ;
187+ let mut items: C = self . 0 . collect ( ) ;
188+
189+ let slice = items. borrow_mut ( ) ;
190+ if slice. len ( ) > 1 {
191+ if !K :: CAN_USE_UNSTABLE_SORT {
192+ slice. sort_by ( |a, b| {
193+ let a_key = project_to_key ( a) ;
194+ let b_key = project_to_key ( b) ;
195+ a_key. stable_cmp ( b_key)
196+ } ) ;
197+ } else {
198+ slice. sort_unstable_by ( |a, b| {
199+ let a_key = project_to_key ( a) ;
200+ let b_key = project_to_key ( b) ;
201+ a_key. stable_cmp ( b_key)
202+ } ) ;
203+ }
204+ }
205+
190206 items
191207 }
192208}
0 commit comments