Skip to content

Commit

Permalink
util/IntrusiveForwardList: add method remove_and_dispose_if()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 22, 2023
1 parent 78d2806 commit 5b28a98
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/util/IntrusiveForwardList.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 5b28a98

Please sign in to comment.