[llvm] [ADT] Add DenseSet(llvm::from_t, Range) (PR #131832)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 22 16:29:45 PDT 2025
https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/131832
>From 8420bb157cd82b7afb91c58e4b644f5cca0b10b9 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 18 Mar 2025 00:43:53 -0700
Subject: [PATCH 1/3] [ADT] Add DenseSet(llvm::from_t, Range)
This patch adds a constructor of the form:
DenseSet Set(llvm::from_range, Range)
while forward-porting std::from_range from c++23.
---
llvm/include/llvm/ADT/DenseSet.h | 5 +++++
llvm/include/llvm/ADT/STLForwardCompat.h | 6 ++++++
llvm/unittests/ADT/DenseSetTest.cpp | 6 ++++++
3 files changed, 17 insertions(+)
diff --git a/llvm/include/llvm/ADT/DenseSet.h b/llvm/include/llvm/ADT/DenseSet.h
index ac766e778df0d..5642f1400bc39 100644
--- a/llvm/include/llvm/ADT/DenseSet.h
+++ b/llvm/include/llvm/ADT/DenseSet.h
@@ -17,6 +17,7 @@
#include "llvm/ADT/ADL.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseMapInfo.h"
+#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/type_traits.h"
#include <cstddef>
@@ -78,6 +79,10 @@ class DenseSetImpl {
insert(Elems.begin(), Elems.end());
}
+ template <typename Range>
+ DenseSetImpl(llvm::from_range_t, Range &&R)
+ : DenseSetImpl(adl_begin(R), adl_end(R)) {}
+
bool empty() const { return TheMap.empty(); }
size_type size() const { return TheMap.size(); }
size_t getMemorySize() const { return TheMap.getMemorySize(); }
diff --git a/llvm/include/llvm/ADT/STLForwardCompat.h b/llvm/include/llvm/ADT/STLForwardCompat.h
index 6afe3610b257f..10c274ea8e23e 100644
--- a/llvm/include/llvm/ADT/STLForwardCompat.h
+++ b/llvm/include/llvm/ADT/STLForwardCompat.h
@@ -67,6 +67,12 @@ template <typename Enum>
return static_cast<std::underlying_type_t<Enum>>(E);
}
+// A tag for constructors accepting ranges.
+struct from_range_t {
+ explicit from_range_t() = default;
+};
+inline constexpr from_range_t from_range{};
+
} // namespace llvm
#endif // LLVM_ADT_STLFORWARDCOMPAT_H
diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp
index 8fc2bab44b124..3f668b831a450 100644
--- a/llvm/unittests/ADT/DenseSetTest.cpp
+++ b/llvm/unittests/ADT/DenseSetTest.cpp
@@ -33,6 +33,12 @@ TEST(DenseSetTest, DoubleEntrySetTest) {
EXPECT_EQ(0u, set.count(2));
}
+TEST(DenseSetTest, CtorRange) {
+ constexpr unsigned Args[] = {3, 1, 2};
+ llvm::DenseSet<unsigned> set(llvm::from_range, Args);
+ EXPECT_THAT(set, ::testing::UnorderedElementsAre(1, 2, 3));
+}
+
TEST(DenseSetTest, InsertRange) {
llvm::DenseSet<unsigned> set;
constexpr unsigned Args[] = {3, 1, 2};
>From 28e915b67697df503303fce94689df6016233832 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 22 Mar 2025 11:16:38 -0700
Subject: [PATCH 2/3] Address a comment.
---
llvm/unittests/ADT/DenseSetTest.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp
index 3f668b831a450..2b62324c469a0 100644
--- a/llvm/unittests/ADT/DenseSetTest.cpp
+++ b/llvm/unittests/ADT/DenseSetTest.cpp
@@ -39,6 +39,12 @@ TEST(DenseSetTest, CtorRange) {
EXPECT_THAT(set, ::testing::UnorderedElementsAre(1, 2, 3));
}
+TEST(SmallDenseSetTest, CtorRange) {
+ constexpr unsigned Args[] = {9, 7, 8};
+ llvm::SmallDenseSet<unsigned> set(llvm::from_range, Args);
+ EXPECT_THAT(set, ::testing::UnorderedElementsAre(7, 8, 9));
+}
+
TEST(DenseSetTest, InsertRange) {
llvm::DenseSet<unsigned> set;
constexpr unsigned Args[] = {3, 1, 2};
>From 26e020aa0663f5cde22ab268a3e469531acf5f30 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 22 Mar 2025 16:28:39 -0700
Subject: [PATCH 3/3] Add a test with implicit conversion.
---
llvm/unittests/ADT/DenseSetTest.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/llvm/unittests/ADT/DenseSetTest.cpp b/llvm/unittests/ADT/DenseSetTest.cpp
index 2b62324c469a0..5a8ee592ddfc7 100644
--- a/llvm/unittests/ADT/DenseSetTest.cpp
+++ b/llvm/unittests/ADT/DenseSetTest.cpp
@@ -39,6 +39,12 @@ TEST(DenseSetTest, CtorRange) {
EXPECT_THAT(set, ::testing::UnorderedElementsAre(1, 2, 3));
}
+TEST(DenseSetTest, CtorRangeImplicitConversion) {
+ constexpr char Args[] = {3, 1, 2};
+ llvm::DenseSet<unsigned> set(llvm::from_range, Args);
+ EXPECT_THAT(set, ::testing::UnorderedElementsAre(1, 2, 3));
+}
+
TEST(SmallDenseSetTest, CtorRange) {
constexpr unsigned Args[] = {9, 7, 8};
llvm::SmallDenseSet<unsigned> set(llvm::from_range, Args);
More information about the llvm-commits
mailing list