Title: [222170] trunk
Revision
222170
Author
[email protected]
Date
2017-09-18 11:52:10 -0700 (Mon, 18 Sep 2017)

Log Message

Allow WTF::map to use any class that is iterable and has a size getter
https://bugs.webkit.org/show_bug.cgi?id=177026

Patch by Youenn Fablet <[email protected]> on 2017-09-18
Reviewed by Darin Adler.

Source/WebCore:

No change of behavior.
Using WTF::map to go from maps to vectors.

* loader/appcache/ApplicationCacheHost.cpp:
(WebCore::ApplicationCacheHost::resourceList):
* page/DOMWindow.cpp:
(WebCore::DOMWindow::dispatchAllPendingUnloadEvents):

Source/WTF:

Computing the Item type given to the lambda using the iterator instead of ValueType which is specific to Vector.
Adding the possibility to pass a non const container reference and a lambda taking non const references as well.

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

Tools:

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

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (222169 => 222170)


--- trunk/Source/WTF/ChangeLog	2017-09-18 18:51:52 UTC (rev 222169)
+++ trunk/Source/WTF/ChangeLog	2017-09-18 18:52:10 UTC (rev 222170)
@@ -1,3 +1,18 @@
+2017-09-18  Youenn Fablet  <[email protected]>
+
+        Allow WTF::map to use any class that is iterable and has a size getter
+        https://bugs.webkit.org/show_bug.cgi?id=177026
+
+        Reviewed by Darin Adler.
+
+        Computing the Item type given to the lambda using the iterator instead of ValueType which is specific to Vector.
+        Adding the possibility to pass a non const container reference and a lambda taking non const references as well. 
+
+        * wtf/Vector.h:
+        (WTF::MapFunctionInspector::acceptsReference):
+        (WTF::Mapper::map):
+        (WTF::map):
+
 2017-09-15  JF Bastien  <[email protected]>
 
         WTF: use Forward.h when appropriate instead of Vector.h

Modified: trunk/Source/WTF/wtf/Vector.h (222169 => 222170)


--- trunk/Source/WTF/wtf/Vector.h	2017-09-18 18:51:52 UTC (rev 222169)
+++ trunk/Source/WTF/wtf/Vector.h	2017-09-18 18:52:10 UTC (rev 222170)
@@ -1562,34 +1562,49 @@
     return removeRepeatedElements(vector, [] (T& a, T& b) { return a == b; });
 }
 
-template<typename Transformer, typename SourceType> struct Mapper {
+template<typename MapFunction, typename SourceType>
+struct MapFunctionInspector {
     using RealSourceType = typename std::remove_reference<SourceType>::type;
-    using SourceItemType = typename RealSourceType::ValueType;
-    using DestinationItemType = typename std::result_of<Transformer(SourceItemType&&)>::type;
+    using IteratorType = decltype(std::begin(std::declval<RealSourceType>()));
+    using SourceItemType = typename std::iterator_traits<IteratorType>::value_type;
+};
 
-    static Vector<DestinationItemType> map(const RealSourceType& source, const Transformer& transformer)
+template<typename MapFunction, typename SourceType, typename Enable = void>
+struct Mapper {
+    using SourceItemType = typename MapFunctionInspector<MapFunction, SourceType>::SourceItemType;
+    using DestinationItemType = typename std::result_of<MapFunction(SourceItemType&)>::type;
+
+    static Vector<DestinationItemType> map(SourceType source, const MapFunction& mapFunction)
     {
         Vector<DestinationItemType> result;
+        // FIXME: Use std::size when available on all compilers.
         result.reserveInitialCapacity(source.size());
         for (auto& item : source)
-            result.uncheckedAppend(transformer(item));
+            result.uncheckedAppend(mapFunction(item));
         return result;
     }
+};
 
-    static Vector<DestinationItemType> map(RealSourceType&& source, const Transformer& transformer)
+template<typename MapFunction, typename SourceType>
+struct Mapper<MapFunction, SourceType, typename std::enable_if<std::is_rvalue_reference<SourceType&&>::value>::type> {
+    using SourceItemType = typename MapFunctionInspector<MapFunction, SourceType>::SourceItemType;
+    using DestinationItemType = typename std::result_of<MapFunction(SourceItemType&&)>::type;
+
+    static Vector<DestinationItemType> map(SourceType source, const MapFunction& mapFunction)
     {
         Vector<DestinationItemType> result;
+        // FIXME: Use std::size when available on all compilers.
         result.reserveInitialCapacity(source.size());
         for (auto& item : source)
-            result.uncheckedAppend(transformer(std::forward<SourceItemType>(item)));
+            result.uncheckedAppend(mapFunction(WTFMove(item)));
         return result;
     }
 };
 
-template<typename Transformer, typename VectorType>
-Vector<typename Mapper<Transformer, VectorType>::DestinationItemType> map(VectorType&& source, Transformer&& transformer)
+template<typename MapFunction, typename SourceType>
+Vector<typename Mapper<MapFunction, SourceType>::DestinationItemType> map(SourceType&& source, MapFunction&& mapFunction)
 {
-    return Mapper<Transformer, VectorType>::map(std::forward<VectorType>(source), std::forward<Transformer>(transformer));
+    return Mapper<MapFunction, SourceType>::map(std::forward<SourceType>(source), std::forward<MapFunction>(mapFunction));
 }
 
 } // namespace WTF

