Struct heapless::IndexMap [−][src]
pub struct IndexMap<K, V, N, S> where
K: Eq + Hash,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>, { /* fields omitted */ }
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; use heapless::consts::*; // A hash map with a capacity of 16 key-value pairs allocated on the stack let mut book_reviews = FnvIndexMap::<_, _, U16>::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
impl<K, V, N, S> IndexMap<K, V, N, BuildHasherDefault<S>> where
K: Eq + Hash,
S: Default + Hasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>> + PowerOfTwo,
impl<K, V, N, S> IndexMap<K, V, N, BuildHasherDefault<S>> where
K: Eq + Hash,
S: Default + Hasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>> + PowerOfTwo,
impl<K, V, N, S> IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S> IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
Return an iterator over the keys of the map, in their order
use heapless::FnvIndexMap; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U16>::new(); map.insert("a", 1).unwrap(); map.insert("b", 2).unwrap(); map.insert("c", 3).unwrap(); for key in map.keys() { println!("{}", key); }
Return an iterator over the values of the map, in their order
use heapless::FnvIndexMap; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U16>::new(); map.insert("a", 1).unwrap(); map.insert("b", 2).unwrap(); map.insert("c", 3).unwrap(); for val in map.values() { println!("{}", val); }
Return an iterator over mutable references to the the values of the map, in their order
use heapless::FnvIndexMap; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U16>::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); }
Return an iterator over the key-value pairs of the map, in their order
use heapless::FnvIndexMap; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U16>::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); }
Return an iterator over the key-value pairs of the map, in their order
use heapless::FnvIndexMap; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U16>::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); }
Return the number of key-value pairs in the map.
Computes in O(1) time.
use heapless::FnvIndexMap; use heapless::consts::*; let mut a = FnvIndexMap::<_, _, U16>::new(); assert_eq!(a.len(), 0); a.insert(1, "a").unwrap(); assert_eq!(a.len(), 1);
Returns true if the map contains no elements.
Computes in O(1) time.
use heapless::FnvIndexMap; use heapless::consts::*; let mut a = FnvIndexMap::<_, _, U16>::new(); assert!(a.is_empty()); a.insert(1, "a"); assert!(!a.is_empty());
Remove all key-value pairs in the map, while preserving its capacity.
Computes in O(n) time.
use heapless::FnvIndexMap; use heapless::consts::*; let mut a = FnvIndexMap::<_, _, U16>::new(); a.insert(1, "a"); a.clear(); assert!(a.is_empty());
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; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U16>::new(); map.insert(1, "a").unwrap(); assert_eq!(map.get(&1), Some(&"a")); assert_eq!(map.get(&2), None);
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; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U8>::new(); map.insert(1, "a").unwrap(); assert_eq!(map.contains_key(&1), true); assert_eq!(map.contains_key(&2), false);
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; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U8>::new(); map.insert(1, "a").unwrap(); if let Some(x) = map.get_mut(&1) { *x = "b"; } assert_eq!(map[&1], "b");
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; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U8>::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");
Same as swap_remove
Computes in O(1) time (average).
Examples
use heapless::FnvIndexMap; use heapless::consts::*; let mut map = FnvIndexMap::<_, _, U8>::new(); map.insert(1, "a").unwrap(); assert_eq!(map.remove(&1), Some("a")); assert_eq!(map.remove(&1), None);
pub fn swap_remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
pub fn swap_remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
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
impl<K, V, N, S> Clone for IndexMap<K, V, N, S> where
K: Eq + Hash + Clone,
V: Clone,
S: Clone,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S> Clone for IndexMap<K, V, N, S> where
K: Eq + Hash + Clone,
V: Clone,
S: Clone,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
Performs copy-assignment from source
. Read more
impl<K, V, N, S> Debug for IndexMap<K, V, N, S> where
K: Eq + Hash + Debug,
V: Debug,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S> Debug for IndexMap<K, V, N, S> where
K: Eq + Hash + Debug,
V: Debug,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S> Default for IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher + Default,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S> Default for IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher + Default,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, V, N, S> Extend<(&'a K, &'a V)> for IndexMap<K, V, N, S> where
K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, V, N, S> Extend<(&'a K, &'a V)> for IndexMap<K, V, N, S> where
K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
Extends a collection with the contents of an iterator. Read more
fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
impl<K, V, N, S> Extend<(K, V)> for IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S> Extend<(K, V)> for IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
Extends a collection with the contents of an iterator. Read more
fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
impl<K, V, N, S> FromIterator<(K, V)> for IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher + Default,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S> FromIterator<(K, V)> for IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher + Default,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, Q: ?Sized, V, N, S> Index<&'a Q> for IndexMap<K, V, N, S> where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, Q: ?Sized, V, N, S> Index<&'a Q> for IndexMap<K, V, N, S> where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, Q: ?Sized, V, N, S> IndexMut<&'a Q> for IndexMap<K, V, N, S> where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, Q: ?Sized, V, N, S> IndexMut<&'a Q> for IndexMap<K, V, N, S> where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, V, N, S> IntoIterator for &'a IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, V, N, S> IntoIterator for &'a IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, V, N, S> IntoIterator for &'a mut IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<'a, K, V, N, S> IntoIterator for &'a mut IndexMap<K, V, N, S> where
K: Eq + Hash,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S, N2, S2> PartialEq<IndexMap<K, V, N2, S2>> for IndexMap<K, V, N, S> where
K: Eq + Hash,
V: Eq,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
S2: BuildHasher,
N2: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S, N2, S2> PartialEq<IndexMap<K, V, N2, S2>> for IndexMap<K, V, N, S> where
K: Eq + Hash,
V: Eq,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
S2: BuildHasher,
N2: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
impl<K, V, N, S> Eq for IndexMap<K, V, N, S> where
K: Eq + Hash,
V: Eq,
S: BuildHasher,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
Auto Trait Implementations
impl<K, V, N, S> Send for IndexMap<K, V, N, S> where
K: Send,
S: Send,
V: Send,
impl<K, V, N, S> Sync for IndexMap<K, V, N, S> where
K: Sync,
S: Sync,
V: Sync,
impl<K, V, N, S> Unpin for IndexMap<K, V, N, S> where
S: Unpin,
<N as ArrayLength<Bucket<K, V>>>::ArrayType: Unpin,
<N as ArrayLength<Option<Pos>>>::ArrayType: Unpin,
Blanket Implementations
pub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
pub fn from(t: T) -> T
pub fn from(t: T) -> T
Performs the conversion.
pub fn into(self) -> U
pub fn into(self) -> U
Performs the conversion.