util/IntrusiveHashSet: add insert_check_if()

This commit is contained in:
Max Kellermann 2023-09-11 20:57:51 +02:00
parent 44f55e1866
commit 847ae7dd7c

View File

@ -276,6 +276,25 @@ public:
return {bucket.end(), true}; return {bucket.end(), true};
} }
/**
* Like insert_check(), but existing items are only considered
* conflicting if they match the given predicate.
*/
[[nodiscard]] [[gnu::pure]]
constexpr std::pair<bucket_iterator, bool> insert_check_if(const auto &key,
std::predicate<const_reference> auto pred) noexcept {
auto &bucket = GetBucket(key);
for (auto &i : bucket)
if (ops.equal(key, ops.get_key(i)) && pred(i))
return {bucket.iterator_to(i), false};
/* bucket.end() is a pointer to the bucket's list
head, a stable value that is guaranteed to be still
valid when insert_commit() gets called
eventually */
return {bucket.end(), true};
}
/** /**
* Finish the insertion if insert_check() has returned true. * Finish the insertion if insert_check() has returned true.
* *