Changeset 222170 in webkit


Ignore:
Timestamp:
Sep 18, 2017 11:52:10 AM (7 years ago)
Author:
commit-queue@webkit.org
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 <youenn@apple.com> 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):

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r222113 r222170  
     12017-09-18  Youenn Fablet  <youenn@apple.com>
     2
     3        Allow WTF::map to use any class that is iterable and has a size getter
     4        https://bugs.webkit.org/show_bug.cgi?id=177026
     5
     6        Reviewed by Darin Adler.
     7
     8        Computing the Item type given to the lambda using the iterator instead of ValueType which is specific to Vector.
     9        Adding the possibility to pass a non const container reference and a lambda taking non const references as well.
     10
     11        * wtf/Vector.h:
     12        (WTF::MapFunctionInspector::acceptsReference):
     13        (WTF::Mapper::map):
     14        (WTF::map):
     15
    1162017-09-15  JF Bastien  <jfbastien@apple.com>
    217
  • trunk/Source/WTF/wtf/Vector.h

    r222113 r222170  
    15631563}
    15641564
    1565 template<typename Transformer, typename SourceType> struct Mapper {
     1565template<typename MapFunction, typename SourceType>
     1566struct MapFunctionInspector {
    15661567    using RealSourceType = typename std::remove_reference<SourceType>::type;
    1567     using SourceItemType = typename RealSourceType::ValueType;
    1568     using DestinationItemType = typename std::result_of<Transformer(SourceItemType&&)>::type;
    1569 
    1570     static Vector<DestinationItemType> map(const RealSourceType& source, const Transformer& transformer)
     1568    using IteratorType = decltype(std::begin(std::declval<RealSourceType>()));
     1569    using SourceItemType = typename std::iterator_traits<IteratorType>::value_type;
     1570};
     1571
     1572template<typename MapFunction, typename SourceType, typename Enable = void>
     1573struct Mapper {
     1574    using SourceItemType = typename MapFunctionInspector<MapFunction, SourceType>::SourceItemType;
     1575    using DestinationItemType = typename std::result_of<MapFunction(SourceItemType&)>::type;
     1576
     1577    static Vector<DestinationItemType> map(SourceType source, const MapFunction& mapFunction)
    15711578    {
    15721579        Vector<DestinationItemType> result;
     1580        // FIXME: Use std::size when available on all compilers.
    15731581        result.reserveInitialCapacity(source.size());
    15741582        for (auto& item : source)
    1575             result.uncheckedAppend(transformer(item));
     1583            result.uncheckedAppend(mapFunction(item));
    15761584        return result;
    15771585    }
    1578 
    1579     static Vector<DestinationItemType> map(RealSourceType&& source, const Transformer& transformer)
     1586};
     1587
     1588template<typename MapFunction, typename SourceType>
     1589struct Mapper<MapFunction, SourceType, typename std::enable_if<std::is_rvalue_reference<SourceType&&>::value>::type> {
     1590    using SourceItemType = typename MapFunctionInspector<MapFunction, SourceType>::SourceItemType;
     1591    using DestinationItemType = typename std::result_of<MapFunction(SourceItemType&&)>::type;
     1592
     1593    static Vector<DestinationItemType> map(SourceType source, const MapFunction& mapFunction)
    15801594    {
    15811595        Vector<DestinationItemType> result;
     1596        // FIXME: Use std::size when available on all compilers.
    15821597        result.reserveInitialCapacity(source.size());
    15831598        for (auto& item : source)
    1584             result.uncheckedAppend(transformer(std::forward<SourceItemType>(item)));
     1599            result.uncheckedAppend(mapFunction(WTFMove(item)));
    15851600        return result;
    15861601    }
    15871602};
    15881603
    1589 template<typename Transformer, typename VectorType>
    1590 Vector<typename Mapper<Transformer, VectorType>::DestinationItemType> map(VectorType&& source, Transformer&& transformer)
    1591 {
    1592     return Mapper<Transformer, VectorType>::map(std::forward<VectorType>(source), std::forward<Transformer>(transformer));
     1604template<typename MapFunction, typename SourceType>
     1605Vector<typename Mapper<MapFunction, SourceType>::DestinationItemType> map(SourceType&& source, MapFunction&& mapFunction)
     1606{
     1607    return Mapper<MapFunction, SourceType>::map(std::forward<SourceType>(source), std::forward<MapFunction>(mapFunction));
    15931608}
    15941609
  • trunk/Source/WebCore/ChangeLog

    r222168 r222170  
     12017-09-18  Youenn Fablet  <youenn@apple.com>
     2
     3        Allow WTF::map to use any class that is iterable and has a size getter
     4        https://bugs.webkit.org/show_bug.cgi?id=177026
     5
     6        Reviewed by Darin Adler.
     7
     8        No change of behavior.
     9        Using WTF::map to go from maps to vectors.
     10
     11        * loader/appcache/ApplicationCacheHost.cpp:
     12        (WebCore::ApplicationCacheHost::resourceList):
     13        * page/DOMWindow.cpp:
     14        (WebCore::DOMWindow::dispatchAllPendingUnloadEvents):
     15
    1162017-09-18  Emilio Cobos Álvarez  <emilio@crisal.io>
    217
  • trunk/Source/WebCore/loader/appcache/ApplicationCacheHost.cpp

    r219856 r222170  
    328328Vector<ApplicationCacheHost::ResourceInfo> ApplicationCacheHost::resourceList()
    329329{
    330     Vector<ResourceInfo> result;
    331 
    332330    auto* cache = applicationCache();
    333331    if (!cache || !cache->isComplete())
    334         return result;
    335 
    336     result.reserveInitialCapacity(cache->resources().size());
    337 
    338     for (auto& urlAndResource : cache->resources()) {
     332        return { };
     333
     334    return WTF::map(cache->resources(), [] (auto& urlAndResource) -> ApplicationCacheHost::ResourceInfo {
    339335        ASSERT(urlAndResource.value);
    340336        auto& resource = *urlAndResource.value;
     
    347343        bool isFallback = type & ApplicationCacheResource::Fallback;
    348344
    349         result.uncheckedAppend({ resource.url(), isMaster, isManifest, isFallback, isForeign, isExplicit, resource.estimatedSizeInStorage() });
    350     }
    351 
    352     return result;
     345        return { resource.url(), isMaster, isManifest, isFallback, isForeign, isExplicit, resource.estimatedSizeInStorage() };
     346    });
    353347}
    354348
  • trunk/Source/WebCore/page/DOMWindow.cpp

    r222064 r222170  
    298298        return;
    299299
    300     Vector<Ref<DOMWindow>> windows;
    301     windows.reserveInitialCapacity(set.size());
    302     for (auto& keyValue : set)
    303         windows.uncheckedAppend(*keyValue.key);
     300    auto windows = WTF::map(set, [] (auto& keyValue) {
     301        return Ref<DOMWindow>(*(keyValue.key));
     302    });
    304303
    305304    for (auto& window : windows) {
  • trunk/Tools/ChangeLog

    r222169 r222170  
     12017-09-18  Youenn Fablet  <youenn@apple.com>
     2
     3        Allow WTF::map to use any class that is iterable and has a size getter
     4        https://bugs.webkit.org/show_bug.cgi?id=177026
     5
     6        Reviewed by Darin Adler.
     7
     8        * TestWebKitAPI/Tests/WTF/Vector.cpp:
     9        (TestWebKitAPI::TEST):
     10
    1112017-09-16  Filip Pizlo  <fpizlo@apple.com>
    212
  • trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp

    r222039 r222170  
    2727
    2828#include "MoveOnly.h"
     29#include <wtf/HashMap.h>
    2930#include <wtf/Vector.h>
    30 #include <wtf/text/CString.h>
     31#include <wtf/text/StringHash.h>
     32#include <wtf/text/WTFString.h>
    3133
    3234namespace TestWebKitAPI {
     
    727729}
    728730
     731TEST(WTF_Vector, MapFromHashMap)
     732{
     733    HashMap<String, String> map;
     734    map.set(String { "k1" }, String { "v1" });
     735    map.set(String { "k2" }, String { "v2" });
     736    map.set(String { "k3" }, String { "v3" });
     737
     738    auto mapped = WTF::map(map, [&] (KeyValuePair<String, String>& pair) -> String {
     739        return pair.value;
     740    });
     741    std::sort(mapped.begin(), mapped.end(), WTF::codePointCompareLessThan);
     742
     743    EXPECT_EQ(3U, mapped.size());
     744    EXPECT_TRUE(mapped[0] == "v1");
     745    EXPECT_TRUE(mapped[1] == "v2");
     746    EXPECT_TRUE(mapped[2] == "v3");
     747
     748    mapped = WTF::map(map, [&] (const auto& pair) -> String {
     749        return pair.key;
     750    });
     751    std::sort(mapped.begin(), mapped.end(), WTF::codePointCompareLessThan);
     752
     753    EXPECT_EQ(3U, mapped.size());
     754    EXPECT_TRUE(mapped[0] == "k1");
     755    EXPECT_TRUE(mapped[1] == "k2");
     756    EXPECT_TRUE(mapped[2] == "k3");
     757
     758    mapped = WTF::map(WTFMove(map), [&] (KeyValuePair<String, String>&& pair) -> String {
     759        return WTFMove(pair.value);
     760    });
     761    std::sort(mapped.begin(), mapped.end(), WTF::codePointCompareLessThan);
     762
     763    EXPECT_EQ(3U, mapped.size());
     764    EXPECT_TRUE(mapped[0] == "v1");
     765    EXPECT_TRUE(mapped[1] == "v2");
     766    EXPECT_TRUE(mapped[2] == "v3");
     767
     768    EXPECT_TRUE(map.contains("k1"));
     769    EXPECT_TRUE(map.contains("k2"));
     770    EXPECT_TRUE(map.contains("k3"));
     771
     772    EXPECT_TRUE(map.get("k1").isNull());
     773    EXPECT_TRUE(map.get("k2").isNull());
     774    EXPECT_TRUE(map.get("k3").isNull());
     775
     776}
     777
    729778} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.