SmallVector: return a valid iterator for the rare case of inserting an empty range into a SmallVector.

Patch by Johannes Schaub!

llvm-svn: 158643
This commit is contained in:
Benjamin Kramer 2012-06-17 11:52:22 +00:00
parent 7a39ad6444
commit 371b9b0e99
2 changed files with 9 additions and 2 deletions

View File

@ -542,7 +542,7 @@ public:
iterator insert(iterator I, size_type NumToInsert, const T &Elt) { iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
if (I == this->end()) { // Important special case for empty vector. if (I == this->end()) { // Important special case for empty vector.
append(NumToInsert, Elt); append(NumToInsert, Elt);
return this->end()-1; return NumToInsert == 0 ? this->end() : this->end()-1;
} }
// Convert iterator to elt# to avoid invalidating iterator when we reserve() // Convert iterator to elt# to avoid invalidating iterator when we reserve()
@ -590,7 +590,7 @@ public:
iterator insert(iterator I, ItTy From, ItTy To) { iterator insert(iterator I, ItTy From, ItTy To) {
if (I == this->end()) { // Important special case for empty vector. if (I == this->end()) { // Important special case for empty vector.
append(From, To); append(From, To);
return this->end()-1; return From == To ? this->end() : this->end()-1;
} }
size_t NumToInsert = std::distance(From, To); size_t NumToInsert = std::distance(From, To);

View File

@ -353,6 +353,9 @@ TEST_F(SmallVectorTest, InsertRepeatedTest) {
makeSequence(theVector, 10, 15); makeSequence(theVector, 10, 15);
theVector.insert(theVector.begin() + 1, 2, Constructable(16)); theVector.insert(theVector.begin() + 1, 2, Constructable(16));
assertValuesInOrder(theVector, 8u, 10, 16, 16, 11, 12, 13, 14, 15); assertValuesInOrder(theVector, 8u, 10, 16, 16, 11, 12, 13, 14, 15);
EXPECT_EQ(theVector.end(),
theVector.insert(theVector.end(), 0, Constructable(42)));
} }
// Insert range. // Insert range.
@ -362,6 +365,10 @@ TEST_F(SmallVectorTest, InsertRangeTest) {
makeSequence(theVector, 1, 3); makeSequence(theVector, 1, 3);
theVector.insert(theVector.begin() + 1, 3, Constructable(77)); theVector.insert(theVector.begin() + 1, 3, Constructable(77));
assertValuesInOrder(theVector, 6u, 1, 77, 77, 77, 2, 3); assertValuesInOrder(theVector, 6u, 1, 77, 77, 77, 2, 3);
EXPECT_EQ(theVector.end(), theVector.insert(theVector.end(),
theVector.begin(),
theVector.begin()));
} }
// Comparison tests. // Comparison tests.