cairo_lang_utils::unordered_hash_map

Struct UnorderedHashMap

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

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Source§

impl<Key: Eq + Hash, Value, BH: BuildHasher> UnorderedHashMap<Key, Value, BH>

Source

pub fn get<Q>(&self, key: &Q) -> Option<&Value>
where Key: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value corresponding to the key.

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.

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value>
where Key: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a mutable reference to the value corresponding to the key.

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.

Source

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.

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<Value>
where Key: Borrow<Q>, Q: Hash + Eq + ?Sized,

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.

Source

pub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value>

Gets the given key’s corresponding entry in the map for in-place manipulation.

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where Q: ?Sized + Hash + Eq, Key: Borrow<Q>,

Returns true if the map contains a value for the specified key.

Source

pub fn map<TargetValue>( &self, mapper: impl Fn(&Value) -> TargetValue, ) -> UnorderedHashMap<Key, TargetValue, BH>
where Key: Clone, BH: Clone,

Maps the values of the map to new values using the given function.

Source

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).

Source

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).

Source

pub fn into_iter_sorted(self) -> impl Iterator<Item = (Key, Value)>
where Key: Ord + Clone,

A consuming version of iter_sorted.

Source

pub fn iter_sorted_by_key<TargetKey, F>(&self, f: F) -> IntoIter<(&Key, &Value)>
where TargetKey: Ord, F: FnMut(&(&Key, &Value)) -> TargetKey,

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).

Source

pub fn into_iter_sorted_by_key<TargetKey, F>( self, f: F, ) -> IntoIter<(Key, Value)>
where TargetKey: Ord, F: FnMut(&(Key, Value)) -> TargetKey,

A consuming version of iter_sorted_by_key.

Source

pub fn filter<P>(self, p: P) -> Self
where BH: Default, P: FnMut(&Key, &Value) -> bool,

Creates a new map with only the elements from the original map for which the given predicate returns true. Consuming.

Source

pub fn filter_cloned<P>(&self, p: P) -> Self
where BH: Default, P: FnMut(&Key, &Value) -> bool, Key: Clone, Value: Clone,

Non consuming version of filter. Only clones the filtered entries. Requires Key and Value to implement Clone.

Source

pub fn merge<HandleDuplicate>( &mut self, other: &Self, handle_duplicate: HandleDuplicate, )
where BH: Clone, HandleDuplicate: Fn(OccupiedEntry<'_, Key, Value>, &Value), Key: Clone, Value: Clone,

Merges the map with another map. If a key is present in both maps, the given handler function is used to combine the values.

Trait Implementations§

Source§

impl<Key: Clone, Value: Clone, BH: Clone> Clone for UnorderedHashMap<Key, Value, BH>

Source§

fn clone(&self) -> UnorderedHashMap<Key, Value, BH>

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<Key: Debug, Value: Debug, BH: Debug> Debug for UnorderedHashMap<Key, Value, BH>

Source§

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

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

impl<Key, Value, BH: Default> Default for UnorderedHashMap<Key, Value, BH>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Converts to this type from the input type.
Source§

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

Creates a value from an iterator. Read more
Source§

impl<Key, Q, Value, BH: BuildHasher> Index<&Q> for UnorderedHashMap<Key, Value, BH>
where Key: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized,

Source§

type Output = Value

The returned type after indexing.
Source§

fn index(&self, key: &Q) -> &Self::Output

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

impl<Key, Value, BH> PartialEq for UnorderedHashMap<Key, Value, BH>
where Key: Eq + Hash, Value: PartialEq, BH: BuildHasher,

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Key, Value, BH> Eq for UnorderedHashMap<Key, Value, BH>
where Key: Eq + Hash, Value: Eq, BH: BuildHasher,

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>
where BH: RefUnwindSafe, Key: RefUnwindSafe, Value: RefUnwindSafe,

§

impl<Key, Value, BH> Send for UnorderedHashMap<Key, Value, BH>
where BH: Send, Key: Send, Value: Send,

§

impl<Key, Value, BH> Sync for UnorderedHashMap<Key, Value, BH>
where BH: Sync, Key: Sync, Value: Sync,

§

impl<Key, Value, BH> Unpin for UnorderedHashMap<Key, Value, BH>
where BH: Unpin, Key: Unpin, Value: Unpin,

§

impl<Key, Value, BH> UnwindSafe for UnorderedHashMap<Key, Value, BH>
where Key: UnwindSafe, Value: UnwindSafe, BH: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.
Source§

impl<T> Upcast<T> for T
where T: ?Sized,

Source§

fn upcast(&self) -> &T

Source§

impl<T> UpcastMut<T> for T
where T: ?Sized,

Source§

fn upcast_mut(&mut self) -> &mut T