Trait collections::slice::OrdSliceExtUnstable
[-]
[+]
[src]
pub trait OrdSliceExt<T>: ?Sized { fn sort(&mut self); fn binary_search(&self, x: &T) -> Result<uint, uint>; fn next_permutation(&mut self) -> bool; fn prev_permutation(&mut self) -> bool; fn binary_search_elem(&self, x: &T) -> Result<uint, uint> { ... } }
Allocating extension methods for slices on Ord values.
Required Methods
fn sort(&mut self)
Sorts the slice, in place.
This is equivalent to self.sort_by(|a, b| a.cmp(b))
.
Examples
fn main() { let mut v = [-5i, 4, 1, -3, 2]; v.sort(); assert!(v == [-5i, -3, 1, 2, 4]); }let mut v = [-5i, 4, 1, -3, 2]; v.sort(); assert!(v == [-5i, -3, 1, 2, 4]);
fn binary_search(&self, x: &T) -> Result<uint, uint>
Binary search a sorted slice for a given element.
If the value is found then Ok
is returned, containing the
index of the matching element; if the value is not found then
Err
is returned, containing the index where a matching
element could be inserted while maintaining sorted order.
Example
Looks up a series of four elements. The first is found, with a
uniquely determined position; the second and third are not
found; the fourth could match any position in [1,4]
.
let s = [0i, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let s = s.as_slice(); assert_eq!(s.binary_search(&13), Ok(9)); assert_eq!(s.binary_search(&4), Err(7)); assert_eq!(s.binary_search(&100), Err(13)); let r = s.binary_search(&1); assert!(match r { Ok(1...4) => true, _ => false, });
fn next_permutation(&mut self) -> bool
Mutates the slice to the next lexicographic permutation.
Returns true
if successful and false
if the slice is at the
last-ordered permutation.
Example
fn main() { let v: &mut [_] = &mut [0i, 1, 2]; v.next_permutation(); let b: &mut [_] = &mut [0i, 2, 1]; assert!(v == b); v.next_permutation(); let b: &mut [_] = &mut [1i, 0, 2]; assert!(v == b); }let v: &mut [_] = &mut [0i, 1, 2]; v.next_permutation(); let b: &mut [_] = &mut [0i, 2, 1]; assert!(v == b); v.next_permutation(); let b: &mut [_] = &mut [1i, 0, 2]; assert!(v == b);
fn prev_permutation(&mut self) -> bool
Mutates the slice to the previous lexicographic permutation.
Returns true
if successful and false
if the slice is at the
first-ordered permutation.
Example
fn main() { let v: &mut [_] = &mut [1i, 0, 2]; v.prev_permutation(); let b: &mut [_] = &mut [0i, 2, 1]; assert!(v == b); v.prev_permutation(); let b: &mut [_] = &mut [0i, 1, 2]; assert!(v == b); }let v: &mut [_] = &mut [1i, 0, 2]; v.prev_permutation(); let b: &mut [_] = &mut [0i, 2, 1]; assert!(v == b); v.prev_permutation(); let b: &mut [_] = &mut [0i, 1, 2]; assert!(v == b);
Provided Methods
fn binary_search_elem(&self, x: &T) -> Result<uint, uint>
Deprecated: use binary_search
instead.
Implementors
impl<T: Ord> OrdSliceExt<T> for [T]