dfir_rs/compiled/pull/half_join_state/
reduce.rs

1use std::collections::hash_map::Entry::*;
2
3use rustc_hash::FxHashMap;
4
5use crate::util::clear::Clear;
6
7pub struct HalfJoinStateReduce<K, A> {
8    pub table: FxHashMap<K, A>,
9}
10
11impl<K, A> Default for HalfJoinStateReduce<K, A> {
12    fn default() -> Self {
13        Self {
14            table: Default::default(),
15        }
16    }
17}
18
19impl<K, A> Clear for HalfJoinStateReduce<K, A> {
20    fn clear(&mut self) {
21        self.table.clear()
22    }
23}
24
25impl<K, A> HalfJoinStateReduce<K, A>
26where
27    K: Eq + std::hash::Hash,
28{
29    pub fn reduce_into<X>(
30        &mut self,
31        iter: impl Iterator<Item = (K, A)>,
32        mut reduce: impl FnMut(&mut A, A) -> X,
33    ) {
34        for (k, v) in iter {
35            let entry = self.table.entry(k);
36
37            match entry {
38                Occupied(mut e) => {
39                    (reduce)(e.get_mut(), v);
40                }
41                Vacant(e) => {
42                    e.insert(v);
43                }
44            }
45        }
46    }
47}