Modified: trunk/Source/WebCore/ChangeLog (222169 => 222170)


--- trunk/Source/WebCore/ChangeLog	2017-09-18 18:51:52 UTC (rev 222169)
+++ trunk/Source/WebCore/ChangeLog	2017-09-18 18:52:10 UTC (rev 222170)
@@ -1,3 +1,18 @@
+2017-09-18  Youenn Fablet  <[email protected]>
+
+        Allow WTF::map to use any class that is iterable and has a size getter
+        https://bugs.webkit.org/show_bug.cgi?id=177026
+
+        Reviewed by Darin Adler.
+
+        No change of behavior.
+        Using WTF::map to go from maps to vectors.
+
+        * loader/appcache/ApplicationCacheHost.cpp:
+        (WebCore::ApplicationCacheHost::resourceList):
+        * page/DOMWindow.cpp:
+        (WebCore::DOMWindow::dispatchAllPendingUnloadEvents):
+
 2017-09-18  Emilio Cobos Álvarez  <[email protected]>
 
         Always update display: contents styles in RenderTreeUpdater.

Modified: trunk/Source/WebCore/loader/appcache/ApplicationCacheHost.cpp (222169 => 222170)


--- trunk/Source/WebCore/loader/appcache/ApplicationCacheHost.cpp	2017-09-18 18:51:52 UTC (rev 222169)
+++ trunk/Source/WebCore/loader/appcache/ApplicationCacheHost.cpp	2017-09-18 18:52:10 UTC (rev 222170)
@@ -327,15 +327,11 @@
 
 Vector<ApplicationCacheHost::ResourceInfo> ApplicationCacheHost::resourceList()
 {
-    Vector<ResourceInfo> result;
-
     auto* cache = applicationCache();
     if (!cache || !cache->isComplete())
-        return result;
+        return { };
 
-    result.reserveInitialCapacity(cache->resources().size());
-
-    for (auto& urlAndResource : cache->resources()) {
+    return WTF::map(cache->resources(), [] (auto& urlAndResource) -> ApplicationCacheHost::ResourceInfo {
         ASSERT(urlAndResource.value);
         auto& resource = *urlAndResource.value;
 
@@ -346,10 +342,8 @@
         bool isForeign = type & ApplicationCacheResource::Foreign;
         bool isFallback = type & ApplicationCacheResource::Fallback;
 
-        result.uncheckedAppend({ resource.url(), isMaster, isManifest, isFallback, isForeign, isExplicit, resource.estimatedSizeInStorage() });
-    }
-
-    return result;
+        return { resource.url(), isMaster, isManifest, isFallback, isForeign, isExplicit, resource.estimatedSizeInStorage() };
+    });
 }
 
 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()

Modified: trunk/Source/WebCore/page/DOMWindow.cpp (222169 => 222170)


