Title: [238467] trunk
Revision
238467
Author
[email protected]
Date
2018-11-23 22:08:31 -0800 (Fri, 23 Nov 2018)

Log Message

Add raw pointer overloads to ListHashSet via SmartPtr specialized functions
https://bugs.webkit.org/show_bug.cgi?id=191936

Patch by Sam Weinig <[email protected]> on 2018-11-23
Reviewed by Zalan Bujtas.

Source/WTF:

Adds overloads for find, contains, insertBefore and remove that take raw pointers
when the value type V of a ListHashSet is true for the predicate IsSmartPtr<V>::value.
This brings the interface to ListHashSet closer inline with HashSet, HashMap and HashCountedSet
which already have this functionality. Like in the other collections, this is especially
useful when using std::unique_ptr<> as the value, since there would be no way to pass it
to these functions. One difference between this set of overloads is the inclusion of insertBefore,
which is unique to ListHashSet. As would be expected, this specialization only changes the first
parameter, the one that needs to be found, to support a raw pointer.

* wtf/ListHashSet.h:
(WTF::U>::find):
(WTF::U>::find const):
(WTF::U>::contains const):
(WTF::U>::insertBefore):
(WTF::U>::remove):

Tools:

Adds tests for raw pointer overloads in ListHashSet.

* TestWebKitAPI/Tests/WTF/HashSet.cpp:
* TestWebKitAPI/Tests/WTF/ListHashSet.cpp:

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (238466 => 238467)


--- trunk/Source/WTF/ChangeLog	2018-11-24 03:51:16 UTC (rev 238466)
+++ trunk/Source/WTF/ChangeLog	2018-11-24 06:08:31 UTC (rev 238467)
@@ -1,3 +1,26 @@
+2018-11-23  Sam Weinig  <[email protected]>
+
+        Add raw pointer overloads to ListHashSet via SmartPtr specialized functions
+        https://bugs.webkit.org/show_bug.cgi?id=191936
+
+        Reviewed by Zalan Bujtas.
+
+        Adds overloads for find, contains, insertBefore and remove that take raw pointers
+        when the value type V of a ListHashSet is true for the predicate IsSmartPtr<V>::value.
+        This brings the interface to ListHashSet closer inline with HashSet, HashMap and HashCountedSet
+        which already have this functionality. Like in the other collections, this is especially
+        useful when using std::unique_ptr<> as the value, since there would be no way to pass it
+        to these functions. One difference between this set of overloads is the inclusion of insertBefore,
+        which is unique to ListHashSet. As would be expected, this specialization only changes the first
+        parameter, the one that needs to be found, to support a raw pointer.  
+
+        * wtf/ListHashSet.h:
+        (WTF::U>::find):
+        (WTF::U>::find const):
+        (WTF::U>::contains const):
+        (WTF::U>::insertBefore):
+        (WTF::U>::remove):
+
 2018-11-21  Yusuke Suzuki  <[email protected]>
 
         [JSC] Drop ARM_TRADITIONAL support in LLInt, baseline JIT, and DFG

Modified: trunk/Source/WTF/wtf/ListHashSet.h (238466 => 238467)


--- trunk/Source/WTF/wtf/ListHashSet.h	2018-11-24 03:51:16 UTC (rev 238466)
+++ trunk/Source/WTF/wtf/ListHashSet.h	2018-11-24 06:08:31 UTC (rev 238467)
@@ -142,6 +142,14 @@
     bool remove(iterator);
     void clear();
 
+    // Overloads for smart pointer values that take the raw pointer type as the parameter.
+    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, iterator>::type find(typename GetPtrHelper<V>::PtrType);
+    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, const_iterator>::type find(typename GetPtrHelper<V>::PtrType) const;
+    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, bool>::type contains(typename GetPtrHelper<V>::PtrType) const;
+    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, AddResult>::type insertBefore(typename GetPtrHelper<V>::PtrType, const ValueType&);
+    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, AddResult>::type insertBefore(typename GetPtrHelper<V>::PtrType, ValueType&&);
+    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, bool>::type remove(typename GetPtrHelper<V>::PtrType);
+
 private:
     void unlink(Node*);
     void unlinkAndDelete(Node*);
