Skip to content

Commit 9ba78f4

Browse files
vhscamposSterling-Augustine
authored andcommitted
[ADT] Add more useful methods to SmallSet API (llvm#108601)
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.
1 parent be2f01e commit 9ba78f4

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

llvm/include/llvm/ADT/SmallSet.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/iterator.h"
2020
#include <cstddef>
2121
#include <functional>
22+
#include <initializer_list>
2223
#include <set>
2324
#include <utility>
2425

@@ -147,6 +148,22 @@ class SmallSet {
147148
using const_iterator = SmallSetIterator<T, N, C>;
148149

149150
SmallSet() = default;
151+
SmallSet(const SmallSet &) = default;
152+
SmallSet(SmallSet &&) = default;
153+
154+
template <typename IterT> SmallSet(IterT Begin, IterT End) {
155+
insert(Begin, End);
156+
}
157+
158+
template <typename RangeT>
159+
explicit SmallSet(const iterator_range<RangeT> &R) {
160+
insert(R.begin(), R.end());
161+
}
162+
163+
SmallSet(std::initializer_list<T> L) { insert(L.begin(), L.end()); }
164+
165+
SmallSet &operator=(const SmallSet &) = default;
166+
SmallSet &operator=(SmallSet &&) = default;
150167

151168
[[nodiscard]] bool empty() const { return Vector.empty() && Set.empty(); }
152169

llvm/unittests/ADT/SmallSetTest.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,63 @@
1212

1313
#include "llvm/ADT/SmallSet.h"
1414
#include "llvm/ADT/STLExtras.h"
15+
#include "gmock/gmock.h"
1516
#include "gtest/gtest.h"
1617
#include <string>
1718

1819
using namespace llvm;
1920

21+
TEST(SmallSetTest, ConstructorIteratorPair) {
22+
std::initializer_list<int> L = {1, 2, 3, 4, 5};
23+
SmallSet<int, 4> S(std::begin(L), std::end(L));
24+
EXPECT_THAT(S, testing::UnorderedElementsAreArray(L));
25+
}
26+
27+
TEST(SmallSet, ConstructorRange) {
28+
std::initializer_list<int> L = {1, 2, 3, 4, 5};
29+
30+
SmallSet<int, 4> S(llvm::make_range(std::begin(L), std::end(L)));
31+
EXPECT_THAT(S, testing::UnorderedElementsAreArray(L));
32+
}
33+
34+
TEST(SmallSet, ConstructorInitializerList) {
35+
std::initializer_list<int> L = {1, 2, 3, 4, 5};
36+
SmallSet<int, 4> S = {1, 2, 3, 4, 5};
37+
EXPECT_THAT(S, testing::UnorderedElementsAreArray(L));
38+
}
39+
40+
TEST(SmallSet, CopyConstructor) {
41+
SmallSet<int, 4> S = {1, 2, 3};
42+
SmallSet<int, 4> T = S;
43+
44+
EXPECT_THAT(S, testing::ContainerEq(T));
45+
}
46+
47+
TEST(SmallSet, MoveConstructor) {
48+
std::initializer_list<int> L = {1, 2, 3};
49+
SmallSet<int, 4> S = L;
50+
SmallSet<int, 4> T = std::move(S);
51+
52+
EXPECT_THAT(T, testing::UnorderedElementsAreArray(L));
53+
}
54+
55+
TEST(SmallSet, CopyAssignment) {
56+
SmallSet<int, 4> S = {1, 2, 3};
57+
SmallSet<int, 4> T;
58+
T = S;
59+
60+
EXPECT_THAT(S, testing::ContainerEq(T));
61+
}
62+
63+
TEST(SmallSet, MoveAssignment) {
64+
std::initializer_list<int> L = {1, 2, 3};
65+
SmallSet<int, 4> S = L;
66+
SmallSet<int, 4> T;
67+
T = std::move(S);
68+
69+
EXPECT_THAT(T, testing::UnorderedElementsAreArray(L));
70+
}
71+
2072
TEST(SmallSetTest, Insert) {
2173

2274
SmallSet<int, 4> s1;

0 commit comments

Comments
 (0)