diff options
| author | levlam <levlam@telegram.org> | 2024-08-28 10:44:02 +0300 |
|---|---|---|
| committer | levlam <levlam@telegram.org> | 2024-08-28 10:44:02 +0300 |
| commit | d9a2d07e55e64b06a108e4085a29b50c8e7fa990 (patch) | |
| tree | 20044c92ab0e8091d7693efe06ecca0142720849 /tdutils | |
| parent | 11b068547865cf707d727105661b4880b3a88478 (diff) | |
Return whether something was removed by table_remove_if.
Diffstat (limited to 'tdutils')
| -rw-r--r-- | tdutils/td/utils/FlatHashMapChunks.h | 9 | ||||
| -rw-r--r-- | tdutils/td/utils/FlatHashTable.h | 8 | ||||
| -rw-r--r-- | tdutils/td/utils/algorithm.h | 9 |
3 files changed, 18 insertions, 8 deletions
diff --git a/tdutils/td/utils/FlatHashMapChunks.h b/tdutils/td/utils/FlatHashMapChunks.h index c5fcf4a32..fcf9d89d6 100644 --- a/tdutils/td/utils/FlatHashMapChunks.h +++ b/tdutils/td/utils/FlatHashMapChunks.h @@ -399,13 +399,16 @@ class FlatHashTableChunks { } template <class F> - void remove_if(F &&f) { + bool remove_if(F &&f) { + bool is_removed = false; for (auto it = nodes_.begin(), end = nodes_.end(); it != end; ++it) { if (!it->empty() && f(it->get_public())) { erase_node(it); + is_removed = true; } } try_shrink(); + return is_removed; } private: @@ -568,8 +571,8 @@ template <class KeyT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>> using FlatHashSetChunks = FlatHashTableChunks<SetNode<KeyT, EqT>, HashT, EqT>; template <class NodeT, class HashT, class EqT, class FuncT> -void table_remove_if(FlatHashTableChunks<NodeT, HashT, EqT> &table, FuncT &&func) { - table.remove_if(func); +bool table_remove_if(FlatHashTableChunks<NodeT, HashT, EqT> &table, FuncT &&func) { + return table.remove_if(func); } } // namespace td diff --git a/tdutils/td/utils/FlatHashTable.h b/tdutils/td/utils/FlatHashTable.h index f2af44b86..cd7e7509a 100644 --- a/tdutils/td/utils/FlatHashTable.h +++ b/tdutils/td/utils/FlatHashTable.h @@ -385,9 +385,9 @@ class FlatHashTable { } template <class F> - void remove_if(F &&f) { + bool remove_if(F &&f) { if (empty()) { - return; + return false; } auto it = begin_impl(); @@ -401,9 +401,11 @@ class FlatHashTable { } while (!it->empty()); } auto first_empty = it; + bool is_removed = false; while (it != end) { if (!it->empty() && f(it->get_public())) { erase_node(it); + is_removed = true; } else { ++it; } @@ -411,11 +413,13 @@ class FlatHashTable { for (it = nodes_; it != first_empty;) { if (!it->empty() && f(it->get_public())) { erase_node(it); + is_removed = true; } else { ++it; } } try_shrink(); + return is_removed; } private: diff --git a/tdutils/td/utils/algorithm.h b/tdutils/td/utils/algorithm.h index b883999f0..7046c8e65 100644 --- a/tdutils/td/utils/algorithm.h +++ b/tdutils/td/utils/algorithm.h @@ -254,22 +254,25 @@ detail::reversion_wrapper<T> reversed(T &iterable) { } template <class TableT, class FuncT> -void table_remove_if(TableT &table, FuncT &&func) { +bool table_remove_if(TableT &table, FuncT &&func) { + bool is_removed = false; for (auto it = table.begin(); it != table.end();) { if (func(*it)) { it = table.erase(it); + is_removed = true; } else { ++it; } } + return is_removed; } template <class NodeT, class HashT, class EqT> class FlatHashTable; template <class NodeT, class HashT, class EqT, class FuncT> -void table_remove_if(FlatHashTable<NodeT, HashT, EqT> &table, FuncT &&func) { - table.remove_if(func); +bool table_remove_if(FlatHashTable<NodeT, HashT, EqT> &table, FuncT &&func) { + return table.remove_if(func); } } // namespace td |
