From e3ef0929f1a05862336aad7a55713dcfdfcb06fb Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 30 Nov 2022 09:04:56 +0100 Subject: [PATCH] util/IntrusiveHashSet: add `const` overloads --- src/util/IntrusiveHashSet.hxx | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/util/IntrusiveHashSet.hxx b/src/util/IntrusiveHashSet.hxx index dc10674c9..fe512272e 100644 --- a/src/util/IntrusiveHashSet.hxx +++ b/src/util/IntrusiveHashSet.hxx @@ -134,6 +134,7 @@ class IntrusiveHashSet { std::array table; using bucket_iterator = typename Bucket::iterator; + using const_bucket_iterator = typename Bucket::const_iterator; public: using value_type = T; @@ -248,10 +249,24 @@ public: return end(); } + [[nodiscard]] [[gnu::pure]] + constexpr const_bucket_iterator find(const auto &key) const noexcept { + auto &bucket = GetBucket(key); + for (auto &i : bucket) + if (equal(key, i)) + return bucket.iterator_to(i); + + return end(); + } + constexpr bucket_iterator end() noexcept { return table.front().end(); } + constexpr const_bucket_iterator end() const noexcept { + return table.front().end(); + } + constexpr void for_each(auto &&f) { for (auto &bucket : table) for (auto &i : bucket) @@ -272,4 +287,12 @@ private: const auto h = hash(std::forward(key)); return table[h % table_size]; } + + template + [[gnu::pure]] + [[nodiscard]] + constexpr const auto &GetBucket(K &&key) const noexcept { + const auto h = hash(std::forward(key)); + return table[h % table_size]; + } };