Title: [287472] trunk
Revision
287472
Author
wei...@apple.com
Date
2021-12-28 10:01:43 -0800 (Tue, 28 Dec 2021)

Log Message

Enhance Vector::map to allow specifying what kind of Vector to return (e.g. inline capacity, overflow, etc.)
https://bugs.webkit.org/show_bug.cgi?id=234683

Reviewed by Darin Adler.

Source/WebCore:

* platform/graphics/GradientColorStops.h:
(WebCore::GradientColorStops::mapColors const):
(WebCore::GradientColorStops::GradientColorStops):
Utilize new overloaded Vector::map to streamline GradientColorStops::mapColors(). Also
made it preserve the isSorted bit, which is valid because the offsets don't change.

Source/WTF:

* wtf/Vector.h:
(WTF::Vector::map const):
Add an overload of Vector::map() that allows specifying the type of the result Vector. To make this
work without giving up the argument deduction in the common case, SFINAE is used to determine which
overload to use.

Tools:

* TestWebKitAPI/Tests/WTF/Vector.cpp:
Add test for new Vector::map overload.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (287471 => 287472)


--- trunk/Source/WTF/ChangeLog	2021-12-28 16:52:05 UTC (rev 287471)
+++ trunk/Source/WTF/ChangeLog	2021-12-28 18:01:43 UTC (rev 287472)
@@ -1,3 +1,16 @@
+2021-12-28  Sam Weinig  <wei...@apple.com>
+
+        Enhance Vector::map to allow specifying what kind of Vector to return (e.g. inline capacity, overflow, etc.)
+        https://bugs.webkit.org/show_bug.cgi?id=234683
+
+        Reviewed by Darin Adler.
+
+        * wtf/Vector.h:
+        (WTF::Vector::map const):
+        Add an overload of Vector::map() that allows specifying the type of the result Vector. To make this
+        work without giving up the argument deduction in the common case, SFINAE is used to determine which
+        overload to use.
+
 2021-12-28  Zan Dobersek  <zdober...@igalia.com>
 
         [RISCV64] Enable signal-based VM traps for CPU(RISCV64)

Modified: trunk/Source/WTF/wtf/Vector.h (287471 => 287472)


--- trunk/Source/WTF/wtf/Vector.h	2021-12-28 16:52:05 UTC (rev 287471)
+++ trunk/Source/WTF/wtf/Vector.h	2021-12-28 18:01:43 UTC (rev 287472)
@@ -843,8 +843,12 @@
 
     void checkConsistency();
 
-    template<typename MapFunction, typename R = typename std::invoke_result<MapFunction, const T&>::type> Vector<R> map(MapFunction) const;
+    template<typename ResultVector, typename MapFunction>
+    auto map(MapFunction&&) const -> std::enable_if_t<std::is_invocable_v<MapFunction, const T&>, ResultVector>;
 
+    template<typename MapFunction>
+    auto map(MapFunction&&) const -> std::enable_if_t<std::is_invocable_v<MapFunction, const T&>, Vector<typename std::invoke_result_t<MapFunction, const T&>>>;
+
     bool isHashTableDeletedValue() const { return m_size == std::numeric_limits<decltype(m_size)>::max(); }
 
 private:
@@ -1575,10 +1579,10 @@
 }
 
 template<typename T, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity, typename Malloc>
-template<typename MapFunction, typename R>
-inline Vector<R> Vector<T, inlineCapacity, OverflowHandler, minCapacity, Malloc>::map(MapFunction mapFunction) const
+template<typename ResultVector, typename MapFunction>
+inline auto Vector<T, inlineCapacity, OverflowHandler, minCapacity, Malloc>::map(MapFunction&& mapFunction) const -> std::enable_if_t<std::is_invocable_v<MapFunction, const T&>, ResultVector>
 {
-    Vector<R> result;
+    ResultVector result;
     result.reserveInitialCapacity(size());
     for (size_t i = 0; i < size(); ++i)
         result.uncheckedAppend(mapFunction(at(i)));
@@ -1586,6 +1590,13 @@
 }
 
 template<typename T, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity, typename Malloc>
