Skip to content

Commit 9958c45

Browse files
bors[bot]zeon256
andauthored
Merge #494
494: Added unstable variants of sort from std r=jswrenn a=BudiNverse Hello! This PR adds the following methods - `sorted_unstable` - `sorted_unstable_by` - `sorted_unstable_by_key` The methods mentioned above have the same signature as the normal sorted version. Internally, all of them uses the unstable variants of sort from std. Co-authored-by: BudiNverse <[email protected]>
2 parents a2b3aef + 33efe95 commit 9958c45

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/lib.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,101 @@ pub trait Itertools : Iterator {
21872187
.map(|first| once(first).chain(self).product())
21882188
}
21892189

2190+
/// Sort all iterator elements into a new iterator in ascending order.
2191+
///
2192+
/// **Note:** This consumes the entire iterator, uses the
2193+
/// `slice::sort_unstable()` method and returns the result as a new
2194+
/// iterator that owns its elements.
2195+
///
2196+
/// The sorted iterator, if directly collected to a `Vec`, is converted
2197+
/// without any extra copying or allocation cost.
2198+
///
2199+
/// ```
2200+
/// use itertools::Itertools;
2201+
///
2202+
/// // sort the letters of the text in ascending order
2203+
/// let text = "bdacfe";
2204+
/// itertools::assert_equal(text.chars().sorted_unstable(),
2205+
/// "abcdef".chars());
2206+
/// ```
2207+
#[cfg(feature = "use_alloc")]
2208+
fn sorted_unstable(self) -> VecIntoIter<Self::Item>
2209+
where Self: Sized,
2210+
Self::Item: Ord
2211+
{
2212+
// Use .sort_unstable() directly since it is not quite identical with
2213+
// .sort_by(Ord::cmp)
2214+
let mut v = Vec::from_iter(self);
2215+
v.sort_unstable();
2216+
v.into_iter()
2217+
}
2218+
2219+
/// Sort all iterator elements into a new iterator in ascending order.
2220+
///
2221+
/// **Note:** This consumes the entire iterator, uses the
2222+
/// `slice::sort_unstable_by()` method and returns the result as a new
2223+
/// iterator that owns its elements.
2224+
///
2225+
/// The sorted iterator, if directly collected to a `Vec`, is converted
2226+
/// without any extra copying or allocation cost.
2227+
///
2228+
/// ```
2229+
/// use itertools::Itertools;
2230+
///
2231+
/// // sort people in descending order by age
2232+
/// let people = vec![("Jane", 20), ("John", 18), ("Jill", 30), ("Jack", 27)];
2233+
///
2234+
/// let oldest_people_first = people
2235+
/// .into_iter()
2236+
/// .sorted_unstable_by(|a, b| Ord::cmp(&b.1, &a.1))
2237+
/// .map(|(person, _age)| person);
2238+
///
2239+
/// itertools::assert_equal(oldest_people_first,
2240+
/// vec!["Jill", "Jack", "Jane", "John"]);
2241+
/// ```
2242+
#[cfg(feature = "use_alloc")]
2243+
fn sorted_unstable_by<F>(self, cmp: F) -> VecIntoIter<Self::Item>
2244+
where Self: Sized,
2245+
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
2246+
{
2247+
let mut v = Vec::from_iter(self);
2248+
v.sort_unstable_by(cmp);
2249+
v.into_iter()
2250+
}
2251+
2252+
/// Sort all iterator elements into a new iterator in ascending order.
2253+
///
2254+
/// **Note:** This consumes the entire iterator, uses the
2255+
/// `slice::sort_unstable_by_key()` method and returns the result as a new
2256+
/// iterator that owns its elements.
2257+
///
2258+
/// The sorted iterator, if directly collected to a `Vec`, is converted
2259+
/// without any extra copying or allocation cost.
2260+
///
2261+
/// ```
2262+
/// use itertools::Itertools;
2263+
///
2264+
/// // sort people in descending order by age
2265+
/// let people = vec![("Jane", 20), ("John", 18), ("Jill", 30), ("Jack", 27)];
2266+
///
2267+
/// let oldest_people_first = people
2268+
/// .into_iter()
2269+
/// .sorted_unstable_by_key(|x| -x.1)
2270+
/// .map(|(person, _age)| person);
2271+
///
2272+
/// itertools::assert_equal(oldest_people_first,
2273+
/// vec!["Jill", "Jack", "Jane", "John"]);
2274+
/// ```
2275+
#[cfg(feature = "use_alloc")]
2276+
fn sorted_unstable_by_key<K, F>(self, f: F) -> VecIntoIter<Self::Item>
2277+
where Self: Sized,
2278+
K: Ord,
2279+
F: FnMut(&Self::Item) -> K,
2280+
{
2281+
let mut v = Vec::from_iter(self);
2282+
v.sort_unstable_by_key(f);
2283+
v.into_iter()
2284+
}
21902285

21912286
/// Sort all iterator elements into a new iterator in ascending order.
21922287
///

tests/test_std.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,26 @@ fn join() {
343343
assert_eq!(none.iter().join(", "), "");
344344
}
345345

346+
#[test]
347+
fn sorted_unstable_by() {
348+
let sc = [3, 4, 1, 2].iter().cloned().sorted_by(|&a, &b| {
349+
a.cmp(&b)
350+
});
351+
it::assert_equal(sc, vec![1, 2, 3, 4]);
352+
353+
let v = (0..5).sorted_unstable_by(|&a, &b| a.cmp(&b).reverse());
354+
it::assert_equal(v, vec![4, 3, 2, 1, 0]);
355+
}
356+
357+
#[test]
358+
fn sorted_unstable_by_key() {
359+
let sc = [3, 4, 1, 2].iter().cloned().sorted_unstable_by_key(|&x| x);
360+
it::assert_equal(sc, vec![1, 2, 3, 4]);
361+
362+
let v = (0..5).sorted_unstable_by_key(|&x| -x);
363+
it::assert_equal(v, vec![4, 3, 2, 1, 0]);
364+
}
365+
346366
#[test]
347367
fn sorted_by() {
348368
let sc = [3, 4, 1, 2].iter().cloned().sorted_by(|&a, &b| {

0 commit comments

Comments
 (0)