mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 07:14:28 +00:00
Add initial impl of is_sorted to Iterator
This commit is contained in:
parent
daa53a52a2
commit
8dea0d0172
@ -2605,6 +2605,90 @@ pub trait Iterator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if the elements of this iterator are sorted.
|
||||||
|
///
|
||||||
|
/// That is, for each element `a` and its following element `b`, `a <= b`
|
||||||
|
/// must hold. If the iterator yields exactly zero or one element, `true`
|
||||||
|
/// is returned.
|
||||||
|
///
|
||||||
|
/// Note that if `Self::Item` is only `PartialOrd`, but not `Ord`, the above
|
||||||
|
/// definition implies that this function returns `false` if any two
|
||||||
|
/// consecutive items are not comparable.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// #![feature(is_sorted)]
|
||||||
|
///
|
||||||
|
/// assert!([1, 2, 2, 9].iter().is_sorted());
|
||||||
|
/// assert!(![1, 3, 2, 4].iter().is_sorted());
|
||||||
|
/// assert!([0].iter().is_sorted());
|
||||||
|
/// assert!(std::iter::empty::<i32>().is_sorted());
|
||||||
|
/// assert!(![0.0, 1.0, std::f32::NAN].iter().is_sorted());
|
||||||
|
/// ```
|
||||||
|
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
|
||||||
|
fn is_sorted(self) -> bool
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
Self::Item: PartialOrd,
|
||||||
|
{
|
||||||
|
self.is_sorted_by(|a, b| a.partial_cmp(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.
|
||||||
|
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
|
||||||
|
fn is_sorted_by<F>(mut self, mut compare: F) -> bool
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>
|
||||||
|
{
|
||||||
|
let mut last = match self.next() {
|
||||||
|
Some(e) => e,
|
||||||
|
None => return true,
|
||||||
|
};
|
||||||
|
|
||||||
|
while let Some(curr) = self.next() {
|
||||||
|
if compare(&last, &curr).map(|o| o == Ordering::Greater).unwrap_or(true) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
last = curr;
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if the elements of this iterator are sorted using the given
|
||||||
|
/// key extraction function.
|
||||||
|
///
|
||||||
|
/// Instead of comparing the iterator's elements directly, this function
|
||||||
|
/// compares the keys of the elements, as determined by `f`. Apart from
|
||||||
|
/// that, it's equivalent to `is_sorted`; see its documentation for more
|
||||||
|
/// information.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// #![feature(is_sorted)]
|
||||||
|
///
|
||||||
|
/// assert!(["c", "bb", "aaa"].iter().is_sorted_by_key(|s| s.len()));
|
||||||
|
/// assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
|
||||||
|
/// ```
|
||||||
|
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
|
||||||
|
fn is_sorted_by_key<F, K>(self, mut f: F) -> bool
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: FnMut(&Self::Item) -> K,
|
||||||
|
K: PartialOrd
|
||||||
|
{
|
||||||
|
self.is_sorted_by(|a, b| f(a).partial_cmp(&f(b)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Select an element from an iterator based on the given "projection"
|
/// Select an element from an iterator based on the given "projection"
|
||||||
|
@ -79,6 +79,7 @@
|
|||||||
#![feature(extern_types)]
|
#![feature(extern_types)]
|
||||||
#![feature(fundamental)]
|
#![feature(fundamental)]
|
||||||
#![feature(intrinsics)]
|
#![feature(intrinsics)]
|
||||||
|
#![feature(is_sorted)]
|
||||||
#![feature(iter_once_with)]
|
#![feature(iter_once_with)]
|
||||||
#![feature(lang_items)]
|
#![feature(lang_items)]
|
||||||
#![feature(link_llvm_intrinsics)]
|
#![feature(link_llvm_intrinsics)]
|
||||||
|
@ -2235,3 +2235,16 @@ fn test_monad_laws_associativity() {
|
|||||||
assert_eq!((0..10).flat_map(f).flat_map(g).sum::<usize>(),
|
assert_eq!((0..10).flat_map(f).flat_map(g).sum::<usize>(),
|
||||||
(0..10).flat_map(|x| f(x).flat_map(g)).sum::<usize>());
|
(0..10).flat_map(|x| f(x).flat_map(g)).sum::<usize>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_is_sorted() {
|
||||||
|
assert!([1, 2, 2, 9].iter().is_sorted());
|
||||||
|
assert!(![1, 3, 2].iter().is_sorted());
|
||||||
|
assert!([0].iter().is_sorted());
|
||||||
|
assert!(std::iter::empty::<i32>().is_sorted());
|
||||||
|
assert!(![0.0, 1.0, std::f32::NAN].iter().is_sorted());
|
||||||
|
assert!([-2, -1, 0, 3].iter().is_sorted());
|
||||||
|
assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
|
||||||
|
assert!(!["c", "bb", "aaa"].iter().is_sorted());
|
||||||
|
assert!(["c", "bb", "aaa"].iter().is_sorted_by_key(|s| s.len()));
|
||||||
|
}
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#![feature(flt2dec)]
|
#![feature(flt2dec)]
|
||||||
#![feature(fmt_internals)]
|
#![feature(fmt_internals)]
|
||||||
#![feature(hashmap_internals)]
|
#![feature(hashmap_internals)]
|
||||||
|
#![feature(is_sorted)]
|
||||||
#![feature(iter_copied)]
|
#![feature(iter_copied)]
|
||||||
#![feature(iter_nth_back)]
|
#![feature(iter_nth_back)]
|
||||||
#![feature(iter_once_with)]
|
#![feature(iter_once_with)]
|
||||||
|
16
src/test/ui/feature-gates/feature-gate-is_sorted.rs
Normal file
16
src/test/ui/feature-gates/feature-gate-is_sorted.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert!([1, 2, 2, 9].iter().is_sorted());
|
||||||
|
//^ ERROR: use of unstable library feature 'is_sorted'
|
||||||
|
assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
|
||||||
|
//^ ERROR: use of unstable library feature 'is_sorted'
|
||||||
|
}
|
19
src/test/ui/feature-gates/feature-gate-is_sorted.stderr
Normal file
19
src/test/ui/feature-gates/feature-gate-is_sorted.stderr
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
|
||||||
|
--> $DIR/feature-gate-is_sorted.rs:12:33
|
||||||
|
|
|
||||||
|
LL | assert!([1, 2, 2, 9].iter().is_sorted());
|
||||||
|
| ^^^^^^^^^
|
||||||
|
|
|
||||||
|
= help: add #![feature(is_sorted)] to the crate attributes to enable
|
||||||
|
|
||||||
|
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485)
|
||||||
|
--> $DIR/feature-gate-is_sorted.rs:14:39
|
||||||
|
|
|
||||||
|
LL | assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs()));
|
||||||
|
| ^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= help: add #![feature(is_sorted)] to the crate attributes to enable
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0658`.
|
Loading…
Reference in New Issue
Block a user