cairo_lang_utils/
unordered_hash_map.rs

1#[cfg(test)]
2#[path = "unordered_hash_map_test.rs"]
3mod test;
4
5#[cfg(not(feature = "std"))]
6use alloc::vec;
7use core::borrow::Borrow;
8use core::hash::{BuildHasher, Hash};
9use core::ops::Index;
10#[cfg(feature = "std")]
11use std::collections::HashMap;
12#[cfg(feature = "std")]
13pub use std::collections::hash_map::Entry;
14#[cfg(feature = "std")]
15use std::collections::hash_map::OccupiedEntry;
16#[cfg(feature = "std")]
17use std::collections::hash_map::RandomState;
18#[cfg(feature = "std")]
19use std::vec;
20
21#[cfg(not(feature = "std"))]
22use hashbrown::HashMap;
23#[cfg(not(feature = "std"))]
24pub use hashbrown::hash_map::Entry;
25use itertools::Itertools;
26
27#[cfg(feature = "std")]
28type BHImpl = RandomState;
29#[cfg(not(feature = "std"))]
30type BHImpl = hashbrown::DefaultHashBuilder;
31
32/// A hash map that does not care about the order of insertion.
33///
34/// In particular, it does not support iterating, in order to guarantee deterministic compilation.
35/// It does support aggregation which can be used in intermediate computations (see `aggregate_by`).
36/// For an iterable version see [OrderedHashMap](crate::ordered_hash_map::OrderedHashMap).
37#[derive(Clone, Debug)]
38pub struct UnorderedHashMap<Key, Value, BH = BHImpl>(HashMap<Key, Value, BH>);
39
40#[cfg(feature = "salsa")]
41unsafe impl<Key: salsa::Update + Eq + Hash, Value: salsa::Update> salsa::Update
42    for UnorderedHashMap<Key, Value, BHImpl>
43{
44    // This code was taken from the salsa::Update trait implementation for IndexMap.
45    // It is defined privately in macro_rules! maybe_update_map in the db-ext-macro repo.
46    unsafe fn maybe_update(old_pointer: *mut Self, new_map: Self) -> bool {
47        let new_map = new_map;
48        let old_map: &mut Self = unsafe { &mut *old_pointer };
49
50        // To be considered "equal", the set of keys
51        // must be the same between the two maps.
52        let same_keys =
53            old_map.len() == new_map.len() && old_map.0.keys().all(|k| new_map.0.contains_key(k));
54
55        // If the set of keys has changed, then just pull in the new values
56        // from new_map and discard the old ones.
57        if !same_keys {
58            old_map.0.clear();
59            old_map.0.extend(new_map.0);
60            return true;
61        }
62
63        // Otherwise, recursively descend to the values.
64        // We do not invoke `K::update` because we assume
65        // that if the values are `Eq` they must not need
66        // updating (see the trait criteria).
67        let mut changed = false;
68        for (key, new_value) in new_map.0.into_iter() {
69            let old_value = old_map.0.get_mut(&key).unwrap();
70            changed |= unsafe { Value::maybe_update(old_value, new_value) };
71        }
72        changed
73    }
74}
75
76impl<Key, Value, BH> UnorderedHashMap<Key, Value, BH> {
77    fn with_hasher(hash_builder: BH) -> Self {
78        Self(HashMap::<Key, Value, BH>::with_hasher(hash_builder))
79    }
80}
81
82impl<Key, Value, BH> PartialEq for UnorderedHashMap<Key, Value, BH>
83where
84    Key: Eq + Hash,
85    Value: PartialEq,
86    BH: BuildHasher,
87{
88    fn eq(&self, other: &Self) -> bool {
89        self.0 == other.0
90    }
91}
92
93impl<Key, Value, BH> Eq for UnorderedHashMap<Key, Value, BH>
94where
95    Key: Eq + Hash,
96    Value: Eq,
97    BH: BuildHasher,
98{
99}
100
101impl<Key, Value, BH> UnorderedHashMap<Key, Value, BH> {
102    /// Returns the number of elements in the map.
103    pub fn len(&self) -> usize {
104        self.0.len()
105    }
106
107    /// Returns true if the map contains no elements.
108    pub fn is_empty(&self) -> bool {
109        self.0.is_empty()
110    }
111}
112
113impl<Key: Eq + Hash, Value, BH: BuildHasher> UnorderedHashMap<Key, Value, BH> {
114    /// Returns a reference to the value corresponding to the key.
115    ///
116    /// The key may be any borrowed form of the map's key type, but [`Hash`] and [`Eq`] on the
117    /// borrowed form *must* match those for the key type.
118    pub fn get<Q>(&self, key: &Q) -> Option<&Value>
119    where
120        Key: Borrow<Q>,
121        Q: Hash + Eq + ?Sized,
122    {
123        self.0.get(key)
124    }
125
126    /// Returns a mutable reference to the value corresponding to the key.
127    ///
128    /// The key may be any borrowed form of the map's key type, but [`Hash`] and [`Eq`] on the
129    /// borrowed form *must* match those for the key type.
130    pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value>
131    where
132        Key: Borrow<Q>,
133        Q: Hash + Eq + ?Sized,
134    {
135        self.0.get_mut(key)
136    }
137
138    /// Inserts a key-value pair into the map.
139    ///
140    /// If the map did not have this key present, None is returned.
141    ///
142    /// If the map did have this key present, the value is updated, and the old value is returned.
143    /// The key is not updated, though; this matters for types that can be == without being
144    /// identical.
145    pub fn insert(&mut self, key: Key, value: Value) -> Option<Value> {
146        self.0.insert(key, value)
147    }
148
149    /// Removes a key from the map, returning the value at the key if the key was previously in the
150    /// map.
151    ///
152    /// The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form
153    /// must match those for the key type.
154    pub fn remove<Q>(&mut self, key: &Q) -> Option<Value>
155    where
156        Key: Borrow<Q>,
157        Q: Hash + Eq + ?Sized,
158    {
159        self.0.remove(key)
160    }
161
162    #[cfg(feature = "std")]
163    /// Gets the given key's corresponding entry in the map for in-place manipulation.
164    pub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value> {
165        self.0.entry(key)
166    }
167
168    #[cfg(not(feature = "std"))]
169    /// Gets the given key's corresponding entry in the map for in-place manipulation.
170    pub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value, BH> {
171        self.0.entry(key)
172    }
173
174    /// Returns true if the map contains a value for the specified key.
175    pub fn contains_key<Q>(&self, key: &Q) -> bool
176    where
177        Q: ?Sized,
178        Key: Borrow<Q>,
179        Q: Hash + Eq,
180    {
181        self.0.contains_key(key)
182    }
183
184    /// Maps the values of the map to new values using the given function.
185    pub fn map<TargetValue>(
186        &self,
187        mapper: impl Fn(&Value) -> TargetValue,
188    ) -> UnorderedHashMap<Key, TargetValue, BH>
189    where
190        Key: Clone,
191        BH: Clone,
192    {
193        self.0.iter().fold(
194            UnorderedHashMap::<_, _, _>::with_hasher(self.0.hasher().clone()),
195            |mut acc, (key, value)| {
196                match acc.entry(key.clone()) {
197                    Entry::Occupied(_) => {
198                        unreachable!("The original map should not contain duplicate keys.");
199                    }
200                    Entry::Vacant(vacant) => {
201                        vacant.insert(mapper(value));
202                    }
203                };
204                acc
205            },
206        )
207    }
208
209    /// Aggregates values of the map using the given functions.
210    /// `mapping_function` maps each key to a new key, possibly mapping multiple original keys to
211    /// the same target key.
212    /// `reduce_function` dictates how to aggregate any two values of the same target key.
213    /// `default_value` is the initial value for each target key.
214    /// Note! as the map is unordered, `reduce_function` should be commutative. Otherwise, the
215    /// result is undefined (nondeterministic).
216    pub fn aggregate_by<TargetKey: Eq + Hash, TargetValue>(
217        &self,
218        mapping_function: impl Fn(&Key) -> TargetKey,
219        reduce_function: impl Fn(&TargetValue, &Value) -> TargetValue,
220        default_value: &TargetValue,
221    ) -> UnorderedHashMap<TargetKey, TargetValue, BH>
222    where
223        BH: Clone,
224    {
225        self.0.iter().fold(
226            UnorderedHashMap::<_, _, _>::with_hasher(self.0.hasher().clone()),
227            |mut acc, (key, value)| {
228                let target_key = mapping_function(key);
229                match acc.entry(target_key) {
230                    Entry::Occupied(occupied) => {
231                        let old_target_value = occupied.into_mut();
232                        let new_target_value = reduce_function(old_target_value, value);
233                        *old_target_value = new_target_value;
234                    }
235                    Entry::Vacant(vacant) => {
236                        let new_value = reduce_function(default_value, value);
237                        vacant.insert(new_value);
238                    }
239                };
240                acc
241            },
242        )
243    }
244
245    /// Iterates the map in an ascending order applied by the `Ord` implementation of `Key`.
246    /// NOTE! To guarantee a deterministic output, the `Ord` implementation must apply a strict
247    /// ordering. That is, `a <= b` and `b <= a`, then `a == b`. If `Ord` is derived (in all
248    /// hierarchy levels), this is probably the case. If the ordering is not strict, the order of
249    /// the output OrderedHashMap is undefined (nondeterministic).
250    /// This can be used to convert an unordered map to an ordered map (mostly when the unordered
251    /// map was used for intermediate processing).
252    pub fn iter_sorted(&self) -> impl Iterator<Item = (&Key, &Value)>
253    where
254        Key: Ord,
255    {
256        self.0.iter().sorted_by_key(|(key, _)| *key)
257    }
258
259    /// A consuming version of `iter_sorted`.
260    pub fn into_iter_sorted(self) -> impl Iterator<Item = (Key, Value)>
261    where
262        Key: Ord + Clone,
263    {
264        self.0.into_iter().sorted_by_key(|(key, _)| (*key).clone())
265    }
266
267    /// Iterates the map in an ascending order of the keys produced by the given function `f`.
268    /// NOTE! To guarantee a deterministic output, `f`'s implementation must apply a strict
269    /// ordering of the (Key, Value) pairs. That is, for any given pair of entries `a=(k_a, v_a)`
270    /// and `b=(k_b, v_b)`, if `a <= b` and `b <= a`, then `a == b`. If the ordering is not strict,
271    /// the order of the output OrderedHashMap is undefined (nondeterministic).
272    /// This can be used to convert an unordered map to an ordered map (mostly when the unordered
273    /// map was used for intermediate processing).
274    pub fn iter_sorted_by_key<TargetKey, F>(&self, f: F) -> vec::IntoIter<(&Key, &Value)>
275    where
276        TargetKey: Ord,
277        F: FnMut(&(&Key, &Value)) -> TargetKey,
278    {
279        self.0.iter().sorted_by_key(f)
280    }
281
282    /// A consuming version of `iter_sorted_by_key`.
283    pub fn into_iter_sorted_by_key<TargetKey, F>(self, f: F) -> vec::IntoIter<(Key, Value)>
284    where
285        TargetKey: Ord,
286        F: FnMut(&(Key, Value)) -> TargetKey,
287    {
288        self.0.into_iter().sorted_by_key(f)
289    }
290
291    /// Creates a new map with only the elements from the original map for which the given predicate
292    /// returns `true`. Consuming.
293    pub fn filter<P>(self, mut p: P) -> Self
294    where
295        BH: Default,
296        P: FnMut(&Key, &Value) -> bool,
297    {
298        Self(self.0.into_iter().filter(|(key, value)| p(key, value)).collect())
299    }
300
301    /// Non consuming version of `filter`. Only clones the filtered entries. Requires `Key` and
302    /// `Value` to implement `Clone`.
303    pub fn filter_cloned<P>(&self, mut p: P) -> Self
304    where
305        BH: Default,
306        P: FnMut(&Key, &Value) -> bool,
307        Key: Clone,
308        Value: Clone,
309    {
310        Self(
311            self.0
312                .iter()
313                .filter_map(
314                    |(key, value)| {
315                        if p(key, value) { Some((key.clone(), value.clone())) } else { None }
316                    },
317                )
318                .collect(),
319        )
320    }
321
322    #[cfg(feature = "std")]
323    /// Merges the map with another map. If a key is present in both maps, the given handler
324    /// function is used to combine the values.
325    pub fn merge<HandleDuplicate>(&mut self, other: &Self, handle_duplicate: HandleDuplicate)
326    where
327        BH: Clone,
328        HandleDuplicate: Fn(OccupiedEntry<'_, Key, Value>, &Value),
329        Key: Clone,
330        Value: Clone,
331    {
332        for (key, value) in &other.0 {
333            match self.0.entry(key.clone()) {
334                Entry::Occupied(e) => {
335                    handle_duplicate(e, value);
336                }
337                Entry::Vacant(e) => {
338                    e.insert(value.clone());
339                }
340            }
341        }
342    }
343
344    /// Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
345    pub fn clear(&mut self) {
346        self.0.clear();
347    }
348}
349
350impl<Key, Q: ?Sized, Value, BH: BuildHasher> Index<&Q> for UnorderedHashMap<Key, Value, BH>
351where
352    Key: Eq + Hash + Borrow<Q>,
353    Q: Eq + Hash,
354{
355    type Output = Value;
356
357    fn index(&self, key: &Q) -> &Self::Output {
358        self.0.index(key)
359    }
360}
361
362impl<Key, Value, BH: Default> Default for UnorderedHashMap<Key, Value, BH> {
363    #[cfg(feature = "std")]
364    fn default() -> Self {
365        Self(Default::default())
366    }
367    #[cfg(not(feature = "std"))]
368    fn default() -> Self {
369        Self(HashMap::with_hasher(Default::default()))
370    }
371}
372
373impl<Key: Hash + Eq, Value, BH: BuildHasher + Default> FromIterator<(Key, Value)>
374    for UnorderedHashMap<Key, Value, BH>
375{
376    fn from_iter<T: IntoIterator<Item = (Key, Value)>>(iter: T) -> Self {
377        Self(iter.into_iter().collect())
378    }
379}
380
381impl<Key: Hash + Eq, Value, const N: usize, BH: BuildHasher + Default> From<[(Key, Value); N]>
382    for UnorderedHashMap<Key, Value, BH>
383{
384    fn from(items: [(Key, Value); N]) -> Self {
385        Self(HashMap::from_iter(items))
386    }
387}
388
389impl<Key: Hash + Eq, Value, BH: BuildHasher> Extend<(Key, Value)>
390    for UnorderedHashMap<Key, Value, BH>
391{
392    fn extend<T: IntoIterator<Item = (Key, Value)>>(&mut self, iter: T) {
393        self.0.extend(iter)
394    }
395}