Skip to content

Commit 12b657a

Browse files
committed
[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.
1 parent 8a9e20e commit 12b657a

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-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+
this->insert(Begin, End);
156+
}
157+
158+
template <typename RangeT>
159+
explicit SmallSet(const iterator_range<RangeT> &R) {
160+
this->insert(R.begin(), R.end());
161+
}
162+
163+
SmallSet(std::initializer_list<T> L) { this->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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,66 @@
1717

1818
using namespace llvm;
1919

20+
TEST(SmallSetTest, ConstructorIteratorPair) {
21+
auto L = {1, 2, 3, 4, 5};
22+
SmallSet<int, 4> S(std::begin(L), std::end(L));
23+
for (int Value : L)
24+
EXPECT_TRUE(S.contains(Value));
25+
}
26+
27+
TEST(SmallSet, ConstructorRange) {
28+
auto L = {1, 2, 3, 4, 5};
29+
30+
SmallSet<int, 4> S(llvm::make_range(std::begin(L), std::end(L)));
31+
for (int Value : L)
32+
EXPECT_TRUE(S.contains(Value));
33+
}
34+
35+
TEST(SmallSet, ConstructorInitializerList) {
36+
auto L = {1, 2, 3, 4, 5};
37+
SmallSet<int, 4> S = {1, 2, 3, 4, 5};
38+
for (int Value : L)
39+
EXPECT_TRUE(S.contains(Value));
40+
}
41+
42+
TEST(SmallSet, CopyConstructor) {
43+
SmallSet<int, 4> S = {1, 2, 3};
44+
SmallSet<int, 4> T = S;
45+
46+
EXPECT_EQ(S, T);
47+
}
48+
49+
TEST(SmallSet, MoveConstructor) {
50+
auto L = {1, 2, 3};
51+
SmallSet<int, 4> S = L;
52+
SmallSet<int, 4> T = std::move(S);
53+
54+
EXPECT_TRUE(T.size() == L.size());
55+
for (int Value : L) {
56+
EXPECT_TRUE(T.contains(Value));
57+
}
58+
}
59+
60+
TEST(SmallSet, CopyAssignment) {
61+
SmallSet<int, 4> S = {1, 2, 3};
62+
SmallSet<int, 4> T;
63+
T = S;
64+
65+
EXPECT_EQ(S, T);
66+
}
67+
68+
TEST(SmallSet, MoveAssignment) {
69+
auto L = {1, 2, 3};
70+
SmallSet<int, 4> S = L;
71+
SmallSet<int, 4> T;
72+
T = std::move(S);
73+
74+
EXPECT_TRUE(T.size() == L.size());
75+
for (int Value : L) {
76+
EXPECT_TRUE(T.contains(Value));
77+
}
78+
}
79+
2080
TEST(SmallSetTest, Insert) {
2181

2282
SmallSet<int, 4> s1;

0 commit comments

Comments
 (0)