Skip to main content

Comparator

Trait Comparator 

Source
pub trait Comparator<K>
where K: Copy,
{ // Required method fn cmp(&self, a: K, b: K) -> Ordering; // Provided method fn search(&self, k: K, s: &[K]) -> Result<usize, usize> { ... } }
Expand description

Key comparator.

Keys don’t need to implement Ord. They are compared using a comparator object which provides a context for comparison.

Required Methods§

Source

fn cmp(&self, a: K, b: K) -> Ordering

Compare keys a and b.

This relation must provide a total ordering or the key space.

Provided Methods§

Source

fn search(&self, k: K, s: &[K]) -> Result<usize, usize>

Binary search for k in an ordered slice.

Assume that s is already sorted according to this ordering, search for the key k.

Returns Ok(idx) if k was found in the slice or Err(idx) with the position where it should be inserted to preserve the ordering.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<K> Comparator<K> for ()
where K: Copy + Ord,

Trivial comparator that doesn’t actually provide any context.

Source§

fn cmp(&self, a: K, b: K) -> Ordering

Implementors§