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§

source

fn is_sorted(&mut self) -> bool
where Self: Sized, Self::Item: PartialOrd,

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()));
source

fn is_sorted_by<F>(&mut self, compare: F) -> bool
where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,

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));
source

fn is_sorted_by_key<F, B>(&mut self, key: F) -> bool
where Self: Sized, B: PartialOrd, F: FnMut(&Self::Item) -> B,

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()));
source

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>,

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]);

Implementors§

source§

impl<I> IsSorted for I
where I: Iterator,