[llvm-branch-commits] [llvm] [ADT] Add more useful methods to SmallSet API (PR #108601)
Victor Campos via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Sep 25 03:20:42 PDT 2024
https://github.com/vhscampos updated https://github.com/llvm/llvm-project/pull/108601
>From 12b657a4761351d52fccb93ce52e64c3c1b1e91f Mon Sep 17 00:00:00 2001
From: Victor Campos <victor.campos at arm.com>
Date: Fri, 9 Aug 2024 14:00:32 +0100
Subject: [PATCH 1/3] [ADT] Add more useful methods to SmallSet API
This patch adds useful methods to the SmallSet API:
- Constructor that takes pair of iterators.
- Constructor that takes a range.
- Constructor that takes an initializer list.
- Copy constructor.
- Move constructor.
- Copy assignment operator.
- Move assignment operator.
---
llvm/include/llvm/ADT/SmallSet.h | 17 ++++++++
llvm/unittests/ADT/SmallSetTest.cpp | 60 +++++++++++++++++++++++++++++
2 files changed, 77 insertions(+)
diff --git a/llvm/include/llvm/ADT/SmallSet.h b/llvm/include/llvm/ADT/SmallSet.h
index 56259ea7cf9d0f..431fdee56c20e0 100644
--- a/llvm/include/llvm/ADT/SmallSet.h
+++ b/llvm/include/llvm/ADT/SmallSet.h
@@ -19,6 +19,7 @@
#include "llvm/ADT/iterator.h"
#include <cstddef>
#include <functional>
+#include <initializer_list>
#include <set>
#include <utility>
@@ -147,6 +148,22 @@ class SmallSet {
using const_iterator = SmallSetIterator<T, N, C>;
SmallSet() = default;
+ SmallSet(const SmallSet &) = default;
+ SmallSet(SmallSet &&) = default;
+
+ template <typename IterT> SmallSet(IterT Begin, IterT End) {
+ this->insert(Begin, End);
+ }
+
+ template <typename RangeT>
+ explicit SmallSet(const iterator_range<RangeT> &R) {
+ this->insert(R.begin(), R.end());
+ }
+
+ SmallSet(std::initializer_list<T> L) { this->insert(L.begin(), L.end()); }
+
+ SmallSet &operator=(const SmallSet &) = default;
+ SmallSet &operator=(SmallSet &&) = default;
[[nodiscard]] bool empty() const { return Vector.empty() && Set.empty(); }
diff --git a/llvm/unittests/ADT/SmallSetTest.cpp b/llvm/unittests/ADT/SmallSetTest.cpp
index 0fb20b19df9254..8219bf6f4b4c55 100644
--- a/llvm/unittests/ADT/SmallSetTest.cpp
+++ b/llvm/unittests/ADT/SmallSetTest.cpp
@@ -17,6 +17,66 @@
using namespace llvm;
+TEST(SmallSetTest, ConstructorIteratorPair) {
+ auto L = {1, 2, 3, 4, 5};
+ SmallSet<int, 4> S(std::begin(L), std::end(L));
+ for (int Value : L)
+ EXPECT_TRUE(S.contains(Value));
+}
+
+TEST(SmallSet, ConstructorRange) {
+ auto L = {1, 2, 3, 4, 5};
+
+ SmallSet<int, 4> S(llvm::make_range(std::begin(L), std::end(L)));
+ for (int Value : L)
+ EXPECT_TRUE(S.contains(Value));
+}
+
+TEST(SmallSet, ConstructorInitializerList) {
+ auto L = {1, 2, 3, 4, 5};
+ SmallSet<int, 4> S = {1, 2, 3, 4, 5};
+ for (int Value : L)
+ EXPECT_TRUE(S.contains(Value));
+}
+
+TEST(SmallSet, CopyConstructor) {
+ SmallSet<int, 4> S = {1, 2, 3};
+ SmallSet<int, 4> T = S;
+
+ EXPECT_EQ(S, T);
+}
+
+TEST(SmallSet, MoveConstructor) {
+ auto L = {1, 2, 3};
+ SmallSet<int, 4> S = L;
+ SmallSet<int, 4> T = std::move(S);
+
+ EXPECT_TRUE(T.size() == L.size());
+ for (int Value : L) {
+ EXPECT_TRUE(T.contains(Value));
+ }
+}
+
+TEST(SmallSet, CopyAssignment) {
+ SmallSet<int, 4> S = {1, 2, 3};
+ SmallSet<int, 4> T;
+ T = S;
+
+ EXPECT_EQ(S, T);
+}
+
+TEST(SmallSet, MoveAssignment) {
+ auto L = {1, 2, 3};
+ SmallSet<int, 4> S = L;
+ SmallSet<int, 4> T;
+ T = std::move(S);
+
+ EXPECT_TRUE(T.size() == L.size());
+ for (int Value : L) {
+ EXPECT_TRUE(T.contains(Value));
+ }
+}
+
TEST(SmallSetTest, Insert) {
SmallSet<int, 4> s1;
>From d122983eb4f1f66da2a4a6b5bcdb9c8171d18205 Mon Sep 17 00:00:00 2001
From: Victor Campos <victor.campos at arm.com>
Date: Tue, 24 Sep 2024 17:43:42 +0100
Subject: [PATCH 2/3] fixup! [ADT] Add more useful methods to SmallSet API
---
llvm/include/llvm/ADT/SmallSet.h | 4 ++--
llvm/unittests/ADT/SmallSetTest.cpp | 34 +++++++++++------------------
2 files changed, 15 insertions(+), 23 deletions(-)
diff --git a/llvm/include/llvm/ADT/SmallSet.h b/llvm/include/llvm/ADT/SmallSet.h
index 431fdee56c20e0..1b8ad542846630 100644
--- a/llvm/include/llvm/ADT/SmallSet.h
+++ b/llvm/include/llvm/ADT/SmallSet.h
@@ -152,12 +152,12 @@ class SmallSet {
SmallSet(SmallSet &&) = default;
template <typename IterT> SmallSet(IterT Begin, IterT End) {
- this->insert(Begin, End);
+ insert(Begin, End);
}
template <typename RangeT>
explicit SmallSet(const iterator_range<RangeT> &R) {
- this->insert(R.begin(), R.end());
+ insert(R.begin(), R.end());
}
SmallSet(std::initializer_list<T> L) { this->insert(L.begin(), L.end()); }
diff --git a/llvm/unittests/ADT/SmallSetTest.cpp b/llvm/unittests/ADT/SmallSetTest.cpp
index 8219bf6f4b4c55..2feb0b1feb421b 100644
--- a/llvm/unittests/ADT/SmallSetTest.cpp
+++ b/llvm/unittests/ADT/SmallSetTest.cpp
@@ -12,49 +12,44 @@
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/STLExtras.h"
+#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string>
using namespace llvm;
TEST(SmallSetTest, ConstructorIteratorPair) {
- auto L = {1, 2, 3, 4, 5};
+ std::initializer_list<int> L = {1, 2, 3, 4, 5};
SmallSet<int, 4> S(std::begin(L), std::end(L));
- for (int Value : L)
- EXPECT_TRUE(S.contains(Value));
+ EXPECT_THAT(S, testing::UnorderedElementsAreArray(L));
}
TEST(SmallSet, ConstructorRange) {
- auto L = {1, 2, 3, 4, 5};
+ std::initializer_list<int> L = {1, 2, 3, 4, 5};
SmallSet<int, 4> S(llvm::make_range(std::begin(L), std::end(L)));
- for (int Value : L)
- EXPECT_TRUE(S.contains(Value));
+ EXPECT_THAT(S, testing::UnorderedElementsAreArray(L));
}
TEST(SmallSet, ConstructorInitializerList) {
- auto L = {1, 2, 3, 4, 5};
+ std::initializer_list<int> L = {1, 2, 3, 4, 5};
SmallSet<int, 4> S = {1, 2, 3, 4, 5};
- for (int Value : L)
- EXPECT_TRUE(S.contains(Value));
+ EXPECT_THAT(S, testing::UnorderedElementsAreArray(L));
}
TEST(SmallSet, CopyConstructor) {
SmallSet<int, 4> S = {1, 2, 3};
SmallSet<int, 4> T = S;
- EXPECT_EQ(S, T);
+ EXPECT_THAT(S, testing::ContainerEq(T));
}
TEST(SmallSet, MoveConstructor) {
- auto L = {1, 2, 3};
+ std::initializer_list<int> L = {1, 2, 3};
SmallSet<int, 4> S = L;
SmallSet<int, 4> T = std::move(S);
- EXPECT_TRUE(T.size() == L.size());
- for (int Value : L) {
- EXPECT_TRUE(T.contains(Value));
- }
+ EXPECT_THAT(T, testing::UnorderedElementsAreArray(L));
}
TEST(SmallSet, CopyAssignment) {
@@ -62,19 +57,16 @@ TEST(SmallSet, CopyAssignment) {
SmallSet<int, 4> T;
T = S;
- EXPECT_EQ(S, T);
+ EXPECT_THAT(S, testing::ContainerEq(T));
}
TEST(SmallSet, MoveAssignment) {
- auto L = {1, 2, 3};
+ std::initializer_list<int> L = {1, 2, 3};
SmallSet<int, 4> S = L;
SmallSet<int, 4> T;
T = std::move(S);
- EXPECT_TRUE(T.size() == L.size());
- for (int Value : L) {
- EXPECT_TRUE(T.contains(Value));
- }
+ EXPECT_THAT(T, testing::UnorderedElementsAreArray(L));
}
TEST(SmallSetTest, Insert) {
>From 44a223f597d13b6945b788657805ba6e4765b01f Mon Sep 17 00:00:00 2001
From: Victor Campos <victor.campos at arm.com>
Date: Wed, 25 Sep 2024 10:32:33 +0100
Subject: [PATCH 3/3] fixup! fixup! [ADT] Add more useful methods to SmallSet
API
---
llvm/include/llvm/ADT/SmallSet.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/SmallSet.h b/llvm/include/llvm/ADT/SmallSet.h
index 1b8ad542846630..97ea4c642bf556 100644
--- a/llvm/include/llvm/ADT/SmallSet.h
+++ b/llvm/include/llvm/ADT/SmallSet.h
@@ -160,7 +160,7 @@ class SmallSet {
insert(R.begin(), R.end());
}
- SmallSet(std::initializer_list<T> L) { this->insert(L.begin(), L.end()); }
+ SmallSet(std::initializer_list<T> L) { insert(L.begin(), L.end()); }
SmallSet &operator=(const SmallSet &) = default;
SmallSet &operator=(SmallSet &&) = default;
More information about the llvm-branch-commits
mailing list