Skip to content

Commit 13468f2

Browse files
authored
Merge pull request rust-lang#211 from bhgomes/add-sort-unstable-methods
feat: add unstable sorting methods
2 parents 8bb46ca + 6fca269 commit 13468f2

File tree

4 files changed

+161
-15
lines changed

4 files changed

+161
-15
lines changed

src/map.rs

+51-5
Original file line numberDiff line numberDiff line change
@@ -669,18 +669,18 @@ where
669669

670670
/// Sort the map’s key-value pairs by the default ordering of the keys.
671671
///
672-
/// See `sort_by` for details.
672+
/// See [`sort_by`](Self::sort_by) for details.
673673
pub fn sort_keys(&mut self)
674674
where
675675
K: Ord,
676676
{
677-
self.with_entries(|entries| {
678-
entries.sort_by(|a, b| Ord::cmp(&a.key, &b.key));
677+
self.with_entries(move |entries| {
678+
entries.sort_by(move |a, b| K::cmp(&a.key, &b.key));
679679
});
680680
}
681681

682682
/// Sort the map’s key-value pairs in place using the comparison
683-
/// function `compare`.
683+
/// function `cmp`.
684684
///
685685
/// The comparison function receives two key and value pairs to compare (you
686686
/// can sort by keys or values or their combination as needed).
@@ -696,7 +696,7 @@ where
696696
});
697697
}
698698

699-
/// Sort the key-value pairs of the map and return a by value iterator of
699+
/// Sort the key-value pairs of the map and return a by-value iterator of
700700
/// the key-value pairs with the result.
701701
///
702702
/// The sort is stable.
@@ -711,6 +711,52 @@ where
711711
}
712712
}
713713

714+
/// Sort the map's key-value pairs by the default ordering of the keys, but
715+
/// may not preserve the order of equal elements.
716+
///
717+
/// See [`sort_unstable_by`](Self::sort_unstable_by) for details.
718+
pub fn sort_unstable_keys(&mut self)
719+
where
720+
K: Ord,
721+
{
722+
self.with_entries(move |entries| {
723+
entries.sort_unstable_by(move |a, b| K::cmp(&a.key, &b.key));
724+
});
725+
}
726+
727+
/// Sort the map's key-value pairs in place using the comparison function `cmp`, but
728+
/// may not preserve the order of equal elements.
729+
///
730+
/// The comparison function receives two key and value pairs to compare (you
731+
/// can sort by keys or values or their combination as needed).
732+
///
733+
/// Computes in **O(n log n + c)** time where *n* is
734+
/// the length of the map and *c* is the capacity. The sort is unstable.
735+
pub fn sort_unstable_by<F>(&mut self, mut cmp: F)
736+
where
737+
F: FnMut(&K, &V, &K, &V) -> Ordering,
738+
{
739+
self.with_entries(move |entries| {
740+
entries.sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
741+
});
742+
}
743+
744+
/// Sort the key-value pairs of the map and return a by-value iterator of
745+
/// the key-value pairs with the result.
746+
///
747+
/// The sort is unstable.
748+
#[inline]
749+
pub fn sorted_unstable_by<F>(self, mut cmp: F) -> IntoIter<K, V>
750+
where
751+
F: FnMut(&K, &V, &K, &V) -> Ordering,
752+
{
753+
let mut entries = self.into_entries();
754+
entries.sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
755+
IntoIter {
756+
iter: entries.into_iter(),
757+
}
758+
}
759+
714760
/// Reverses the order of the map’s key-value pairs in place.
715761
///
716762
/// Computes in **O(n)** time and **O(1)** space.

src/rayon/map.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ where
348348
}
349349

350350
/// Sort the map’s key-value pairs in place and in parallel, using the comparison
351-
/// function `compare`.
351+
/// function `cmp`.
352352
///
353353
/// The comparison function receives two key and value pairs to compare (you
354354
/// can sort by keys or values or their combination as needed).
@@ -361,7 +361,7 @@ where
361361
});
362362
}
363363

364-
/// Sort the key-value pairs of the map in parallel and return a by value parallel
364+
/// Sort the key-value pairs of the map in parallel and return a by-value parallel
365365
/// iterator of the key-value pairs with the result.
366366
pub fn par_sorted_by<F>(self, cmp: F) -> IntoParIter<K, V>
367367
where
@@ -371,6 +371,41 @@ where
371371
entries.par_sort_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
372372
IntoParIter { entries }
373373
}
374+
375+
/// Sort the map's key-value pairs in parallel, by the default ordering of the keys.
376+
pub fn par_sort_unstable_keys(&mut self)
377+
where
378+
K: Ord,
379+
{
380+
self.with_entries(|entries| {
381+
entries.par_sort_unstable_by(|a, b| K::cmp(&a.key, &b.key));
382+
});
383+
}
384+
385+
/// Sort the map's key-value pairs in place and in parallel, using the comparison
386+
/// function `cmp`.
387+
///
388+
/// The comparison function receives two key and value pairs to compare (you
389+
/// can sort by keys or values or their combination as needed).
390+
pub fn par_sort_unstable_by<F>(&mut self, cmp: F)
391+
where
392+
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
393+
{
394+
self.with_entries(|entries| {
395+
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
396+
});
397+
}
398+
399+
/// Sort the key-value pairs of the map in parallel and return a by-value parallel
400+
/// iterator of the key-value pairs with the result.
401+
pub fn par_sorted_unstable_by<F>(self, cmp: F) -> IntoParIter<K, V>
402+
where
403+
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
404+
{
405+
let mut entries = self.into_entries();
406+
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
407+
IntoParIter { entries }
408+
}
374409
}
375410

