[llvm] [ADT] Use range-based for loops in DenseMap.h (NFC) (PR #151900)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 3 22:55:45 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/151900
This patch introduces helper function buckets() to convert several
loops to range-based for loops.
>From ded12075ca2744f6d9fefe1e885f4baab0720b3a Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 3 Aug 2025 21:36:09 -0700
Subject: [PATCH] [ADT] Use range-based for loops in DenseMap.h (NFC)
This patch introduces helper function buckets() to convert several
loops to range-based for loops.
---
llvm/include/llvm/ADT/DenseMap.h | 32 ++++++++++++++++++--------------
1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 3825f8d523728..9557c9d8e34a1 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -142,18 +142,18 @@ class DenseMapBase : public DebugEpochBase {
const KeyT EmptyKey = getEmptyKey();
if constexpr (std::is_trivially_destructible_v<ValueT>) {
// Use a simpler loop when values don't need destruction.
- for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P)
- P->getFirst() = EmptyKey;
+ for (BucketT &B : buckets())
+ B.getFirst() = EmptyKey;
} else {
const KeyT TombstoneKey = getTombstoneKey();
unsigned NumEntries = getNumEntries();
- for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
- if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey)) {
- if (!KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
- P->getSecond().~ValueT();
+ for (BucketT &B : buckets()) {
+ if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey)) {
+ if (!KeyInfoT::isEqual(B.getFirst(), TombstoneKey)) {
+ B.getSecond().~ValueT();
--NumEntries;
}
- P->getFirst() = EmptyKey;
+ B.getFirst() = EmptyKey;
}
}
assert(NumEntries == 0 && "Node count imbalance!");
@@ -424,11 +424,11 @@ class DenseMapBase : public DebugEpochBase {
return;
const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
- for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
- if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
- !KeyInfoT::isEqual(P->getFirst(), TombstoneKey))
- P->getSecond().~ValueT();
- P->getFirst().~KeyT();
+ for (BucketT &B : buckets()) {
+ if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey) &&
+ !KeyInfoT::isEqual(B.getFirst(), TombstoneKey))
+ B.getSecond().~ValueT();
+ B.getFirst().~KeyT();
}
}
@@ -439,8 +439,8 @@ class DenseMapBase : public DebugEpochBase {
assert((getNumBuckets() & (getNumBuckets() - 1)) == 0 &&
"# initial buckets must be a power of two!");
const KeyT EmptyKey = getEmptyKey();
- for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B)
- ::new (&B->getFirst()) KeyT(EmptyKey);
+ for (BucketT &B : buckets())
+ ::new (&B.getFirst()) KeyT(EmptyKey);
}
/// Returns the number of buckets to allocate to ensure that the DenseMap can
@@ -584,6 +584,10 @@ class DenseMapBase : public DebugEpochBase {
return getBuckets() + getNumBuckets();
}
+ iterator_range<BucketT *> buckets() {
+ return llvm::make_range(getBuckets(), getBucketsEnd());
+ }
+
void grow(unsigned AtLeast) { static_cast<DerivedT *>(this)->grow(AtLeast); }
void shrink_and_clear() { static_cast<DerivedT *>(this)->shrink_and_clear(); }
More information about the llvm-commits
mailing list