pub struct LinearMap<K, V, const N: usize> { /* private fields */ }
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
sourceimpl<K, V, const N: usize> LinearMap<K, V, N> where
K: Eq,
impl<K, V, const N: usize> LinearMap<K, V, N> where
K: Eq,
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
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);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
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());
sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
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);
sourcepub fn get<Q>(&self, key: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Eq + ?Sized,
pub fn get<Q>(&self, key: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Eq + ?Sized,
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);
sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Eq + ?Sized,
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Eq + ?Sized,
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");
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
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);
sourcepub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)>
pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)>
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");
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
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());
sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
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);
}
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
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);
}
sourcepub fn keys(&self) -> impl Iterator<Item = &K>
pub fn keys(&self) -> impl Iterator<Item = &K>
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);
}
sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Eq + ?Sized,
pub fn remove<Q>(&mut self, key: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Eq + ?Sized,
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);
sourcepub fn values(&self) -> impl Iterator<Item = &V>
pub fn values(&self) -> impl Iterator<Item = &V>
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);
}
sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
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
sourceimpl<'de, K, V, const N: usize> Deserialize<'de> for LinearMap<K, V, N> where
K: Eq + Deserialize<'de>,
V: Deserialize<'de>,
impl<'de, K, V, const N: usize> Deserialize<'de> for LinearMap<K, V, N> where
K: Eq + Deserialize<'de>,
V: Deserialize<'de>,
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<'a, K, V, Q, const N: usize> Index<&'a Q> for LinearMap<K, V, N> where
K: Borrow<Q> + Eq,
Q: Eq + ?Sized,
impl<'a, K, V, Q, const N: usize> Index<&'a Q> for LinearMap<K, V, N> where
K: Borrow<Q> + Eq,
Q: Eq + ?Sized,
sourceimpl<'a, K, V, Q, const N: usize> IndexMut<&'a Q> for LinearMap<K, V, N> where
K: Borrow<Q> + Eq,
Q: Eq + ?Sized,
impl<'a, K, V, Q, const N: usize> IndexMut<&'a Q> for LinearMap<K, V, N> where
K: Borrow<Q> + Eq,
Q: Eq + ?Sized,
sourceimpl<'a, K, V, const N: usize> IntoIterator for &'a LinearMap<K, V, N> where
K: Eq,
impl<'a, K, V, const N: usize> IntoIterator for &'a LinearMap<K, V, N> where
K: Eq,
sourceimpl<K, V, const N: usize, const N2: usize> PartialEq<LinearMap<K, V, N2>> for LinearMap<K, V, N> where
K: Eq,
V: PartialEq,
impl<K, V, const N: usize, const N2: usize> PartialEq<LinearMap<K, V, N2>> for LinearMap<K, V, N> where
K: Eq,
V: PartialEq,
sourceimpl<K, V, const N: usize> Serialize for LinearMap<K, V, N> where
K: Eq + Serialize,
V: Serialize,
impl<K, V, const N: usize> Serialize for LinearMap<K, V, N> where
K: Eq + Serialize,
V: Serialize,
impl<K, V, const N: usize> Eq for LinearMap<K, V, N> where
K: Eq,
V: PartialEq,
Auto Trait Implementations
impl<K, V, const N: usize> RefUnwindSafe for LinearMap<K, V, N> where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V, const N: usize> Send for LinearMap<K, V, N> where
K: Send,
V: Send,
impl<K, V, const N: usize> Sync for LinearMap<K, V, N> where
K: Sync,
V: Sync,
impl<K, V, const N: usize> Unpin for LinearMap<K, V, N> where
K: Unpin,
V: Unpin,
impl<K, V, const N: usize> UnwindSafe for LinearMap<K, V, N> where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more