1
0
Fork 0

Add misc/set_intersection.cc

Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
subcrip 2024-04-12 09:27:25 +08:00
parent ed523135db
commit bd0cfba0fb
1 changed files with 15 additions and 0 deletions

15
misc/set_intersection.cc Normal file
View File

@ -0,0 +1,15 @@
// Set intersection supporting std::unordered_set.
template <typename Set, typename Key = typename Set::value_type>
static inline Set set_intersection(const Set& lhs, const Set& rhs) {
if (lhs.size() <= rhs.size()) {
Set iset;
for (const Key& key : lhs) {
if (rhs.count(key) > 0) {
iset.insert(key);
}
}
return std::move(iset);
} else {
return set_intersection(rhs, lhs);
}
}