[llvm] [ADT] Add a helper function to create iterators in DenseMap (NFC) (PR #155133)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 23 20:26:24 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/155133
This patch adds a private helper function, makeInsertIterator, to
encapsulate the logic for creating iterators within functions like
try_emplace and insert.
This refactoring reduces code duplication and improves readability at
the call sites.
>From 11936e95d065997b3896bdbf36e6c9046048bd04 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 23 Aug 2025 16:09:09 -0700
Subject: [PATCH] [ADT] Add a helper function to create iterators in DenseMap
(NFC)
This patch adds a private helper function, makeInsertIterator, to
encapsulate the logic for creating iterators within functions like
try_emplace and insert.
This refactoring reduces code duplication and improves readability at
the call sites.
---
llvm/include/llvm/ADT/DenseMap.h | 46 +++++++++-----------------------
1 file changed, 13 insertions(+), 33 deletions(-)
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 156894aa7f0e3..643dee98f0e28 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -257,22 +257,13 @@ class DenseMapBase : public DebugEpochBase {
std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
BucketT *TheBucket;
if (LookupBucketFor(Key, TheBucket))
- return std::make_pair(makeIterator(TheBucket,
- shouldReverseIterate<KeyT>()
- ? getBuckets()
- : getBucketsEnd(),
- *this, true),
+ return std::make_pair(makeInsertIterator(TheBucket),
false); // Already in map.
// Otherwise, insert the new element.
TheBucket =
InsertIntoBucket(TheBucket, std::move(Key), std::forward<Ts>(Args)...);
- return std::make_pair(makeIterator(TheBucket,
- shouldReverseIterate<KeyT>()
- ? getBuckets()
- : getBucketsEnd(),
- *this, true),
- true);
+ return std::make_pair(makeInsertIterator(TheBucket), true);
}
// Inserts key,value pair into the map if the key isn't already in the map.
@@ -282,21 +273,12 @@ class DenseMapBase : public DebugEpochBase {
std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
BucketT *TheBucket;
if (LookupBucketFor(Key, TheBucket))
- return std::make_pair(makeIterator(TheBucket,
- shouldReverseIterate<KeyT>()
- ? getBuckets()
- : getBucketsEnd(),
- *this, true),
+ return std::make_pair(makeInsertIterator(TheBucket),
false); // Already in map.
// Otherwise, insert the new element.
TheBucket = InsertIntoBucket(TheBucket, Key, std::forward<Ts>(Args)...);
- return std::make_pair(makeIterator(TheBucket,
- shouldReverseIterate<KeyT>()
- ? getBuckets()
- : getBucketsEnd(),
- *this, true),
- true);
+ return std::make_pair(makeInsertIterator(TheBucket), true);
}
/// Alternate version of insert() which allows a different, and possibly
@@ -309,22 +291,13 @@ class DenseMapBase : public DebugEpochBase {
const LookupKeyT &Val) {
BucketT *TheBucket;
if (LookupBucketFor(Val, TheBucket))
- return std::make_pair(makeIterator(TheBucket,
- shouldReverseIterate<KeyT>()
- ? getBuckets()
- : getBucketsEnd(),
- *this, true),
+ return std::make_pair(makeInsertIterator(TheBucket),
false); // Already in map.
// Otherwise, insert the new element.
TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first),
std::move(KV.second), Val);
- return std::make_pair(makeIterator(TheBucket,
- shouldReverseIterate<KeyT>()
- ? getBuckets()
- : getBucketsEnd(),
- *this, true),
- true);
+ return std::make_pair(makeInsertIterator(TheBucket), true);
}
/// insert - Range insertion of pairs.
@@ -545,6 +518,13 @@ class DenseMapBase : public DebugEpochBase {
return const_iterator(P, E, Epoch, NoAdvance);
}
+ iterator makeInsertIterator(BucketT *TheBucket) {
+ return makeIterator(TheBucket,
+ shouldReverseIterate<KeyT>() ? getBuckets()
+ : getBucketsEnd(),
+ *this, true);
+ }
+
unsigned getNumEntries() const {
return static_cast<const DerivedT *>(this)->getNumEntries();
}
More information about the llvm-commits
mailing list