-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ADT] Add range constructors to *Set #132623
Merged
kazutakahirata
merged 1 commit into
llvm:main
from
kazutakahirata:cleanup_001_set_ctor_range
Mar 23, 2025
Merged
[ADT] Add range constructors to *Set #132623
kazutakahirata
merged 1 commit into
llvm:main
from
kazutakahirata:cleanup_001_set_ctor_range
Mar 23, 2025
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DenseSet recently gained a range constructor: DenseSet<T> Dest(llvm::from_range, Src); This patch adds the same signature to SetVector, SmallPtrSet, SmallSet, and StringSet for consistency.
@llvm/pr-subscribers-llvm-adt Author: Kazu Hirata (kazutakahirata) ChangesDenseSet recently gained a range constructor: DenseSet<T> Dest(llvm::from_range, Src); This patch adds the same signature to SetVector, SmallPtrSet, Full diff: https://github.com/llvm/llvm-project/pull/132623.diff 8 Files Affected:
diff --git a/llvm/include/llvm/ADT/SetVector.h b/llvm/include/llvm/ADT/SetVector.h
index 395b22685240f..6ee9375b175b7 100644
--- a/llvm/include/llvm/ADT/SetVector.h
+++ b/llvm/include/llvm/ADT/SetVector.h
@@ -24,6 +24,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compiler.h"
#include <cassert>
@@ -82,6 +83,10 @@ class SetVector {
insert(Start, End);
}
+ template <typename Range>
+ SetVector(llvm::from_range_t, Range &&R)
+ : SetVector(adl_begin(R), adl_end(R)) {}
+
ArrayRef<value_type> getArrayRef() const { return vector_; }
/// Clear the SetVector and return the underlying vector.
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index af26661699444..e47d5c0c7cdc0 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -17,6 +17,7 @@
#include "llvm/ADT/ADL.h"
#include "llvm/ADT/EpochTracker.h"
+#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/ReverseIteration.h"
#include "llvm/Support/type_traits.h"
@@ -556,6 +557,10 @@ class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
this->insert(I, E);
}
+ template <typename Range>
+ SmallPtrSet(llvm::from_range_t, Range &&R)
+ : SmallPtrSet(adl_begin(R), adl_end(R)) {}
+
SmallPtrSet(std::initializer_list<PtrType> IL)
: BaseT(SmallStorage, SmallSizePowTwo) {
this->insert(IL.begin(), IL.end());
diff --git a/llvm/include/llvm/ADT/SmallSet.h b/llvm/include/llvm/ADT/SmallSet.h
index 163690edda1bf..d5702c64efb41 100644
--- a/llvm/include/llvm/ADT/SmallSet.h
+++ b/llvm/include/llvm/ADT/SmallSet.h
@@ -15,6 +15,7 @@
#define LLVM_ADT_SMALLSET_H
#include "llvm/ADT/ADL.h"
+#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/iterator.h"
@@ -156,6 +157,10 @@ class SmallSet {
insert(Begin, End);
}
+ template <typename Range>
+ SmallSet(llvm::from_range_t, Range &&R)
+ : SmallSet(adl_begin(R), adl_end(R)) {}
+
template <typename RangeT>
explicit SmallSet(const iterator_range<RangeT> &R) {
insert(R.begin(), R.end());
diff --git a/llvm/include/llvm/ADT/StringSet.h b/llvm/include/llvm/ADT/StringSet.h
index 454af55172d66..be3cbc676d641 100644
--- a/llvm/include/llvm/ADT/StringSet.h
+++ b/llvm/include/llvm/ADT/StringSet.h
@@ -15,6 +15,7 @@
#define LLVM_ADT_STRINGSET_H
#include "llvm/ADT/ADL.h"
+#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/StringMap.h"
namespace llvm {
@@ -30,6 +31,9 @@ class StringSet : public StringMap<std::nullopt_t, AllocatorTy> {
for (StringRef str : initializer)
insert(str);
}
+ template <typename Range> StringSet(llvm::from_range_t, Range &&R) {
+ insert(adl_begin(R), adl_end(R));
+ }
template <typename Container> explicit StringSet(Container &&C) {
for (auto &&Str : C)
insert(Str);
diff --git a/llvm/unittests/ADT/SetVectorTest.cpp b/llvm/unittests/ADT/SetVectorTest.cpp
index ee565a01fd922..6ce4360cab90e 100644
--- a/llvm/unittests/ADT/SetVectorTest.cpp
+++ b/llvm/unittests/ADT/SetVectorTest.cpp
@@ -88,6 +88,12 @@ TEST(SetVector, ConstPtrKeyTest) {
EXPECT_FALSE(S.contains((const int *)&j));
}
+TEST(SetVector, CtorRange) {
+ constexpr unsigned Args[] = {3, 1, 2};
+ SetVector<unsigned> Set(llvm::from_range, Args);
+ EXPECT_THAT(Set, ::testing::ElementsAre(3, 1, 2));
+}
+
TEST(SetVector, InsertRange) {
SetVector<unsigned> Set;
constexpr unsigned Args[] = {3, 1, 2};
diff --git a/llvm/unittests/ADT/SmallPtrSetTest.cpp b/llvm/unittests/ADT/SmallPtrSetTest.cpp
index fb002cce65092..4ea7403b65abc 100644
--- a/llvm/unittests/ADT/SmallPtrSetTest.cpp
+++ b/llvm/unittests/ADT/SmallPtrSetTest.cpp
@@ -411,6 +411,15 @@ TEST(SmallPtrSetTest, RemoveIf) {
EXPECT_FALSE(Removed);
}
+TEST(SmallPtrSetTest, CtorRange) {
+ int V0 = 0;
+ int V1 = 1;
+ int V2 = 2;
+ int *Args[] = {&V2, &V0, &V1};
+ SmallPtrSet<int *, 4> Set(llvm::from_range, Args);
+ EXPECT_THAT(Set, UnorderedElementsAre(&V0, &V1, &V2));
+}
+
TEST(SmallPtrSetTest, InsertRange) {
int V0 = 0;
int V1 = 1;
diff --git a/llvm/unittests/ADT/SmallSetTest.cpp b/llvm/unittests/ADT/SmallSetTest.cpp
index 7d2431d4832a9..9e410c6ee2b23 100644
--- a/llvm/unittests/ADT/SmallSetTest.cpp
+++ b/llvm/unittests/ADT/SmallSetTest.cpp
@@ -127,6 +127,12 @@ TEST(SmallSetTest, InsertPerfectFwd) {
}
}
+TEST(SmallSetTest, CtorRange) {
+ constexpr unsigned Args[] = {3, 1, 2};
+ SmallSet<int, 4> s1(llvm::from_range, Args);
+ EXPECT_THAT(s1, ::testing::UnorderedElementsAre(1, 2, 3));
+}
+
TEST(SmallSetTest, InsertRange) {
SmallSet<int, 4> s1;
constexpr unsigned Args[] = {3, 1, 2};
diff --git a/llvm/unittests/ADT/StringSetTest.cpp b/llvm/unittests/ADT/StringSetTest.cpp
index 8de05a2fe79d4..81ace101bd061 100644
--- a/llvm/unittests/ADT/StringSetTest.cpp
+++ b/llvm/unittests/ADT/StringSetTest.cpp
@@ -81,6 +81,15 @@ TEST_F(StringSetTest, Equal) {
ASSERT_TRUE(A == A);
}
+TEST_F(StringSetTest, CtorRange) {
+ const char *Args[] = {"chair", "desk", "bed"};
+ StringSet<> Set(llvm::from_range, Args);
+ EXPECT_EQ(Set.size(), 3U);
+ EXPECT_TRUE(Set.contains("bed"));
+ EXPECT_TRUE(Set.contains("chair"));
+ EXPECT_TRUE(Set.contains("desk"));
+}
+
TEST_F(StringSetTest, InsertRange) {
StringSet<> Set;
const char *Args[] = {"chair", "desk", "bed"};
|
kuhar
approved these changes
Mar 23, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
DenseSet recently gained a range constructor:
DenseSet Dest(llvm::from_range, Src);
This patch adds the same signature to SetVector, SmallPtrSet,
SmallSet, and StringSet for consistency.