Struct fchashmap::FcHashMap[][src]

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

A fixed capacity no_std hashmap.

The realization of the hashmap is based on the Robin Hood hashing algorithm. This method
is simple and robust with reasonable performance. However, the fixed capacity implementation has some limitations:

  • The size of the hashmap must be fixed at compile time
  • 8 bytes ram are consumed per entry without keys and values
  • The maximum capacity is limited to 32768 entries
  • The capacity must be chosen as a power of 2
  • The hashmap should not be used to its full capacity, otherwise it will become slow. 10 to 20 percent of the capacity should always be kept free.

Example

use fchashmap::FcHashMap;
use hash32_derive::Hash32;
use hash32::Hash;

#[derive(Debug)]
struct Reading {
    temperature: f32,
    humidy: f32,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash32)]
struct DeviceId([u8; 8]);

impl DeviceId {
    fn new(input: &[u8; 8]) -> Self {
        let mut id = [0_u8; 8];
        id.copy_from_slice(input);
        Self(id)
    }
}

let mut fc_hash_map = FcHashMap::<DeviceId, Reading, 128>::new();

let dev1 = DeviceId::new(b"12345678");
let dev2 = DeviceId::new(b"12345679");
let dev3 = DeviceId::new(b"12345680");

fc_hash_map.insert(dev1, Reading { temperature: 23.1, humidy: 76.3 }).unwrap();
fc_hash_map.insert(dev2, Reading { temperature: 22.7, humidy: 55.5 }).unwrap();

let reading = fc_hash_map.get(&dev1).unwrap();
assert_eq!(reading.temperature, 23.1);
assert_eq!(reading.humidy, 76.3);

let reading = fc_hash_map.get(&dev2).unwrap();
assert_eq!(reading.temperature, 22.7);
assert_eq!(reading.humidy, 55.5);

assert!(fc_hash_map.get(&dev3).is_none());

Performance

The following diagram shows the timing behavior on a Cortex M4f system (STM32F3) at 72 MHz. It can be seen that the performance of the hashmap decreases significantly from a fill margin of about 80%.

Image

Implementations

impl<K, V, const CAP: usize> FcHashMap<K, V, CAP>[src]

pub fn new() -> Self[src]

Creates an empty HashMap.

The hash map is initially created with no elements inside. The maximum capacity must be set at complile time.

Example

use fchashmap::FcHashMap;
let mut map: FcHashMap<u32, i32, 16> = FcHashMap::new();

pub fn capacity(&self) -> usize[src]

Returns the number of elements the map can hold.

pub fn clear(&mut self)[src]

Remove all key-value pairs in the map.

Example

use fchashmap::FcHashMap;

let mut map = FcHashMap::<_, _, 16>::new();
map.insert(1, "a");

