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

Use bool instead of PartiolOrd as return value of the comparison closure in {slice,Iteraotr}::is_sorted_by #118811

Merged
merged 1 commit into from
Jan 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_monomorphize/src/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ where
}

// Ensure CGUs are sorted by name, so that we get deterministic results.
if !codegen_units.is_sorted_by(|a, b| Some(a.name().as_str().cmp(b.name().as_str()))) {
if !codegen_units.is_sorted_by(|a, b| a.name().as_str() <= b.name().as_str()) {
let mut names = String::new();
for cgu in codegen_units.iter() {
names += &format!("- {}\n", cgu.name());
Expand Down Expand Up @@ -311,7 +311,7 @@ fn merge_codegen_units<'tcx>(
assert!(cx.tcx.sess.codegen_units().as_usize() >= 1);

// A sorted order here ensures merging is deterministic.
assert!(codegen_units.is_sorted_by(|a, b| Some(a.name().as_str().cmp(b.name().as_str()))));
assert!(codegen_units.is_sorted_by(|a, b| a.name().as_str() <= b.name().as_str()));

// This map keeps track of what got merged into what.
let mut cgu_contents: FxHashMap<Symbol, Vec<Symbol>> =
Expand Down
26 changes: 13 additions & 13 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4026,42 +4026,42 @@ pub trait Iterator {
Self: Sized,
Self::Item: PartialOrd,
{
self.is_sorted_by(PartialOrd::partial_cmp)
self.is_sorted_by(|a, b| a <= b)
}

/// Checks if the elements of this iterator are sorted using the given comparator function.
///
/// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
/// [`is_sorted`]; see its documentation for more information.
/// function to determine whether two elements are to be considered in sorted order.
///
/// # Examples
///
/// ```
/// #![feature(is_sorted)]
///
/// assert!([1, 2, 2, 9].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
/// assert!(![1, 3, 2, 4].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
/// assert!([0].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| a.partial_cmp(b)));
/// assert!(![0.0, 1.0, f32::NAN].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
/// ```
/// assert!([1, 2, 2, 9].iter().is_sorted_by(|a, b| a <= b));
/// assert!(![1, 2, 2, 9].iter().is_sorted_by(|a, b| a < b));
///
/// [`is_sorted`]: Iterator::is_sorted
/// assert!([0].iter().is_sorted_by(|a, b| true));
/// assert!([0].iter().is_sorted_by(|a, b| false));
///
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| false));
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| true));
/// ```
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
#[rustc_do_not_const_check]
fn is_sorted_by<F>(mut self, compare: F) -> bool
where
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
F: FnMut(&Self::Item, &Self::Item) -> bool,
{
#[inline]
fn check<'a, T>(
last: &'a mut T,
mut compare: impl FnMut(&T, &T) -> Option<Ordering> + 'a,
mut compare: impl FnMut(&T, &T) -> bool + 'a,
) -> impl FnMut(T) -> bool + 'a {
move |curr| {
if let Some(Ordering::Greater) | None = compare(&last, &curr) {
if !compare(&last, &curr) {
return false;
}
*last = curr;
Expand Down
3 changes: 1 addition & 2 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
mod macros;

use crate::cmp;
use crate::cmp::Ordering;
use crate::fmt;
use crate::intrinsics::assume;
use crate::iter::{
Expand Down Expand Up @@ -133,7 +132,7 @@ iterator! {struct Iter -> *const T, &'a T, const, {/* no mut */}, as_ref, {
fn is_sorted_by<F>(self, mut compare: F) -> bool
where
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
F: FnMut(&Self::Item, &Self::Item) -> bool,
{
self.as_slice().is_sorted_by(|a, b| compare(&a, &b))
}
Expand Down
25 changes: 19 additions & 6 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4103,23 +4103,36 @@ impl<T> [T] {
where
T: PartialOrd,
{
self.is_sorted_by(|a, b| a.partial_cmp(b))
self.is_sorted_by(|a, b| a <= b)
}

/// Checks if the elements of this slice are sorted using the given comparator function.
///
/// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
/// [`is_sorted`]; see its documentation for more information.
/// function to determine whether two elements are to be considered in sorted order.
///
/// [`is_sorted`]: slice::is_sorted
/// # Examples
///
/// ```
/// #![feature(is_sorted)]
///
/// assert!([1, 2, 2, 9].is_sorted_by(|a, b| a <= b));
/// assert!(![1, 2, 2, 9].is_sorted_by(|a, b| a < b));
///
/// assert!([0].is_sorted_by(|a, b| true));
/// assert!([0].is_sorted_by(|a, b| false));
///
/// let empty: [i32; 0] = [];
/// assert!(empty.is_sorted_by(|a, b| false));
/// assert!(empty.is_sorted_by(|a, b| true));
/// ```
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
#[must_use]
pub fn is_sorted_by<'a, F>(&'a self, mut compare: F) -> bool
where
F: FnMut(&'a T, &'a T) -> Option<Ordering>,
F: FnMut(&'a T, &'a T) -> bool,
{
self.array_windows().all(|[a, b]| compare(a, b).map_or(false, Ordering::is_le))
self.array_windows().all(|[a, b]| compare(a, b))
}

/// Checks if the elements of this slice are sorted using the given key extraction function.
Expand Down
3 changes: 1 addition & 2 deletions library/core/tests/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::cmp::Ordering;
use core::num::NonZeroUsize;

/// A wrapper struct that implements `Eq` and `Ord` based on the wrapped
Expand Down Expand Up @@ -402,7 +401,7 @@ fn test_is_sorted() {

// Tests for is_sorted_by
assert!(![6, 2, 8, 5, 1, -60, 1337].iter().is_sorted());
assert!([6, 2, 8, 5, 1, -60, 1337].iter().is_sorted_by(|_, _| Some(Ordering::Less)));
assert!([6, 2, 8, 5, 1, -60, 1337].iter().is_sorted_by(|_, _| true));

// Tests for is_sorted_by_key
assert!([-2, -1, 0, 3].iter().is_sorted());
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2307,7 +2307,7 @@ fn test_is_sorted() {

// Tests for is_sorted_by
assert!(![6, 2, 8, 5, 1, -60, 1337].is_sorted());
assert!([6, 2, 8, 5, 1, -60, 1337].is_sorted_by(|_, _| Some(Ordering::Less)));
assert!([6, 2, 8, 5, 1, -60, 1337].is_sorted_by(|_, _| true));

// Tests for is_sorted_by_key
assert!([-2, -1, 0, 3].is_sorted());
Expand Down
Loading