Skip to content

Commit

Permalink
feat: add unstable sorting methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bhgomes committed Dec 4, 2021
1 parent a6e0188 commit 2a2b163
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 13 deletions.
56 changes: 51 additions & 5 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,18 +664,18 @@ where

/// Sort the map’s key-value pairs by the default ordering of the keys.
///
/// See `sort_by` for details.
/// See [`sort_by`](Self::sort_by) for details.
pub fn sort_keys(&mut self)
where
K: Ord,
{
self.with_entries(|entries| {
entries.sort_by(|a, b| Ord::cmp(&a.key, &b.key));
self.with_entries(move |entries| {
entries.sort_by(move |a, b| K::cmp(&a.key, &b.key));
});
}

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

/// Sort the key-value pairs of the map and return a by value iterator of
/// Sort the key-value pairs of the map and return a by-value iterator of
/// the key-value pairs with the result.
///
/// The sort is stable.
Expand All @@ -706,6 +706,52 @@ where
}
}

/// Sort the map's key-value pairs by the default ordering of the keys, but
/// may not preserve the order of equal elements.
///
/// See [`sort_unstable_by`](Self::sort_unstable_by) for details.
pub fn sort_unstable_keys(&mut self)
where
K: Ord,
{
self.with_entries(move |entries| {
entries.sort_unstable_by(move |a, b| K::cmp(&a.key, &b.key));
});
}

/// Sort the map's key-value pairs in place using the comparison function `cmp`, but
/// may not preserve the order of equal elements.
///
/// The comparison function receives two key and value pairs to compare (you
/// can sort by keys or values or their combination as needed).
///
/// Computes in **O(n log n + c)** time and **O(n)** space where *n* is
/// the length of the map and *c* is the capacity. The sort is unstable.
pub fn sort_unstable_by<F>(&mut self, mut cmp: F)
where
F: FnMut(&K, &V, &K, &V) -> Ordering,
{
self.with_entries(move |entries| {
entries.sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
});
}

/// Sort the key-value pairs of the map and return a by-value iterator of
/// the key-value pairs with the result.
///
/// The sort is unstable.
#[inline]
pub fn sorted_unstable_by<F>(self, mut cmp: F) -> IntoIter<K, V>
where
F: FnMut(&K, &V, &K, &V) -> Ordering,
{
let mut entries = self.into_entries();
entries.sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
IntoIter {
iter: entries.into_iter(),
}
}

/// Reverses the order of the map’s key-value pairs in place.
///
/// Computes in **O(n)** time and **O(1)** space.
Expand Down
39 changes: 37 additions & 2 deletions src/rayon/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ where
}

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

/// Sort the key-value pairs of the map in parallel and return a by value parallel
/// Sort the key-value pairs of the map in parallel and return a by-value parallel
/// iterator of the key-value pairs with the result.
pub fn par_sorted_by<F>(self, cmp: F) -> IntoParIter<K, V>
where
Expand All @@ -326,6 +326,41 @@ where
entries.par_sort_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
IntoParIter { entries }
}

/// Sort the map's key-value pairs in parallel, by the default ordering of the keys.
pub fn par_sort_unstable_keys(&mut self)
where
K: Ord,
{
self.with_entries(|entries| {
entries.par_sort_unstable_by(|a, b| K::cmp(&a.key, &b.key));
});
}

/// Sort the map's key-value pairs in place and in parallel, using the comparison
/// function `cmp`.
///
/// The comparison function receives two key and value pairs to compare (you
/// can sort by keys or values or their combination as needed).
pub fn par_sort_unstable_by<F>(&mut self, cmp: F)
where
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
{
self.with_entries(|entries| {
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
});
}

/// Sort the key-value pairs of the map in parallel and return a by-value parallel
/// iterator of the key-value pairs with the result.
pub fn par_sorted_unstable_by<F>(self, cmp: F) -> IntoParIter<K, V>
where
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
{
let mut entries = self.into_entries();
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
IntoParIter { entries }
}
}

/// A parallel mutable iterator over the values of a `IndexMap`.
Expand Down
46 changes: 40 additions & 6 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,25 +553,25 @@ where

/// Sort the set’s values by their default ordering.
///
/// See `sort_by` for details.
/// See [`sort_by`](Self::sort_by) for details.
pub fn sort(&mut self)
where
T: Ord,
{
self.map.sort_keys()
}

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

/// Sort the values of the set and return a by value iterator of
/// Sort the values of the set and return a by-value iterator of
/// the values with the result.
///
/// The sort is stable.
Expand All @@ -580,7 +580,41 @@ where
F: FnMut(&T, &T) -> Ordering,
{
IntoIter {
iter: self.map.sorted_by(move |a, &(), b, &()| cmp(a, b)).iter,
iter: self.map.sorted_by(move |a, _, b, _| cmp(a, b)).iter,
}
}

/// Sort the set's values by their default ordering.
///
/// See [`sort_unstable_by`](Self::sort_unstable_by) for details.
pub fn sort_unstable(&mut self)
where
T: Ord,
{
self.map.sort_unstable_keys()
}

/// Sort the set's values in place using the comparison funtion `cmp`.
///
/// Computes in **O(n log n)** time and **O(n)** space. The sort is unstable.
pub fn sort_unstable_by<F>(&mut self, mut cmp: F)
where
F: FnMut(&T, &T) -> Ordering,
{
self.map.sort_unstable_by(move |a, _, b, _| cmp(a, b))
}

/// Sort the values of the set and return a by-value iterator of
/// the values with the result.
pub fn sorted_unstable_by<F>(self, mut cmp: F) -> IntoIter<T>
where
F: FnMut(&T, &T) -> Ordering,
{
IntoIter {
iter: self
.map
.sorted_unstable_by(move |a, _, b, _| cmp(a, b))
.iter,
}
}

Expand Down

0 comments on commit 2a2b163

Please sign in to comment.