1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

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
This commit is contained in:
Duncan P. N. Exon Smith 2020-11-17 18:06:21 -08:00 committed by Duncan P. N. Exon Smith
parent 8aa150edd0
commit d59bc177d6

View File

@ -533,9 +533,10 @@ public:
return(N);
}
iterator insert(iterator I, T &&Elt) {
private:
template <class ArgType> 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<ArgType>(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<ArgType> *EltPtr = &Elt;
if (I <= EltPtr && EltPtr < this->end())
++EltPtr;
*I = ::std::move(*EltPtr);
*I = ::std::forward<ArgType>(*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();