From 2276be971d16c300ece5d469d15944169af61bd4 Mon Sep 17 00:00:00 2001 From: BudiNverse Date: Mon, 9 Nov 2020 17:15:51 +0800 Subject: [PATCH 1/4] Added `sorted_unstable` Added `sorted_unstable_by` Added `sorted_unstable_by_key` --- src/lib.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 0082cc32a..4858b7c62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2187,6 +2187,101 @@ pub trait Itertools : Iterator { .map(|first| once(first).chain(self).product()) } + /// Sort all iterator elements into a new iterator in ascending order. + /// + /// **Note:** This consumes the entire iterator, uses the + /// `slice::sort_unstable()` method and returns the result as a new + /// iterator that owns its elements. + /// + /// The sorted iterator, if directly collected to a `Vec`, is converted + /// without any extra copying or allocation cost. + /// + /// ``` + /// use itertools::Itertools; + /// + /// // sort the letters of the text in ascending order + /// let text = "bdacfe"; + /// itertools::assert_equal(text.chars().sorted_unstable(), + /// "abcdef".chars()); + /// ``` + #[cfg(feature = "use_alloc")] + fn sorted_unstable(self) -> VecIntoIter + where Self: Sized, + Self::Item: Ord + { + // Use .sort() directly since it is not quite identical with + // .sort_by(Ord::cmp) + let mut v = Vec::from_iter(self); + v.sort_unstable(); + v.into_iter() + } + + /// Sort all iterator elements into a new iterator in ascending order. + /// + /// **Note:** This consumes the entire iterator, uses the + /// `slice::sort__unstable_by()` method and returns the result as a new + /// iterator that owns its elements. + /// + /// The sorted iterator, if directly collected to a `Vec`, is converted + /// without any extra copying or allocation cost. + /// + /// ``` + /// use itertools::Itertools; + /// + /// // sort people in descending order by age + /// let people = vec![("Jane", 20), ("John", 18), ("Jill", 30), ("Jack", 27)]; + /// + /// let oldest_people_first = people + /// .into_iter() + /// .sorted_unstable_by(|a, b| Ord::cmp(&b.1, &a.1)) + /// .map(|(person, _age)| person); + /// + /// itertools::assert_equal(oldest_people_first, + /// vec!["Jill", "Jack", "Jane", "John"]); + /// ``` + #[cfg(feature = "use_alloc")] + fn sorted_unstable_by(self, cmp: F) -> VecIntoIter + where Self: Sized, + F: FnMut(&Self::Item, &Self::Item) -> Ordering, + { + let mut v = Vec::from_iter(self); + v.sort_unstable_by(cmp); + v.into_iter() + } + + /// Sort all iterator elements into a new iterator in ascending order. + /// + /// **Note:** This consumes the entire iterator, uses the + /// `slice::sort_unstable_by_key()` method and returns the result as a new + /// iterator that owns its elements. + /// + /// The sorted iterator, if directly collected to a `Vec`, is converted + /// without any extra copying or allocation cost. + /// + /// ``` + /// use itertools::Itertools; + /// + /// // sort people in descending order by age + /// let people = vec![("Jane", 20), ("John", 18), ("Jill", 30), ("Jack", 27)]; + /// + /// let oldest_people_first = people + /// .into_iter() + /// .sorted_unstable_by_key(|x| -x.1) + /// .map(|(person, _age)| person); + /// + /// itertools::assert_equal(oldest_people_first, + /// vec!["Jill", "Jack", "Jane", "John"]); + /// ``` + #[cfg(feature = "use_alloc")] + fn sorted_unstable_by_key(self, f: F) -> VecIntoIter + where Self: Sized, + K: Ord, + F: FnMut(&Self::Item) -> K, + { + let mut v = Vec::from_iter(self); + v.sort_unstable_by_key(f); + v.into_iter() + } /// Sort all iterator elements into a new iterator in ascending order. /// From c94846fefae054fb4072ae34d6cc91004f5d2254 Mon Sep 17 00:00:00 2001 From: BudiNverse Date: Mon, 9 Nov 2020 17:30:46 +0800 Subject: [PATCH 2/4] Added test for `sorted_unstable` Added test for `sorted_unstable_by` Added test for `sorted_unstable_by_key` --- tests/test_std.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_std.rs b/tests/test_std.rs index 24d0f0888..86528216b 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -343,6 +343,26 @@ fn join() { assert_eq!(none.iter().join(", "), ""); } +#[test] +fn sorted_unstable_by() { + let sc = [3, 4, 1, 2].iter().cloned().sorted_by(|&a, &b| { + a.cmp(&b) + }); + it::assert_equal(sc, vec![1, 2, 3, 4]); + + let v = (0..5).sorted_unstable_by(|&a, &b| a.cmp(&b).reverse()); + it::assert_equal(v, vec![4, 3, 2, 1, 0]); +} + +#[test] +fn sorted_unstable_by_key() { + let sc = [3, 4, 1, 2].iter().cloned().sorted_unstable_by_key(|&x| x); + it::assert_equal(sc, vec![1, 2, 3, 4]); + + let v = (0..5).sorted_unstable_by_key(|&x| -x); + it::assert_equal(v, vec![4, 3, 2, 1, 0]); +} + #[test] fn sorted_by() { let sc = [3, 4, 1, 2].iter().cloned().sorted_by(|&a, &b| { From a8f7f5eaad44051efcd4c3b6fb6fa50452f3f862 Mon Sep 17 00:00:00 2001 From: BudiNverse Date: Mon, 9 Nov 2020 17:31:09 +0800 Subject: [PATCH 3/4] Fixed typo in `sorted_unstable_by` docs --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 4858b7c62..0046819cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2219,7 +2219,7 @@ pub trait Itertools : Iterator { /// Sort all iterator elements into a new iterator in ascending order. /// /// **Note:** This consumes the entire iterator, uses the - /// `slice::sort__unstable_by()` method and returns the result as a new + /// `slice::sort_unstable_by()` method and returns the result as a new /// iterator that owns its elements. /// /// The sorted iterator, if directly collected to a `Vec`, is converted From 33efe9504a46183f89ae76231e9623f5b4b6e6e1 Mon Sep 17 00:00:00 2001 From: BudiNverse Date: Mon, 9 Nov 2020 18:22:57 +0800 Subject: [PATCH 4/4] Fixed wrong comment for `sorted_unstable` --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0046819cb..ba7085bfd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2209,7 +2209,7 @@ pub trait Itertools : Iterator { where Self: Sized, Self::Item: Ord { - // Use .sort() directly since it is not quite identical with + // Use .sort_unstable() directly since it is not quite identical with // .sort_by(Ord::cmp) let mut v = Vec::from_iter(self); v.sort_unstable();