Trait ord_subset::OrdSubsetSliceExt [] [src]

pub trait OrdSubsetSliceExt<T: OrdSubset> {
    fn ord_subset_sort(&mut self);
    fn ord_subset_sort_rev(&mut self);
    fn ord_subset_sort_by<F>(&mut self, compare: F) where F: FnMut(&T, &T) -> Ordering;
    fn ord_subset_binary_search(&self, x: &T) -> Result<usizeusize>;
    fn ord_subset_binary_search_by<F>(&self, f: F) -> Result<usizeusize> where F: FnMut(&T) -> Ordering;
    fn ord_subset_binary_search_rev(&self, x: &T) -> Result<usizeusize>;
}

Required Methods

fn ord_subset_sort(&mut self)

Sort the slice, in place. Values outside the ordered subset are put at the end in no particular order.

This is equivalent to self.ord_subset_sort_by(|a,b| a.partial_cmp(b).unwrap())

Panics

Panics when a.partial_cmp(b) returns None for two values a,b inside the total order (Violated OrdSubset contract).

fn ord_subset_sort_rev(&mut self)

UNSTABLE Will likely remove these. Easily recreated by .sort_by()

Sort the slice in reverse order, in place. Values outside the ordered subset are put at the end in no particular order.

Panics

Panics when a.partial_cmp(b) returns None for two values a,b inside the total order (Violated OrdSubset contract).

fn ord_subset_sort_by<F>(&mut self, compare: F) where F: FnMut(&T, &T) -> Ordering

Sorts the slice, in place, using compare to compare elements. Values outside the total order are put at the end. The comparator will not be called on them. If you wish to handle these yourself, use the regular .sort_by(). The comparator function will only be used for elements inside the total order.

Warning: The function interface is identical to the .sort_by() interface. Be careful not to miss ord_subset_ in front. It would work until you have unordered values in your slice, then crash unexpectedly.

This uses the sort in the std library. It is therefore O(n log n) worst-case and stable, but allocates approximately 2 * n, where n is the length of self.

Panics

This method doesn't panic on its own. However, if OrdSubset was implemented incorrectly, unwrapping the result of a.partial_cmp(b) inside compare could panic. Apart from that possibility, unwrapping is safe in that situation.

Binary search a sorted slice for a given element. Values outside the ordered subset need to be at the end of the slice.

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 five 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 ord_subset::OrdSubsetSliceExt;
use std::f64;

let s = [0., 1., 1., 1., 1., 2., 3., 5., 8., 13., 21., 34., 55., f64::NAN, f64::NAN];

assert_eq!(s.ord_subset_binary_search(&13.),  Ok(9));
assert_eq!(s.ord_subset_binary_search(&4.),   Err(7));
assert_eq!(s.ord_subset_binary_search(&100.), Err(13));
let r = s.ord_subset_binary_search(&1.);
assert!(match r { Ok(1...4) => true, _ => false, });
assert_eq!(s.ord_subset_binary_search(&f64::INFINITY), Err(13));

Panics

Panics if the argument is outside of the total order. Also panics when a.partial_cmp(b) returns None for two values a,b inside the total order (Violated OrdSubset contract).

fn ord_subset_binary_search_by<F>(&self, f: F) -> Result<usizeusize> where F: FnMut(&T) -> Ordering

Binary search a sorted slice with a comparator function.

The comparator function should implement an order consistent with the sort order of the underlying slice, returning an order code that indicates whether its argument is Less, Equal or Greater the desired target. The comparator will only be called for values inside the total order.

It's imperative, that the comparator function doesn't compare its arguments with values outside the total order. This will result in bogus output which cannot be caught by this function.

If a matching value is found then returns Ok, containing the index for the matched element; if no match is found then Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

fn ord_subset_binary_search_rev(&self, x: &T) -> Result<usizeusize>

UNSTABLE Will likely remove these. Easily recreated by .binary_search_by()

Binary search a slice sorted in reverse order for a given element. Values outside the ordered subset need to be at the end of the slice.

If a matching value is found then returns Ok, containing the index for the matched element; if no match is found then Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

Panics

Panics if the argument is outside of the total order. Also panics when a.partial_cmp(b) returns None for two values a,b inside the total order (Violated OrdSubset contract).

Implementors