[PATCH] D129781: [NFC] Introduce llvm::to_vector_of to allow creation of SmallVector<T> from range of items convertible to type T
Dawid Jurczak via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 12 06:22:57 PDT 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8a17e74ca991: [NFC] Introduce llvm::to_vector_of to allow creation of SmallVector<T> from… (authored by yurai007).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D129781/new/
https://reviews.llvm.org/D129781
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
@@ -203,7 +203,7 @@
}
template <typename T, unsigned N>
-static unsigned NumBuiltinElts(const SmallVector<T, N> &) {
+constexpr static unsigned NumBuiltinElts(const SmallVector<T, N> &) {
return N;
}
@@ -1142,6 +1142,25 @@
EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2}));
}
+TEST(SmallVectorTest, ToVector) {
+ {
+ std::vector<char> v = {'a', 'b', 'c'};
+ auto Vector = to_vector<4>(v);
+ static_assert(NumBuiltinElts(Vector) == 4u);
+ ASSERT_EQ(3u, Vector.size());
+ for (size_t I = 0; I < v.size(); ++I)
+ EXPECT_EQ(v[I], Vector[I]);
+ }
+ {
+ std::vector<char> v = {'a', 'b', 'c'};
+ auto Vector = to_vector(v);
+ static_assert(NumBuiltinElts(Vector) != 4u);
+ ASSERT_EQ(3u, Vector.size());
+ for (size_t I = 0; I < v.size(); ++I)
+ EXPECT_EQ(v[I], Vector[I]);
+ }
+}
+
struct To {
int Content;
friend bool operator==(const To &LHS, const To &RHS) {
@@ -1180,6 +1199,26 @@
}
}
+TEST(SmallVectorTest, ToVectorOf) {
+ To to1{1}, to2{2}, to3{3};
+ std::vector<From> StdVector = {From(to1), From(to2), From(to3)};
+ {
+ llvm::SmallVector<To> Vector = llvm::to_vector_of<To>(StdVector);
+
+ ASSERT_EQ(StdVector.size(), Vector.size());
+ for (size_t I = 0; I < StdVector.size(); ++I)
+ EXPECT_EQ(StdVector[I], Vector[I]);
+ }
+ {
+ auto Vector = llvm::to_vector_of<To, 4>(StdVector);
+
+ ASSERT_EQ(StdVector.size(), Vector.size());
+ static_assert(NumBuiltinElts(Vector) == 4u);
+ for (size_t I = 0; I < StdVector.size(); ++I)
+ EXPECT_EQ(StdVector[I], Vector[I]);
+ }
+}
+
template <class VectorT>
class SmallVectorReferenceInvalidationTest : public SmallVectorTestBase {
protected:
@@ -1476,11 +1515,6 @@
VectorT V;
- template <typename T, unsigned N>
- static unsigned NumBuiltinElts(const SmallVector<T, N> &) {
- return N;
- }
-
void SetUp() override {
SmallVectorTestBase::SetUp();
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -1287,6 +1287,15 @@
return {std::begin(Range), std::end(Range)};
}
+template <typename Out, unsigned Size, typename R>
+SmallVector<Out, Size> to_vector_of(R &&Range) {
+ return {std::begin(Range), std::end(Range)};
+}
+
+template <typename Out, typename R> SmallVector<Out> to_vector_of(R &&Range) {
+ return {std::begin(Range), std::end(Range)};
+}
+
} // end namespace llvm
namespace std {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129781.452171.patch
Type: text/x-patch
Size: 2723 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220812/ac95c7ac/attachment.bin>
More information about the llvm-commits
mailing list