[PATCH] D91674: ADT: Share an implementation for single-element insert in SmallVector, NFC

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 17 21:22:25 PST 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rGe2537353e638: ADT: Share an implementation for single-element insert in SmallVector, NFC (authored by dexonsmith).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91674/new/

https://reviews.llvm.org/D91674

Files:
  llvm/include/llvm/ADT/SmallVector.h


Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -533,9 +533,10 @@
     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 @@
 
     // 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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91674.305969.patch
Type: text/x-patch
Size: 2219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201118/2ef841c5/attachment.bin>


More information about the llvm-commits mailing list