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,
impl<S> EytzingerMap<S>where
S: AsSlice,
pub fn new(s: S) -> Selfwhere
S: AsMutSlice,
pub fn from_sorted(s: S) -> Selfwhere
S: AsSlice + AsMutSlice,
pub fn from_eytzingerized(s: S) -> Selfwhere
S: AsSlice,
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&S::Value>
pub fn get<Q>(&self, key: &Q) -> Option<&S::Value>
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);Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<&(S::Key, S::Value)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<&(S::Key, S::Value)>
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);Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
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);Sourcepub fn iter(&self) -> impl Iterator<Item = &(S::Key, S::Value)>
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);
}Sourcepub fn keys(&self) -> impl Iterator<Item = &S::Key>
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);
}Sourcepub fn values(&self) -> impl Iterator<Item = &S::Value>
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§impl<S> EytzingerMap<S>where
S: AsMutSlice,
impl<S> EytzingerMap<S>where
S: AsMutSlice,
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut S::Value>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut S::Value>
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");Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (S::Key, S::Value)>
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);
}Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut S::Value>
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);
}Trait Implementations§
Source§impl<S, K, V> AsMut<[(K, V)]> for EytzingerMap<S>
impl<S, K, V> AsMut<[(K, V)]> for EytzingerMap<S>
Source§impl<S, K, V> AsRef<[(K, V)]> for EytzingerMap<S>
impl<S, K, V> AsRef<[(K, V)]> for EytzingerMap<S>
Source§impl<S: Clone> Clone for EytzingerMap<S>
impl<S: Clone> Clone for EytzingerMap<S>
Source§fn clone(&self) -> EytzingerMap<S>
fn clone(&self) -> EytzingerMap<S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more