[PATCH] D112968: [NFC] Add size inference to to_vector
Geoffrey Martin-Noble via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 1 16:41:56 PDT 2021
GMNGeoffrey updated this revision to Diff 383922.
GMNGeoffrey added a comment.
- Add test for implicit to_vector size
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D112968/new/
https://reviews.llvm.org/D112968
Files:
llvm/include/llvm/ADT/SmallVector.h
llvm/unittests/ADT/STLExtrasTest.cpp
Index: llvm/unittests/ADT/STLExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/STLExtrasTest.cpp
+++ llvm/unittests/ADT/STLExtrasTest.cpp
@@ -307,6 +307,13 @@
EXPECT_EQ(I, Enumerated[I].index());
EXPECT_EQ(v[I], Enumerated[I].value());
}
+
+ auto EnumeratedImplicitSize = to_vector(enumerate(v));
+ ASSERT_EQ(3u, EnumeratedImplicitSize.size());
+ for (size_t I = 0; I < v.size(); ++I) {
+ EXPECT_EQ(I, EnumeratedImplicitSize[I].index());
+ EXPECT_EQ(v[I], EnumeratedImplicitSize[I].value());
+ }
}
TEST(STLExtrasTest, ConcatRange) {
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -1239,13 +1239,22 @@
return X.capacity_in_bytes();
}
+template <typename RangeType>
+using ValueTypeFromRangeType =
+ typename std::remove_const<typename std::remove_reference<
+ decltype(*std::begin(std::declval<RangeType &>()))>::type>::type;
+
/// Given a range of type R, iterate the entire range and return a
/// SmallVector with elements of the vector. This is useful, for example,
/// when you want to iterate a range and then sort the results.
template <unsigned Size, typename R>
-SmallVector<typename std::remove_const<typename std::remove_reference<
- decltype(*std::begin(std::declval<R &>()))>::type>::type,
- Size>
+SmallVector<ValueTypeFromRangeType<R>, Size> to_vector(R &&Range) {
+ return {std::begin(Range), std::end(Range)};
+}
+template <typename R>
+SmallVector<ValueTypeFromRangeType<R>,
+ CalculateSmallVectorDefaultInlinedElements<
+ ValueTypeFromRangeType<R>>::value>
to_vector(R &&Range) {
return {std::begin(Range), std::end(Range)};
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112968.383922.patch
Type: text/x-patch
Size: 1853 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211101/90a9356a/attachment.bin>
More information about the llvm-commits
mailing list