- Revision
- 223039
- Author
- [email protected]
- Date
- 2017-10-08 20:08:13 -0700 (Sun, 08 Oct 2017)
Log Message
There should be a version of copyToVector that returns a Vector, rather than using an out parameter
https://bugs.webkit.org/show_bug.cgi?id=177732
Patch by Sam Weinig <[email protected]> on 2017-10-08
Reviewed by Saam Barati.
Source/WTF:
Add two new helper functions, copyToVector and copyToVectorOf, which will be able to
replace the existing out parameter taking copyToVector. Like it's ancestral namesake,
copyToVector takes an object that is both iterable (supports begin() and end()) and
has a size, and returns a Vector with a copy of all the items iterated. The copyToVectorOf
variant allow the user to specify a type to convert to, so if you have a HashSet<int>
but want those as floats in a Vector, you can do:
auto floatVector = copyToVectorOf<float>(intSet);
* wtf/Vector.h:
(WTF::copyToVector):
(WTF::copyToVectorOf):
Tools:
* TestWebKitAPI/Tests/WTF/Vector.cpp:
(TestWebKitAPI::TEST):
Add tests for the new copyToVector and copyToVectorOf functions.
Modified Paths
Diff
Modified: trunk/Source/WTF/ChangeLog (223038 => 223039)
--- trunk/Source/WTF/ChangeLog 2017-10-09 01:10:43 UTC (rev 223038)
+++ trunk/Source/WTF/ChangeLog 2017-10-09 03:08:13 UTC (rev 223039)
@@ -1,3 +1,23 @@
+2017-10-08 Sam Weinig <[email protected]>
+
+ There should be a version of copyToVector that returns a Vector, rather than using an out parameter
+ https://bugs.webkit.org/show_bug.cgi?id=177732
+
+ Reviewed by Saam Barati.
+
+ Add two new helper functions, copyToVector and copyToVectorOf, which will be able to
+ replace the existing out parameter taking copyToVector. Like it's ancestral namesake,
+ copyToVector takes an object that is both iterable (supports begin() and end()) and
+ has a size, and returns a Vector with a copy of all the items iterated. The copyToVectorOf
+ variant allow the user to specify a type to convert to, so if you have a HashSet<int>
+ but want those as floats in a Vector, you can do:
+
+ auto floatVector = copyToVectorOf<float>(intSet);
+
+ * wtf/Vector.h:
+ (WTF::copyToVector):
+ (WTF::copyToVectorOf):
+
2017-10-08 Darin Adler <[email protected]>
Fix bugs related to setting reflected floating point DOM attributes
Modified: trunk/Source/WTF/wtf/Vector.h (223038 => 223039)
--- trunk/Source/WTF/wtf/Vector.h 2017-10-09 01:10:43 UTC (rev 223038)
+++ trunk/Source/WTF/wtf/Vector.h 2017-10-09 03:08:13 UTC (rev 223039)
@@ -1562,8 +1562,8 @@
return removeRepeatedElements(vector, [] (T& a, T& b) { return a == b; });
}
-template<typename MapFunction, typename SourceType>
-struct MapFunctionInspector {
+template<typename SourceType>
+struct CollectionInspector {
using RealSourceType = typename std::remove_reference<SourceType>::type;
using IteratorType = decltype(std::begin(std::declval<RealSourceType>()));
using SourceItemType = typename std::iterator_traits<IteratorType>::value_type;
@@ -1571,7 +1571,7 @@
template<typename MapFunction, typename SourceType, typename Enable = void>
struct Mapper {
- using SourceItemType = typename MapFunctionInspector<MapFunction, SourceType>::SourceItemType;
+ using SourceItemType = typename CollectionInspector<SourceType>::SourceItemType;
using DestinationItemType = typename std::result_of<MapFunction(SourceItemType&)>::type;
static Vector<DestinationItemType> map(SourceType source, const MapFunction& mapFunction)
@@ -1587,7 +1587,7 @@
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 SourceItemType = typename CollectionInspector<SourceType>::SourceItemType;
using DestinationItemType = typename std::result_of<MapFunction(SourceItemType&&)>::type;
static Vector<DestinationItemType> map(SourceType&& source, const MapFunction& mapFunction)
@@ -1607,10 +1607,29 @@
return Mapper<MapFunction, SourceType>::map(std::forward<SourceType>(source), std::forward<MapFunction>(mapFunction));
}
+template<typename DestinationItemType, typename Collection>
+inline auto copyToVectorOf(const Collection& collection) -> Vector<DestinationItemType>
+{
+ return WTF::map(collection, [] (const auto& v) -> DestinationItemType { return v; });
+}
+
+template<typename Collection>
+struct CopyToVectorResult {
+ using Type = typename std::remove_cv<typename CollectionInspector<Collection>::SourceItemType>::type;
+};
+
+template<typename Collection>
+inline auto copyToVector(const Collection& collection) -> Vector<typename CopyToVectorResult<Collection>::Type>
+{
+ return copyToVectorOf<typename CopyToVectorResult<Collection>::Type>(collection);
+}
+
} // namespace WTF
using WTF::Vector;
using WTF::UnsafeVectorOverflow;
using WTF::removeRepeatedElements;
+using WTF::copyToVector;
+using WTF::copyToVectorOf;
#endif // WTF_Vector_h
Modified: trunk/Tools/ChangeLog (223038 => 223039)
--- trunk/Tools/ChangeLog 2017-10-09 01:10:43 UTC (rev 223038)
+++ trunk/Tools/ChangeLog 2017-10-09 03:08:13 UTC (rev 223039)
@@ -1,3 +1,14 @@
+2017-10-08 Sam Weinig <[email protected]>
+
+ There should be a version of copyToVector that returns a Vector, rather than using an out parameter
+ https://bugs.webkit.org/show_bug.cgi?id=177732
+
+ Reviewed by Saam Barati.
+
+ * TestWebKitAPI/Tests/WTF/Vector.cpp:
+ (TestWebKitAPI::TEST):
+ Add tests for the new copyToVector and copyToVectorOf functions.
+
2017-10-08 Darin Adler <[email protected]>
Fix bugs related to setting reflected floating point DOM attributes
Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp (223038 => 223039)
--- trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp 2017-10-09 01:10:43 UTC (rev 223038)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp 2017-10-09 03:08:13 UTC (rev 223039)
@@ -27,6 +27,8 @@
#include "MoveOnly.h"
#include <wtf/HashMap.h>
+#include <wtf/HashSet.h>
+#include <wtf/ListHashSet.h>
#include <wtf/Vector.h>
#include <wtf/text/StringHash.h>
#include <wtf/text/WTFString.h>
@@ -775,4 +777,45 @@
}
+TEST(WTF_Vector, CopyToVector)
+{
+ HashSet<int> intSet { 1, 2, 3 };
+
+ auto vector = copyToVector(intSet);
+ EXPECT_EQ(3U, vector.size());
+
+ std::sort(vector.begin(), vector.end());
+ EXPECT_EQ(1, vector[0]);
+ EXPECT_EQ(2, vector[1]);
+ EXPECT_EQ(3, vector[2]);
+}
+
+TEST(WTF_Vector, CopyToVectorOrderPreserving)
+{
+ ListHashSet<int> orderedIntSet;
+ orderedIntSet.add(1);
+ orderedIntSet.add(2);
+ orderedIntSet.add(3);
+
+ auto vector = copyToVector(orderedIntSet);
+ EXPECT_EQ(3U, vector.size());
+
+ EXPECT_EQ(1, vector[0]);
+ EXPECT_EQ(2, vector[1]);
+ EXPECT_EQ(3, vector[2]);
+}
+
+TEST(WTF_Vector, CopyToVectorOf)
+{
+ HashSet<int> intSet { 1, 2, 3 };
+
+ Vector<float> vector = copyToVectorOf<float>(intSet);
+ EXPECT_EQ(3U, vector.size());
+
+ std::sort(vector.begin(), vector.end());
+ EXPECT_FLOAT_EQ(1, vector[0]);
+ EXPECT_FLOAT_EQ(2, vector[1]);
+ EXPECT_FLOAT_EQ(3, vector[2]);
+}
+
} // namespace TestWebKitAPI