Changeset 222170 in webkit
- Timestamp:
- Sep 18, 2017 11:52:10 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r222113 r222170 1 2017-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 1 16 2017-09-15 JF Bastien <jfbastien@apple.com> 2 17 -
trunk/Source/WTF/wtf/Vector.h
r222113 r222170 1563 1563 } 1564 1564 1565 template<typename Transformer, typename SourceType> struct Mapper { 1565 template<typename MapFunction, typename SourceType> 1566 struct MapFunctionInspector { 1566 1567 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 1572 template<typename MapFunction, typename SourceType, typename Enable = void> 1573 struct 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) 1571 1578 { 1572 1579 Vector<DestinationItemType> result; 1580 // FIXME: Use std::size when available on all compilers. 1573 1581 result.reserveInitialCapacity(source.size()); 1574 1582 for (auto& item : source) 1575 result.uncheckedAppend( transformer(item));1583 result.uncheckedAppend(mapFunction(item)); 1576 1584 return result; 1577 1585 } 1578 1579 static Vector<DestinationItemType> map(RealSourceType&& source, const Transformer& transformer) 1586 }; 1587 1588 template<typename MapFunction, typename SourceType> 1589 struct 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) 1580 1594 { 1581 1595 Vector<DestinationItemType> result; 1596 // FIXME: Use std::size when available on all compilers. 1582 1597 result.reserveInitialCapacity(source.size()); 1583 1598 for (auto& item : source) 1584 result.uncheckedAppend( transformer(std::forward<SourceItemType>(item)));1599 result.uncheckedAppend(mapFunction(WTFMove(item))); 1585 1600 return result; 1586 1601 } 1587 1602 }; 1588 1603 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));1604 template<typename MapFunction, typename SourceType> 1605 Vector<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)); 1593 1608 } 1594 1609 -
trunk/Source/WebCore/ChangeLog
r222168 r222170 1 2017-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 1 16 2017-09-18 Emilio Cobos Álvarez <emilio@crisal.io> 2 17 -
trunk/Source/WebCore/loader/appcache/ApplicationCacheHost.cpp
r219856 r222170 328 328 Vector<ApplicationCacheHost::ResourceInfo> ApplicationCacheHost::resourceList() 329 329 { 330 Vector<ResourceInfo> result;331 332 330 auto* cache = applicationCache(); 333 331 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 { 339 335 ASSERT(urlAndResource.value); 340 336 auto& resource = *urlAndResource.value; … … 347 343 bool isFallback = type & ApplicationCacheResource::Fallback; 348 344 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 }); 353 347 } 354 348 -
trunk/Source/WebCore/page/DOMWindow.cpp
r222064 r222170 298 298 return; 299 299 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 }); 304 303 305 304 for (auto& window : windows) { -
trunk/Tools/ChangeLog
r222169 r222170 1 2017-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 1 11 2017-09-16 Filip Pizlo <fpizlo@apple.com> 2 12 -
trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp
r222039 r222170 27 27 28 28 #include "MoveOnly.h" 29 #include <wtf/HashMap.h> 29 30 #include <wtf/Vector.h> 30 #include <wtf/text/CString.h> 31 #include <wtf/text/StringHash.h> 32 #include <wtf/text/WTFString.h> 31 33 32 34 namespace TestWebKitAPI { … … 727 729 } 728 730 731 TEST(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 729 778 } // namespace TestWebKitAPI
Note: See TracChangeset
for help on using the changeset viewer.