[][src]Trait is_sorted::IsSorted

pub trait IsSorted: Iterator {
    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>
, { ... } }

Extends Iterator with is_sorted, is_sorted_by, and is_sorted_by_key.

Provided methods

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

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

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

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]);
Loading content...

Implementors

impl<I: Iterator> IsSorted for I
[src]

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

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

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

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>, 
[src]

Loading content...