[llvm] [ADT] Add SmallVectorImpl::append_range (PR #93384)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 13 18:07:14 PDT 2024
https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/93384
>From 0f95ab08081f2d9dbc585f39c8ec6192c7a3367b 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 1/2] [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 bd3e887e36bce4..f6081825e89c41 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -688,6 +688,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 137dd43b473068..d5c5b7619d525d 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");
>From 51b4b6e59b17391c5d0d54821f34753d2771f0e2 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 13 Sep 2024 18:06:36 -0700
Subject: [PATCH 2/2] Use adl_begin and adl_end.
---
llvm/include/llvm/ADT/SmallVector.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index f6081825e89c41..727d8c57747d3d 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -689,7 +689,7 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
}
template <typename RangeTy> void append_range(RangeTy &&R) {
- this->append(R.begin(), R.end());
+ this->append(adl_begin(R), adl_end(R));
}
/// Append \p NumInputs copies of \p Elt to the end.
More information about the llvm-commits
mailing list