Skip to content

[ADT] Add range constructors to *Set #132623

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

Merged
merged 1 commit into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/include/llvm/ADT/SetVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/ADT/SmallPtrSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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());
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/ADT/SmallSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/ADT/StringSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions llvm/unittests/ADT/SetVectorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
9 changes: 9 additions & 0 deletions llvm/unittests/ADT/SmallPtrSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions llvm/unittests/ADT/SmallSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
9 changes: 9 additions & 0 deletions llvm/unittests/ADT/StringSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Expand Down