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

Baranov Victor via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 29 15:04:27 PDT 2025


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

This PR add `DenseMap::insert_range` to `DenseMap` for consistency with existing `DenseSet::insert_range` and `SmallSet::insert_range`. 

>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] [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



More information about the llvm-commits mailing list