Trait ckb_rust_unstable_port::IsSorted
source · pub trait IsSorted: Iterator {
// Provided methods
fn is_sorted(&mut self) -> bool
where Self: Sized,
Self::Item: PartialOrd { ... }
fn is_sorted_by<F>(&mut self, compare: F) -> bool
where Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering> { ... }
fn is_sorted_by_key<F, B>(&mut self, key: F) -> bool
where Self: Sized,
B: PartialOrd,
F: FnMut(&Self::Item) -> B { ... }
fn is_sorted_until_by<F>(
self,
compare: F
) -> (Option<(Self::Item, Self::Item)>, Self)
where Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering> { ... }
}
Expand description
Extends Iterator
with is_sorted
, is_sorted_by
, and
is_sorted_by_key
.
Provided Methods§
sourcefn is_sorted(&mut self) -> bool
fn is_sorted(&mut self) -> bool
Returns true
if the elements of the iterator are sorted in increasing
order according to <Self::Item as PartialOrd>::partial_cmp
.
let v = vec![0, 1, 2, 3];
assert!(IsSorted::is_sorted(&mut v.iter()));
let v = vec![0, 1, 2, -1];
assert!(!IsSorted::is_sorted(&mut v.iter()));
sourcefn is_sorted_by<F>(&mut self, compare: F) -> bool
fn is_sorted_by<F>(&mut self, compare: F) -> bool
Returns true
if the elements of the iterator
are sorted according to the compare
function.
// Is an iterator sorted in decreasing order?
fn decr<T: PartialOrd>(a: &T, b: &T) -> Option<Ordering> {
a.partial_cmp(b).map(|v| v.reverse())
}
let v = vec![3, 2, 1, 0];
assert!(IsSorted::is_sorted_by(&mut v.iter(), decr));
let v = vec![3, 2, 1, 4];
assert!(!IsSorted::is_sorted_by(&mut v.iter(), decr));
sourcefn is_sorted_by_key<F, B>(&mut self, key: F) -> bool
fn is_sorted_by_key<F, B>(&mut self, key: F) -> bool
Returns true
if the elements of the iterator
are sorted according to the key
extraction function.
let v = vec![0_i32, -1, 2, -3];
assert!(IsSorted::is_sorted_by_key(&mut v.iter(), |v| v.abs()));
let v = vec![0_i32, -1, 2, 0];
assert!(!IsSorted::is_sorted_by_key(&mut v.iter(), |v| v.abs()));
sourcefn is_sorted_until_by<F>(
self,
compare: F
) -> (Option<(Self::Item, Self::Item)>, Self)
fn is_sorted_until_by<F>( self, compare: F ) -> (Option<(Self::Item, Self::Item)>, Self)
Returns the first unsorted pair of items in the iterator and its tail.
let v: &[i32] = &[0, 1, 2, 3, 4, 1, 2, 3];
let (first, tail) = v.iter().is_sorted_until_by(|a, b| a.partial_cmp(b));
assert_eq!(first, Some((&4, &1)));
assert_eq!(tail.as_slice(), &[2, 3]);