From: Michael Gottesman Date: Wed, 31 Dec 2014 23:33:24 +0000 (+0000) Subject: Add 2x constructors for TinyPtrVector, one that takes in one elemenet and the other... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=69338a993e43d1be9b7cc05c11cb95e134ca3cdb;p=llvm Add 2x constructors for TinyPtrVector, one that takes in one elemenet and the other that takes in an ArrayRef Currently one can only construct an empty TinyPtrVector. These are just missing elements of the API. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225055 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/TinyPtrVector.h b/include/llvm/ADT/TinyPtrVector.h index e158f9d278b..15137f5ebf8 100644 --- a/include/llvm/ADT/TinyPtrVector.h +++ b/include/llvm/ADT/TinyPtrVector.h @@ -96,6 +96,13 @@ public: return *this; } + /// Constructor from a single element. + explicit TinyPtrVector(EltTy Elt) : Val(Elt) {} + + /// Constructor from an ArrayRef. + explicit TinyPtrVector(ArrayRef Elts) + : Val(new VecTy(Elts.begin(), Elts.end())) {} + // implicit conversion operator to ArrayRef. operator ArrayRef() const { if (Val.isNull()) diff --git a/unittests/ADT/TinyPtrVectorTest.cpp b/unittests/ADT/TinyPtrVectorTest.cpp index ec868d4258a..a402e8be9df 100644 --- a/unittests/ADT/TinyPtrVectorTest.cpp +++ b/unittests/ADT/TinyPtrVectorTest.cpp @@ -412,3 +412,29 @@ TYPED_TEST(TinyPtrVectorTest, InsertRange) { } } + +TEST(TinyPtrVectorTest, SingleEltCtorTest) { + int v = 55; + TinyPtrVector V(&v); + + EXPECT_TRUE(V.size() == 1); + EXPECT_FALSE(V.empty()); + EXPECT_TRUE(V.front() == &v); +} + +TEST(TinyPtrVectorTest, ArrayRefCtorTest) { + int data_array[128]; + std::vector data; + + for (unsigned i = 0, e = 128; i != e; ++i) { + data_array[i] = 324 - int(i); + data.push_back(&data_array[i]); + } + + TinyPtrVector V(data); + EXPECT_TRUE(V.size() == 128); + EXPECT_FALSE(V.empty()); + for (unsigned i = 0, e = 128; i != e; ++i) { + EXPECT_TRUE(V[i] == data[i]); + } +}