pub struct DoubleMap<K1, K2, V> { /* private fields */ }Expand description
A map-like container supporting O(1) lookup by two key types.
Implementations§
Source§impl<K1, K2, V> DoubleMap<K1, K2, V>
impl<K1, K2, V> DoubleMap<K1, K2, V>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new, empty DoubleMap with at least the specified capacity for
both internal maps.
Sourcepub fn keys(&self) -> Keys<'_, K1, K2, V> ⓘ
pub fn keys(&self) -> Keys<'_, K1, K2, V> ⓘ
Returns an iterator over the keys, yielding a (&K1, &K2) tuple for each entry.
Sourcepub fn into_keys(self) -> IntoKeys<K1, K2, V> ⓘ
pub fn into_keys(self) -> IntoKeys<K1, K2, V> ⓘ
Creates a consuming iterator yielding owned (K1, K2) key tuples in arbitrary
order. The map cannot be used after calling this.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K1, K2, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K1, K2, V> ⓘ
Returns an iterator yielding mutable references to each value.
Sourcepub fn into_values(self) -> IntoValues<K1, K2, V> ⓘ
pub fn into_values(self) -> IntoValues<K1, K2, V> ⓘ
Creates a consuming iterator yielding owned values in arbitrary order. The map cannot be used after calling this.
Sourcepub fn iter(&self) -> Iter<'_, K1, K2, V> ⓘ
pub fn iter(&self) -> Iter<'_, K1, K2, V> ⓘ
Returns an iterator yielding every entry as (&K1, &K2, &V).
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K1, K2, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K1, K2, V> ⓘ
Returns an iterator yielding every entry as (&K1, &K2, &mut V).
Neither key is mutably accessible — mutating them would desync the two internal maps.
Source§impl<K1, K2, V> DoubleMap<K1, K2, V>
impl<K1, K2, V> DoubleMap<K1, K2, V>
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the entries for which the predicate returns true.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more entries in both internal maps.
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional more entries in both
internal maps. Returns an error on allocation failure.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the internal maps to fit the current number of entries.
Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the internal maps toward the given lower bound.
Sourcepub fn get_by_key1<Q>(&self, key: &Q) -> Option<&V>
pub fn get_by_key1<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the K1 key.
Sourcepub fn get_by_key2<Q>(&self, key: &Q) -> Option<&V>
pub fn get_by_key2<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the K2 key.
Sourcepub fn get_by_keys<Q1, Q2>(&self, key1: &Q1, key2: &Q2) -> Option<&V>
pub fn get_by_keys<Q1, Q2>(&self, key1: &Q1, key2: &Q2) -> Option<&V>
Returns a reference to the value for the entry whose K1 key equals key1
and whose K2 key equals key2. Returns None if either key is missing,
or if they refer to different entries.
Sourcepub fn get_key1_value<Q>(&self, key: &Q) -> Option<(&K1, &V)>
pub fn get_key1_value<Q>(&self, key: &Q) -> Option<(&K1, &V)>
Returns the stored K1 key and value for the entry identified by the given
K1 key.
Sourcepub fn get_key2_value<Q>(&self, key: &Q) -> Option<(&K2, &V)>
pub fn get_key2_value<Q>(&self, key: &Q) -> Option<(&K2, &V)>
Returns the stored K2 key and value for the entry identified by the given
K2 key.
Sourcepub fn get_keys_value<Q1, Q2>(
&self,
key1: &Q1,
key2: &Q2,
) -> Option<(&K1, &K2, &V)>
pub fn get_keys_value<Q1, Q2>( &self, key1: &Q1, key2: &Q2, ) -> Option<(&K1, &K2, &V)>
Returns the stored (K1, K2, V) triple for the entry whose K1 key equals
key1 and whose K2 key equals key2. Returns None if either key is
missing, or if they refer to different entries.
Sourcepub fn contains_key1<Q>(&self, key: &Q) -> bool
pub fn contains_key1<Q>(&self, key: &Q) -> bool
Returns true if the map contains a value for the given K1 key.
Sourcepub fn contains_key2<Q>(&self, key: &Q) -> bool
pub fn contains_key2<Q>(&self, key: &Q) -> bool
Returns true if the map contains a value for the given K2 key.
Sourcepub fn contains_keys<Q1, Q2>(&self, key1: &Q1, key2: &Q2) -> bool
pub fn contains_keys<Q1, Q2>(&self, key1: &Q1, key2: &Q2) -> bool
Returns true if the map contains an entry whose K1 key equals key1
and whose K2 key equals key2 (i.e. both keys refer to the same entry).
Sourcepub fn get_mut_by_key1<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut_by_key1<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the K1 key.
Sourcepub fn get_mut_by_key2<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut_by_key2<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the K2 key.
Sourcepub fn get_mut_by_keys<Q1, Q2>(
&mut self,
key1: &Q1,
key2: &Q2,
) -> Option<&mut V>
pub fn get_mut_by_keys<Q1, Q2>( &mut self, key1: &Q1, key2: &Q2, ) -> Option<&mut V>
Returns a mutable reference to the value for the entry whose K1 key equals
key1 and whose K2 key equals key2. Returns None if either key is
missing, or if they refer to different entries.
Sourcepub fn remove_by_key1<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove_by_key1<Q>(&mut self, key: &Q) -> Option<V>
Removes an entry by its K1 key, returning the value if it was present.
Sourcepub fn remove_by_key2<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove_by_key2<Q>(&mut self, key: &Q) -> Option<V>
Removes an entry by its K2 key, returning the value if it was present.
Sourcepub fn remove_by_keys<Q1, Q2>(&mut self, key1: &Q1, key2: &Q2) -> Option<V>
pub fn remove_by_keys<Q1, Q2>(&mut self, key1: &Q1, key2: &Q2) -> Option<V>
Removes an entry only if its K1 key equals key1 and its K2 key equals
key2. Returns the value on success, or None if either key is missing or they
refer to different entries. On mismatch, the map is unchanged.
Sourcepub fn remove_entry_by_key1<Q>(&mut self, key: &Q) -> Option<(K1, K2, V)>
pub fn remove_entry_by_key1<Q>(&mut self, key: &Q) -> Option<(K1, K2, V)>
Removes an entry by its K1 key, returning the full (K1, K2, V) triple if it
was present.
Sourcepub fn remove_entry_by_key2<Q>(&mut self, key: &Q) -> Option<(K1, K2, V)>
pub fn remove_entry_by_key2<Q>(&mut self, key: &Q) -> Option<(K1, K2, V)>
Removes an entry by its K2 key, returning the full (K1, K2, V) triple if it
was present.
Sourcepub fn remove_entry_by_keys<Q1, Q2>(
&mut self,
key1: &Q1,
key2: &Q2,
) -> Option<(K1, K2, V)>
pub fn remove_entry_by_keys<Q1, Q2>( &mut self, key1: &Q1, key2: &Q2, ) -> Option<(K1, K2, V)>
Removes an entry only if its K1 key equals key1 and its K2 key equals
key2. Returns the full (K1, K2, V) triple on success, or None if either
key is missing or they refer to different entries. On mismatch, the map is
unchanged.
Source§impl<K1, K2, V> DoubleMap<K1, K2, V>
impl<K1, K2, V> DoubleMap<K1, K2, V>
Sourcepub fn entry(
&mut self,
key1: K1,
key2: K2,
) -> Result<Entry<'_, K1, K2, V>, KeyConflictError<K1, K2>>
pub fn entry( &mut self, key1: K1, key2: K2, ) -> Result<Entry<'_, K1, K2, V>, KeyConflictError<K1, K2>>
Gets the given (key1, key2) pair’s Entry in the map for in-place
manipulation.
- Returns
Ok(Entry::Occupied)if both keys refer to the same existing entry. - Returns
Ok(Entry::Vacant)if neither key is present. - Returns
Err(KeyConflictError)if the two keys clash with existing entries. In the error case the map is unchanged and the rejected(K1, K2)pair is returned.
Sourcepub fn insert(
&mut self,
key1: K1,
key2: K2,
value: V,
) -> Result<Option<V>, KeyConflictError<K1, K2, V>>
pub fn insert( &mut self, key1: K1, key2: K2, value: V, ) -> Result<Option<V>, KeyConflictError<K1, K2, V>>
Inserts a new entry, or updates the value if both keys are already present and refer to the same entry.
- Returns
Ok(None)when neither key was present and a fresh entry was inserted. - Returns
Ok(Some(old_value))when both keys matched the same existing entry and its value was replaced. - Returns
Err(KeyConflictError)if the two keys clash with existing entries. In all error cases the map is unchanged and the rejected triple is returned.
Trait Implementations§
impl<K1, K2, V> Eq for DoubleMap<K1, K2, V>
Source§impl<'a, K1, K2, V> Extend<(&'a K1, &'a K2, &'a V)> for DoubleMap<K1, K2, V>
impl<'a, K1, K2, V> Extend<(&'a K1, &'a K2, &'a V)> for DoubleMap<K1, K2, V>
Source§fn extend<I>(&mut self, iter: I)
fn extend<I>(&mut self, iter: I)
Extends the map with borrowed (&K1, &K2, &V) triples, copying each element into
place.
Conflicting triples are silently skipped, same as the owned-triple Extend impl.
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K1, K2, V> Extend<(K1, K2, V)> for DoubleMap<K1, K2, V>
impl<K1, K2, V> Extend<(K1, K2, V)> for DoubleMap<K1, K2, V>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (K1, K2, V)>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (K1, K2, V)>,
Extends the map with (K1, K2, V) triples from any iterator.
Triples whose keys conflict with an existing entry (same K1 mapped to a
different K2, or same K2 mapped to a different K1) are silently skipped
and the map is left unchanged for that triple. A consistent repeat (same K1
and same K2) updates the value.
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K1, K2, V, const N: usize> From<[(K1, K2, V); N]> for DoubleMap<K1, K2, V>
impl<K1, K2, V, const N: usize> From<[(K1, K2, V); N]> for DoubleMap<K1, K2, V>
Source§fn from(arr: [(K1, K2, V); N]) -> Self
fn from(arr: [(K1, K2, V); N]) -> Self
Builds a DoubleMap from an array of (K1, K2, V) triples.
Conflicting triples are silently skipped, same as FromIterator.