Part 3: define keep_intersected_keys.

We'll use this to drop unneeded values from input maps, if lazy callers
reuse a general-purpose map for multiple queries.
This commit is contained in:
Richard Newman 2017-04-17 21:10:32 -07:00
parent 651308f721
commit dfc846e483

View file

@ -89,6 +89,7 @@ trait Contains<K, T> {
trait Intersection<K> {
fn with_intersected_keys(&self, ks: &BTreeSet<K>) -> Self;
fn keep_intersected_keys(&mut self, ks: &BTreeSet<K>);
}
impl<K: Ord, T> Contains<K, T> for BTreeSet<K> {
@ -108,6 +109,22 @@ impl<K: Clone + Ord, V: Clone> Intersection<K> for BTreeMap<K, V> {
.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<K>) {
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`.