From 9691b609a29ce7edc46b19c2bc3cd1514418c75a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 2 Aug 2023 15:01:17 +0200 Subject: [PATCH] util/IntrusiveHashSet: remove_and_dispose() returns number of removed elements --- src/util/IntrusiveHashSet.hxx | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/util/IntrusiveHashSet.hxx b/src/util/IntrusiveHashSet.hxx index 69929c95b..5b2520a86 100644 --- a/src/util/IntrusiveHashSet.hxx +++ b/src/util/IntrusiveHashSet.hxx @@ -191,30 +191,44 @@ public: counter.reset(); } - void remove_and_dispose_if(std::predicate auto pred, + /** + * Remove and dispose all items matching the given predicate. + * + * @return the number of removed items + */ + std::size_t remove_and_dispose_if(std::predicate auto pred, Disposer auto disposer) noexcept { + std::size_t n = 0; for (auto &bucket : table) - counter -= bucket.remove_and_dispose_if(pred, disposer); + n += bucket.remove_and_dispose_if(pred, disposer); + counter -= n; + return n; } /** * Remove and dispose all items with the specified key. + * + * @return the number of removed items */ - constexpr void remove_and_dispose_key(const auto &key, - Disposer auto disposer) noexcept { + constexpr std::size_t remove_and_dispose_key(const auto &key, + Disposer auto disposer) noexcept { auto &bucket = GetBucket(key); - counter -= bucket.remove_and_dispose_if([this, &key](const auto &item){ + std::size_t n = bucket.remove_and_dispose_if([this, &key](const auto &item){ return ops.equal(key, item); }, disposer); + counter -= n; + return n; } - constexpr void remove_and_dispose_key_if(const auto &key, - std::predicate auto pred, - Disposer auto disposer) noexcept { + constexpr std::size_t remove_and_dispose_key_if(const auto &key, + std::predicate auto pred, + Disposer auto disposer) noexcept { auto &bucket = GetBucket(key); - counter -= bucket.remove_and_dispose_if([this, &key, &pred](const auto &item){ + std::size_t n = bucket.remove_and_dispose_if([this, &key, &pred](const auto &item){ return ops.equal(key, item) && pred(item); }, disposer); + counter -= n; + return n; } [[nodiscard]]