diff options
Diffstat (limited to 'tdutils')
| -rw-r--r-- | tdutils/td/utils/WaitFreeHashSet.h | 15 | ||||
| -rw-r--r-- | tdutils/td/utils/algorithm.h | 8 | ||||
| -rw-r--r-- | tdutils/test/WaitFreeHashSet.cpp | 19 |
3 files changed, 42 insertions, 0 deletions
diff --git a/tdutils/td/utils/WaitFreeHashSet.h b/tdutils/td/utils/WaitFreeHashSet.h index 68d1baabc..f5a51b095 100644 --- a/tdutils/td/utils/WaitFreeHashSet.h +++ b/tdutils/td/utils/WaitFreeHashSet.h @@ -97,6 +97,21 @@ class WaitFreeHashSet { } } + template <class F> + bool remove_if(const F &f) { + if (wait_free_storage_ == nullptr) { + return default_set_.remove_if(f); + } + + bool is_removed = false; + for (auto &it : wait_free_storage_->sets_) { + if (it.remove_if(f)) { + is_removed = true; + } + } + return is_removed; + } + KeyT get_random() const { if (wait_free_storage_ != nullptr) { for (size_t i = 0; i < MAX_STORAGE_COUNT; i++) { diff --git a/tdutils/td/utils/algorithm.h b/tdutils/td/utils/algorithm.h index 6107b1d29..d225a46a9 100644 --- a/tdutils/td/utils/algorithm.h +++ b/tdutils/td/utils/algorithm.h @@ -309,4 +309,12 @@ bool table_remove_if(WaitFreeHashMap<KeyT, ValueT, HashT, EqT> &table, const Fun return table.remove_if(func); } +template <class KeyT, class HashT, class EqT> +class WaitFreeHashSet; + +template <class KeyT, class HashT, class EqT, class FuncT> +bool table_remove_if(WaitFreeHashSet<KeyT, HashT, EqT> &table, const FuncT &func) { + return table.remove_if(func); +} + } // namespace td diff --git a/tdutils/test/WaitFreeHashSet.cpp b/tdutils/test/WaitFreeHashSet.cpp index d14d4c49f..4c6c03698 100644 --- a/tdutils/test/WaitFreeHashSet.cpp +++ b/tdutils/test/WaitFreeHashSet.cpp @@ -71,3 +71,22 @@ TEST(WaitFreeHashSet, stress_test) { } } } + +TEST(WaitFreeHashSet, remove_if) { + td::WaitFreeHashSet<td::uint64> set; + bool is_removed; + for (td::uint64 i = 0; i < 10000; i++) { + set.insert(2 * i + 1); + set.insert(2 * i + 2); + is_removed = set.remove_if([i](const auto &num) { return num <= i; }); + CHECK(!is_removed); + CHECK(set.calc_size() == i + 2); + is_removed = set.remove_if([i](const auto &num) { return num <= i + 1; }); + CHECK(is_removed); + CHECK(set.calc_size() == i + 1); + } + is_removed = set.remove_if([](const auto &num) { return num <= 19999; }); + CHECK(set.calc_size() == 1); + is_removed = set.remove_if([](const auto &num) { return num <= 20000; }); + CHECK(set.calc_size() == 0); +} |
