[llvm] [ADT] Add `DenseMap::insert_range` (PR #133600)

Baranov Victor via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 30 01:28:27 PDT 2025


https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/133600

>From 489fd2cdb1050f7bd1d0db25187cc17dfcb13985 Mon Sep 17 00:00:00 2001
From: Victor Baranov <bar.victor.2002 at gmail.com>
Date: Sun, 30 Mar 2025 01:00:48 +0300
Subject: [PATCH 1/2] [ADT] add DenseMap::insert_range

---
 llvm/include/llvm/ADT/DenseMap.h    |  5 +++++
 llvm/unittests/ADT/DenseMapTest.cpp | 11 +++++++++++
 2 files changed, 16 insertions(+)

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

>From 9e579b9fb7d00ca3135a17b3b674afad1993b3ad Mon Sep 17 00:00:00 2001
From: Victor Baranov <bar.victor.2002 at gmail.com>
Date: Sun, 30 Mar 2025 11:27:42 +0300
Subject: [PATCH 2/2] fix: pr comment, feat: new test for SmallDenseSet.

---
 llvm/include/llvm/ADT/DenseMap.h    | 1 +
 llvm/unittests/ADT/DenseMapTest.cpp | 2 +-
 llvm/unittests/ADT/DenseSetTest.cpp | 7 +++++++
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 52834c8528eb0..ea9ba6f47ac1a 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -303,6 +303,7 @@ class DenseMapBase : public DebugEpochBase {
       insert(*I);
   }
 
+  /// Inserts range of 'std::pair<KeyT, ValueT>' values into the map.
   template <typename Range> void insert_range(Range &&R) {
     insert(adl_begin(R), adl_end(R));
   }
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index 695ac99728a02..52701b1fa1998 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -385,7 +385,7 @@ TEST(DenseMapCustomTest, InsertRange) {
   std::pair<int, int> InputVals[3] = {{0, 0}, {0, 1}, {1, 2}};
   M.insert_range(InputVals);
 
-  EXPECT_EQ(2u, M.size());
+  EXPECT_EQ(M.size(), 2u);
   EXPECT_THAT(M, testing::UnorderedElementsAre(testing::Pair(0, 0),
                                                testing::Pair(1, 2)));
 }
diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp
index 5a8ee592ddfc7..a24f99b6bb34f 100644
--- a/llvm/unittests/ADT/DenseSetTest.cpp
+++ b/llvm/unittests/ADT/DenseSetTest.cpp
@@ -58,6 +58,13 @@ TEST(DenseSetTest, InsertRange) {
   EXPECT_THAT(set, ::testing::UnorderedElementsAre(1, 2, 3));
 }
 
+TEST(SmallDenseSetTest, InsertRange) {
+  llvm::SmallDenseSet<unsigned> set;
+  constexpr unsigned Args[] = {9, 7, 8};
+  set.insert_range(Args);
+  EXPECT_THAT(set, ::testing::UnorderedElementsAre(7, 8, 9));
+}
+
 struct TestDenseSetInfo {
   static inline unsigned getEmptyKey() { return ~0; }
   static inline unsigned getTombstoneKey() { return ~0U - 1; }



More information about the llvm-commits mailing list