[llvm] [ADT] Swap the two variants of DenseMap::doFind (NFC) (PR #155203)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 24 21:28:07 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/155203
doFind itself makes no modification, so we can implement it as a const
function. The only problem is that the non-const version of find
needs to return a modifiable bucket.
This patch "swaps" the constness of doFind. Specifically, the primary
implementation becomes const, preventing accidental modifications.
Then the non-const variant is derived off of the const variant.
>From fa2e9f2403471e5e6197ec0dc58463a1db7a4500 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 24 Aug 2025 17:10:27 -0700
Subject: [PATCH] [ADT] Swap the two variants of DenseMap::doFind (NFC)
doFind itself makes no modification, so we can implement it as a const
function. The only problem is that the non-const version of find
needs to return a modifiable bucket.
This patch "swaps" the constness of doFind. Specifically, the primary
implementation becomes const, preventing accidental modifications.
Then the non-const variant is derived off of the const variant.
---
llvm/include/llvm/ADT/DenseMap.h | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index ab1bc6356dcb9..0fa911a229c92 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -629,8 +629,9 @@ class DenseMapBase : public DebugEpochBase {
return TheBucket;
}
- template <typename LookupKeyT> BucketT *doFind(const LookupKeyT &Val) {
- BucketT *BucketsPtr = getBuckets();
+ template <typename LookupKeyT>
+ const BucketT *doFind(const LookupKeyT &Val) const {
+ const BucketT *BucketsPtr = getBuckets();
const unsigned NumBuckets = getNumBuckets();
if (NumBuckets == 0)
return nullptr;
@@ -639,7 +640,7 @@ class DenseMapBase : public DebugEpochBase {
unsigned BucketNo = getHashValue(Val) & (NumBuckets - 1);
unsigned ProbeAmt = 1;
while (true) {
- BucketT *Bucket = BucketsPtr + BucketNo;
+ const BucketT *Bucket = BucketsPtr + BucketNo;
if (LLVM_LIKELY(KeyInfoT::isEqual(Val, Bucket->getFirst())))
return Bucket;
if (LLVM_LIKELY(KeyInfoT::isEqual(Bucket->getFirst(), EmptyKey)))
@@ -652,9 +653,9 @@ class DenseMapBase : public DebugEpochBase {
}
}
- template <typename LookupKeyT>
- const BucketT *doFind(const LookupKeyT &Val) const {
- return const_cast<DenseMapBase *>(this)->doFind(Val); // NOLINT
+ template <typename LookupKeyT> BucketT *doFind(const LookupKeyT &Val) {
+ return const_cast<BucketT *>(
+ static_cast<const DenseMapBase *>(this)->doFind(Val));
}
/// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
More information about the llvm-commits
mailing list