From 3c8bbc53c34bd0ef8ffdc406c9c8d8b71db73465 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 23 Jun 2025 01:37:48 +0300 Subject: Add WaitFreeHashMap::remove_if. --- tdutils/td/utils/WaitFreeHashMap.h | 15 +++++++++++++++ tdutils/td/utils/algorithm.h | 8 ++++++++ tdutils/test/WaitFreeHashMap.cpp | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+) (limited to 'tdutils') diff --git a/tdutils/td/utils/WaitFreeHashMap.h b/tdutils/td/utils/WaitFreeHashMap.h index 592ac96d9..956a1a865 100644 --- a/tdutils/td/utils/WaitFreeHashMap.h +++ b/tdutils/td/utils/WaitFreeHashMap.h @@ -161,6 +161,21 @@ class WaitFreeHashMap { } } + template + bool remove_if(const F &f) { + if (wait_free_storage_ == nullptr) { + return default_map_.remove_if(f); + } + + bool is_removed = false; + for (auto &it : wait_free_storage_->maps_) { + if (it.remove_if(f)) { + is_removed = true; + } + } + return is_removed; + } + size_t calc_size() const { if (wait_free_storage_ == nullptr) { return default_map_.size(); diff --git a/tdutils/td/utils/algorithm.h b/tdutils/td/utils/algorithm.h index a87de8ecb..6107b1d29 100644 --- a/tdutils/td/utils/algorithm.h +++ b/tdutils/td/utils/algorithm.h @@ -301,4 +301,12 @@ bool table_remove_if(FlatHashTable &table, FuncT &&func) { return table.remove_if(func); } +template +class WaitFreeHashMap; + +template +bool table_remove_if(WaitFreeHashMap &table, const FuncT &func) { + return table.remove_if(func); +} + } // namespace td diff --git a/tdutils/test/WaitFreeHashMap.cpp b/tdutils/test/WaitFreeHashMap.cpp index ee7e57df6..c73cfda47 100644 --- a/tdutils/test/WaitFreeHashMap.cpp +++ b/tdutils/test/WaitFreeHashMap.cpp @@ -93,3 +93,22 @@ TEST(WaitFreeHashMap, stress_test) { } } } + +TEST(WaitFreeHashMap, remove_if) { + td::WaitFreeHashMap map; + bool is_removed; + for (td::uint64 i = 0; i < 10000; i++) { + map[2 * i + 1] = 0; + map[2 * i + 2] = 1; + is_removed = map.remove_if([i](const auto &num) { return num.first <= i; }); + CHECK(!is_removed); + CHECK(map.calc_size() == i + 2); + is_removed = map.remove_if([i](const auto &num) { return num.first <= i + 1; }); + CHECK(is_removed); + CHECK(map.calc_size() == i + 1); + } + is_removed = map.remove_if([](const auto &num) { return num.first <= 19999; }); + CHECK(map.calc_size() == 1); + is_removed = map.remove_if([](const auto &num) { return num.first <= 20000; }); + CHECK(map.calc_size() == 0); +} -- cgit v1.2.3