From 65f1cafa432acd26b5ca4b94c73cde9b7ca2ac1c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 30 Nov 2022 20:35:28 +0100 Subject: [PATCH] util/IntrusiveHashSet: add method find_if() --- src/util/IntrusiveHashSet.hxx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/util/IntrusiveHashSet.hxx b/src/util/IntrusiveHashSet.hxx index fe512272e..06ec5d6d8 100644 --- a/src/util/IntrusiveHashSet.hxx +++ b/src/util/IntrusiveHashSet.hxx @@ -259,6 +259,23 @@ public: return end(); } + /** + * Like find(), but returns an item that matches the given + * predicate. This is useful if the container can contain + * multiple items that compare equal (according to #Equal, but + * not according to #pred). + */ + [[nodiscard]] [[gnu::pure]] + constexpr bucket_iterator find_if(const auto &key, + auto &&pred) noexcept { + auto &bucket = GetBucket(key); + for (auto &i : bucket) + if (equal(key, i) && pred(i)) + return bucket.iterator_to(i); + + return end(); + } + constexpr bucket_iterator end() noexcept { return table.front().end(); }