From e2537353e63859a2a36da93fa97cb16275681a23 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Tue, 17 Nov 2020 18:06:21 -0800 Subject: [PATCH] ADT: Share an implementation for single-element insert in SmallVector, NFC Factor out `SmallVectorImple::insert_one_impl`, a common implementation for `insert(iterator, T&&)` and `insert(iterator, T const&)`. This is just a clean up and has no functionality change. Differential Revision: https://reviews.llvm.org/D91674 --- llvm/include/llvm/ADT/SmallVector.h | 41 +++++++---------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h index fbc8ede255d2..ccf36c91d9ac 100644 --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -533,9 +533,10 @@ public: return(N); } - iterator insert(iterator I, T &&Elt) { +private: + template iterator insert_one_impl(iterator I, ArgType &&Elt) { if (I == this->end()) { // Important special case for empty vector. - this->push_back(::std::move(Elt)); + this->push_back(::std::forward(Elt)); return this->end()-1; } @@ -555,43 +556,21 @@ public: // If we just moved the element we're inserting, be sure to update // the reference. - T *EltPtr = &Elt; + std::remove_reference_t *EltPtr = &Elt; if (I <= EltPtr && EltPtr < this->end()) ++EltPtr; - *I = ::std::move(*EltPtr); + *I = ::std::forward(*EltPtr); return I; } - iterator insert(iterator I, const T &Elt) { - if (I == this->end()) { // Important special case for empty vector. - this->push_back(Elt); - return this->end()-1; - } - - assert(I >= this->begin() && "Insertion iterator is out of bounds."); - assert(I <= this->end() && "Inserting past the end of the vector."); - - if (this->size() >= this->capacity()) { - size_t EltNo = I-this->begin(); - this->grow(); - I = this->begin()+EltNo; - } - ::new ((void*) this->end()) T(std::move(this->back())); - // Push everything else over. - std::move_backward(I, this->end()-1, this->end()); - this->set_size(this->size() + 1); - - // If we just moved the element we're inserting, be sure to update - // the reference. - const T *EltPtr = &Elt; - if (I <= EltPtr && EltPtr < this->end()) - ++EltPtr; - - *I = *EltPtr; - return I; +public: + iterator insert(iterator I, T &&Elt) { + return insert_one_impl(I, std::move(Elt)); } + iterator insert(iterator I, const T &Elt) { return insert_one_impl(I, Elt); } + iterator insert(iterator I, size_type NumToInsert, const T &Elt) { // Convert iterator to elt# to avoid invalidating iterator when we reserve() size_t InsertElt = I - this->begin();