llvm.org GIT mirror llvm / d45f7b6
Bring the return value of SmallVector::insert in line with std::vector::insert. It always returns the iterator for the first inserted element, or the passed in iterator if the inserted range was empty. Flesh out the unit test more and fix all the cases it uncovered so far. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@158645 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 8 years ago
2 changed file(s) with 44 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
539539 }
540540
541541 iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
542 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
543 size_t InsertElt = I - this->begin();
544
542545 if (I == this->end()) { // Important special case for empty vector.
543546 append(NumToInsert, Elt);
544 return NumToInsert == 0 ? this->end() : this->end()-1;
545 }
546
547 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
548 size_t InsertElt = I - this->begin();
547 return this->begin()+InsertElt;
548 }
549549
550550 // Ensure there is enough space.
551551 reserve(static_cast(this->size() + NumToInsert));
587587
588588 template
589589 iterator insert(iterator I, ItTy From, ItTy To) {
590 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
591 size_t InsertElt = I - this->begin();
592
590593 if (I == this->end()) { // Important special case for empty vector.
591594 append(From, To);
592 return From == To ? this->end() : this->end()-1;
595 return this->begin()+InsertElt;
593596 }
594597
595598 size_t NumToInsert = std::distance(From, To);
596 // Convert iterator to elt# to avoid invalidating iterator when we reserve()
597 size_t InsertElt = I - this->begin();
598599
599600 // Ensure there is enough space.
600601 reserve(static_cast(this->size() + NumToInsert));
627628 this->uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
628629
629630 // Replace the overwritten part.
630 for (; NumOverwritten > 0; --NumOverwritten) {
631 *I = *From;
632 ++I; ++From;
631 for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
632 *J = *From;
633 ++J; ++From;
633634 }
634635
635636 // Insert the non-overwritten middle part.
341341 SCOPED_TRACE("InsertTest");
342342
343343 makeSequence(theVector, 1, 3);
344 theVector.insert(theVector.begin() + 1, Constructable(77));
344 VectorType::iterator I =
345 theVector.insert(theVector.begin() + 1, Constructable(77));
346 EXPECT_EQ(theVector.begin() + 1, I);
345347 assertValuesInOrder(theVector, 4u, 1, 77, 2, 3);
346348 }
347349
350352 SCOPED_TRACE("InsertRepeatedTest");
351353
352354 makeSequence(theVector, 10, 15);
353 theVector.insert(theVector.begin() + 1, 2, Constructable(16));
355 VectorType::iterator I =
356 theVector.insert(theVector.begin() + 1, 2, Constructable(16));
357 EXPECT_EQ(theVector.begin() + 1, I);
354358 assertValuesInOrder(theVector, 8u, 10, 16, 16, 11, 12, 13, 14, 15);
355359
360 // Insert at end.
361 I = theVector.insert(theVector.end(), 2, Constructable(16));
362 EXPECT_EQ(theVector.begin() + 8, I);
363 assertValuesInOrder(theVector, 10u, 10, 16, 16, 11, 12, 13, 14, 15, 16, 16);
364
365 // Empty insert.
356366 EXPECT_EQ(theVector.end(),
357367 theVector.insert(theVector.end(), 0, Constructable(42)));
368 EXPECT_EQ(theVector.begin() + 1,
369 theVector.insert(theVector.begin() + 1, 0, Constructable(42)));
358370 }
359371
360372 // Insert range.
361373 TEST_F(SmallVectorTest, InsertRangeTest) {
362 SCOPED_TRACE("InsertRepeatedTest");
363
364 makeSequence(theVector, 1, 3);
365 theVector.insert(theVector.begin() + 1, 3, Constructable(77));
374 SCOPED_TRACE("InsertRangeTest");
375
376 Constructable Arr[3] =
377 { Constructable(77), Constructable(77), Constructable(77) };
378
379 makeSequence(theVector, 1, 3);
380 VectorType::iterator I =
381 theVector.insert(theVector.begin() + 1, Arr, Arr+3);
382 EXPECT_EQ(theVector.begin() + 1, I);
366383 assertValuesInOrder(theVector, 6u, 1, 77, 77, 77, 2, 3);
367384
385 // Insert at end.
386 I = theVector.insert(theVector.end(), Arr, Arr+3);
387 EXPECT_EQ(theVector.begin() + 6, I);
388 assertValuesInOrder(theVector, 9u, 1, 77, 77, 77, 2, 3, 77, 77, 77);
389
390 // Empty insert.
368391 EXPECT_EQ(theVector.end(), theVector.insert(theVector.end(),
369392 theVector.begin(),
370393 theVector.begin()));
394 EXPECT_EQ(theVector.begin() + 1, theVector.insert(theVector.begin() + 1,
395 theVector.begin(),
396 theVector.begin()));
371397 }
372398
373399 // Comparison tests.