heapless

Struct IndexMap

Source
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>

Source

pub const fn new() -> Self

Creates an empty IndexMap.

Source§

impl<K, V, S, const N: usize> IndexMap<K, V, S, N>

Source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold

Source

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);
}
Source

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);
}
Source

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);
}
Source

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);
}
Source

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);
}
Source

pub fn first(&self) -> Option<(&K, &V)>

Get the first key-value pair

Computes in O(1) time

Source

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

Source

pub fn last(&self) -> Option<(&K, &V)>

Get the last key-value pair

Computes in O(1) time

Source

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

Source

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

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Computes in O(1) time.

use heapless::FnvIndexMap;

let mut a = FnvIndexMap::<_, _, 16>::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());
Source

pub fn clear(&mut self)

Remove all key-value pairs in the map, while preserving its capacity.

Computes in O(n) time.

use heapless::FnvIndexMap;

let mut a = FnvIndexMap::<_, _, 16>::new();
a.insert(1, "a");
a.clear();
assert!(a.is_empty());
Source§

impl<K, V, S, const N: usize> IndexMap<K, V, S, N>
where K: Eq + Hash, S: BuildHasher,

Source

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());
Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: ?Sized + Hash + Eq,

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);
Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: ?Sized + Eq + Hash,

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);
Source

pub fn get_mut<'v, Q>(&'v mut self, key: &Q) -> Option<&'v mut V>
where K: Borrow<Q>, Q: ?Sized + Hash + Eq,

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");
Source

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");
Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: ?Sized + Hash + Eq,

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);
Source

pub fn swap_remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: ?Sized + 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).

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&K, &mut V) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) for which f(&k, &mut v) returns false.

Trait Implementations§

Source§

impl<K, V, S, const N: usize> Clone for IndexMap<K, V, S, N>
where K: Clone, V: Clone, S: Clone,

Source§

fn clone(&self) -> Self

Returns a copy 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<K, V, S, const N: usize> Debug for IndexMap<K, V, S, N>
where K: Debug, V: Debug,

Source§

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

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

impl<K, V, S, const N: usize> Default for IndexMap<K, V, S, N>
where S: Default,

Source§

fn default() -> Self

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

impl<'a, K, V, S, const N: usize> Extend<(&'a K, &'a V)> for IndexMap<K, V, S, N>
where K: Eq + Hash + Copy, V: Copy, S: BuildHasher,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = (&'a K, &'a V)>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, S, const N: usize> Extend<(K, V)> for IndexMap<K, V, S, N>
where K: Eq + Hash, S: BuildHasher,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = (K, V)>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, S, const N: usize> FromIterator<(K, V)> for IndexMap<K, V, S, N>
where K: Eq + Hash, S: BuildHasher + Default,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, K, Q, V, S, const N: usize> Index<&'a Q> for IndexMap<K, V, S, N>
where K: Eq + Hash + Borrow<Q>, Q: ?Sized + Eq + Hash, S: BuildHasher,

Source§

type Output = V

The returned type after indexing.
Source§

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

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K, Q, V, S, const N: usize> IndexMut<&'a Q> for IndexMap<K, V, S, N>
where K: Eq + Hash + Borrow<Q>, Q: ?Sized + Eq + Hash, S: BuildHasher,

Source§

fn index_mut(&mut self, key: &Q) -> &mut V

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, K, V, S, const N: usize> IntoIterator for &'a IndexMap<K, V, S, N>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S, const N: usize> IntoIterator for &'a mut IndexMap<K, V, S, N>

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, S, const N: usize> IntoIterator for IndexMap<K, V, S, N>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V, N>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, S, S2, const N: usize, const N2: usize> PartialEq<IndexMap<K, V, S2, N2>> for IndexMap<K, V, S, N>
where K: Eq + Hash, V: Eq, S: BuildHasher, S2: BuildHasher,

Source§

fn eq(&self, other: &IndexMap<K, V, S2, N2>) -> 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<K, V, S, const N: usize> Eq for IndexMap<K, V, S, N>
where K: Eq + Hash, V: Eq, S: BuildHasher,

Auto Trait Implementations§

§

impl<K, V, S, const N: usize> Freeze for IndexMap<K, V, S, N>
where S: Freeze, K: Freeze, V: Freeze,

§

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>
where S: Send, K: Send, V: Send,

§

impl<K, V, S, const N: usize> Sync for IndexMap<K, V, S, N>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, S, const N: usize> Unpin for IndexMap<K, V, S, N>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S, const N: usize> UnwindSafe for IndexMap<K, V, S, N>
where S: UnwindSafe, K: UnwindSafe, V: 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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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, 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.

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.