Skip to content

[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 3 commits into from
Sep 27, 2024

Conversation

vhscampos
Copy link
Member

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.

@llvmbot
Copy link
Member

llvmbot commented Sep 13, 2024

@llvm/pr-subscribers-llvm-adt

Author: Victor Campos (vhscampos)

Changes

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.

Full diff: https://github.com/llvm/llvm-project/pull/108601.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/SmallSet.h (+18-3)
  • (modified) llvm/unittests/ADT/SmallSetTest.cpp (+60)
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;

@vhscampos vhscampos requested a review from kuhar September 13, 2024 16:26
@vhscampos vhscampos force-pushed the users/vhscampos/smallset-insert-perfect-forwarding branch from e9587c0 to 8a9e20e Compare September 24, 2024 09:39
@vhscampos vhscampos force-pushed the users/vhscampos/smallset-api-more-methods branch from 09a3542 to 12b657a Compare September 24, 2024 09:39
Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vhscampos vhscampos force-pushed the users/vhscampos/smallset-insert-perfect-forwarding branch from 8a9e20e to 8605d54 Compare September 25, 2024 10:27
@vhscampos vhscampos force-pushed the users/vhscampos/smallset-api-more-methods branch from 44a223f to 5cd8957 Compare September 25, 2024 10:28
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.
@vhscampos vhscampos force-pushed the users/vhscampos/smallset-api-more-methods branch from 5cd8957 to c920940 Compare September 25, 2024 12:07
@vhscampos vhscampos merged commit 0c6ee1f into main Sep 27, 2024
5 of 8 checks passed
@vhscampos vhscampos deleted the users/vhscampos/smallset-api-more-methods branch September 27, 2024 12:14
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
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants