From bd0cfba0fb4fad6c76faff0de4142f384409b019 Mon Sep 17 00:00:00 2001 From: subcrip Date: Fri, 12 Apr 2024 09:27:25 +0800 Subject: [PATCH] Add misc/set_intersection.cc Signed-off-by: subcrip --- misc/set_intersection.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 misc/set_intersection.cc diff --git a/misc/set_intersection.cc b/misc/set_intersection.cc new file mode 100644 index 0000000..cdeacb0 --- /dev/null +++ b/misc/set_intersection.cc @@ -0,0 +1,15 @@ +// Set intersection supporting std::unordered_set. +template +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); + } +} \ No newline at end of file