Diff
Modified: trunk/Source/WTF/ChangeLog (221976 => 221977)
--- trunk/Source/WTF/ChangeLog 2017-09-13 17:47:52 UTC (rev 221976)
+++ trunk/Source/WTF/ChangeLog 2017-09-13 17:50:48 UTC (rev 221977)
@@ -1,3 +1,17 @@
+2017-09-13 Youenn Fablet <[email protected]>
+
+ 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 <[email protected]>
[DFG] Optimize WeakMap::get by adding intrinsic and fixup
Modified: trunk/Source/WTF/wtf/Vector.h (221976 => 221977)
--- trunk/Source/WTF/wtf/Vector.h 2017-09-13 17:47:52 UTC (rev 221976)
+++ trunk/Source/WTF/wtf/Vector.h 2017-09-13 17:50:48 UTC (rev 221977)
@@ -1560,6 +1560,36 @@
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;
Modified: trunk/Source/WebKit/ChangeLog (221976 => 221977)
--- trunk/Source/WebKit/ChangeLog 2017-09-13 17:47:52 UTC (rev 221976)
+++ trunk/Source/WebKit/ChangeLog 2017-09-13 17:50:48 UTC (rev 221977)
@@ -1,3 +1,13 @@
+2017-09-13 Youenn Fablet <[email protected]>
+
+ 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 <[email protected]>
Introduce Storage Access API (document parts) as an experimental feature
Modified: trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngineCaches.cpp (221976 => 221977)
--- trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngineCaches.cpp 2017-09-13 17:47:52 UTC (rev 221976)
+++ trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngineCaches.cpp 2017-09-13 17:50:48 UTC (rev 221977)
@@ -240,12 +240,9 @@
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) };
+ }));
});
}
Modified: trunk/Tools/ChangeLog (221976 => 221977)
--- trunk/Tools/ChangeLog 2017-09-13 17:47:52 UTC (rev 221976)
+++ trunk/Tools/ChangeLog 2017-09-13 17:50:48 UTC (rev 221977)
@@ -1,3 +1,13 @@
+2017-09-13 Youenn Fablet <[email protected]>
+
+ 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 <[email protected]>
Introduce Storage Access API (document parts) as an experimental feature
Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp (221976 => 221977)
--- trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp 2017-09-13 17:47:52 UTC (rev 221976)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp 2017-09-13 17:50:48 UTC (rev 221977)
@@ -651,4 +651,41 @@
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