[llvm] [ADT] Add DenseSet::insert_range (PR #131567)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 17 09:37:57 PDT 2025
https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/131567
>From 88a4372816dd332b04eecd56b26f00c1a34e8d5a Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 16 Mar 2025 22:25:54 -0700
Subject: [PATCH 1/2] [ADT] Add DenseSet::insert_range
This pach adds DenseSet::insert_range, named after
std::map::insert_range from C++23. The intent is to allow a heavy
operation as the argument like:
Set.insert_range(Map[Key]);
Without insert_range, we would have to do:
Set.insert(Map[Key].begin(), Map[Key].end());
or:
auto &M = Map[Key];
Set.insert(M.begin(), M.end());
Neither is elegant.
---
llvm/include/llvm/ADT/DenseSet.h | 5 +++++
llvm/unittests/ADT/DenseSetTest.cpp | 11 +++++++++++
2 files changed, 16 insertions(+)
diff --git a/llvm/include/llvm/ADT/DenseSet.h b/llvm/include/llvm/ADT/DenseSet.h
index a307bd8c9198b..ac766e778df0d 100644
--- a/llvm/include/llvm/ADT/DenseSet.h
+++ b/llvm/include/llvm/ADT/DenseSet.h
@@ -14,6 +14,7 @@
#ifndef LLVM_ADT_DENSESET_H
#define LLVM_ADT_DENSESET_H
+#include "llvm/ADT/ADL.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/Support/MathExtras.h"
@@ -237,6 +238,10 @@ class DenseSetImpl {
for (; I != E; ++I)
insert(*I);
}
+
+ template <typename Range> void insert_range(Range &&R) {
+ insert(adl_begin(R), adl_end(R));
+ }
};
/// Equality comparison for DenseSet.
diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp
index f14542eafc251..14cef2ab678f9 100644
--- a/llvm/unittests/ADT/DenseSetTest.cpp
+++ b/llvm/unittests/ADT/DenseSetTest.cpp
@@ -32,6 +32,17 @@ TEST(DenseSetTest, DoubleEntrySetTest) {
EXPECT_EQ(0u, set.count(2));
}
+TEST(DenseSetTest, InsertRange) {
+ llvm::DenseSet<unsigned> set1;
+ llvm::DenseSet<unsigned> set2;
+ set1.insert(1);
+ set1.insert(2);
+ set1.insert(3);
+ constexpr unsigned Args[] = {3, 1, 2};
+ set2.insert_range(Args);
+ EXPECT_EQ(set1, set2);
+}
+
struct TestDenseSetInfo {
static inline unsigned getEmptyKey() { return ~0; }
static inline unsigned getTombstoneKey() { return ~0U - 1; }
>From e6157a104bee94b36333e402d0751f7fe2e137ac Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 17 Mar 2025 09:32:27 -0700
Subject: [PATCH 2/2] Address a comment.
---
llvm/unittests/ADT/DenseSetTest.cpp | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp
index 14cef2ab678f9..8fc2bab44b124 100644
--- a/llvm/unittests/ADT/DenseSetTest.cpp
+++ b/llvm/unittests/ADT/DenseSetTest.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/ADT/DenseSet.h"
+#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <type_traits>
@@ -33,14 +34,10 @@ TEST(DenseSetTest, DoubleEntrySetTest) {
}
TEST(DenseSetTest, InsertRange) {
- llvm::DenseSet<unsigned> set1;
- llvm::DenseSet<unsigned> set2;
- set1.insert(1);
- set1.insert(2);
- set1.insert(3);
+ llvm::DenseSet<unsigned> set;
constexpr unsigned Args[] = {3, 1, 2};
- set2.insert_range(Args);
- EXPECT_EQ(set1, set2);
+ set.insert_range(Args);
+ EXPECT_THAT(set, ::testing::UnorderedElementsAre(1, 2, 3));
}
struct TestDenseSetInfo {
More information about the llvm-commits
mailing list