Add a lambda-based map for Vectors
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 13 Sep 2017 17:50:48 +0000 (17:50 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 13 Sep 2017 17:50:48 +0000 (17:50 +0000)
https://bugs.webkit.org/show_bug.cgi?id=176487

Patch by Youenn Fablet <youenn@apple.com> on 2017-09-13
Reviewed by Darin Adler.

Source/WebKit:

* NetworkProcess/cache/CacheStorageEngineCaches.cpp:
(WebKit::CacheStorage::Caches::readCachesFromDisk):

Source/WTF:

This helper routine allows refactoring the reserveInitialCapacity/uncheckedAppend pattern, the mapper between source and destination item being a lambda.

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

Tools:

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

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

Source/WTF/ChangeLog
Source/WTF/wtf/Vector.h
Source/WebKit/ChangeLog
Source/WebKit/NetworkProcess/cache/CacheStorageEngineCaches.cpp
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/Vector.cpp

index 955cb00..baca227 100644 (file)
@@ -1,3 +1,17 @@
+2017-09-13  Youenn Fablet  <youenn@apple.com>
+
+        Add a lambda-based map for Vectors
+        https://bugs.webkit.org/show_bug.cgi?id=176487
+
+        Reviewed by Darin Adler.
+
+        This helper routine allows refactoring the reserveInitialCapacity/uncheckedAppend pattern, the mapper between source and destination item being a lambda.
+
+        * wtf/Vector.h:
+        (WTF::Mapper::transform):
+        (WTF::Mapper::map):
+        (WTF::map):
+
 2017-09-12  Yusuke Suzuki  <utatane.tea@gmail.com>
 
         [DFG] Optimize WeakMap::get by adding intrinsic and fixup
index cf9c7fb..9ad8968 100644 (file)
@@ -1560,6 +1560,36 @@ size_t removeRepeatedElements(Vector<T, inlineCapacity, OverflowHandler, minCapa
     return removeRepeatedElements(vector, [] (T& a, T& b) { return a == b; });
 }
 
+template<typename Transformer, typename SourceType> struct Mapper {
+    using RealSourceType = typename std::remove_reference<SourceType>::type;
+    using SourceItemType = typename RealSourceType::ValueType;
+    using DestinationItemType = typename std::result_of<Transformer(SourceItemType&&)>::type;
+
+    static Vector<DestinationItemType> map(const RealSourceType& source, const Transformer& transformer)
+    {
+        Vector<DestinationItemType> result;
+        result.reserveInitialCapacity(source.size());
+        for (auto& item : source)
+            result.uncheckedAppend(transformer(item));
+        return result;
+    }
+
+    static Vector<DestinationItemType> map(RealSourceType&& source, const Transformer& transformer)
+    {
+        Vector<DestinationItemType> result;
+        result.reserveInitialCapacity(source.size());
+        for (auto& item : source)
+            result.uncheckedAppend(transformer(std::forward<SourceItemType>(item)));
+        return result;
+    }
+};
+
+template<typename Transformer, typename VectorType>
+Vector<typename Mapper<Transformer, VectorType>::DestinationItemType> map(VectorType&& source, const Transformer& transformer)
+{
+    return Mapper<Transformer, VectorType>::map(std::forward<VectorType>(source), transformer);
+}
+
 } // namespace WTF
 
 using WTF::Vector;
index a68077d..7a9b7b0 100644 (file)
@@ -1,3 +1,13 @@
+2017-09-13  Youenn Fablet  <youenn@apple.com>
+
+        Add a lambda-based map for Vectors
+        https://bugs.webkit.org/show_bug.cgi?id=176487
+
+        Reviewed by Darin Adler.
+
+        * NetworkProcess/cache/CacheStorageEngineCaches.cpp:
+        (WebKit::CacheStorage::Caches::readCachesFromDisk):
+
 2017-09-13  John Wilander  <wilander@apple.com>
 
         Introduce Storage Access API (document parts) as an experimental feature
index 2c1bfa5..b31e289 100644 (file)
@@ -240,12 +240,9 @@ void Caches::readCachesFromDisk(WTF::Function<void(Expected<Vector<Cache>, Error
             callback(makeUnexpected(result.error()));
             return;
         }
-        Vector<Cache> caches;
-        caches.reserveInitialCapacity(result.value().size());
-        for (auto& name : result.value())
-            caches.uncheckedAppend(Cache { *this, m_engine->nextCacheIdentifier(), Cache::State::Uninitialized, WTFMove(name) });
-
-        callback(WTFMove(caches));
+        callback(WTF::map(WTFMove(result.value()), [this] (String&& name) {
+            return Cache { *this, m_engine->nextCacheIdentifier(), Cache::State::Uninitialized, WTFMove(name) };
+        }));
     });
 }
 
index 302b74c..5b7b9ea 100644 (file)
@@ -1,3 +1,13 @@
+2017-09-13  Youenn Fablet  <youenn@apple.com>
+
+        Add a lambda-based map for Vectors
+        https://bugs.webkit.org/show_bug.cgi?id=176487
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/Tests/WTF/Vector.cpp:
+        (TestWebKitAPI::TEST):
+
 2017-09-13  John Wilander  <wilander@apple.com>
 
         Introduce Storage Access API (document parts) as an experimental feature
index 4daba30..91f7ce0 100644 (file)
@@ -651,4 +651,41 @@ TEST(WTF_Vector, RemoveAllMatching)
     EXPECT_EQ(0U, v.size());
 }
 
+TEST(WTF_Vector, MapLambda)
+{
+    Vector<int> vector { 2, 3, 4};
+
+    int counter = 0;
+    auto mapped = WTF::map(vector, [&] (int item) {
+        counter += 2;
+        return counter <= item;
+    });
+
+    EXPECT_EQ(3U, mapped.size());
+    EXPECT_TRUE(mapped[0]);
+    EXPECT_FALSE(mapped[1]);
+    EXPECT_FALSE(mapped[2]);
+}
+
+TEST(WTF_Vector, MapLambdaMove)
+{
+    Vector<MoveOnly> vector;
+
+    vector.reserveInitialCapacity(3);
+    for (unsigned i = 0; i < 3; ++i)
+        vector.uncheckedAppend(MoveOnly { i });
+
+
+    unsigned counter = 0;
+    auto mapped = WTF::map(WTFMove(vector), [&] (MoveOnly&& item) {
+        item = item.value() + ++counter;
+        return WTFMove(item);
+    });
+
+    EXPECT_EQ(3U, mapped.size());
+    EXPECT_EQ(1U, mapped[0].value());
+    EXPECT_EQ(3U, mapped[1].value());
+    EXPECT_EQ(5U, mapped[2].value());
+}
+
 } // namespace TestWebKitAPI