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) { // 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 buckets() { + return llvm::make_range(getBuckets(), getBucketsEnd()); + } + void grow(unsigned AtLeast) { static_cast(this)->grow(AtLeast); } void shrink_and_clear() { static_cast(this)->shrink_and_clear(); }