Struct heapless::LinearMap[][src]

pub struct LinearMap<K, V, const N: usize> { /* fields omitted */ }
Expand description

A fixed capacity map / dictionary that performs lookups via linear search

Note that as this map doesn’t use hashing so most operations are O(N) instead of O(1)

Implementations

Creates an empty LinearMap

Examples

use heapless::LinearMap;

// allocate the map on the stack
let mut map: LinearMap<&str, isize, 8> = LinearMap::new();

// allocate the map in a static variable
static mut MAP: LinearMap<&str, isize, 8> = LinearMap::new();

Returns the number of elements that the map can hold

Computes in O(1) time

Examples

use heapless::LinearMap;

let map: LinearMap<&str, isize, 8> = LinearMap::new();
assert_eq!(map.capacity(), 8);

Clears the map, removing all key-value pairs

Computes in O(1) time

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::new();
map.insert(1, "a").unwrap();
map.clear();
assert!(map.is_empty());

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

Computes in O(N) time

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::new();
map.insert(1, "a").unwrap();
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);

Returns a reference to the value corresponding to the key

Computes in O(N) time

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::new();
map.insert(1, "a").unwrap();
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);

Returns a mutable reference to the value corresponding to the key

Computes in O(N) time

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::new();
map.insert(1, "a").unwrap();
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map[&1], "b");

Returns the number of elements in this map

Computes in O(1) time

Examples

use heapless::LinearMap;

let mut a: LinearMap<_, _, 8> = LinearMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a").unwrap();
assert_eq!(a.len(), 1);

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and the old value is returned.

Computes in O(N) time

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::new();
assert_eq!(map.insert(37, "a").unwrap(), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b").unwrap();
assert_eq!(map.insert(37, "c").unwrap(), Some("b"));
assert_eq!(map[&37], "c");

Returns true if the map contains no elements

Computes in O(1) time

Examples

use heapless::LinearMap;

let mut a: LinearMap<_, _, 8> = LinearMap::new();
assert!(a.is_empty());
a.insert(1, "a").unwrap();
assert!(!a.is_empty());

An iterator visiting all key-value pairs in arbitrary order.

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::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);
}

An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::new();
map.insert("a", 1).unwrap();
map.insert("b", 2).unwrap();
map.insert("c", 3).unwrap();

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

for (key, val) in &map {
    println!("key: {} val: {}", key, val);
}

An iterator visiting all keys in arbitrary order

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::new();
map.insert("a", 1).unwrap();
map.insert("b", 2).unwrap();
map.insert("c", 3).unwrap();

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

Removes a key from the map, returning the value at the key if the key was previously in the map

Computes in O(N) time

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::new();
map.insert(1, "a").unwrap();
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);

An iterator visiting all values in arbitrary order

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::new();
map.insert("a", 1).unwrap();
map.insert("b", 2).unwrap();
map.insert("c", 3).unwrap();

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

An iterator visiting all values mutably in arbitrary order

Examples

use heapless::LinearMap;

let mut map: LinearMap<_, _, 8> = LinearMap::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);
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Executes the destructor for this type. Read more

Creates a value from an iterator. Read more

The returned type after indexing.

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.