[llvm] [ADT] Add SmallVectorImpl::append_range (PR #93384)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sat May 25 13:23:14 PDT 2024
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/93384
The new function is modeled after std::vector::append_range from
C++23.
>From 042260fb6e6a3c6197aff491d154ef8504e09d1c Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 25 May 2024 11:23:26 -0700
Subject: [PATCH] [ADT] Add SmallVectorImpl::append_range
The new function is modeled after std::vector::append_range from
C++23.
---
llvm/include/llvm/ADT/SmallVector.h | 4 ++++
llvm/unittests/ADT/SmallVectorTest.cpp | 13 +++++++++++++
2 files changed, 17 insertions(+)
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");
More information about the llvm-commits
mailing list