pub struct Map<K, V> { /* private fields */ }Expand description
An ordered, deduplicated collection of key-value pairs.
Implementations§
Source§impl<K: Ord, V> Map<K, V>
impl<K: Ord, V> Map<K, V>
Sourcepub fn from_iter_dedup<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self
pub fn from_iter_dedup<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self
Creates a new Map from an iterator, removing duplicate keys.
Unlike FromIterator and From, this method tolerates duplicate
keys by silently discarding them (keeping the first occurrence).
Source§impl<K, V> Map<K, V>
impl<K, V> Map<K, V>
Sourcepub fn from_parts(keys: Set<K>, values: Vec<V>) -> Option<Self>
pub fn from_parts(keys: Set<K>, values: Vec<V>) -> Option<Self>
Creates a map from ordered keys and corresponding values, reusing both allocations.
Each value is paired with the key at the same index. Returns None if
the lengths differ.
§Examples
use commonware_utils::ordered::{Map, Set};
let keys = Set::try_from([3, 1, 2]).unwrap();
let map = Map::from_parts(keys, vec!["one", "two", "three"]).unwrap();
assert_eq!(map.get_value(&2), Some(&"two"));Sourcepub fn position(&self, key: &K) -> Option<usize>where
K: Ord,
pub fn position(&self, key: &K) -> Option<usize>where
K: Ord,
Returns the position of the provided key, if it exists.
Sourcepub fn value(&self, index: usize) -> Option<&V>
pub fn value(&self, index: usize) -> Option<&V>
Returns the associated value at index, if it exists.
Sourcepub fn get_value(&self, key: &K) -> Option<&V>where
K: Ord,
pub fn get_value(&self, key: &K) -> Option<&V>where
K: Ord,
Returns the associated value for key, if it exists.
Sourcepub fn get_value_mut(&mut self, key: &K) -> Option<&mut V>where
K: Ord,
pub fn get_value_mut(&mut self, key: &K) -> Option<&mut V>where
K: Ord,
Returns a mutable reference to the associated value for key, if it exists.
Sourcepub fn values_mut(&mut self) -> &mut [V]
pub fn values_mut(&mut self) -> &mut [V]
Returns a mutable reference to the associated values
Sourcepub fn map_values<'a, U, F>(&'a self, f: F) -> Map<K, U>
pub fn map_values<'a, U, F>(&'a self, f: F) -> Map<K, U>
Maps each value in key order, cloning the keys without sorting or validating them.
The closure receives references to the key and value. The new values may borrow from this map.
§Examples
use commonware_utils::ordered::Map;
let map = Map::try_from([(4, "four"), (1, "one")]).unwrap();
let lengths = map.map_values(|_, value| value.len());
assert_eq!(lengths.keys(), map.keys());
assert_eq!(lengths.values(), &[3, 4]);Sourcepub fn try_map_values<'a, U, E, F>(&'a self, f: F) -> Result<Map<K, U>, E>
pub fn try_map_values<'a, U, E, F>(&'a self, f: F) -> Result<Map<K, U>, E>
Fallible version of Self::map_values.
Returns the first error without calling the closure for subsequent entries. Clones the keys only on success.
§Examples
use commonware_utils::ordered::Map;
let map = Map::try_from([(2, "invalid"), (1, "10")]).unwrap();
let result = map.try_map_values(|_, value| value.parse::<u32>());
assert!(result.is_err());
assert_eq!(map.values(), &["10", "invalid"]);Sourcepub fn map_values_into<U, F>(self, f: F) -> Map<K, U>
pub fn map_values_into<U, F>(self, f: F) -> Map<K, U>
Consumes the map and maps each value in key order, reusing the key allocation.
The closure receives a reference to the key and the owned value. Keys are not cloned, sorted, or validated.
§Examples
use commonware_utils::ordered::Map;
let map = Map::try_from([(2, String::from("two")), (1, String::from("one"))]).unwrap();
let bytes = map.map_values_into(|_, value| value.into_bytes());
assert_eq!(bytes.keys().as_ref(), &[1, 2]);
assert_eq!(bytes.values(), &[b"one".to_vec(), b"two".to_vec()]);Sourcepub fn try_map_values_into<U, E, F>(self, f: F) -> Result<Map<K, U>, E>
pub fn try_map_values_into<U, E, F>(self, f: F) -> Result<Map<K, U>, E>
Fallible version of Self::map_values_into.
Returns the first error without calling the closure for subsequent entries.
§Examples
use commonware_utils::ordered::Map;
let map = Map::try_from([(2, String::from("20")), (1, String::from("10"))]).unwrap();
let parsed = map.try_map_values_into(|_, value| value.parse::<u32>()).unwrap();
assert_eq!(parsed.keys().as_ref(), &[1, 2]);
assert_eq!(parsed.values(), &[10, 20]);Sourcepub fn iter_pairs(
&self,
) -> impl ExactSizeIterator<Item = (&K, &V)> + DoubleEndedIterator
pub fn iter_pairs( &self, ) -> impl ExactSizeIterator<Item = (&K, &V)> + DoubleEndedIterator
Returns a zipped iterator over keys and values.
Sourcepub fn iter_pairs_mut(
&mut self,
) -> impl ExactSizeIterator<Item = (&K, &mut V)> + DoubleEndedIterator
pub fn iter_pairs_mut( &mut self, ) -> impl ExactSizeIterator<Item = (&K, &mut V)> + DoubleEndedIterator
Returns a zipped iterator over keys and mutable references to values.
This is provided because the borrow checker does not let you combine the other functions yourself.
Source§impl<K: MaxEncodeSize, V> Map<K, V>
impl<K: MaxEncodeSize, V> Map<K, V>
Sourcepub fn max_encode_size(len: usize, value: usize) -> usize
pub fn max_encode_size(len: usize, value: usize) -> usize
Returns an upper bound on the encoded size of a map of at most len entries whose values
encode to at most value bytes each.
Methods from Deref<Target = Set<K>>§
Trait Implementations§
Source§impl<K: EncodeSize, V: EncodeSize> EncodeSize for Map<K, V>
impl<K: EncodeSize, V: EncodeSize> EncodeSize for Map<K, V>
Source§fn encode_size(&self) -> usize
fn encode_size(&self) -> usize
Source§fn encode_inline_size(&self) -> usize
fn encode_inline_size(&self) -> usize
BufsMut::push
during Write::write_bufs. Used to size the fixed working buffer for
inline writes, so it must not be less than the bytes written inline.
Override alongside Write::write_bufs for types where large
byte fields go via push, or the working buffer will over-allocate.impl<K: Eq, V: Eq> Eq for Map<K, V>
Source§impl<K, V> IntoIterator for Map<K, V>
impl<K, V> IntoIterator for Map<K, V>
Source§impl<'a, K, V> IntoIterator for &'a Map<K, V>
impl<'a, K, V> IntoIterator for &'a Map<K, V>
Source§impl<K: Read + Ord, V: Read> Read for Map<K, V>
impl<K: Read + Ord, V: Read> Read for Map<K, V>
impl<K: PartialEq, V: PartialEq> StructuralPartialEq for Map<K, V>
Source§impl<K: Write, V: Write> Write for Map<K, V>
impl<K: Write, V: Write> Write for Map<K, V>
Source§fn write_bufs(&self, buf: &mut impl BufsMut)
fn write_bufs(&self, buf: &mut impl BufsMut)
BufsMut, allowing existing byte chunks to be
appended via BufsMut::push instead of written inline. Must encode
to the same format as Write::write. Defaults to Write::write.Auto Trait Implementations§
impl<K, V> Freeze for Map<K, V>
impl<K, V> RefUnwindSafe for Map<K, V>
impl<K, V> Send for Map<K, V>
impl<K, V> Sync for Map<K, V>
impl<K, V> Unpin for Map<K, V>
impl<K, V> UnsafeUnpin for Map<K, V>
impl<K, V> UnwindSafe for Map<K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Codec for T
Source§impl<T> Encode for Twhere
T: Write + EncodeSize,
impl<T> Encode for Twhere
T: Write + EncodeSize,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
use ToHexExt instead
self into the result.
Lower case letters are used (e.g. f9b4ca).Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
use ToHexExt instead
self into the result.
Upper case letters are used (e.g. F9B4CA).Source§impl<T> ToHexExt for T
impl<T> ToHexExt for T
Source§fn encode_hex(&self) -> String
fn encode_hex(&self) -> String
self into the result.
Lower case letters are used (e.g. f9b4ca).Source§fn encode_hex_upper(&self) -> String
fn encode_hex_upper(&self) -> String
self into the result.
Upper case letters are used (e.g. F9B4CA).Source§fn encode_hex_with_prefix(&self) -> String
fn encode_hex_with_prefix(&self) -> String
self into the result with prefix 0x.
Lower case letters are used (e.g. 0xf9b4ca).Source§fn encode_hex_upper_with_prefix(&self) -> String
fn encode_hex_upper_with_prefix(&self) -> String
self into the result with prefix 0X.
Upper case letters are used (e.g. 0xF9B4CA).