Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unstable sorting methods #211

Merged
merged 3 commits into from Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 51 additions & 5 deletions src/map.rs
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 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
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
35 changes: 33 additions & 2 deletions src/rayon/set.rs
Expand Up @@ -452,7 +452,7 @@ where
});
}

/// Sort the set’s values in place and in parallel, using the comparison function `compare`.
/// Sort the set’s values in place and in parallel, using the comparison function `cmp`.
pub fn par_sort_by<F>(&mut self, cmp: F)
where
F: Fn(&T, &T) -> Ordering + Sync,
Expand All @@ -462,7 +462,7 @@ where
});
}

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

/// Sort the set's values in parallel by their default ordering.
pub fn par_sort_unstable(&mut self)
where
T: Ord,
{
self.with_entries(|entries| {
entries.par_sort_unstable_by(|a, b| T::cmp(&a.key, &b.key));
});
}

/// Sort the set’s values in place and in parallel, using the comparison function `cmp`.
pub fn par_sort_unstable_by<F>(&mut self, cmp: F)
where
F: Fn(&T, &T) -> Ordering + Sync,
{
self.with_entries(|entries| {
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &b.key));
});
}

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

/// Requires crate feature `"rayon"`.
Expand Down
46 changes: 40 additions & 6 deletions src/set.rs
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. 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