Allow WTF::map to take function as parameter
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Sep 2017 18:56:11 +0000 (18:56 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 14 Sep 2017 18:56:11 +0000 (18:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=176909

Patch by Youenn Fablet <youenn@apple.com> on 2017-09-14
Reviewed by Jer Noble.

Source/WTF:

* wtf/Vector.h:
(WTF::map):

Tools:

* TestWebKitAPI/Tests/WTF/Vector.cpp:
(TestWebKitAPI::multiplyByTwo):
(TestWebKitAPI::TEST):
(TestWebKitAPI::multiplyByTwoMoveOnly):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@222039 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/Vector.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/Vector.cpp

index baca227..20b3dd4 100644 (file)
@@ -1,3 +1,13 @@
+2017-09-14  Youenn Fablet  <youenn@apple.com>
+
+        Allow WTF::map to take function as parameter
+        https://bugs.webkit.org/show_bug.cgi?id=176909
+
+        Reviewed by Jer Noble.
+
+        * wtf/Vector.h:
+        (WTF::map):
+
 2017-09-13  Youenn Fablet  <youenn@apple.com>
 
         Add a lambda-based map for Vectors
index 9ad8968..98d2374 100644 (file)
@@ -1585,9 +1585,9 @@ template<typename Transformer, typename SourceType> struct Mapper {
 };
 
 template<typename Transformer, typename VectorType>
-Vector<typename Mapper<Transformer, VectorType>::DestinationItemType> map(VectorType&& source, const Transformer& transformer)
+Vector<typename Mapper<Transformer, VectorType>::DestinationItemType> map(VectorType&& source, Transformer&& transformer)
 {
-    return Mapper<Transformer, VectorType>::map(std::forward<VectorType>(source), transformer);
+    return Mapper<Transformer, VectorType>::map(std::forward<VectorType>(source), std::forward<Transformer>(transformer));
 }
 
 } // namespace WTF
index 221a98c..5f74b60 100644 (file)
@@ -1,3 +1,15 @@
+2017-09-14  Youenn Fablet  <youenn@apple.com>
+
+        Allow WTF::map to take function as parameter
+        https://bugs.webkit.org/show_bug.cgi?id=176909
+
+        Reviewed by Jer Noble.
+
+        * TestWebKitAPI/Tests/WTF/Vector.cpp:
+        (TestWebKitAPI::multiplyByTwo):
+        (TestWebKitAPI::TEST):
+        (TestWebKitAPI::multiplyByTwoMoveOnly):
+
 2017-09-14  Filip Pizlo  <fpizlo@apple.com>
 
         WSL IntLiteralType should become int32 if unified with a type variable
index 91f7ce0..27200db 100644 (file)
@@ -651,9 +651,47 @@ TEST(WTF_Vector, RemoveAllMatching)
     EXPECT_EQ(0U, v.size());
 }
 
+static int multiplyByTwo(int value)
+{
+    return 2 * value;
+}
+
+TEST(WTF_Vector, MapStaticFunction)
+{
+    Vector<int> vector { 2, 3, 4 };
+
+    auto mapped = WTF::map(vector, multiplyByTwo);
+
+    EXPECT_EQ(3U, mapped.size());
+    EXPECT_EQ(4, mapped[0]);
+    EXPECT_EQ(6, mapped[1]);
+    EXPECT_EQ(8, mapped[2]);
+}
+
+static MoveOnly multiplyByTwoMoveOnly(const MoveOnly& value)
+{
+    return MoveOnly(2 * value.value());
+}
+
+TEST(WTF_Vector, MapStaticFunctionMoveOnly)
+{
+    Vector<MoveOnly> vector;
+
+    vector.reserveInitialCapacity(3);
+    for (unsigned i = 0; i < 3; ++i)
+        vector.uncheckedAppend(MoveOnly { i });
+
+    auto mapped = WTF::map(vector, multiplyByTwoMoveOnly);
+
+    EXPECT_EQ(3U, mapped.size());
+    EXPECT_EQ(0U, mapped[0].value());
+    EXPECT_EQ(2U, mapped[1].value());
+    EXPECT_EQ(4U, mapped[2].value());
+}
+
 TEST(WTF_Vector, MapLambda)
 {
-    Vector<int> vector { 2, 3, 4};
+    Vector<int> vector { 2, 3, 4 };
 
     int counter = 0;
     auto mapped = WTF::map(vector, [&] (int item) {