Skip to content

Conversation

kazutakahirata
Copy link
Contributor

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Aug 25, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/155203.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/DenseMap.h (+7-6)
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

@kazutakahirata kazutakahirata merged commit d2de309 into llvm:main Aug 25, 2025
10 of 11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250824_DenseMap_doFind branch August 25, 2025 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants