Trait std::slice::OrdSlicePreludeUnstable
[-]
[+]
[src]
pub trait OrdSlicePrelude<T: Ord> {
fn binary_search_elem(&self, x: &T) -> BinarySearchResult;
fn next_permutation(&mut self) -> bool;
fn prev_permutation(&mut self) -> bool;
}Extension methods for slices containing Ord elements.
Required Methods
fn binary_search_elem(&self, x: &T) -> BinarySearchResult
Binary search a sorted slice for a given element.
If the value is found then Found is returned, containing the
index of the matching element; if the value is not found then
NotFound 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].
use std::slice::{Found, NotFound}; 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_elem(&13), Found(9)); assert_eq!(s.binary_search_elem(&4), NotFound(7)); assert_eq!(s.binary_search_elem(&100), NotFound(13)); let r = s.binary_search_elem(&1); assert!(match r { Found(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);
Implementors
impl<T: Ord> OrdSlicePrelude<T> for [T]