EytzingerMap

Struct EytzingerMap 

Source
pub struct EytzingerMap<S>(/* private fields */);
Expand description

A map based on a generic slice-compatible type with Eytzinger binary search.

§Examples

use eytzinger_map::EytzingerMap;

// `EytzingerMap` doesn't have insert. Build one from another map.
let mut movie_reviews = std::collections::BTreeMap::new();

// review some movies.
movie_reviews.insert("Office Space",       "Deals with real issues in the workplace.");
movie_reviews.insert("Pulp Fiction",       "Masterpiece.");
movie_reviews.insert("The Godfather",      "Very enjoyable.");

let movie_reviews: EytzingerMap<_> = movie_reviews.into_iter().collect();

// check for a specific one.
if !movie_reviews.contains_key("Les Misérables") {
    println!("We've got {} reviews, but Les Misérables ain't one.",
             movie_reviews.len());
}

// look up the values associated with some keys.
let to_find = ["Up!", "Office Space"];
for movie in &to_find {
    match movie_reviews.get(movie) {
       Some(review) => println!("{}: {}", movie, review),
       None => println!("{} is unreviewed.", movie)
    }
}

// Look up the value for a key (will panic if the key is not found).
println!("Movie review: {}", movie_reviews["Office Space"]);

// iterate over everything.
for (movie, review) in movie_reviews.iter() {
    println!("{}: \"{}\"", movie, review);
}

Implementations§

Source§

impl<S> EytzingerMap<S>
where S: AsSlice,

Source

pub fn new(s: S) -> Self
where S: AsMutSlice,

Source

pub fn from_sorted(s: S) -> Self
where S: AsSlice + AsMutSlice,

Source

pub fn from_eytzingerized(s: S) -> Self
where S: AsSlice,

Source

pub fn get<Q>(&self, key: &Q) -> Option<&S::Value>
where S::Key: Borrow<Q> + Ord, Q: Ord + ?Sized,

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let map = EytzingerMap::new(vec![(1, "a")]);
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
Source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<&(S::Key, S::Value)>
where S::Key: Borrow<Q> + Ord, Q: Ord + ?Sized,

Returns the key-value pair corresponding to the supplied key.

The supplied key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

§Examples
use eytzinger_map::EytzingerMap;

let map = EytzingerMap::new(vec![(1, "a")]);
assert_eq!(map.get_key_value(&1), Some(&(1, "a")));
assert_eq!(map.get_key_value(&2), None);
Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where S::Key: Borrow<Q> + Ord, Q: Ord + ?Sized,

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let map = EytzingerMap::new(vec![(1, "a")]);
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
Source

pub fn iter(&self) -> impl Iterator<Item = &(S::Key, S::Value)>

Gets an iterator over the entries of the map.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let map = EytzingerMap::new(vec![(3, "c"), (2, "b"), (1, "a")]);

for (key, val) in map.iter() {
    println!("key: {} val: {}", key, val);
}
Source

pub fn keys(&self) -> impl Iterator<Item = &S::Key>

Gets an iterator over the keys of the map.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let map = EytzingerMap::new(vec![(2, "b"), (1, "a")]);

for key in map.keys() {
    println!("{}", key);
}
Source

pub fn values(&self) -> impl Iterator<Item = &S::Value>

Gets an iterator over the values of the map.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let map = EytzingerMap::new(vec![(1, "hello"), (2, "goodbye")]);

for val in map.values() {
    println!("{}", val);
}
Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let mut a = EytzingerMap::new(vec![]);
assert_eq!(a.len(), 0);
a = EytzingerMap::new(vec![(1, "a")]);
assert_eq!(a.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let mut a = EytzingerMap::new(vec![]);
assert!(a.is_empty());
a = EytzingerMap::new(vec![(1, "a")]);
assert!(!a.is_empty());
Source§

impl<S> EytzingerMap<S>
where S: AsMutSlice,

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut S::Value>
where S::Key: Borrow<Q> + Ord, Q: Ord + ?Sized,

Returns a mutable reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let mut map = EytzingerMap::new(vec![(1, "a")]);
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map[&1], "b");
Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (S::Key, S::Value)>

Gets a mutable iterator over the entries of the map.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let mut map = EytzingerMap::new(vec![("a", 1), ("b", 2), ("c", 3)]);

// Update all values
for (_, val) in map.iter_mut() {
    *val *= 2;
}

for (key, val) in map.iter() {
    println!("key: {} val: {}", key, val);
}
Source

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut S::Value>

Gets a mutable iterator over the values of the map.

§Examples

Basic usage:

use eytzinger_map::EytzingerMap;

let mut map = EytzingerMap::new(vec![("a", 1), ("b", 2), ("c", 3)]);

for val in map.values_mut() {
    *val = *val + 10;
}

for val in map.values() {
    println!("{}", val);
}
Source§

impl<'a, K, V> EytzingerMap<&'a [(K, V)]>
where K: Ord,

Source

pub fn from_sorted_ref(s: &'a mut [(K, V)]) -> Self

Source

pub fn from_ref(s: &'a mut [(K, V)]) -> Self

Trait Implementations§

Source§

impl<S, K, V> AsMut<[(K, V)]> for EytzingerMap<S>
where K: Ord, S: AsMut<[(K, V)]>,

Source§

fn as_mut(&mut self) -> &mut [(K, V)]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<S, K, V> AsRef<[(K, V)]> for EytzingerMap<S>
where S: AsRef<[(K, V)]>,

Source§

fn as_ref(&self) -> &[(K, V)]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<S: Clone> Clone for EytzingerMap<S>

Source§

fn clone(&self) -> EytzingerMap<S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug> Debug for EytzingerMap<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Default> Default for EytzingerMap<S>

Source§

fn default() -> EytzingerMap<S>

Returns the “default value” for a type. Read more
Source§

impl<K, V> FromIterator<(K, V)> for EytzingerMap<Vec<(K, V)>>
where K: Ord,

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<S: Hash> Hash for EytzingerMap<S>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<Q, S> Index<&Q> for EytzingerMap<S>
where S::Key: Borrow<Q> + Ord, Q: Ord + ?Sized, S: AsSlice,

Source§

fn index(&self, key: &Q) -> &S::Value

Returns a reference to the value corresponding to the supplied key.

§Panics

Panics if the key is not present in the BTreeMap.

Source§

type Output = <S as AsSlice>::Value

The returned type after indexing.
Source§

impl<S, K, V> IntoIterator for EytzingerMap<S>
where S: IntoIterator<Item = (K, V)>,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = <S as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> S::IntoIter

Creates an iterator from a value. Read more
Source§

impl<S: PartialEq> PartialEq for EytzingerMap<S>

Source§

fn eq(&self, other: &EytzingerMap<S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S: Copy> Copy for EytzingerMap<S>

Source§

impl<S: Eq> Eq for EytzingerMap<S>

Source§

impl<S> StructuralPartialEq for EytzingerMap<S>

Auto Trait Implementations§

§

impl<S> Freeze for EytzingerMap<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for EytzingerMap<S>
where S: RefUnwindSafe,

§

impl<S> Send for EytzingerMap<S>
where S: Send,

§

impl<S> Sync for EytzingerMap<S>
where S: Sync,

§

impl<S> Unpin for EytzingerMap<S>
where S: Unpin,

§

impl<S> UnwindSafe for EytzingerMap<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.