+template<typename MapFunction>
+inline auto Vector<T, inlineCapacity, OverflowHandler, minCapacity, Malloc>::map(MapFunction&& mapFunction) const -> std::enable_if_t<std::is_invocable_v<MapFunction, const T&>, Vector<typename std::invoke_result_t<MapFunction, const T&>>>
+{
+    return map<Vector<typename std::invoke_result_t<MapFunction, const T&>>, MapFunction>(std::forward<MapFunction>(mapFunction));
+}
+
+template<typename T, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity, typename Malloc>
 inline MallocPtr<T, Malloc> Vector<T, inlineCapacity, OverflowHandler, minCapacity, Malloc>::releaseBuffer()
 {
     // FIXME: Find a way to preserve annotations on the returned buffer.

Modified: trunk/Source/WebCore/ChangeLog (287471 => 287472)


--- trunk/Source/WebCore/ChangeLog	2021-12-28 16:52:05 UTC (rev 287471)
+++ trunk/Source/WebCore/ChangeLog	2021-12-28 18:01:43 UTC (rev 287472)
@@ -1,3 +1,16 @@
+2021-12-28  Sam Weinig  <wei...@apple.com>
+
+        Enhance Vector::map to allow specifying what kind of Vector to return (e.g. inline capacity, overflow, etc.)
+        https://bugs.webkit.org/show_bug.cgi?id=234683
+
+        Reviewed by Darin Adler.
+
+        * platform/graphics/GradientColorStops.h:
+        (WebCore::GradientColorStops::mapColors const):
+        (WebCore::GradientColorStops::GradientColorStops):
+        Utilize new overloaded Vector::map to streamline GradientColorStops::mapColors(). Also
+        made it preserve the isSorted bit, which is valid because the offsets don't change.
+
 2021-12-28  Alan Bujtas  <za...@apple.com>
 
         [LFC][IFC] Hanging content may not be whitespace type

Modified: trunk/Source/WebCore/platform/graphics/GradientColorStops.h (287471 => 287472)


--- trunk/Source/WebCore/platform/graphics/GradientColorStops.h	2021-12-28 16:52:05 UTC (rev 287471)
+++ trunk/Source/WebCore/platform/graphics/GradientColorStops.h	2021-12-28 18:01:43 UTC (rev 287472)
@@ -90,11 +90,12 @@
 
     template<typename MapFunction> GradientColorStops mapColors(MapFunction&& mapFunction) const
     {
-        GradientColorStops result;
-        result.m_stops.reserveInitialCapacity(size());
-        for (auto& stop : m_stops)
-            result.m_stops.uncheckedAppend({ stop.offset, mapFunction(stop.color) });
-        return result;
+        return {
+            m_stops.map<StopVector>([&] (const GradientColorStop& stop) -> GradientColorStop {
+                return { stop.offset, mapFunction(stop.color) };
+            }),
+            m_isSorted
+        };
     }
 
     const StopVector& stops() const { return m_stops; }
@@ -103,6 +104,12 @@
     template<typename Decoder> static std::optional<GradientColorStops> decode(Decoder&);
 
 private:
+    GradientColorStops(StopVector stops, bool isSorted)
+        : m_stops { WTFMove(stops) }
+        , m_isSorted { isSorted }
+    {
+    }
+
 #if ASSERT_ENABLED
     bool validateIsSorted() const
     {

Modified: trunk/Tools/ChangeLog (287471 => 287472)


--- trunk/Tools/ChangeLog	2021-12-28 16:52:05 UTC (rev 287471)
+++ trunk/Tools/ChangeLog	2021-12-28 18:01:43 UTC (rev 287472)
@@ -1,3 +1,13 @@
+2021-12-28  Sam Weinig  <wei...@apple.com>
+
+        Enhance Vector::map to allow specifying what kind of Vector to return (e.g. inline capacity, overflow, etc.)
+        https://bugs.webkit.org/show_bug.cgi?id=234683
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/Tests/WTF/Vector.cpp:
+        Add test for new Vector::map overload.
+
 2021-12-26  Carlos Garcia Campos  <cgar...@igalia.com>
 
         [GTK][a11y] Stop setting manages-descendants on web process root object with ATSPI

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp (287471 => 287472)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2021-12-28 16:52:05 UTC (rev 287471)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2021-12-28 18:01:43 UTC (rev 287472)
@@ -1458,5 +1458,17 @@
     EXPECT_EQ(vector[3], 4);
     EXPECT_EQ(vector[4], 5);
 }
-    
+
+TEST(WTF_Vector, MapCustomReturnType)
+{
+    Vector<int> input { 1, 2 };
+    Vector<float, 2> output = input.map<Vector<float, 2>>([] (int value) {
+        return static_cast<float>(value);
+    });
+
+    ASSERT_EQ(output.size(), input.size());
+    EXPECT_FLOAT_EQ(output[0], 1.0f);
+    EXPECT_FLOAT_EQ(output[1], 2.0f);
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to