diff --git a/query-algebrizer/src/clauses/mod.rs b/query-algebrizer/src/clauses/mod.rs index c8407583..e606a24e 100644 --- a/query-algebrizer/src/clauses/mod.rs +++ b/query-algebrizer/src/clauses/mod.rs @@ -89,6 +89,7 @@ trait Contains { trait Intersection { fn with_intersected_keys(&self, ks: &BTreeSet) -> Self; + fn keep_intersected_keys(&mut self, ks: &BTreeSet); } impl Contains for BTreeSet { @@ -108,6 +109,22 @@ impl Intersection for BTreeMap { .filter_map(|(k, v)| ks.when_contains(k, || (k.clone(), v.clone()))) .collect() } + + /// Remove all keys from the map that are not present in `ks`. + /// This implementation is terrible because there's no mutable iterator for BTreeMap. + fn keep_intersected_keys(&mut self, ks: &BTreeSet) { + let mut to_remove = Vec::with_capacity(self.len() - ks.len()); + { + for k in self.keys() { + if !ks.contains(k) { + to_remove.push(k.clone()) + } + } + } + for k in to_remove.into_iter() { + self.remove(&k); + } + } } /// A `ConjoiningClauses` (CC) is a collection of clauses that are combined with `JOIN`.