map.clear();
assert!(map.is_empty());

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

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 ’HashandEq` on the borrowed form must match those for the key type.

Example

use fchashmap::FcHashMap;

let mut map = FcHashMap::<_, _, 8>::new();
map.insert(1, "a").unwrap();

assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);

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

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.

Example

use fchashmap::FcHashMap;

let mut map = FcHashMap::<_, _, 16>::new();
map.insert(1, "a").unwrap();

assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);

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

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.

Example

use fchashmap::FcHashMap;

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

pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)> where
    K: Hash + PartialEq
[src]

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, and None is returned.

Example

use fchashmap::FcHashMap;

let mut map = FcHashMap::<_, _, 8>::new();
assert_eq!(map.insert(37, "a"), Ok(None));

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Ok(Some("b")));
assert_eq!(map.get(&37), Some(&"c"));

pub fn is_empty(&self) -> bool[src]

Returns true if the map contains no elements.

Example

use fchashmap::FcHashMap;

let mut map = FcHashMap::<_, _, 16>::new();
assert_eq!(map.is_empty(), true);

map.insert(1, "a");
assert_eq!(map.is_empty(), false);

pub fn iter(&self) -> Iter<'_, K, V>[src]

Return an iterator over the key-value pairs of the map, in their order.

Example

use fchashmap::FcHashMap;

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

let v: Vec<_> = map.iter().collect();
assert_eq!(v, vec![(&"a", &1), (&"b", &2), (&"c", &3)]);

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>[src]

Return an iterator over the key-value pairs of the map, in their order.

Example

use fchashmap::FcHashMap;

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

for (_, val) in map.iter_mut() {
    *val = 23;
}

let v: Vec<_> = map.iter().collect();
assert_eq!(v, vec![(&"a", &23), (&"b", &23), (&"c", &23)]);

pub fn keys(&self) -> impl Iterator<Item = &K>[src]

Return an iterator over the keys of the map, in their order.

Example

use fchashmap::FcHashMap;

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

let v: Vec<_> = map.keys().collect();
assert_eq!(v, vec![&"a", &"b", &"c"]);

pub fn len(&self) -> usize[src]

Return the number of key-value pairs in the map.

Example

use fchashmap::FcHashMap;

let mut map = FcHashMap::<_, _, 16>::new();
assert_eq!(map.len(), 0);

map.insert(1, "a").unwrap();
assert_eq!(map.len(), 1);

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

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

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.

Example

use fchashmap::FcHashMap;

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

pub fn values(&self) -> impl Iterator<Item = &V>[src]

Return an iterator over the values of the map, in their order.

Example

use fchashmap::FcHashMap;

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

let v: Vec<_> = map.values().collect();
assert_eq!(v, vec![&1, &2, &3]);

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>[src]

Return an iterator over mutable references to the the values of the map, in their order.

Example

use fchashmap::FcHashMap;

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

let v: Vec<_> = map.values().collect();
assert_eq!(v, vec![&11, &12, &13]);

Trait Implementations

impl<K, V, const CAP: usize> Clone for FcHashMap<K, V, CAP> where
    K: Eq + Hash + Clone,
    V: Clone
[src]

fn clone(&self) -> Self[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<K, V, const CAP: usize> Debug for FcHashMap<K, V, CAP> where
    K: Eq + Hash + Debug,
    V: Debug
[src]

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

Formats the value using the given formatter. Read more

impl<'a, K, V, const CAP: usize> Extend<(&'a K, &'a V)> for FcHashMap<K, V, CAP> where
    K: Eq + Hash + Copy,
    V: Copy
[src]

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

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

fn extend_one(&mut self, item: A)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

fn extend_reserve(&mut self, additional: usize)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

impl<K, V, const CAP: usize> Extend<(K, V)> for FcHashMap<K, V, CAP> where
    K: Eq + Hash
[src]

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

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

fn extend_one(&mut self, item: A)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

fn extend_reserve(&mut self, additional: usize)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

impl<K, V, const CAP: usize> FromIterator<(K, V)> for FcHashMap<K, V, CAP> where
    K: Eq + Hash
[src]

fn from_iter<I>(fc_hash_map: I) -> Self where
    I: IntoIterator<Item = (K, V)>, 
[src]

Creates a value from an iterator. Read more

impl<'a, K, Q: ?Sized, V, const CAP: usize> Index<&'a Q> for FcHashMap<K, V, CAP> where
    K: Eq + Hash + Borrow<Q>,
    Q: Eq + Hash
[src]

type Output = V

The returned type after indexing.

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

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

impl<'a, K, Q: ?Sized, V, const N: usize> IndexMut<&'a Q> for FcHashMap<K, V, N> where
    K: Eq + Hash + Borrow<Q>,
    Q: Eq + Hash
[src]

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

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

impl<'a, K, V, const CAP: usize> IntoIterator for &'a FcHashMap<K, V, CAP> where
    K: Eq + Hash
[src]

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

The type of the elements being iterated over.

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

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

Auto Trait Implementations

impl<K, V, const CAP: usize> Send for FcHashMap<K, V, CAP> where
    K: Send,
    V: Send

impl<K, V, const CAP: usize> Sync for FcHashMap<K, V, CAP> where
    K: Sync,
    V: Sync

impl<K, V, const CAP: usize> Unpin for FcHashMap<K, V, CAP> where
    K: Unpin,
    V: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.