pub struct UnorderedHashMap<Key, Value, BH = RandomState>(/* private fields */);
Expand description
A hash map that does not care about the order of insertion.
In particular, it does not support iterating, in order to guarantee deterministic compilation.
It does support aggregation which can be used in intermediate computations (see aggregate_by
).
For an iterable version see OrderedHashMap.
Implementations§
Source§impl<Key, Value, BH> UnorderedHashMap<Key, Value, BH>
impl<Key, Value, BH> UnorderedHashMap<Key, Value, BH>
Source§impl<Key: Eq + Hash, Value, BH: BuildHasher> UnorderedHashMap<Key, Value, BH>
impl<Key: Eq + Hash, Value, BH: BuildHasher> UnorderedHashMap<Key, Value, BH>
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value>
Sourcepub fn insert(&mut self, key: Key, value: Value) -> Option<Value>
pub fn insert(&mut self, key: Key, value: Value) -> Option<Value>
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. The key is not updated, though; this matters for types that can be == without being identical.
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<Value>
pub fn remove<Q>(&mut self, key: &Q) -> Option<Value>
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.
Sourcepub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value>
pub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value>
Gets the given key’s corresponding entry in the map for in-place manipulation.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if the map contains a value for the specified key.
Sourcepub fn map<TargetValue>(
&self,
mapper: impl Fn(&Value) -> TargetValue,
) -> UnorderedHashMap<Key, TargetValue, BH>
pub fn map<TargetValue>( &self, mapper: impl Fn(&Value) -> TargetValue, ) -> UnorderedHashMap<Key, TargetValue, BH>
Maps the values of the map to new values using the given function.
Sourcepub fn aggregate_by<TargetKey: Eq + Hash, TargetValue>(
&self,
mapping_function: impl Fn(&Key) -> TargetKey,
reduce_function: impl Fn(&TargetValue, &Value) -> TargetValue,
default_value: &TargetValue,
) -> UnorderedHashMap<TargetKey, TargetValue, BH>where
BH: Clone,
pub fn aggregate_by<TargetKey: Eq + Hash, TargetValue>(
&self,
mapping_function: impl Fn(&Key) -> TargetKey,
reduce_function: impl Fn(&TargetValue, &Value) -> TargetValue,
default_value: &TargetValue,
) -> UnorderedHashMap<TargetKey, TargetValue, BH>where
BH: Clone,
Aggregates values of the map using the given functions.
mapping_function
maps each key to a new key, possibly mapping multiple original keys to
the same target key.
reduce_function
dictates how to aggregate any two values of the same target key.
default_value
is the initial value for each target key.
Note! as the map is unordered, reduce_function
should be commutative. Otherwise, the
result is undefined (nondeterministic).
Sourcepub fn iter_sorted(&self) -> impl Iterator<Item = (&Key, &Value)>where
Key: Ord,
pub fn iter_sorted(&self) -> impl Iterator<Item = (&Key, &Value)>where
Key: Ord,
Iterates the map in an ascending order applied by the Ord
implementation of Key
.
NOTE! To guarantee a deterministic output, the Ord
implementation must apply a strict
ordering. That is, a <= b
and b <= a
, then a == b
. If Ord
is derived (in all
hierarchy levels), this is probably the case. If the ordering is not strict, the order of
the output OrderedHashMap is undefined (nondeterministic).
This can be used to convert an unordered map to an ordered map (mostly when the unordered
map was used for intermediate processing).
Sourcepub fn into_iter_sorted(self) -> impl Iterator<Item = (Key, Value)>
pub fn into_iter_sorted(self) -> impl Iterator<Item = (Key, Value)>
A consuming version of iter_sorted
.
Sourcepub fn iter_sorted_by_key<TargetKey, F>(&self, f: F) -> IntoIter<(&Key, &Value)>
pub fn iter_sorted_by_key<TargetKey, F>(&self, f: F) -> IntoIter<(&Key, &Value)>
Iterates the map in an ascending order of the keys produced by the given function f
.
NOTE! To guarantee a deterministic output, f
’s implementation must apply a strict
ordering of the (Key, Value) pairs. That is, for any given pair of entries a=(k_a, v_a)
and b=(k_b, v_b)
, if a <= b
and b <= a
, then a == b
. If the ordering is not strict,
the order of the output OrderedHashMap is undefined (nondeterministic).
This can be used to convert an unordered map to an ordered map (mostly when the unordered
map was used for intermediate processing).
Sourcepub fn into_iter_sorted_by_key<TargetKey, F>(
self,
f: F,
) -> IntoIter<(Key, Value)>
pub fn into_iter_sorted_by_key<TargetKey, F>( self, f: F, ) -> IntoIter<(Key, Value)>
A consuming version of iter_sorted_by_key
.
Sourcepub fn filter<P>(self, p: P) -> Self
pub fn filter<P>(self, p: P) -> Self
Creates a new map with only the elements from the original map for which the given predicate
returns true
. Consuming.
Sourcepub fn filter_cloned<P>(&self, p: P) -> Self
pub fn filter_cloned<P>(&self, p: P) -> Self
Non consuming version of filter
. Only clones the filtered entries. Requires Key
and
Value
to implement Clone
.
Trait Implementations§
Source§impl<Key: Clone, Value: Clone, BH: Clone> Clone for UnorderedHashMap<Key, Value, BH>
impl<Key: Clone, Value: Clone, BH: Clone> Clone for UnorderedHashMap<Key, Value, BH>
Source§fn clone(&self) -> UnorderedHashMap<Key, Value, BH>
fn clone(&self) -> UnorderedHashMap<Key, Value, BH>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<Key, Value, BH: Default> Default for UnorderedHashMap<Key, Value, BH>
impl<Key, Value, BH: Default> Default for UnorderedHashMap<Key, Value, BH>
Source§impl<Key: Hash + Eq, Value, const N: usize, BH: BuildHasher + Default> From<[(Key, Value); N]> for UnorderedHashMap<Key, Value, BH>
impl<Key: Hash + Eq, Value, const N: usize, BH: BuildHasher + Default> From<[(Key, Value); N]> for UnorderedHashMap<Key, Value, BH>
Source§fn from(items: [(Key, Value); N]) -> Self
fn from(items: [(Key, Value); N]) -> Self
Source§impl<Key: Hash + Eq, Value, BH: BuildHasher + Default> FromIterator<(Key, Value)> for UnorderedHashMap<Key, Value, BH>
impl<Key: Hash + Eq, Value, BH: BuildHasher + Default> FromIterator<(Key, Value)> for UnorderedHashMap<Key, Value, BH>
Source§fn from_iter<T: IntoIterator<Item = (Key, Value)>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = (Key, Value)>>(iter: T) -> Self
Source§impl<Key, Q, Value, BH: BuildHasher> Index<&Q> for UnorderedHashMap<Key, Value, BH>
impl<Key, Q, Value, BH: BuildHasher> Index<&Q> for UnorderedHashMap<Key, Value, BH>
Source§impl<Key, Value, BH> PartialEq for UnorderedHashMap<Key, Value, BH>
impl<Key, Value, BH> PartialEq for UnorderedHashMap<Key, Value, BH>
impl<Key, Value, BH> Eq for UnorderedHashMap<Key, Value, BH>
Auto Trait Implementations§
impl<Key, Value, BH> Freeze for UnorderedHashMap<Key, Value, BH>where
BH: Freeze,
impl<Key, Value, BH> RefUnwindSafe for UnorderedHashMap<Key, Value, BH>
impl<Key, Value, BH> Send for UnorderedHashMap<Key, Value, BH>
impl<Key, Value, BH> Sync for UnorderedHashMap<Key, Value, BH>
impl<Key, Value, BH> Unpin for UnorderedHashMap<Key, Value, BH>
impl<Key, Value, BH> UnwindSafe for UnorderedHashMap<Key, Value, BH>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more