1use std::borrow::{Borrow, BorrowMut};
6use std::collections::hash_map::{Entry, OccupiedError};
7use std::hash::Hash;
8use std::iter::{Product, Sum};
9use std::ops::Index;
10
11use rustc_hash::{FxHashMap, FxHashSet};
12use rustc_macros::{Decodable_Generic, Encodable_Generic};
13
14use crate::fingerprint::Fingerprint;
15use crate::stable_hasher::{HashStable, StableCompare, StableHasher, ToStableHashKey};
16
17#[derive(Clone)]
32pub struct UnordItems<T, I: Iterator<Item = T>>(I);
33
34impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
35 #[inline]
36 pub fn map<U, F: Fn(T) -> U>(self, f: F) -> UnordItems<U, impl Iterator<Item = U>> {
37 UnordItems(self.0.map(f))
38 }
39
40 #[inline]
41 pub fn all<F: Fn(T) -> bool>(mut self, f: F) -> bool {
42 self.0.all(f)
43 }
44
45 #[inline]
46 pub fn any<F: Fn(T) -> bool>(mut self, f: F) -> bool {
47 self.0.any(f)
48 }
49
50 #[inline]
51 pub fn filter<F: Fn(&T) -> bool>(self, f: F) -> UnordItems<T, impl Iterator<Item = T>> {
52 UnordItems(self.0.filter(f))
53 }
54
55 #[inline]
56 pub fn filter_map<U, F: Fn(T) -> Option<U>>(
57 self,
58 f: F,
59 ) -> UnordItems<U, impl Iterator<Item = U>> {
60 UnordItems(self.0.filter_map(f))
61 }
62
63 #[inline]
64 pub fn max(self) -> Option<T>
65 where
66 T: Ord,
67 {
68 self.0.max()
69 }
70
71 #[inline]
72 pub fn min(self) -> Option<T>
73 where
74 T: Ord,
75 {
76 self.0.min()
77 }
78
79 #[inline]
80 pub fn sum<S>(self) -> S
81 where
82 S: Sum<T>,
83 {
84 self.0.sum()
85 }
86
87 #[inline]
88 pub fn product<S>(self) -> S
89 where
90 S: Product<T>,
91 {
92 self.0.product()
93 }
94
95 #[inline]
96 pub fn count(self) -> usize {
97 self.0.count()
98 }
99
100 #[inline]
101 pub fn flat_map<U, F, O>(self, f: F) -> UnordItems<O, impl Iterator<Item = O>>
102 where
103 U: IntoIterator<Item = O>,
104 F: Fn(T) -> U,
105 {
106 UnordItems(self.0.flat_map(f))
107 }
108
109 pub fn collect<C: From<UnordItems<T, I>>>(self) -> C {
110 self.into()
111 }
112}
113
114impl<T> UnordItems<T, std::iter::Empty<T>> {
115 pub fn empty() -> Self {
116 UnordItems(std::iter::empty())
117 }
118}
119
120impl<'a, T: Clone + 'a, I: Iterator<Item = &'a T>> UnordItems<&'a T, I> {
121 #[inline]
122 pub fn cloned(self) -> UnordItems<T, impl Iterator<Item = T>> {
123 UnordItems(self.0.cloned())
124 }
125}
126
127impl<'a, T: Copy + 'a, I: Iterator<Item = &'a T>> UnordItems<&'a T, I> {
128 #[inline]
129 pub fn copied(self) -> UnordItems<T, impl Iterator<Item = T>> {
130 UnordItems(self.0.copied())
131 }
132}
133
134impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
135 #[inline]
136 pub fn into_sorted<HCX>(self, hcx: &HCX) -> Vec<T>
137 where
138 T: ToStableHashKey<HCX>,
139 {
140 self.collect_sorted(hcx, true)
141 }
142
143 #[inline]
144 pub fn into_sorted_stable_ord(self) -> Vec<T>
145 where
146 T: StableCompare,
147 {
148 self.collect_stable_ord_by_key(|x| x)
149 }
150
151 #[inline]
152 pub fn into_sorted_stable_ord_by_key<K, C>(self, project_to_key: C) -> Vec<T>
153 where
154 K: StableCompare,
155 C: for<'a> Fn(&'a T) -> &'a K,
156 {
157 self.collect_stable_ord_by_key(project_to_key)
158 }
159
160 #[inline]
161 pub fn collect_sorted<HCX, C>(self, hcx: &HCX, cache_sort_key: bool) -> C
162 where
163 T: ToStableHashKey<HCX>,
164 C: FromIterator<T> + BorrowMut<[T]>,
165 {
166 let mut items: C = self.0.collect();
167
168 let slice = items.borrow_mut();
169 if slice.len() > 1 {
170 if cache_sort_key {
171 slice.sort_by_cached_key(|x| x.to_stable_hash_key(hcx));
172 } else {
173 slice.sort_by_key(|x| x.to_stable_hash_key(hcx));
174 }
175 }
176
177 items
178 }
179
180 #[inline]
181 pub fn collect_stable_ord_by_key<K, C, P>(self, project_to_key: P) -> C
182 where
183 K: StableCompare,
184 P: for<'a> Fn(&'a T) -> &'a K,
185 C: FromIterator<T> + BorrowMut<[T]>,
186 {
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
206 items
207 }
208}
209
210trait UnordCollection {}
217
218#[derive(Debug, Eq, PartialEq, Clone, Encodable_Generic, Decodable_Generic)]
228pub struct UnordSet<V: Eq + Hash> {
229 inner: FxHashSet<V>,
230}
231
232impl<V: Eq + Hash> UnordCollection for UnordSet<V> {}
233
234impl<V: Eq + Hash> Default for UnordSet<V> {
235 #[inline]
236 fn default() -> Self {
237 Self { inner: FxHashSet::default() }
238 }
239}
240
241impl<V: Eq + Hash> UnordSet<V> {
242 #[inline]
243 pub fn new() -> Self {
244 Self { inner: Default::default() }
245 }
246
247 #[inline]
248 pub fn with_capacity(capacity: usize) -> Self {
249 Self { inner: FxHashSet::with_capacity_and_hasher(capacity, Default::default()) }
250 }
251
252 #[inline]
253 pub fn len(&self) -> usize {
254 self.inner.len()
255 }
256
257 #[inline]
258 pub fn is_empty(&self) -> bool {
259 self.inner.is_empty()
260 }
261
262 #[inline]
263 pub fn insert(&mut self, v: V) -> bool {
264 self.inner.insert(v)
265 }
266
267 #[inline]
268 pub fn contains<Q: ?Sized>(&self, v: &Q) -> bool
269 where
270 V: Borrow<Q>,
271 Q: Hash + Eq,
272 {
273 self.inner.contains(v)
274 }
275
276 #[inline]
277 pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> bool
278 where
279 V: Borrow<Q>,
280 Q: Hash + Eq,
281 {
282 self.inner.remove(k)
283 }
284
285 #[inline]
286 pub fn items(&self) -> UnordItems<&V, impl Iterator<Item = &V>> {
287 UnordItems(self.inner.iter())
288 }
289
290 #[inline]
291 pub fn into_items(self) -> UnordItems<V, impl Iterator<Item = V>> {
292 UnordItems(self.inner.into_iter())
293 }
294
295 #[inline]
302 pub fn to_sorted<HCX>(&self, hcx: &HCX, cache_sort_key: bool) -> Vec<&V>
303 where
304 V: ToStableHashKey<HCX>,
305 {
306 to_sorted_vec(hcx, self.inner.iter(), cache_sort_key, |&x| x)
307 }
308
309 #[inline]
314 pub fn to_sorted_stable_ord(&self) -> Vec<&V>
315 where
316 V: StableCompare,
317 {
318 let mut items: Vec<&V> = self.inner.iter().collect();
319 items.sort_unstable_by(|a, b| a.stable_cmp(*b));
320 items
321 }
322
323 #[inline]
328 pub fn into_sorted_stable_ord(self) -> Vec<V>
329 where
330 V: StableCompare,
331 {
332 let mut items: Vec<V> = self.inner.into_iter().collect();
333 items.sort_unstable_by(V::stable_cmp);
334 items
335 }
336
337 #[inline]
344 pub fn into_sorted<HCX>(self, hcx: &HCX, cache_sort_key: bool) -> Vec<V>
345 where
346 V: ToStableHashKey<HCX>,
347 {
348 to_sorted_vec(hcx, self.inner.into_iter(), cache_sort_key, |x| x)
349 }
350
351 #[inline]
352 pub fn clear(&mut self) {
353 self.inner.clear();
354 }
355}
356
357pub trait ExtendUnord<T> {
358 fn extend_unord<I: Iterator<Item = T>>(&mut self, items: UnordItems<T, I>);
364}
365
366impl<C: Extend<T> + UnordCollection, T> ExtendUnord<T> for C {
370 #[inline]
371 fn extend_unord<I: Iterator<Item = T>>(&mut self, items: UnordItems<T, I>) {
372 self.extend(items.0)
373 }
374}
375
376impl<V: Hash + Eq> Extend<V> for UnordSet<V> {
377 #[inline]
378 fn extend<T: IntoIterator<Item = V>>(&mut self, iter: T) {
379 self.inner.extend(iter)
380 }
381}
382
383impl<V: Hash + Eq> FromIterator<V> for UnordSet<V> {
384 #[inline]
385 fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self {
386 UnordSet { inner: FxHashSet::from_iter(iter) }
387 }
388}
389
390impl<V: Hash + Eq> From<FxHashSet<V>> for UnordSet<V> {
391 fn from(value: FxHashSet<V>) -> Self {
392 UnordSet { inner: value }
393 }
394}
395
396impl<V: Hash + Eq, I: Iterator<Item = V>> From<UnordItems<V, I>> for UnordSet<V> {
397 fn from(value: UnordItems<V, I>) -> Self {
398 UnordSet { inner: FxHashSet::from_iter(value.0) }
399 }
400}
401
402impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordSet<V> {
403 #[inline]
404 fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
405 hash_iter_order_independent(self.inner.iter(), hcx, hasher);
406 }
407}
408
409#[derive(Debug, Eq, PartialEq, Clone, Encodable_Generic, Decodable_Generic)]
419pub struct UnordMap<K: Eq + Hash, V> {
420 inner: FxHashMap<K, V>,
421}
422
423impl<K: Eq + Hash, V> UnordCollection for UnordMap<K, V> {}
424
425impl<K: Eq + Hash, V> Default for UnordMap<K, V> {
426 #[inline]
427 fn default() -> Self {
428 Self { inner: FxHashMap::default() }
429 }
430}
431
432impl<K: Hash + Eq, V> Extend<(K, V)> for UnordMap<K, V> {
433 #[inline]
434 fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
435 self.inner.extend(iter)
436 }
437}
438
439impl<K: Hash + Eq, V> FromIterator<(K, V)> for UnordMap<K, V> {
440 #[inline]
441 fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
442 UnordMap { inner: FxHashMap::from_iter(iter) }
443 }
444}
445
446impl<K: Hash + Eq, V, I: Iterator<Item = (K, V)>> From<UnordItems<(K, V), I>> for UnordMap<K, V> {
447 #[inline]
448 fn from(items: UnordItems<(K, V), I>) -> Self {
449 UnordMap { inner: FxHashMap::from_iter(items.0) }
450 }
451}
452
453impl<K: Eq + Hash, V> UnordMap<K, V> {
454 #[inline]
455 pub fn with_capacity(capacity: usize) -> Self {
456 Self { inner: FxHashMap::with_capacity_and_hasher(capacity, Default::default()) }
457 }
458
459 #[inline]
460 pub fn len(&self) -> usize {
461 self.inner.len()
462 }
463
464 #[inline]
465 pub fn insert(&mut self, k: K, v: V) -> Option<V> {
466 self.inner.insert(k, v)
467 }
468
469 #[inline]
470 pub fn try_insert(&mut self, k: K, v: V) -> Result<&mut V, OccupiedError<'_, K, V>> {
471 self.inner.try_insert(k, v)
472 }
473
474 #[inline]
475 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
476 where
477 K: Borrow<Q>,
478 Q: Hash + Eq,
479 {
480 self.inner.contains_key(k)
481 }
482
483 #[inline]
484 pub fn is_empty(&self) -> bool {
485 self.inner.is_empty()
486 }
487
488 #[inline]
489 pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
490 self.inner.entry(key)
491 }
492
493 #[inline]
494 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
495 where
496 K: Borrow<Q>,
497 Q: Hash + Eq,
498 {
499 self.inner.get(k)
500 }
501
502 #[inline]
503 pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
504 where
505 K: Borrow<Q>,
506 Q: Hash + Eq,
507 {
508 self.inner.get_mut(k)
509 }
510
511 #[inline]
512 pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
513 where
514 K: Borrow<Q>,
515 Q: Hash + Eq,
516 {
517 self.inner.remove(k)
518 }
519
520 #[inline]
521 pub fn items(&self) -> UnordItems<(&K, &V), impl Iterator<Item = (&K, &V)>> {
522 UnordItems(self.inner.iter())
523 }
524
525 #[inline]
526 pub fn into_items(self) -> UnordItems<(K, V), impl Iterator<Item = (K, V)>> {
527 UnordItems(self.inner.into_iter())
528 }
529
530 #[inline]
531 pub fn keys(&self) -> UnordItems<&K, impl Iterator<Item = &K>> {
532 UnordItems(self.inner.keys())
533 }
534
535 #[inline]
542 pub fn to_sorted<HCX>(&self, hcx: &HCX, cache_sort_key: bool) -> Vec<(&K, &V)>
543 where
544 K: ToStableHashKey<HCX>,
545 {
546 to_sorted_vec(hcx, self.inner.iter(), cache_sort_key, |&(k, _)| k)
547 }
548
549 #[inline]
553 pub fn to_sorted_stable_ord(&self) -> Vec<(&K, &V)>
554 where
555 K: StableCompare,
556 {
557 let mut items: Vec<_> = self.inner.iter().collect();
558 items.sort_unstable_by(|(a, _), (b, _)| a.stable_cmp(*b));
559 items
560 }
561
562 #[inline]
569 pub fn into_sorted<HCX>(self, hcx: &HCX, cache_sort_key: bool) -> Vec<(K, V)>
570 where
571 K: ToStableHashKey<HCX>,
572 {
573 to_sorted_vec(hcx, self.inner.into_iter(), cache_sort_key, |(k, _)| k)
574 }
575
576 #[inline]
580 pub fn into_sorted_stable_ord(self) -> Vec<(K, V)>
581 where
582 K: StableCompare,
583 {
584 let mut items: Vec<(K, V)> = self.inner.into_iter().collect();
585 items.sort_unstable_by(|a, b| a.0.stable_cmp(&b.0));
586 items
587 }
588
589 #[inline]
597 pub fn values_sorted<HCX>(&self, hcx: &HCX, cache_sort_key: bool) -> impl Iterator<Item = &V>
598 where
599 K: ToStableHashKey<HCX>,
600 {
601 to_sorted_vec(hcx, self.inner.iter(), cache_sort_key, |&(k, _)| k)
602 .into_iter()
603 .map(|(_, v)| v)
604 }
605
606 #[inline]
607 pub fn clear(&mut self) {
608 self.inner.clear()
609 }
610}
611
612impl<K, Q: ?Sized, V> Index<&Q> for UnordMap<K, V>
613where
614 K: Eq + Hash + Borrow<Q>,
615 Q: Eq + Hash,
616{
617 type Output = V;
618
619 #[inline]
620 fn index(&self, key: &Q) -> &V {
621 &self.inner[key]
622 }
623}
624
625impl<HCX, K: Hash + Eq + HashStable<HCX>, V: HashStable<HCX>> HashStable<HCX> for UnordMap<K, V> {
626 #[inline]
627 fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
628 hash_iter_order_independent(self.inner.iter(), hcx, hasher);
629 }
630}
631
632#[derive(Default, Debug, Eq, PartialEq, Clone, Encodable_Generic, Decodable_Generic)]
643pub struct UnordBag<V> {
644 inner: Vec<V>,
645}
646
647impl<V> UnordBag<V> {
648 #[inline]
649 pub fn new() -> Self {
650 Self { inner: Default::default() }
651 }
652
653 #[inline]
654 pub fn len(&self) -> usize {
655 self.inner.len()
656 }
657
658 #[inline]
659 pub fn push(&mut self, v: V) {
660 self.inner.push(v);
661 }
662
663 #[inline]
664 pub fn items(&self) -> UnordItems<&V, impl Iterator<Item = &V>> {
665 UnordItems(self.inner.iter())
666 }
667
668 #[inline]
669 pub fn into_items(self) -> UnordItems<V, impl Iterator<Item = V>> {
670 UnordItems(self.inner.into_iter())
671 }
672}
673
674impl<T> UnordCollection for UnordBag<T> {}
675
676impl<T> Extend<T> for UnordBag<T> {
677 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
678 self.inner.extend(iter)
679 }
680}
681
682impl<T, I: Iterator<Item = T>> From<UnordItems<T, I>> for UnordBag<T> {
683 fn from(value: UnordItems<T, I>) -> Self {
684 UnordBag { inner: Vec::from_iter(value.0) }
685 }
686}
687
688impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordBag<V> {
689 #[inline]
690 fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
691 hash_iter_order_independent(self.inner.iter(), hcx, hasher);
692 }
693}
694
695#[inline]
696fn to_sorted_vec<HCX, T, K, I>(
697 hcx: &HCX,
698 iter: I,
699 cache_sort_key: bool,
700 extract_key: fn(&T) -> &K,
701) -> Vec<T>
702where
703 I: Iterator<Item = T>,
704 K: ToStableHashKey<HCX>,
705{
706 let mut items: Vec<T> = iter.collect();
707 if cache_sort_key {
708 items.sort_by_cached_key(|x| extract_key(x).to_stable_hash_key(hcx));
709 } else {
710 items.sort_unstable_by_key(|x| extract_key(x).to_stable_hash_key(hcx));
711 }
712
713 items
714}
715
716fn hash_iter_order_independent<
717 HCX,
718 T: HashStable<HCX>,
719 I: Iterator<Item = T> + ExactSizeIterator,
720>(
721 mut it: I,
722 hcx: &mut HCX,
723 hasher: &mut StableHasher,
724) {
725 let len = it.len();
726 len.hash_stable(hcx, hasher);
727
728 match len {
729 0 => {
730 }
732 1 => {
733 it.next().unwrap().hash_stable(hcx, hasher);
735 }
736 _ => {
737 let mut accumulator = Fingerprint::ZERO;
738 for item in it {
739 let mut item_hasher = StableHasher::new();
740 item.hash_stable(hcx, &mut item_hasher);
741 let item_fingerprint: Fingerprint = item_hasher.finish();
742 accumulator = accumulator.combine_commutative(item_fingerprint);
743 }
744 accumulator.hash_stable(hcx, hasher);
745 }
746 }
747}
748
749impl<T> !IntoIterator for UnordBag<T> {}
752impl<V> !IntoIterator for UnordSet<V> {}
753impl<K, V> !IntoIterator for UnordMap<K, V> {}
754impl<T, I> !IntoIterator for UnordItems<T, I> {}