-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ADT] Add more useful methods to SmallSet API #108601
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
Conversation
This file contains hidden or 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
@llvm/pr-subscribers-llvm-adt Author: Victor Campos (vhscampos) ChangesThis patch adds useful methods to the SmallSet API:
Full diff: https://github.com/llvm/llvm-project/pull/108601.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/SmallSet.h b/llvm/include/llvm/ADT/SmallSet.h
index c8641111eda8f8..2e26ad65eb2dd6 100644
--- a/llvm/include/llvm/ADT/SmallSet.h
+++ b/llvm/include/llvm/ADT/SmallSet.h
@@ -14,14 +14,13 @@
#ifndef LLVM_ADT_SMALLSET_H
#define LLVM_ADT_SMALLSET_H
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/iterator.h"
-#include "llvm/Support/Compiler.h"
-#include "llvm/Support/type_traits.h"
#include <cstddef>
#include <functional>
+#include <initializer_list>
#include <set>
#include <type_traits>
#include <utility>
@@ -151,6 +150,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 b50b368ae66361..ced1ba5dce34d8 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;
|
e9587c0
to
8a9e20e
Compare
09a3542
to
12b657a
Compare
kuhar
reviewed
Sep 24, 2024
kuhar
approved these changes
Sep 24, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
8a9e20e
to
8605d54
Compare
44a223f
to
5cd8957
Compare
Base automatically changed from
users/vhscampos/smallset-insert-perfect-forwarding
to
main
September 25, 2024 11:43
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.
5cd8957
to
c920940
Compare
Sterling-Augustine
pushed a commit
to Sterling-Augustine/llvm-project
that referenced
this pull request
Sep 27, 2024
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.
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.
This patch adds useful methods to the SmallSet API: