[llvm] [ADT] Add SmallVectorImpl::append_range (PR #93384)

via llvm-commits llvm-commits at lists.llvm.org
Sat May 25 13:23:42 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>

The new function is modeled after std::vector::append_range from
C++23.

---
Full diff: https://github.com/llvm/llvm-project/pull/93384.diff


2 Files Affected:

- (modified) llvm/include/llvm/ADT/SmallVector.h (+4) 
- (modified) llvm/unittests/ADT/SmallVectorTest.cpp (+13) 


``````````diff
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 09676d792dfeb..a012165a52aa5 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -701,6 +701,10 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
     this->set_size(this->size() + NumInputs);
   }
 
+  template <typename RangeTy> void append_range(RangeTy &&R) {
+    this->append(R.begin(), R.end());
+  }
+
   /// Append \p NumInputs copies of \p Elt to the end.
   void append(size_type NumInputs, ValueParamT Elt) {
     const T *EltPtr = this->reserveForParamAndGetAddress(Elt, NumInputs);
diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp
index 137dd43b47306..d5c5b7619d525 100644
--- a/llvm/unittests/ADT/SmallVectorTest.cpp
+++ b/llvm/unittests/ADT/SmallVectorTest.cpp
@@ -517,6 +517,19 @@ TYPED_TEST(SmallVectorTest, AppendTest) {
   assertValuesInOrder(V, 3u, 1, 2, 3);
 }
 
+// Append range test
+TYPED_TEST(SmallVectorTest, AppendRangeTest) {
+  SCOPED_TRACE("AppendRangeTest");
+  auto &V = this->theVector;
+  auto &U = this->otherVector;
+  makeSequence(U, 5, 6);
+
+  V.push_back(Constructable(4));
+  V.append_range(U);
+
+  assertValuesInOrder(V, 3u, 4, 5, 6);
+}
+
 // Append repeated test
 TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
   SCOPED_TRACE("AppendRepeatedTest");

``````````

</details>


https://github.com/llvm/llvm-project/pull/93384


More information about the llvm-commits mailing list