vortex_expr/pruning/
relation.rs

1use std::hash::Hash;
2
3use vortex_utils::aliases::hash_map::{HashMap, IntoIter};
4use vortex_utils::aliases::hash_set::HashSet;
5
6#[derive(Debug, Clone)]
7pub struct Relation<K, V> {
8    map: HashMap<K, HashSet<V>>,
9}
10
11impl<K: Hash + Eq, V: Hash + Eq> Default for Relation<K, V> {
12    fn default() -> Self {
13        Self::new()
14    }
15}
16
17impl<K: Hash + Eq, V: Hash + Eq> Relation<K, V> {
18    pub fn new() -> Self {
19        Relation {
20            map: HashMap::new(),
21        }
22    }
23
24    pub fn insert(&mut self, k: K, v: V) {
25        self.map.entry(k).or_default().insert(v);
26    }
27
28    pub fn map(&self) -> &HashMap<K, HashSet<V>> {
29        &self.map
30    }
31}
32
33impl<K, V> From<HashMap<K, HashSet<V>>> for Relation<K, V> {
34    fn from(value: HashMap<K, HashSet<V>>) -> Self {
35        Self { map: value }
36    }
37}
38
39impl<K, V> IntoIterator for Relation<K, V> {
40    type Item = (K, HashSet<V>);
41    type IntoIter = IntoIter<K, HashSet<V>>;
42
43    fn into_iter(self) -> Self::IntoIter {
44        self.map.into_iter()
45    }
46}