vortex_array/expr/pruning/
relation.rs

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