aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdutils
diff options
context:
space:
mode:
authorlevlam <levlam@telegram.org>2025-06-23 01:54:42 +0300
committerlevlam <levlam@telegram.org>2025-06-23 01:54:42 +0300
commit6614c148781db9cdc492c7c428ce685fa3f49abe (patch)
treed2ffa658d7ff3751707ea39c53bc367dc057eefe /tdutils
parent3c8bbc53c34bd0ef8ffdc406c9c8d8b71db73465 (diff)
Add WaitFreeHashSet::remove_if.
Diffstat (limited to 'tdutils')
-rw-r--r--tdutils/td/utils/WaitFreeHashSet.h15
-rw-r--r--tdutils/td/utils/algorithm.h8
-rw-r--r--tdutils/test/WaitFreeHashSet.cpp19
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);
+}