Skip to content

Commit 2a2b163

Browse files
committed
feat: add unstable sorting methods
1 parent a6e0188 commit 2a2b163

File tree

3 files changed

+128
-13
lines changed

3 files changed

+128
-13
lines changed

src/map.rs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,18 +664,18 @@ where
664664

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

677677
/// Sort the map’s key-value pairs in place using the comparison
678-
/// function `compare`.
678+
/// function `cmp`.
679679
///
680680
/// The comparison function receives two key and value pairs to compare (you
681681
/// can sort by keys or values or their combination as needed).
@@ -691,7 +691,7 @@ where
691691
});
692692
}
693693

694-
/// Sort the key-value pairs of the map and return a by value iterator of
694+
/// Sort the key-value pairs of the map and return a by-value iterator of
695695
/// the key-value pairs with the result.
696696
///
697697
/// The sort is stable.
@@ -706,6 +706,52 @@ where
706706
}
707707
}
708708

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

src/rayon/map.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ where
303303
}
304304

305305
/// Sort the map’s key-value pairs in place and in parallel, using the comparison
306-
/// function `compare`.
306+
/// function `cmp`.
307307
///
308308
/// The comparison function receives two key and value pairs to compare (you
309309
/// can sort by keys or values or their combination as needed).
@@ -316,7 +316,7 @@ where
316316
});
317317
}
318318

319-
/// Sort the key-value pairs of the map in parallel and return a by value parallel
319+
/// Sort the key-value pairs of the map in parallel and return a by-value parallel
320320
/// iterator of the key-value pairs with the result.
321321
pub fn par_sorted_by<F>(self, cmp: F) -> IntoParIter<K, V>
322322
where
@@ -326,6 +326,41 @@ where
326326
entries.par_sort_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
327327
IntoParIter { entries }
328328
}
329+
330+
/// Sort the map's key-value pairs in parallel, by the default ordering of the keys.
331+
pub fn par_sort_unstable_keys(&mut self)
332+
where
333+
K: Ord,
334+
{
335+
self.with_entries(|entries| {
336+
entries.par_sort_unstable_by(|a, b| K::cmp(&a.key, &b.key));
337+
});
338+
}
339+
340+
/// Sort the map's key-value pairs in place and in parallel, using the comparison
341+
/// function `cmp`.
342+
///
343+
/// The comparison function receives two key and value pairs to compare (you
344+
/// can sort by keys or values or their combination as needed).
345+
pub fn par_sort_unstable_by<F>(&mut self, cmp: F)
346+
where
347+
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
348+
{
349+
self.with_entries(|entries| {
350+
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
351+
});
352+
}
353+
354+
/// Sort the key-value pairs of the map in parallel and return a by-value parallel
355+
/// iterator of the key-value pairs with the result.
356+
pub fn par_sorted_unstable_by<F>(self, cmp: F) -> IntoParIter<K, V>
357+
where
358+
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
359+
{
360+
let mut entries = self.into_entries();
361+
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
362+
IntoParIter { entries }
363+
}
329364
}
330365

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

src/set.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,25 +553,25 @@ where
553553

554554
/// Sort the set’s values by their default ordering.
555555
///
556-
/// See `sort_by` for details.
556+
/// See [`sort_by`](Self::sort_by) for details.
557557
pub fn sort(&mut self)
558558
where
559559
T: Ord,
560560
{
561561
self.map.sort_keys()
562562
}
563563

564-
/// Sort the set’s values in place using the comparison function `compare`.
564+
/// Sort the set’s values in place using the comparison function `cmp`.
565565
///
566566
/// Computes in **O(n log n)** time and **O(n)** space. The sort is stable.
567-
pub fn sort_by<F>(&mut self, mut compare: F)
567+
pub fn sort_by<F>(&mut self, mut cmp: F)
568568
where
569569
F: FnMut(&T, &T) -> Ordering,
570570
{
571-
self.map.sort_by(move |a, _, b, _| compare(a, b));
571+
self.map.sort_by(move |a, _, b, _| cmp(a, b));
572572
}
573573

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

0 commit comments

Comments
 (0)