<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 2, 2014 at 8:45 PM, Duncan P. N. Exon Smith <span dir="ltr"><<a href="mailto:dexonsmith@apple.com" target="_blank">dexonsmith@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: dexonsmith<br>
Date: Tue Dec 2 22:45:09 2014<br>
New Revision: 223201<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=223201&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=223201&view=rev</a><br>
Log:<br>
ADT: Add SmallVector<>::emplace_back()<br>
<br>
Modified:<br>
llvm/trunk/include/llvm/ADT/SmallVector.h<br>
llvm/trunk/unittests/ADT/SmallVectorTest.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ADT/SmallVector.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallVector.h?rev=223201&r1=223200&r2=223201&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallVector.h?rev=223201&r1=223200&r2=223201&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ADT/SmallVector.h (original)<br>
+++ llvm/trunk/include/llvm/ADT/SmallVector.h Tue Dec 2 22:45:09 2014<br>
@@ -236,6 +236,51 @@ public:<br>
this->setEnd(this->end()-1);<br>
this->end()->~T();<br>
}<br>
+<br>
+#if LLVM_HAS_VARIADIC_TEMPLATES<br>
+ template <typename... ArgTypes> void emplace_back(ArgTypes &&... Args) {<br>
+ if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))<br>
+ this->grow();<br>
+ ::new ((void *)this->end()) T(std::forward<ArgTypes>(Args)...);<br>
+ this->setEnd(this->end() + 1);<br>
+ }<br>
+#else<br>
+private:<br>
+ template <typename Constructor> emplace_back_impl(Constructor emplace) {<br></blockquote><div><br>wow, calling that functor 'emplace' confused me for quite a while (I was wondering why we had an emplace function that took a void* and didn't update the end function) - at some point we might grow SmallVector::emplace to match std::vector::emplace (emplace at a specified location, rather than the back) - so it might be best not to use that name in this context.<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))<br>
+ this->grow();<br></blockquote><div><br>This can be tricky - I seem to recall seeing some previous changes being careful about allowing, I think it was, insertion of a value referenced within the container being inserted into. That has to carefully split the grow operation - allocating the new buffer, constructing the new element, then moving/copying the old elements over.<br><br>I don't know if the C++ standard guarantees that, say, v.emplace_back(v[0]), is required to be valid, or if we should bother making that valid, but it's a thing to think about.<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ emplace((void *)this->end());<br>
+ this->setEnd(this->end() + 1);<br>
+ }<br>
+<br>
+public:<br>
+ void emplace_back() {<br>
+ emplace_back_impl([](void *Mem) { ::new (Mem) T(); });<br>
+ }<br>
+ template <typename T1> void emplace_back(T1 &&A1) {<br>
+ emplace_back_impl([&](void *Mem) { ::new (Mem) T(std::forward<T1>(A1)); });<br>
+ }<br>
+ template <typename T1, typename T2> void emplace_back(T1 &&A1, T2 &&A2) {<br>
+ emplace_back_impl([&](void *Mem) {<br>
+ ::new (Mem) T(std::forward<T1>(A1), std::forward<T2>(A2));<br>
+ });<br>
+ }<br>
+ template <typename T1, typename T2, typename T3><br>
+ void emplace_back(T1 &&A1, T2 &&A2, T3 &&A3) {<br>
+ T(std::forward<T1>(A1), std::forward<T2>(A2), std::forward<T3>(A3));<br>
+ emplace_back_impl([&](void *Mem) {<br>
+ ::new (Mem)<br>
+ T(std::forward<T1>(A1), std::forward<T2>(A2), std::forward<T3>(A3));<br>
+ });<br>
+ }<br>
+ template <typename T1, typename T2, typename T3, typename T4><br>
+ void emplace_back(T1 &&A1, T2 &&A2, T3 &&A3, T4 &&A4) {<br>
+ emplace_back_impl([&](void *Mem) {<br>
+ ::new (Mem) T(std::forward<T1>(A1), std::forward<T2>(A2),<br>
+ std::forward<T3>(A3), std::forward<T4>(A4));<br>
+ });<br>
+ }<br>
+#endif // LLVM_HAS_VARIADIC_TEMPLATES<br>
};<br>
<br>
// Define this out-of-line to dissuade the C++ compiler from inlining it.<br>
<br>
Modified: llvm/trunk/unittests/ADT/SmallVectorTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallVectorTest.cpp?rev=223201&r1=223200&r2=223201&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallVectorTest.cpp?rev=223201&r1=223200&r2=223201&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/ADT/SmallVectorTest.cpp (original)<br>
+++ llvm/trunk/unittests/ADT/SmallVectorTest.cpp Tue Dec 2 22:45:09 2014<br>
@@ -699,4 +699,135 @@ TEST(SmallVectorTest, MidInsert) {<br>
EXPECT_TRUE(m.hasValue);<br>
}<br>
<br>
+enum EmplaceableArgState {<br>
+ EAS_Defaulted,<br>
+ EAS_Arg,<br>
+ EAS_LValue,<br>
+ EAS_RValue,<br>
+ EAS_Failure<br>
+};<br>
+template <int I> struct EmplaceableArg {<br>
+ EmplaceableArgState State;<br>
+ EmplaceableArg() : State(EAS_Defaulted) {}<br>
+ EmplaceableArg(EmplaceableArg &&X)<br>
+ : State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {}<br>
+ EmplaceableArg(EmplaceableArg &X)<br>
+ : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}<br>
+<br>
+ explicit EmplaceableArg(bool) : State(EAS_Arg) {}<br>
+<br>
+private:<br>
+ EmplaceableArg(const EmplaceableArg &X) LLVM_DELETED_FUNCTION;<br>
+ EmplaceableArg &operator=(EmplaceableArg &&) LLVM_DELETED_FUNCTION;<br>
+ EmplaceableArg &operator=(const EmplaceableArg &) LLVM_DELETED_FUNCTION;<br>
+};<br>
+<br>
+enum EmplaceableState { ES_Emplaced, ES_Moved };<br>
+struct Emplaceable {<br>
+ EmplaceableArg<0> A0;<br>
+ EmplaceableArg<1> A1;<br>
+ EmplaceableArg<2> A2;<br>
+ EmplaceableArg<3> A3;<br>
+ EmplaceableState State;<br>
+<br>
+ Emplaceable() : State(ES_Emplaced) {}<br>
+<br>
+ template <class A0Ty><br>
+ explicit Emplaceable(A0Ty &&A0)<br>
+ : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}<br>
+<br>
+ template <class A0Ty, class A1Ty><br>
+ Emplaceable(A0Ty &&A0, A1Ty &&A1)<br>
+ : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),<br>
+ State(ES_Emplaced) {}<br>
+<br>
+ template <class A0Ty, class A1Ty, class A2Ty><br>
+ Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2)<br>
+ : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),<br>
+ A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {}<br>
+<br>
+ template <class A0Ty, class A1Ty, class A2Ty, class A3Ty><br>
+ Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3)<br>
+ : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),<br>
+ A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)),<br>
+ State(ES_Emplaced) {}<br>
+<br>
+ Emplaceable(Emplaceable &&) : State(ES_Moved) {}<br>
+ Emplaceable &operator=(Emplaceable &&) {<br>
+ State = ES_Moved;<br>
+ return *this;<br>
+ }<br>
+<br>
+private:<br>
+ Emplaceable(const Emplaceable &) LLVM_DELETED_FUNCTION;<br>
+ Emplaceable &operator=(const Emplaceable &) LLVM_DELETED_FUNCTION;<br>
+};<br>
+<br>
+TEST(SmallVectorTest, EmplaceBack) {<br>
+ EmplaceableArg<0> A0(true);<br>
+ EmplaceableArg<1> A1(true);<br>
+ EmplaceableArg<2> A2(true);<br>
+ EmplaceableArg<3> A3(true);<br>
+ {<br>
+ SmallVector<Emplaceable, 3> V;<br>
+ V.emplace_back();<br>
+ EXPECT_TRUE(V.size() == 1);<br>
+ EXPECT_TRUE(V.back().State == ES_Emplaced);<br>
+ EXPECT_TRUE(V.back().A0.State == EAS_Defaulted);<br>
+ EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);<br>
+ EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);<br>
+ EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);<br>
+ }<br>
+ {<br>
+ SmallVector<Emplaceable, 3> V;<br>
+ V.emplace_back(std::move(A0));<br>
+ EXPECT_TRUE(V.size() == 1);<br>
+ EXPECT_TRUE(V.back().State == ES_Emplaced);<br>
+ EXPECT_TRUE(V.back().A0.State == EAS_RValue);<br>
+ EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);<br>
+ EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);<br>
+ EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);<br>
+ }<br>
+ {<br>
+ SmallVector<Emplaceable, 3> V;<br>
+ V.emplace_back(A0);<br>
+ EXPECT_TRUE(V.size() == 1);<br>
+ EXPECT_TRUE(V.back().State == ES_Emplaced);<br>
+ EXPECT_TRUE(V.back().A0.State == EAS_LValue);<br>
+ EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);<br>
+ EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);<br>
+ EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);<br>
+ }<br>
+ {<br>
+ SmallVector<Emplaceable, 3> V;<br>
+ V.emplace_back(A0, A1);<br>
+ EXPECT_TRUE(V.size() == 1);<br>
+ EXPECT_TRUE(V.back().State == ES_Emplaced);<br>
+ EXPECT_TRUE(V.back().A0.State == EAS_LValue);<br>
+ EXPECT_TRUE(V.back().A1.State == EAS_LValue);<br>
+ EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);<br>
+ EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);<br>
+ }<br>
+ {<br>
+ SmallVector<Emplaceable, 3> V;<br>
+ V.emplace_back(std::move(A0), std::move(A1));<br>
+ EXPECT_TRUE(V.size() == 1);<br>
+ EXPECT_TRUE(V.back().State == ES_Emplaced);<br>
+ EXPECT_TRUE(V.back().A0.State == EAS_RValue);<br>
+ EXPECT_TRUE(V.back().A1.State == EAS_RValue);<br>
+ EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);<br>
+ EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);<br>
+ }<br>
+ {<br>
+ SmallVector<Emplaceable, 3> V;<br>
+ V.emplace_back(std::move(A0), A1, std::move(A2), A3);<br>
+ EXPECT_TRUE(V.size() == 1);<br>
+ EXPECT_TRUE(V.back().State == ES_Emplaced);<br>
+ EXPECT_TRUE(V.back().A0.State == EAS_RValue);<br>
+ EXPECT_TRUE(V.back().A1.State == EAS_LValue);<br>
+ EXPECT_TRUE(V.back().A2.State == EAS_RValue);<br>
+ EXPECT_TRUE(V.back().A3.State == EAS_LValue);<br>
+ }<br>
}<br>
+<br>
+} // end namespace<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>