util/IntrusiveForwardList: add method remove_and_dispose_if()

This commit is contained in:
Max Kellermann 2023-09-21 13:19:02 +02:00 committed by Max Kellermann
parent 78d28063c4
commit 5b28a987e5
1 changed files with 24 additions and 0 deletions

View File

@ -199,6 +199,30 @@ public:
last_cache = {};
}
/**
* @return the number of removed items
*/
std::size_t remove_and_dispose_if(std::predicate<const_reference> auto pred,
Disposer<value_type> 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);
}