376411
/// A parallel mutable iterator over the values of a `IndexMap`.

src/rayon/set.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ where
489489
});
490490
}
491491

492-
/// Sort the set’s values in place and in parallel, using the comparison function `compare`.
492+
/// Sort the set’s values in place and in parallel, using the comparison function `cmp`.
493493
pub fn par_sort_by<F>(&mut self, cmp: F)
494494
where
495495
F: Fn(&T, &T) -> Ordering + Sync,
@@ -499,7 +499,7 @@ where
499499
});
500500
}
501501

502-
/// Sort the values of the set in parallel and return a by value parallel iterator of
502+
/// Sort the values of the set in parallel and return a by-value parallel iterator of
503503
/// the values with the result.
504504
pub fn par_sorted_by<F>(self, cmp: F) -> IntoParIter<T>
505505
where
@@ -509,6 +509,37 @@ where
509509
entries.par_sort_by(move |a, b| cmp(&a.key, &b.key));
510510
IntoParIter { entries }
511511
}
512+
513+
/// Sort the set's values in parallel by their default ordering.
514+
pub fn par_sort_unstable(&mut self)
515+
where
516+
T: Ord,
517+
{
518+
self.with_entries(|entries| {
519+
entries.par_sort_unstable_by(|a, b| T::cmp(&a.key, &b.key));
520+
});
521+
}
522+
523+
/// Sort the set’s values in place and in parallel, using the comparison function `cmp`.
524+
pub fn par_sort_unstable_by<F>(&mut self, cmp: F)
525+
where
526+
F: Fn(&T, &T) -> Ordering + Sync,
527+
{
528+
self.with_entries(|entries| {
529+
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &b.key));
530+
});
531+
}
532+
533+
/// Sort the values of the set in parallel and return a by-value parallel iterator of
534+
/// the values with the result.
535+
pub fn par_sorted_unstable_by<F>(self, cmp: F) -> IntoParIter<T>
536+
where
537+
F: Fn(&T, &T) -> Ordering + Sync,
538+
{
539+
let mut entries = self.into_entries();
540+
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &b.key));
541+
IntoParIter { entries }
542+
}
512543
}
513544

514545
/// Requires crate feature `"rayon"`.

src/set.rs

+40-6
Original file line numberDiff line numberDiff line change
@@ -558,25 +558,25 @@ where
558558

559559
/// Sort the set’s values by their default ordering.
560560
///
561-
/// See `sort_by` for details.
561+
/// See [`sort_by`](Self::sort_by) for details.
562562
pub fn sort(&mut self)
563563
where
564564
T: Ord,
565565
{
566566
self.map.sort_keys()
567567
}
568568

569-
/// Sort the set’s values in place using the comparison function `compare`.
569+
/// Sort the set’s values in place using the comparison function `cmp`.
570570
///
571571
/// Computes in **O(n log n)** time and **O(n)** space. The sort is stable.
572-
pub fn sort_by<F>(&mut self, mut compare: F)
572+
pub fn sort_by<F>(&mut self, mut cmp: F)
573573
where
574574
F: FnMut(&T, &T) -> Ordering,
575575
{
576-
self.map.sort_by(move |a, _, b, _| compare(a, b));
576+
self.map.sort_by(move |a, _, b, _| cmp(a, b));
577577
}
578578

579-
/// Sort the values of the set and return a by value iterator of
579+
/// Sort the values of the set and return a by-value iterator of
580580
/// the values with the result.
581581
///
582582
/// The sort is stable.
@@ -585,7 +585,41 @@ where
585585
F: FnMut(&T, &T) -> Ordering,
586586
{
587587
IntoIter {
588-
iter: self.map.sorted_by(move |a, &(), b, &()| cmp(a, b)).iter,
588+
iter: self.map.sorted_by(move |a, _, b, _| cmp(a, b)).iter,
589+
}
590+
}
591+
592+
/// Sort the set's values by their default ordering.
593+
///
594+
/// See [`sort_unstable_by`](Self::sort_unstable_by) for details.
595+
pub fn sort_unstable(&mut self)
596+
where
597+
T: Ord,
598+
{
599+
self.map.sort_unstable_keys()
600+
}
601+
602+
/// Sort the set's values in place using the comparison funtion `cmp`.
603+
///
604+
/// Computes in **O(n log n)** time. The sort is unstable.
605+
pub fn sort_unstable_by<F>(&mut self, mut cmp: F)
606+
where
607+
F: FnMut(&T, &T) -> Ordering,
608+
{
609+
self.map.sort_unstable_by(move |a, _, b, _| cmp(a, b))
610+
}
611+
612+
/// Sort the values of the set and return a by-value iterator of
613+
/// the values with the result.
614+
pub fn sorted_unstable_by<F>(self, mut cmp: F) -> IntoIter<T>
615+
where
616+
F: FnMut(&T, &T) -> Ordering,
617+
{
618+
IntoIter {
619+
iter: self
620+
.map
621+
.sorted_unstable_by(move |a, _, b, _| cmp(a, b))
622+
.iter,
589623
}
590624
}
591625

0 commit comments

Comments
 (0)