3
3
//! as required by the query system.
4
4
5
5
use rustc_hash::{FxHashMap, FxHashSet};
6
- use smallvec::SmallVec;
7
6
use std::{
8
- borrow::Borrow,
7
+ borrow::{ Borrow, BorrowMut} ,
9
8
collections::hash_map::Entry,
10
9
hash::Hash,
11
10
iter::{Product, Sum},
@@ -135,27 +134,20 @@ impl<'a, T: Copy + 'a, I: Iterator<Item = &'a T>> UnordItems<&'a T, I> {
135
134
}
136
135
137
136
impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
137
+ #[inline]
138
138
pub fn into_sorted<HCX>(self, hcx: &HCX) -> Vec<T>
139
139
where
140
140
T: ToStableHashKey<HCX>,
141
141
{
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)
145
143
}
146
144
147
145
#[inline]
148
146
pub fn into_sorted_stable_ord(self) -> Vec<T>
149
147
where
150
148
T: StableCompare,
151
149
{
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)
159
151
}
160
152
161
153
#[inline]
@@ -164,29 +156,53 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
164
156
K: StableCompare,
165
157
C: for<'a> Fn(&'a T) -> &'a K,
166
158
{
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
+ }
180
176
}
177
+
181
178
items
182
179
}
183
180
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
185
182
where
186
- T: ToStableHashKey<HCX>,
183
+ K: StableCompare,
184
+ P: for<'a> Fn(&'a T) -> &'a K,
185
+ C: FromIterator<T> + BorrowMut<[T]>,
187
186
{
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
+
190
206
items
191
207
}
192
208
}
0 commit comments