--- trunk/Source/WebCore/page/DOMWindow.cpp	2017-09-18 18:51:52 UTC (rev 222169)
+++ trunk/Source/WebCore/page/DOMWindow.cpp	2017-09-18 18:52:10 UTC (rev 222170)
@@ -297,10 +297,9 @@
     if (alreadyDispatched)
         return;
 
-    Vector<Ref<DOMWindow>> windows;
-    windows.reserveInitialCapacity(set.size());
-    for (auto& keyValue : set)
-        windows.uncheckedAppend(*keyValue.key);
+    auto windows = WTF::map(set, [] (auto& keyValue) {
+        return Ref<DOMWindow>(*(keyValue.key));
+    });
 
     for (auto& window : windows) {
         if (!set.contains(window.ptr()))

Modified: trunk/Tools/ChangeLog (222169 => 222170)


--- trunk/Tools/ChangeLog	2017-09-18 18:51:52 UTC (rev 222169)
+++ trunk/Tools/ChangeLog	2017-09-18 18:52:10 UTC (rev 222170)
@@ -1,3 +1,13 @@
+2017-09-18  Youenn Fablet  <[email protected]>
+
+        Allow WTF::map to use any class that is iterable and has a size getter
+        https://bugs.webkit.org/show_bug.cgi?id=177026
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/Tests/WTF/Vector.cpp:
+        (TestWebKitAPI::TEST):
+
 2017-09-16  Filip Pizlo  <[email protected]>
 
         Figure out how WSL will support field overloads like float4.xz and friends

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp (222169 => 222170)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2017-09-18 18:51:52 UTC (rev 222169)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2017-09-18 18:52:10 UTC (rev 222170)
@@ -26,8 +26,10 @@
 #include "config.h"
 
 #include "MoveOnly.h"
+#include <wtf/HashMap.h>
 #include <wtf/Vector.h>
-#include <wtf/text/CString.h>
+#include <wtf/text/StringHash.h>
+#include <wtf/text/WTFString.h>
 
 namespace TestWebKitAPI {
 
@@ -726,4 +728,51 @@
     EXPECT_EQ(5U, mapped[2].value());
 }
 
+TEST(WTF_Vector, MapFromHashMap)
+{
+    HashMap<String, String> map;
+    map.set(String { "k1" }, String { "v1" });
+    map.set(String { "k2" }, String { "v2" });
+    map.set(String { "k3" }, String { "v3" });
+
+    auto mapped = WTF::map(map, [&] (KeyValuePair<String, String>& pair) -> String {
+        return pair.value;
+    });
+    std::sort(mapped.begin(), mapped.end(), WTF::codePointCompareLessThan);
+
+    EXPECT_EQ(3U, mapped.size());
+    EXPECT_TRUE(mapped[0] == "v1");
+    EXPECT_TRUE(mapped[1] == "v2");
+    EXPECT_TRUE(mapped[2] == "v3");
+
+    mapped = WTF::map(map, [&] (const auto& pair) -> String {
+        return pair.key;
+    });
+    std::sort(mapped.begin(), mapped.end(), WTF::codePointCompareLessThan);
+
+    EXPECT_EQ(3U, mapped.size());
+    EXPECT_TRUE(mapped[0] == "k1");
+    EXPECT_TRUE(mapped[1] == "k2");
+    EXPECT_TRUE(mapped[2] == "k3");
+
+    mapped = WTF::map(WTFMove(map), [&] (KeyValuePair<String, String>&& pair) -> String {
+        return WTFMove(pair.value);
+    });
+    std::sort(mapped.begin(), mapped.end(), WTF::codePointCompareLessThan);
+
+    EXPECT_EQ(3U, mapped.size());
+    EXPECT_TRUE(mapped[0] == "v1");
+    EXPECT_TRUE(mapped[1] == "v2");
+    EXPECT_TRUE(mapped[2] == "v3");
+
+    EXPECT_TRUE(map.contains("k1"));
+    EXPECT_TRUE(map.contains("k2"));
+    EXPECT_TRUE(map.contains("k3"));
+
+    EXPECT_TRUE(map.get("k1").isNull());
+    EXPECT_TRUE(map.get("k2").isNull());
+    EXPECT_TRUE(map.get("k3").isNull());
+
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to