Added function to recursively take elements in a hashmap only if the key
is present in `left` and `right`.
Gabouchet (1):
feat(hashmap): Intersection
src/lib.rs | 15 +++++++++++++++
1 file changed, 15 insertions(+)
--
2.34.5
From: Gabouchet <gabriel.hamel.pro@gmail.com>
---
src/lib.rs | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/src/lib.rs b/src/lib.rs
index ab9b4ab..e289d84 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -298,4 +298,19 @@ pub mod hashmap {
}
}
}
+
+ /// Merge recursively elements if the key is present in `left` and `right`.
+ pub fn intersection<K: Eq + Hash, V: crate::Merge>(
+ left: &mut HashMap<K, V>,
+ right: HashMap<K, V>,
+ ) {
+ use std::collections::hash_map::Entry;
+
+ for (k, v) in right {
+ match left.entry(k) {
+ Entry::Occupied(mut existing) => existing.get_mut().merge(v),
+ _ => {}
+ }
+ }
+ }
}
--
2.34.5