add for_each extension api for safe keys iteration #113

Thanks @amitma1 for the changes
This commit is contained in:
greg
2021-11-02 16:50:45 -04:00
parent e9cc706a97
commit 3b5ecf0c2a
2 changed files with 39 additions and 0 deletions
+23
View File
@@ -3094,6 +3094,29 @@ public:
return std::get<2>(res);
}
// Extension API: support iterating over all values
//
// flat_hash_set<std::string> s;
// s.insert(...);
// s.for_each([](auto const & key) {
// // Safely iterates over all the keys
// });
template <class F>
void for_each(F&& fCallback) const {
for (auto const& inner : sets_) {
typename Lockable::SharedLock m(const_cast<Inner&>(inner));
std::for_each(inner.set_.begin(), inner.set_.end(), fCallback);
}
}
// this version allows to modify the values
void for_each_m(std::function<void (value_type&)> && fCallback) {
for (auto& inner : sets_) {
typename Lockable::UniqueLock m(const_cast<Inner&>(inner));
std::for_each(inner.set_.begin(), inner.set_.end(), fCallback);
}
}
// Extension API: support for heterogeneous keys.
//
// std::unordered_set<std::string> s;