[llvm] 8ecb2f9 - [ADT] Add `DenseMap::insert_range` (#133600)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 30 11:39:37 PDT 2025
Author: Baranov Victor
Date: 2025-03-30T14:39:32-04:00
New Revision: 8ecb2f9c4a8e39620935c1933fe1fbd2b96c723a
URL: https://github.com/llvm/llvm-project/commit/8ecb2f9c4a8e39620935c1933fe1fbd2b96c723a
DIFF: https://github.com/llvm/llvm-project/commit/8ecb2f9c4a8e39620935c1933fe1fbd2b96c723a.diff
LOG: [ADT] Add `DenseMap::insert_range` (#133600)
This PR add `DenseMap::insert_range` to `DenseMap` for consistency with
existing `DenseSet::insert_range`, `SmallSet::insert_range` and
`std::map::insert_range`.
Added:
Modified:
llvm/include/llvm/ADT/DenseMap.h
llvm/unittests/ADT/DenseMapTest.cpp
llvm/unittests/ADT/DenseSetTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index f0f992f8eac38..ea9ba6f47ac1a 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,11 @@ 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));
+ }
+
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..a4c045585fc28 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -379,6 +379,28 @@ 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(M.size(), 2u);
+ EXPECT_THAT(M, testing::UnorderedElementsAre(testing::Pair(0, 0),
+ testing::Pair(1, 2)));
+}
+
+TEST(SmallDenseMapCustomTest, InsertRange) {
+ SmallDenseMap<int, int> M;
+
+ std::pair<int, int> InputVals[3] = {{0, 0}, {0, 1}, {1, 2}};
+ M.insert_range(InputVals);
+
+ EXPECT_EQ(M.size(), 2u);
+ 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
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