diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -465,6 +465,10 @@ append(IL.begin(), IL.end()); } + template void append(ContainerTy &&C) { + append(C.begin(), C.end()); + } + // FIXME: Consider assigning over existing elements, rather than clearing & // re-initializing them - for all assign(...) variants. @@ -490,6 +494,10 @@ append(IL); } + template void assign(ContainerTy &&C) { + assign(C.begin(), C.end()); + } + iterator erase(const_iterator CI) { // Just cast away constness because this is a non-const member function. iterator I = const_cast(CI); diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp --- a/llvm/unittests/ADT/SmallVectorTest.cpp +++ b/llvm/unittests/ADT/SmallVectorTest.cpp @@ -438,6 +438,20 @@ this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7); } +// Append container test. +TYPED_TEST(SmallVectorTest, AppendContainerTest) { + SCOPED_TRACE("AppendContainerTest"); + + std::array Array = {1, 2}; + std::vector Vector = {3, 4}; + ArrayRef TheArrayRef(Vector); + + this->theVector.append(Array); + this->theVector.append(Vector); + this->theVector.append(TheArrayRef); + this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 3, 4); +} + struct output_iterator { typedef std::output_iterator_tag iterator_category; typedef int value_type; @@ -484,6 +498,22 @@ this->assertValuesInOrder(this->theVector, 2u, 7, 7); } +// Assign container test. +TYPED_TEST(SmallVectorTest, AssignContainerTest) { + SCOPED_TRACE("AssignContainerTest"); + + std::array Array = {1, 2}; + std::vector Vector = {3, 4}; + ArrayRef TheArrayRef(Vector); + + this->theVector.assign(Array); + this->assertValuesInOrder(this->theVector, 2u, 1, 2); + this->theVector.assign(Vector); + this->assertValuesInOrder(this->theVector, 2u, 3, 4); + this->theVector.assign(TheArrayRef); + this->assertValuesInOrder(this->theVector, 2u, 3, 4); +} + // Move-assign test TYPED_TEST(SmallVectorTest, MoveAssignTest) { SCOPED_TRACE("MoveAssignTest");