diff --git a/include/llvm/ADT/SmallVector.h b/include/llvm/ADT/SmallVector.h index 14e2c7bbcb3..5b208b76a21 100644 --- a/include/llvm/ADT/SmallVector.h +++ b/include/llvm/ADT/SmallVector.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -432,6 +433,10 @@ public: this->setEnd(this->end() + NumInputs); } + void append(std::initializer_list IL) { + append(IL.begin(), IL.end()); + } + void assign(size_type NumElts, const T &Elt) { clear(); if (this->capacity() < NumElts) @@ -440,6 +445,11 @@ public: std::uninitialized_fill(this->begin(), this->end(), Elt); } + void assign(std::initializer_list IL) { + clear(); + append(IL); + } + iterator erase(iterator I) { assert(I >= this->begin() && "Iterator to erase is out of bounds."); assert(I < this->end() && "Erasing at past-the-end iterator."); @@ -633,6 +643,10 @@ public: return I; } + void insert(iterator I, std::initializer_list IL) { + insert(I, IL.begin(), IL.end()); + } + template void emplace_back(ArgTypes &&... Args) { if (LLVM_UNLIKELY(this->EndX >= this->CapacityX)) this->grow(); @@ -865,6 +879,10 @@ public: this->append(R.begin(), R.end()); } + SmallVector(std::initializer_list IL) : SmallVectorImpl(N) { + this->assign(IL); + } + SmallVector(const SmallVector &RHS) : SmallVectorImpl(N) { if (!RHS.empty()) SmallVectorImpl::operator=(RHS); @@ -895,6 +913,10 @@ public: return *this; } + const SmallVector &operator=(std::initializer_list IL) { + this->assign(IL); + return *this; + } }; template diff --git a/unittests/ADT/SmallVectorTest.cpp b/unittests/ADT/SmallVectorTest.cpp index 2bbb4ed8438..97ff90b78f0 100644 --- a/unittests/ADT/SmallVectorTest.cpp +++ b/unittests/ADT/SmallVectorTest.cpp @@ -11,6 +11,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Compiler.h" #include "gtest/gtest.h" @@ -906,4 +907,22 @@ TEST(SmallVectorTest, EmplaceBack) { } } +TEST(SmallVectorTest, InitializerList) { + SmallVector V1 = {}; + EXPECT_TRUE(V1.empty()); + V1 = {0, 0}; + EXPECT_TRUE(makeArrayRef(V1).equals({0, 0})); + V1 = {-1, -1}; + EXPECT_TRUE(makeArrayRef(V1).equals({-1, -1})); + + SmallVector V2 = {1, 2, 3, 4}; + EXPECT_TRUE(makeArrayRef(V2).equals({1, 2, 3, 4})); + V2.assign({4}); + EXPECT_TRUE(makeArrayRef(V2).equals({4})); + V2.append({3, 2}); + EXPECT_TRUE(makeArrayRef(V2).equals({4, 3, 2})); + V2.insert(V2.begin() + 1, 5); + EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2})); +} + } // end namespace