@@ -626,6 +634,54 @@
 }
 
 template<typename T, typename U>
+template<typename V>
+inline auto ListHashSet<T, U>::find(typename GetPtrHelper<V>::PtrType value) -> typename std::enable_if<IsSmartPtr<V>::value, iterator>::type
+{
+    auto it = m_impl.template find<BaseTranslator>(value);
+    if (it == m_impl.end())
+        return end();
+    return makeIterator(*it);
+}
+
+template<typename T, typename U>
+template<typename V>
+inline auto ListHashSet<T, U>::find(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, const_iterator>::type
+{
+    auto it = m_impl.template find<BaseTranslator>(value);
+    if (it == m_impl.end())
+        return end();
+    return makeConstIterator(*it);
+}
+
+template<typename T, typename U>
+template<typename V>
+inline auto ListHashSet<T, U>::contains(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, bool>::type
+{
+    return m_impl.template contains<BaseTranslator>(value);
+}
+
+template<typename T, typename U>
+template<typename V>
+inline auto ListHashSet<T, U>::insertBefore(typename GetPtrHelper<V>::PtrType beforeValue, const ValueType& newValue) -> typename std::enable_if<IsSmartPtr<V>::value, AddResult>::type
+{
+    return insertBefore(find(beforeValue), newValue);
+}
+
+template<typename T, typename U>
+template<typename V>
+inline auto ListHashSet<T, U>::insertBefore(typename GetPtrHelper<V>::PtrType beforeValue, ValueType&& newValue) -> typename std::enable_if<IsSmartPtr<V>::value, AddResult>::type
+{
+    return insertBefore(find(beforeValue), WTFMove(newValue));
+}
+
+template<typename T, typename U>
+template<typename V>
+inline auto ListHashSet<T, U>::remove(typename GetPtrHelper<V>::PtrType value) -> typename std::enable_if<IsSmartPtr<V>::value, bool>::type
+{
+    return remove(find(value));
+}
+
+template<typename T, typename U>
 void ListHashSet<T, U>::unlink(Node* node)
 {
     if (!node->m_prev) {

Modified: trunk/Tools/ChangeLog (238466 => 238467)


--- trunk/Tools/ChangeLog	2018-11-24 03:51:16 UTC (rev 238466)
+++ trunk/Tools/ChangeLog	2018-11-24 06:08:31 UTC (rev 238467)
@@ -1,3 +1,15 @@
+2018-11-23  Sam Weinig  <[email protected]>
+
+        Add raw pointer overloads to ListHashSet via SmartPtr specialized functions
+        https://bugs.webkit.org/show_bug.cgi?id=191936
+
+        Reviewed by Zalan Bujtas.
+
+        Adds tests for raw pointer overloads in ListHashSet.
+
+        * TestWebKitAPI/Tests/WTF/HashSet.cpp:
+        * TestWebKitAPI/Tests/WTF/ListHashSet.cpp:
+
 2018-11-23  Wenson Hsieh  <[email protected]>
 
         Enable drag and drop support for iOSMac

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/HashSet.cpp (238466 => 238467)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/HashSet.cpp	2018-11-24 03:51:16 UTC (rev 238466)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/HashSet.cpp	2018-11-24 06:08:31 UTC (rev 238467)
@@ -36,7 +36,7 @@
 namespace TestWebKitAPI {
 
 template<int initialCapacity>
-    struct InitialCapacityTestHashTraits : public WTF::UnsignedWithZeroKeyHashTraits<int> {
+struct InitialCapacityTestHashTraits : public WTF::UnsignedWithZeroKeyHashTraits<int> {
     static const int minimumTableSize = initialCapacity;
 };
 

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/ListHashSet.cpp (238466 => 238467)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/ListHashSet.cpp	2018-11-24 03:51:16 UTC (rev 238466)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/ListHashSet.cpp	2018-11-24 06:08:31 UTC (rev 238467)
@@ -25,6 +25,7 @@
 
 #include "config.h"
 
+#include "Counters.h"
 #include "MoveOnly.h"
 #include <wtf/ListHashSet.h>
 
@@ -373,4 +374,106 @@
     ++iterator;
 }
 
+TEST(WTF_ListHashSet, UniquePtrKey)
+{
+    ConstructorDestructorCounter::TestingScope scope;
+
+    ListHashSet<std::unique_ptr<ConstructorDestructorCounter>> list;
+
+    auto uniquePtr = std::make_unique<ConstructorDestructorCounter>();
+    list.add(WTFMove(uniquePtr));
+
+    EXPECT_EQ(1u, ConstructorDestructorCounter::constructionCount);
+    EXPECT_EQ(0u, ConstructorDestructorCounter::destructionCount);
+
+    list.clear();
+
+    EXPECT_EQ(1u, ConstructorDestructorCounter::constructionCount);
+    EXPECT_EQ(1u, ConstructorDestructorCounter::destructionCount);
+}
+
+TEST(WTF_ListHashSet, UniquePtrKey_FindUsingRawPointer)
+{
+    ListHashSet<std::unique_ptr<int>> list;
+
+    auto uniquePtr = std::make_unique<int>(5);
+    auto ptr = uniquePtr.get();
+    list.add(WTFMove(uniquePtr));
+
+    auto it = list.find(ptr);
+    ASSERT_TRUE(it != list.end());
+    EXPECT_EQ(ptr, it->get());
+    EXPECT_EQ(5, *it->get());
+}
+
+TEST(WTF_ListHashSet, UniquePtrKey_ContainsUsingRawPointer)
+{
+    ListHashSet<std::unique_ptr<int>> list;
+
+    auto uniquePtr = std::make_unique<int>(5);
+    auto ptr = uniquePtr.get();
+    list.add(WTFMove(uniquePtr));
+
+    EXPECT_EQ(true, list.contains(ptr));
+}
+
+TEST(WTF_ListHashSet, UniquePtrKey_InsertBeforeUsingRawPointer)
+{
+    ListHashSet<std::unique_ptr<int>> list;
+
+    auto uniquePtrWith2 = std::make_unique<int>(2);
+    auto ptrWith2 = uniquePtrWith2.get();
+    auto uniquePtrWith4 = std::make_unique<int>(4);
+    auto ptrWith4 = uniquePtrWith4.get();
+
+    list.add(WTFMove(uniquePtrWith2));
+    list.add(WTFMove(uniquePtrWith4));
+
+    // { 2, 4 }
+    ASSERT_EQ(ptrWith2, list.first().get());
+    ASSERT_EQ(2, *list.first().get());
+    ASSERT_EQ(ptrWith4, list.last().get());
+    ASSERT_EQ(4, *list.last().get());
+
+    auto uniquePtrWith3 = std::make_unique<int>(3);
+    auto ptrWith3 = uniquePtrWith3.get();
+
+    list.insertBefore(ptrWith4, WTFMove(uniquePtrWith3));
+    
+    // { 2, 3, 4 }
+    auto firstWith2 = list.takeFirst();
+    ASSERT_EQ(ptrWith2, firstWith2.get());
+    ASSERT_EQ(2, *firstWith2);
+
+    auto firstWith3 = list.takeFirst();
+    ASSERT_EQ(ptrWith3, firstWith3.get());
+    ASSERT_EQ(3, *firstWith3);
+
+    auto firstWith4 = list.takeFirst();
+    ASSERT_EQ(ptrWith2, firstWith4.get());
+    ASSERT_EQ(4, *firstWith4);
+
+    ASSERT_TRUE(list.isEmpty());
+}
+
+TEST(WTF_ListHashSet, UniquePtrKey_RemoveUsingRawPointer)
+{
+    ConstructorDestructorCounter::TestingScope scope;
+
+    ListHashSet<std::unique_ptr<ConstructorDestructorCounter>> list;
+
+    auto uniquePtr = std::make_unique<ConstructorDestructorCounter>();
+    auto* ptr = uniquePtr.get();
+    list.add(WTFMove(uniquePtr));
+
+    EXPECT_EQ(1u, ConstructorDestructorCounter::constructionCount);
+    EXPECT_EQ(0u, ConstructorDestructorCounter::destructionCount);
+
+    bool result = list.remove(ptr);
+    EXPECT_EQ(true, result);
+
+    EXPECT_EQ(1u, ConstructorDestructorCounter::constructionCount);
+    EXPECT_EQ(1u, ConstructorDestructorCounter::destructionCount);
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to