From 5b28a987e5d403c140e17421c90ff311d010157d Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 21 Sep 2023 13:19:02 +0200 Subject: [PATCH] util/IntrusiveForwardList: add method remove_and_dispose_if() --- src/util/IntrusiveForwardList.hxx | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/util/IntrusiveForwardList.hxx b/src/util/IntrusiveForwardList.hxx index f6e1532f9..64954e617 100644 --- a/src/util/IntrusiveForwardList.hxx +++ b/src/util/IntrusiveForwardList.hxx @@ -199,6 +199,30 @@ public: last_cache = {}; } + /** + * @return the number of removed items + */ + std::size_t remove_and_dispose_if(std::predicate auto pred, + Disposer auto dispose) noexcept { + std::size_t result = 0; + + for (auto prev = before_begin(), current = std::next(prev); + current != end();) { + auto &item = *current; + + if (pred(item)) { + ++result; + ++current; + erase_after(prev); + dispose(&item); + } else { + prev = current++; + } + } + + return result; + } + const_reference front() const noexcept { return *Cast(head.next); }