1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::collections::hash_map::Entry::*;

use rustc_hash::FxHashMap;

use crate::util::clear::Clear;

pub struct HalfJoinStateFold<K, A> {
    pub table: FxHashMap<K, A>,
}

impl<K, A> Default for HalfJoinStateFold<K, A> {
    fn default() -> Self {
        Self {
            table: Default::default(),
        }
    }
}

impl<K, A> Clear for HalfJoinStateFold<K, A> {
    fn clear(&mut self) {
        self.table.clear()
    }
}

impl<K, A> HalfJoinStateFold<K, A>
where
    K: Eq + std::hash::Hash,
{
    pub fn fold_into<V, X>(
        &mut self,
        iter: impl Iterator<Item = (K, V)>,
        mut fold: impl FnMut(&mut A, V) -> X,
        mut default: impl FnMut() -> A,
    ) {
        for (k, v) in iter {
            let entry = self.table.entry(k);

            match entry {
                Occupied(mut e) => {
                    (fold)(e.get_mut(), v);
                }
                Vacant(e) => {
                    (fold)(e.insert((default)()), v);
                }
            }
        }
    }
}