[PATCH] D90882: [SmallVector] Add range versions of append/assign.

Sean Silva via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 5 13:58:56 PST 2020


silvas created this revision.
Herald added subscribers: llvm-commits, dexonsmith.
Herald added a project: LLVM.
silvas requested review of this revision.

These are convenient in a lot of cases, such as when the thing you want
to append/assign is `someReallyLongFunctionName()` that you'd rather not
write twice or assign to a variable for the paired begin/end calls.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90882

Files:
  llvm/include/llvm/ADT/SmallVector.h
  llvm/unittests/ADT/SmallVectorTest.cpp


Index: llvm/unittests/ADT/SmallVectorTest.cpp
===================================================================
--- llvm/unittests/ADT/SmallVectorTest.cpp
+++ llvm/unittests/ADT/SmallVectorTest.cpp
@@ -438,6 +438,20 @@
   this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7);
 }
 
+// Append container test.
+TYPED_TEST(SmallVectorTest, AppendContainerTest) {
+  SCOPED_TRACE("AppendContainerTest");
+
+  std::array<Constructable, 2> Array = {1, 2};
+  std::vector<Constructable> Vector = {3, 4};
+  ArrayRef<Constructable> TheArrayRef(Vector);
+
+  this->theVector.append(Array);
+  this->theVector.append(Vector);
+  this->theVector.append(TheArrayRef);
+  this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 3, 4);
+}
+
 struct output_iterator {
   typedef std::output_iterator_tag iterator_category;
   typedef int value_type;
@@ -484,6 +498,22 @@
   this->assertValuesInOrder(this->theVector, 2u, 7, 7);
 }
 
+// Assign container test.
+TYPED_TEST(SmallVectorTest, AssignContainerTest) {
+  SCOPED_TRACE("AssignContainerTest");
+
+  std::array<Constructable, 2> Array = {1, 2};
+  std::vector<Constructable> Vector = {3, 4};
+  ArrayRef<Constructable> TheArrayRef(Vector);
+
+  this->theVector.assign(Array);
+  this->assertValuesInOrder(this->theVector, 2u, 1, 2);
+  this->theVector.assign(Vector);
+  this->assertValuesInOrder(this->theVector, 2u, 3, 4);
+  this->theVector.assign(TheArrayRef);
+  this->assertValuesInOrder(this->theVector, 2u, 3, 4);
+}
+
 // Move-assign test
 TYPED_TEST(SmallVectorTest, MoveAssignTest) {
   SCOPED_TRACE("MoveAssignTest");
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -465,6 +465,10 @@
     append(IL.begin(), IL.end());
   }
 
+  template <typename ContainerTy> void append(ContainerTy &&C) {
+    append(C.begin(), C.end());
+  }
+
   // FIXME: Consider assigning over existing elements, rather than clearing &
   // re-initializing them - for all assign(...) variants.
 
@@ -490,6 +494,10 @@
     append(IL);
   }
 
+  template <typename ContainerTy> void assign(ContainerTy &&C) {
+    assign(C.begin(), C.end());
+  }
+
   iterator erase(const_iterator CI) {
     // Just cast away constness because this is a non-const member function.
     iterator I = const_cast<iterator>(CI);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90882.303256.patch
Type: text/x-patch
Size: 2392 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201105/18e3b857/attachment.bin>


More information about the llvm-commits mailing list