[llvm] [ADT] Add `DenseMap::insert_range` (PR #133600)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 29 15:04:57 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Baranov Victor (vbvictor)
<details>
<summary>Changes</summary>
This PR add `DenseMap::insert_range` to `DenseMap` for consistency with existing `DenseSet::insert_range` and `SmallSet::insert_range`.
---
Full diff: https://github.com/llvm/llvm-project/pull/133600.diff
2 Files Affected:
- (modified) llvm/include/llvm/ADT/DenseMap.h (+5)
- (modified) llvm/unittests/ADT/DenseMapTest.cpp (+11)
``````````diff
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index f0f992f8eac38..52834c8528eb0 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -14,6 +14,7 @@
#ifndef LLVM_ADT_DENSEMAP_H
#define LLVM_ADT_DENSEMAP_H
+#include "llvm/ADT/ADL.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/EpochTracker.h"
#include "llvm/Support/AlignOf.h"
@@ -302,6 +303,10 @@ class DenseMapBase : public DebugEpochBase {
insert(*I);
}
+ template <typename Range> void insert_range(Range &&R) {
+ insert(adl_begin(R), adl_end(R));
+ }
+
template <typename V>
std::pair<iterator, bool> insert_or_assign(const KeyT &Key, V &&Val) {
auto Ret = try_emplace(Key, std::forward<V>(Val));
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index d1bbdde8dfc26..695ac99728a02 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -379,6 +379,17 @@ TEST(DenseMapCustomTest, EqualityComparison) {
EXPECT_NE(M1, M3);
}
+TEST(DenseMapCustomTest, InsertRange) {
+ DenseMap<int, int> M;
+
+ std::pair<int, int> InputVals[3] = {{0, 0}, {0, 1}, {1, 2}};
+ M.insert_range(InputVals);
+
+ EXPECT_EQ(2u, M.size());
+ EXPECT_THAT(M, testing::UnorderedElementsAre(testing::Pair(0, 0),
+ testing::Pair(1, 2)));
+}
+
// Test for the default minimum size of a DenseMap
TEST(DenseMapCustomTest, DefaultMinReservedSizeTest) {
// IF THIS VALUE CHANGE, please update InitialSizeTest, InitFromIterator, and
``````````
</details>
https://github.com/llvm/llvm-project/pull/133600
More information about the llvm-commits
mailing list