pub struct IndexMap<K, V, S, const N: usize> { /* private fields */ }
Expand description
Fixed capacity IndexMap
Note that you cannot use IndexMap
directly, since it is generic around the hashing algorithm
in use. Pick a concrete instantiation like FnvIndexMap
instead
or create your own.
Note that the capacity of the IndexMap
must be a power of 2.
§Examples
Since IndexMap
cannot be used directly, we’re using its FnvIndexMap
instantiation
for this example.
use heapless::FnvIndexMap;
// A hash map with a capacity of 16 key-value pairs allocated on the stack
let mut book_reviews = FnvIndexMap::<_, _, 16>::new();
// review some books.
book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book.").unwrap();
book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.").unwrap();
book_reviews.insert("Pride and Prejudice", "Very enjoyable.").unwrap();
book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.").unwrap();
// check for a specific one.
if !book_reviews.contains_key("Les Misérables") {
println!("We've got {} reviews, but Les Misérables ain't one.",
book_reviews.len());
}
// oops, this review has a lot of spelling mistakes, let's delete it.
book_reviews.remove("The Adventures of Sherlock Holmes");
// look up the values associated with some keys.
let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
for book in &to_find {
match book_reviews.get(book) {
Some(review) => println!("{}: {}", book, review),
None => println!("{} is unreviewed.", book)
}
}
// iterate over everything.
for (book, review) in &book_reviews {
println!("{}: \"{}\"", book, review);
}
Implementations§
Source§impl<K, V, S, const N: usize> IndexMap<K, V, BuildHasherDefault<S>, N>
impl<K, V, S, const N: usize> IndexMap<K, V, BuildHasherDefault<S>, N>
Source§impl<K, V, S, const N: usize> IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> IndexMap<K, V, S, N>
Sourcepub fn keys(&self) -> Keys<'_, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, K, V> ⓘ
Return an iterator over the keys of the map, in insertion order
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 16>::new();
map.insert("a", 1).unwrap();
map.insert("b", 2).unwrap();
map.insert("c", 3).unwrap();
for key in map.keys() {
println!("{}", key);
}
Sourcepub fn values(&self) -> Values<'_, K, V> ⓘ
pub fn values(&self) -> Values<'_, K, V> ⓘ
Return an iterator over the values of the map, in insertion order
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 16>::new();
map.insert("a", 1).unwrap();
map.insert("b", 2).unwrap();
map.insert("c", 3).unwrap();
for val in map.values() {
println!("{}", val);
}
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
Return an iterator over mutable references to the the values of the map, in insertion order
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 16>::new();
map.insert("a", 1).unwrap();
map.insert("b", 2).unwrap();
map.insert("c", 3).unwrap();
for val in map.values_mut() {
*val += 10;
}
for val in map.values() {
println!("{}", val);
}
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Return an iterator over the key-value pairs of the map, in insertion order
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 16>::new();
map.insert("a", 1).unwrap();
map.insert("b", 2).unwrap();
map.insert("c", 3).unwrap();
for (key, val) in map.iter() {
println!("key: {} val: {}", key, val);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Return an iterator over the key-value pairs of the map, in insertion order
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 16>::new();
map.insert("a", 1).unwrap();
map.insert("b", 2).unwrap();
map.insert("c", 3).unwrap();
for (_, val) in map.iter_mut() {
*val = 2;
}
for (key, val) in &map {
println!("key: {} val: {}", key, val);
}
Sourcepub fn first_mut(&mut self) -> Option<(&K, &mut V)>
pub fn first_mut(&mut self) -> Option<(&K, &mut V)>
Get the first key-value pair, with mutable access to the value
Computes in O(1) time
Sourcepub fn last_mut(&mut self) -> Option<(&K, &mut V)>
pub fn last_mut(&mut self) -> Option<(&K, &mut V)>
Get the last key-value pair, with mutable access to the value
Computes in O(1) time
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the number of key-value pairs in the map.
Computes in O(1) time.
use heapless::FnvIndexMap;
let mut a = FnvIndexMap::<_, _, 16>::new();
assert_eq!(a.len(), 0);
a.insert(1, "a").unwrap();
assert_eq!(a.len(), 1);
Source§impl<K, V, S, const N: usize> IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> IndexMap<K, V, S, N>
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V, N>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, N>
Returns an entry for the corresponding key
use heapless::FnvIndexMap;
use heapless::Entry;
let mut map = FnvIndexMap::<_, _, 16>::new();
if let Entry::Vacant(v) = map.entry("a") {
v.insert(1).unwrap();
}
if let Entry::Occupied(mut o) = map.entry("a") {
println!("found {}", *o.get()); // Prints 1
o.insert(2);
}
// Prints 2
println!("val: {}", *map.get("a").unwrap());
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but Hash
and Eq
on the borrowed
form must match those for the key type.
Computes in O(1) time (average).
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 16>::new();
map.insert(1, "a").unwrap();
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&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 Hash
and Eq
on the borrowed
form must match those for the key type.
Computes in O(1) time (average).
§Examples
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 8>::new();
map.insert(1, "a").unwrap();
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
Sourcepub fn get_mut<'v, Q>(&'v mut self, key: &Q) -> Option<&'v mut V>
pub fn get_mut<'v, Q>(&'v mut self, key: &Q) -> Option<&'v mut V>
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 Hash
and Eq
on the borrowed
form must match those for the key type.
Computes in O(1) time (average).
§Examples
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 8>::new();
map.insert(1, "a").unwrap();
if let Some(x) = map.get_mut(&1) {
*x = "b";
}
assert_eq!(map[&1], "b");
Sourcepub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)>
pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)>
Inserts a key-value pair into the map.
If an equivalent key already exists in the map: the key remains and retains in its place in
the order, its corresponding value is updated with value
and the older value is returned
inside Some(_)
.
If no equivalent key existed in the map: the new key-value pair is inserted, last in order,
and None
is returned.
Computes in O(1) time (average).
See also entry if you you want to insert or modify or if you need to get the index of the corresponding key-value pair.
§Examples
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 8>::new();
assert_eq!(map.insert(37, "a"), Ok(None));
assert_eq!(map.is_empty(), false);
map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Ok(Some("b")));
assert_eq!(map[&37], "c");
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Same as swap_remove
Computes in O(1) time (average).
§Examples
use heapless::FnvIndexMap;
let mut map = FnvIndexMap::<_, _, 8>::new();
map.insert(1, "a").unwrap();
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);
Sourcepub fn swap_remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn swap_remove<Q>(&mut self, key: &Q) -> Option<V>
Remove the key-value pair equivalent to key
and return its value.
Like Vec::swap_remove
, the pair is removed by swapping it with the last element of the map
and popping it off. This perturbs the postion of what used to be the last element!
Return None
if key
is not in map.
Computes in O(1) time (average).
Trait Implementations§
Source§impl<'a, K, V, S, const N: usize> Extend<(&'a K, &'a V)> for IndexMap<K, V, S, N>
impl<'a, K, V, S, const N: usize> Extend<(&'a K, &'a V)> for IndexMap<K, V, S, N>
Source§fn extend<I>(&mut self, iterable: I)
fn extend<I>(&mut self, iterable: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<K, V, S, const N: usize> Extend<(K, V)> for IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> Extend<(K, V)> for IndexMap<K, V, S, N>
Source§fn extend<I>(&mut self, iterable: I)where
I: IntoIterator<Item = (K, V)>,
fn extend<I>(&mut self, iterable: I)where
I: IntoIterator<Item = (K, V)>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'a, K, V, S, const N: usize> IntoIterator for &'a IndexMap<K, V, S, N>
impl<'a, K, V, S, const N: usize> IntoIterator for &'a IndexMap<K, V, S, N>
Source§impl<'a, K, V, S, const N: usize> IntoIterator for &'a mut IndexMap<K, V, S, N>
impl<'a, K, V, S, const N: usize> IntoIterator for &'a mut IndexMap<K, V, S, N>
Source§impl<K, V, S, const N: usize> IntoIterator for IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> IntoIterator for IndexMap<K, V, S, N>
Source§impl<K, V, S, S2, const N: usize, const N2: usize> PartialEq<IndexMap<K, V, S2, N2>> for IndexMap<K, V, S, N>
impl<K, V, S, S2, const N: usize, const N2: usize> PartialEq<IndexMap<K, V, S2, N2>> for IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> Eq for IndexMap<K, V, S, N>
Auto Trait Implementations§
impl<K, V, S, const N: usize> Freeze for IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> RefUnwindSafe for IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> Send for IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> Sync for IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> Unpin for IndexMap<K, V, S, N>
impl<K, V, S, const N: usize> UnwindSafe for IndexMap<K, V, S, N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Layout§
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.