Skip to content

Commit fa2e9f2

Browse files
[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.
1 parent b121cdf commit fa2e9f2

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,9 @@ class DenseMapBase : public DebugEpochBase {
629629
return TheBucket;
630630
}
631631

632-
template <typename LookupKeyT> BucketT *doFind(const LookupKeyT &Val) {
633-
BucketT *BucketsPtr = getBuckets();
632+
template <typename LookupKeyT>
633+
const BucketT *doFind(const LookupKeyT &Val) const {
634+
const BucketT *BucketsPtr = getBuckets();
634635
const unsigned NumBuckets = getNumBuckets();
635636
if (NumBuckets == 0)
636637
return nullptr;
@@ -639,7 +640,7 @@ class DenseMapBase : public DebugEpochBase {
639640
unsigned BucketNo = getHashValue(Val) & (NumBuckets - 1);
640641
unsigned ProbeAmt = 1;
641642
while (true) {
642-
BucketT *Bucket = BucketsPtr + BucketNo;
643+
const BucketT *Bucket = BucketsPtr + BucketNo;
643644
if (LLVM_LIKELY(KeyInfoT::isEqual(Val, Bucket->getFirst())))
644645
return Bucket;
645646
if (LLVM_LIKELY(KeyInfoT::isEqual(Bucket->getFirst(), EmptyKey)))
@@ -652,9 +653,9 @@ class DenseMapBase : public DebugEpochBase {
652653
}
653654
}
654655

655-
template <typename LookupKeyT>
656-
const BucketT *doFind(const LookupKeyT &Val) const {
657-
return const_cast<DenseMapBase *>(this)->doFind(Val); // NOLINT
656+
template <typename LookupKeyT> BucketT *doFind(const LookupKeyT &Val) {
657+
return const_cast<BucketT *>(
658+
static_cast<const DenseMapBase *>(this)->doFind(Val));
658659
}
659660

660661
/// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in

0 commit comments

Comments
 (0)