[llvm] 8a17e74 - [NFC] Introduce llvm::to_vector_of to allow creation of SmallVector<T> from range of items convertible to type T

Dawid Jurczak via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 12 06:22:46 PDT 2022


Author: Dawid Jurczak
Date: 2022-08-12T15:22:12+02:00
New Revision: 8a17e74ca991bc7b9c1bda38107201edac244d0f

URL: https://github.com/llvm/llvm-project/commit/8a17e74ca991bc7b9c1bda38107201edac244d0f
DIFF: https://github.com/llvm/llvm-project/commit/8a17e74ca991bc7b9c1bda38107201edac244d0f.diff

LOG: [NFC] Introduce llvm::to_vector_of to allow creation of SmallVector<T> from range of items convertible to type T

It's https://reviews.llvm.org/D129565 follow-up.

Differential Revision: https://reviews.llvm.org/D129781

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index e2c07fefc637f..67a1901b6db27 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -1287,6 +1287,15 @@ SmallVector<ValueTypeFromRangeType<R>> to_vector(R &&Range) {
   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 {

diff  --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp
index db94929f1e9f6..e7ac9c0d76fdd 100644
--- a/llvm/unittests/ADT/SmallVectorTest.cpp
+++ b/llvm/unittests/ADT/SmallVectorTest.cpp
@@ -203,7 +203,7 @@ template <typename VectorT> void makeSequence(VectorT &v, int start, int end) {
 }
 
 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 @@ TEST(SmallVectorTest, InitializerList) {
   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, ConstructFromArrayRefOfConvertibleType) {
   }
 }
 
+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 @@ class SmallVectorInternalReferenceInvalidationTest
 
   VectorT V;
 
-  template <typename T, unsigned N>
-  static unsigned NumBuiltinElts(const SmallVector<T, N> &) {
-    return N;
-  }
-
   void SetUp() override {
     SmallVectorTestBase::SetUp();
 


        


More information about the llvm-commits mailing list