[ADT] Add initializer list support to SmallPtrSet so that sets can be

easily initialized with some initial values.

llvm-svn: 287610
This commit is contained in:
Chandler Carruth 2016-11-22 03:27:43 +00:00
parent 52ff6267aa
commit 3448ae5add
2 changed files with 27 additions and 4 deletions

View File

@ -22,6 +22,7 @@
#include <cstddef>
#include <cstring>
#include <cstdlib>
#include <initializer_list>
#include <iterator>
#include <utility>
@ -336,6 +337,10 @@ public:
insert(*I);
}
void insert(std::initializer_list<PtrType> IL) {
insert(IL.begin(), IL.end());
}
inline iterator begin() const {
return iterator(CurArray, EndPointer());
}
@ -374,6 +379,11 @@ public:
this->insert(I, E);
}
SmallPtrSet(std::initializer_list<PtrType> IL)
: BaseT(SmallStorage, SmallSizePowTwo) {
this->insert(IL.begin(), IL.end());
}
SmallPtrSet<PtrType, SmallSize> &
operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
if (&RHS != this)
@ -388,6 +398,13 @@ public:
return *this;
}
SmallPtrSet<PtrType, SmallSize> &
operator=(std::initializer_list<PtrType> IL) {
this->clear();
this->insert(IL.begin(), IL.end());
return *this;
}
/// swap - Swaps the elements of two sets.
void swap(SmallPtrSet<PtrType, SmallSize> &RHS) {
SmallPtrSetImplBase::swap(RHS);

View File

@ -21,10 +21,7 @@ TEST(SmallPtrSetTest, Assignment) {
for (int i = 0; i < 8; ++i)
buf[i] = 0;
SmallPtrSet<int *, 4> s1;
s1.insert(&buf[0]);
s1.insert(&buf[1]);
SmallPtrSet<int *, 4> s1 = {&buf[0], &buf[1]};
SmallPtrSet<int *, 4> s2;
(s2 = s1).insert(&buf[2]);
@ -38,6 +35,15 @@ TEST(SmallPtrSetTest, Assignment) {
EXPECT_TRUE(s1.count(&buf[i]));
else
EXPECT_FALSE(s1.count(&buf[i]));
// Assign and insert with initializer lists, and ones that contain both
// duplicates and out-of-order elements.
(s2 = {&buf[6], &buf[7], &buf[6]}).insert({&buf[5], &buf[4]});
for (int i = 0; i < 8; ++i)
if (i < 4)
EXPECT_FALSE(s2.count(&buf[i]));
else
EXPECT_TRUE(s2.count(&buf[i]));
}
TEST(SmallPtrSetTest, GrowthTest) {