Struct micromap::Map

source ·
pub struct Map<K: PartialEq, V, const N: usize> { /* private fields */ }
Expand description

A faster alternative of std::collections::HashMap.

For example, this is how you make a map, which is allocated on stack and is capable of storing up to eight key-values pairs:

let mut m : micromap::Map<u64, &str, 8> = micromap::Map::new();
m.insert(1, "Jeff Lebowski");
m.insert(2, "Walter Sobchak");
assert_eq!(2, m.len());

It is faster because it doesn’t use a hash function at all. It simply keeps all pairs in an array and when it’s necessary to find a value, it goes through all pairs comparing the needle with each pair available. Also it is faster because it doesn’t use heap. When a Map is being created, it allocates the necessary space on stack. That’s why the maximum size of the map must be provided in compile time.

It is also faster because it doesn’t grow in size. When a Map is created, its size is fixed on stack. If an attempt is made to insert too many keys into it, it simply panics. Moreover, in the “release” mode it doesn’t panic, but its behaviour is undefined. In the “release” mode all boundary checks are disabled, for the sake of higher performance.

Implementations§

source§

impl<K: PartialEq, V, const N: usize> Map<K, V, N>

source

pub const fn new() -> Self

Make it.

The size of the map is defined by the generic argument. For example, this is how you make a map of four key-values pairs:

source§

impl<K: PartialEq, V, const N: usize> Map<K, V, N>

source

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

Make an iterator over all pairs.

source

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

An iterator with mutable references to the values but

source§

impl<K: PartialEq, V, const N: usize> Map<K, V, N>

source

pub fn keys(&self) -> Keys<'_, K, V>

An iterator visiting all keys in arbitrary order.

source

pub fn into_keys(self) -> IntoKeys<K, V, N>

Consuming iterator visiting all keys in arbitrary order.

source§

impl<K: PartialEq, V, const N: usize> Map<K, V, N>

source

pub const fn capacity(&self) -> usize

Get its total capacity.

source

pub const fn is_empty(&self) -> bool

Is it empty?

source

pub const fn len(&self) -> usize

Return the total number of pairs inside.

source

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

Does the map contain this key?

source

pub fn remove<Q: PartialEq + ?Sized>(&mut self, k: &Q)
where K: Borrow<Q>,

Remove by key.

source

pub fn insert(&mut self, k: K, v: V)

Insert a single pair into the map.

Panics

It may panic if there are too many pairs in the map already. Pay attention, it panics only in the “debug” mode. In the “release” mode, you are going to get undefined behavior. This is done for the sake of performance, in order to avoid a repetitive check for the boundary condition on every insert().

source

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

Get a reference to a single value.

source

pub fn get_mut<Q: PartialEq + ?Sized>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>,

Get a mutable reference to a single value.

Panics

If can’t turn it into a mutable state.

source

pub fn clear(&mut self)

Remove all pairs from it, but keep the space intact for future use.

source

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

Retains only the elements specified by the predicate.

source

pub fn get_key_value<Q: PartialEq + ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>,

Returns the key-value pair corresponding to the supplied key.

source

pub fn remove_entry<Q: PartialEq + ?Sized>(&mut self, k: &Q) -> Option<(K, V)>
where K: Borrow<Q>,

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

source§

impl<K: PartialEq, V, const N: usize> Map<K, V, N>

source

pub fn values(&self) -> Values<'_, K, V>

An iterator visiting all values in arbitrary order.

source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>

An iterator visiting all values mutably in arbitrary order.

source

pub fn into_values(self) -> IntoValues<K, V, N>

Consuming iterator visiting all the values in arbitrary order.

Trait Implementations§

source§

impl<K: Clone + PartialEq, V: Clone, const N: usize> Clone for Map<K, V, N>

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: PartialEq + Debug, V: Debug, const N: usize> Debug for Map<K, V, N>

source§

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

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

impl<K: PartialEq, V, const N: usize> Default for Map<K, V, N>

source§

fn default() -> Self

Make a default empty Map.

source§

impl<K: PartialEq + Display, V: Display, const N: usize> Display for Map<K, V, N>

source§

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

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

impl<K: PartialEq, V, const N: usize> Drop for Map<K, V, N>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<K: PartialEq, V, const N: usize> From<[(K, V); N]> for Map<K, V, N>

source§

fn from(arr: [(K, V); N]) -> Self

Converts to this type from the input type.
source§

impl<K: PartialEq, V, const N: usize> FromIterator<(K, V)> for Map<K, V, N>

source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<K: PartialEq + Borrow<Q>, Q: PartialEq + ?Sized, V, const N: usize> Index<&Q> for Map<K, V, N>

§

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<K: PartialEq + Borrow<Q>, Q: PartialEq + ?Sized, V, const N: usize> IndexMut<&Q> for Map<K, V, N>

source§

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

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

impl<'a, K: PartialEq, V, const N: usize> IntoIterator for &'a Map<K, V, N>

§

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?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

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

§

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

The type of the elements being iterated over.
§

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: PartialEq, V, const N: usize> IntoIterator for Map<K, V, N>

§

type Item = (K, V)

The type of the elements being iterated over.
§

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: PartialEq, V: PartialEq, const N: usize> PartialEq for Map<K, V, N>

source§

fn eq(&self, other: &Self) -> bool

Two maps can be compared.

For example:

let mut m1: micromap::Map<u8, i32, 10> = micromap::Map::new();
let mut m2: micromap::Map<u8, i32, 10> = micromap::Map::new();
m1.insert(1, 42);
m2.insert(1, 42);
assert_eq!(m1, m2);
// two maps with different order of key-value pairs are still equal:
m1.insert(2, 1);
m1.insert(3, 16);
m2.insert(3, 16);
m2.insert(2, 1);
assert_eq!(m1, m2);
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K: Eq, V: Eq, const N: usize> Eq for Map<K, V, N>

Auto Trait Implementations§

§

impl<K, V, const N: usize> RefUnwindSafe for Map<K, V, N>

§

impl<K, V, const N: usize> Send for Map<K, V, N>
where K: Send, V: Send,

§

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

§

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

§

impl<K, V, const N: usize> UnwindSafe for Map<K, V, N>
where 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> 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.