Reapply r66415, which was reverted in r66426 for
[oota-llvm.git] / include / llvm / ADT / SmallVector.h
index c981d5756b3af2aa8f882f32bd3d79e30fc87b29..445f99190085106c7a2acece129332c49b549e57 100644 (file)
@@ -17,9 +17,9 @@
 #include "llvm/ADT/iterator.h"
 #include "llvm/Support/type_traits.h"
 #include <algorithm>
+#include <cassert>
 #include <cstring>
 #include <memory>
-#include <cassert>
 
 #ifdef _MSC_VER
 namespace std {
@@ -293,10 +293,11 @@ public:
     // Uninvalidate the iterator.
     I = begin()+InsertElt;
 
-    // If we already have this many elements in the collection, append the
-    // dest elements at the end, then copy over the appropriate elements.  Since
-    // we already reserved space, we know that this won't reallocate the vector.
-    if (size() >= NumToInsert) {
+    // If there are more elements between the insertion point and the end of the
+    // range than there are being inserted, we can use a simple approach to
+    // insertion.  Since we already reserved space, we know that this won't
+    // reallocate the vector.
+    if (size_t(end()-I) >= NumToInsert) {
       T *OldEnd = End;
       append(End-NumToInsert, End);
 
@@ -341,10 +342,11 @@ public:
     // Uninvalidate the iterator.
     I = begin()+InsertElt;
 
-    // If we already have this many elements in the collection, append the
-    // dest elements at the end, then copy over the appropriate elements.  Since
-    // we already reserved space, we know that this won't reallocate the vector.
-    if (size() >= NumToInsert) {
+    // If there are more elements between the insertion point and the end of the
+    // range than there are being inserted, we can use a simple approach to
+    // insertion.  Since we already reserved space, we know that this won't
+    // reallocate the vector.
+    if (size_t(end()-I) >= NumToInsert) {
       T *OldEnd = End;
       append(End-NumToInsert, End);