util/IntrusiveHashSet: add method expire_find_if()

This commit is contained in:
Max Kellermann 2022-12-01 14:26:02 +01:00 committed by Max Kellermann
parent 49e122a81d
commit 57673136d8
1 changed files with 34 additions and 0 deletions

View File

@ -291,6 +291,40 @@ public:
return end();
}
/**
* Like find_if(), but while traversing the bucket linked
* list, remove and dispose expired items.
*
* @param expired_pred returns true if an item is expired; it
* will be removed and disposed
*
* @param disposer function which will be called for items
* that were removed (because they are expired)
*
* @param match_pred returns true if the desired item was
* found
*/
[[nodiscard]] [[gnu::pure]]
constexpr bucket_iterator expire_find_if(const auto &key,
Predicate<const_reference> auto expired_pred,
Disposer<value_type> auto disposer,
Predicate<const_reference> auto match_pred) noexcept {
auto &bucket = GetBucket(key);
for (auto i = bucket.begin(), e = bucket.end(); i != e;) {
if (!equal(key, *i))
++i;
else if (expired_pred(*i))
i = erase_and_dispose(i, disposer);
else if (match_pred(*i))
return i;
else
++i;
}
return end();
}
constexpr bucket_iterator end() noexcept {
return table.front().end();
}