Title: [253775] trunk
Revision
253775
Author
[email protected]
Date
2019-12-19 11:29:03 -0800 (Thu, 19 Dec 2019)

Log Message

Allow Vectors as hash keys
https://bugs.webkit.org/show_bug.cgi?id=205449

Reviewed by Geoff Garen.

Source/WTF:

Add traits to allow Vectors of hashable types to act as hash keys.

* WTF.xcodeproj/project.pbxproj:
* wtf/Vector.h:
(WTF::Vector::Vector):
(WTF::Vector::~Vector):
(WTF::Vector::isHashTableDeletedValue const):

Use m_size = numeric_limits::max() as the deleted value.

* wtf/VectorHash.h: Added.
(WTF::VectorHash::hash):
(WTF::VectorHash::equal):

Add traits. Empty Vector is the empty value.

Tools:

* TestWebKitAPI/Tests/WTF/Vector.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (253774 => 253775)


--- trunk/Source/WTF/ChangeLog	2019-12-19 19:27:21 UTC (rev 253774)
+++ trunk/Source/WTF/ChangeLog	2019-12-19 19:29:03 UTC (rev 253775)
@@ -1,3 +1,26 @@
+2019-12-19  Antti Koivisto  <[email protected]>
+
+        Allow Vectors as hash keys
+        https://bugs.webkit.org/show_bug.cgi?id=205449
+
+        Reviewed by Geoff Garen.
+
+        Add traits to allow Vectors of hashable types to act as hash keys.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/Vector.h:
+        (WTF::Vector::Vector):
+        (WTF::Vector::~Vector):
+        (WTF::Vector::isHashTableDeletedValue const):
+
+        Use m_size = numeric_limits::max() as the deleted value.
+
+        * wtf/VectorHash.h: Added.
+        (WTF::VectorHash::hash):
+        (WTF::VectorHash::equal):
+
+        Add traits. Empty Vector is the empty value.
+
 2019-12-19  Jer Noble  <[email protected]>
 
         Safely iterate observers in languageDidChange()

Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (253774 => 253775)


--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2019-12-19 19:27:21 UTC (rev 253774)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2019-12-19 19:29:03 UTC (rev 253775)
@@ -687,6 +687,7 @@
 		E3CF76902115D6BA0091DE48 /* CompactPointerTuple.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompactPointerTuple.h; sourceTree = "<group>"; };
 		E3E158251EADA53C004A079D /* SystemFree.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SystemFree.h; sourceTree = "<group>"; };
 		E3E64F0B22813428001E55B4 /* Nonmovable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Nonmovable.h; sourceTree = "<group>"; };
+		E419F2E623AB9E2300B26129 /* VectorHash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VectorHash.h; sourceTree = "<group>"; };
 		E431CC4A21187ADB000C8A07 /* DispatchSPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DispatchSPI.h; sourceTree = "<group>"; };
 		E4A0AD371A96245500536DF6 /* WorkQueue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WorkQueue.cpp; sourceTree = "<group>"; };
 		E4A0AD381A96245500536DF6 /* WorkQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorkQueue.h; sourceTree = "<group>"; };
@@ -1219,6 +1220,7 @@
 				A8A4736F151A825B004123FF /* ValueCheck.h */,
 				7CD0D5A71D55322A000CC9E1 /* Variant.h */,
 				A8A47370151A825B004123FF /* Vector.h */,
+				E419F2E623AB9E2300B26129 /* VectorHash.h */,
 				A8A47371151A825B004123FF /* VectorTraits.h */,
 				A8A47372151A825B004123FF /* VMTags.h */,
 				0F66B2881DC97BAB004A1D3F /* WallTime.cpp */,

Modified: trunk/Source/WTF/wtf/CMakeLists.txt (253774 => 253775)


--- trunk/Source/WTF/wtf/CMakeLists.txt	2019-12-19 19:27:21 UTC (rev 253774)
+++ trunk/Source/WTF/wtf/CMakeLists.txt	2019-12-19 19:29:03 UTC (rev 253775)
@@ -267,6 +267,7 @@
     ValueCheck.h
     Variant.h
     Vector.h
+    VectorHash.h
     VectorTraits.h
     WTFSemaphore.h
     WallTime.h

Modified: trunk/Source/WTF/wtf/Vector.h (253774 => 253775)


--- trunk/Source/WTF/wtf/Vector.h	2019-12-19 19:27:21 UTC (rev 253774)
+++ trunk/Source/WTF/wtf/Vector.h	2019-12-19 19:29:03 UTC (rev 253775)
@@ -666,6 +666,11 @@
         return result;
     }
 
+    Vector(WTF::HashTableDeletedValueType)
+        : Base(0, std::numeric_limits<decltype(m_size)>::max())
+    {
+    }
+
     ~Vector()
     {
         if (m_size)
@@ -828,6 +833,8 @@
 
     template<typename MapFunction, typename R = typename std::result_of<MapFunction(const T&)>::type> Vector<R> map(MapFunction) const;
 
+    bool isHashTableDeletedValue() const { return m_size == std::numeric_limits<decltype(m_size)>::max(); }
+
 private:
     void expandCapacity(size_t newMinCapacity);
     T* expandCapacity(size_t newMinCapacity, T*);

Added: trunk/Source/WTF/wtf/VectorHash.h (0 => 253775)


--- trunk/Source/WTF/wtf/VectorHash.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/VectorHash.h	2019-12-19 19:29:03 UTC (rev 253775)
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "HashFunctions.h"
+#include "HashTraits.h"
+#include "Hasher.h"
+
+namespace WTF {
+
+template<typename T, size_t inlineCapacity>
+struct VectorHash {
+    static unsigned hash(const Vector<T, inlineCapacity>& vector)
+    {
+        IntegerHasher hasher;
+        for (const auto& value : vector)
+            hasher.add(DefaultHash<T>::Hash::hash(value));
+        return hasher.hash();
+    }
+    static bool equal(const Vector<T, inlineCapacity>& a, const Vector<T, inlineCapacity>& b)
+    {
+        return a == b;
+    }
+    static constexpr bool safeToCompareToEmptyOrDeleted = true;
+};
+
+template<typename T, size_t inlineCapacity>
+struct HashTraits<Vector<T, inlineCapacity>> : GenericHashTraits<Vector<T, inlineCapacity>> {
+    static constexpr bool emptyValueIsZero = !inlineCapacity;
+
+    static void constructDeletedValue(Vector<T, inlineCapacity>& slot) { new (NotNull, std::addressof(slot)) Vector<T, inlineCapacity>(WTF::HashTableDeletedValue); }
+    static bool isDeletedValue(const Vector<T, inlineCapacity>& value) { return value.isHashTableDeletedValue(); }
+};
+
+template<typename T, size_t inlineCapacity>
+struct DefaultHash<Vector<T, inlineCapacity>> {
+    using Hash = VectorHash<T, inlineCapacity>;
+};
+
+}

Modified: trunk/Tools/ChangeLog (253774 => 253775)


--- trunk/Tools/ChangeLog	2019-12-19 19:27:21 UTC (rev 253774)
+++ trunk/Tools/ChangeLog	2019-12-19 19:29:03 UTC (rev 253775)
@@ -1,3 +1,13 @@
+2019-12-19  Antti Koivisto  <[email protected]>
+
+        Allow Vectors as hash keys
+        https://bugs.webkit.org/show_bug.cgi?id=205449
+
+        Reviewed by Geoff Garen.
+
+        * TestWebKitAPI/Tests/WTF/Vector.cpp:
+        (TestWebKitAPI::TEST):
+
 2019-12-19  Carlos Garcia Campos  <[email protected]>
 
         [GTK][WPE] Add initial API for input method

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp (253774 => 253775)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2019-12-19 19:27:21 UTC (rev 253774)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2019-12-19 19:29:03 UTC (rev 253775)
@@ -30,6 +30,7 @@
 #include <wtf/HashSet.h>
 #include <wtf/ListHashSet.h>
 #include <wtf/Vector.h>
+#include <wtf/VectorHash.h>
 #include <wtf/text/StringHash.h>
 #include <wtf/text/WTFString.h>
 
@@ -912,5 +913,201 @@
     Vector<String> b = {{ "a" }};
     EXPECT_TRUE(a == b);
 }
+
+TEST(WTF_Vector, HashKey)
+{
+    Vector<int> a = { 1 };
+    Vector<int> b = { 1, 2 };
+
+    HashSet<Vector<int>> hash;
+
+    EXPECT_FALSE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    hash.add(a);
+
+    EXPECT_TRUE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    hash.add(a);
+
+    EXPECT_TRUE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    hash.add(b);
+
+    EXPECT_TRUE(hash.contains(a));
+    EXPECT_TRUE(hash.contains(b));
+
+    hash.remove(a);
+
+    EXPECT_FALSE(hash.contains(a));
+    EXPECT_TRUE(hash.contains(b));
+
+    hash.remove(b);
+
+    EXPECT_FALSE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+    EXPECT_TRUE(hash.isEmpty());
+
+    for (int i = 0; i < 10; ++i) {
+        for (int j = 0; j < 10; ++j) {
+            hash.add({ i, j });
+            hash.add({ i, j, i });
+        }
+    }
+
+    for (int i = 0; i < 10; ++i) {
+        for (int j = 0; j < 10; ++j) {
+            EXPECT_TRUE(hash.contains({ i, j }));
+            EXPECT_TRUE(hash.contains({ i, j, i }));
+            EXPECT_FALSE(hash.contains({ 10, j }));
+            EXPECT_FALSE(hash.contains({ i, j, 10 }));
+        }
+    }
+
+    for (int i = 0; i < 5; ++i) {
+        for (int j = 0; j < 10; ++j) {
+            hash.remove({ i, j });
+            hash.remove({ i, j, i });
+        }
+    }
+
+    for (int i = 0; i < 10; ++i) {
+        for (int j = 0; j < 10; ++j) {
+            EXPECT_EQ(hash.contains({ i, j }), i > 4);
+            EXPECT_EQ(hash.contains({ i, j, i }), i > 4);
+        }
+    }
+
+    for (int i = 5; i < 10; ++i) {
+        for (int j = 0; j < 10; ++j) {
+            hash.remove({ i, j });
+            hash.remove({ i, j, i });
+        }
+    }
+
+    EXPECT_TRUE(hash.isEmpty());
+}
+
+TEST(WTF_Vector, HashKeyInlineCapacity)
+{
+    Vector<int, 1> a = { 1 };
+    Vector<int, 1> b = { 1, 2 };
+
+    HashSet<Vector<int, 1>> hash;
+
+    EXPECT_FALSE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    hash.add(a);
+
+    EXPECT_TRUE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    hash.add(a);
+
+    EXPECT_TRUE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    hash.add(b);
+
+    EXPECT_TRUE(hash.contains(a));
+    EXPECT_TRUE(hash.contains(b));
+
+    hash.remove(a);
+
+    EXPECT_FALSE(hash.contains(a));
+    EXPECT_TRUE(hash.contains(b));
+
+    hash.remove(b);
+
+    EXPECT_FALSE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+    EXPECT_TRUE(hash.isEmpty());
+}
+
+TEST(WTF_Vector, HashKeyString)
+{
+    Vector<String> a = { "a" };
+    Vector<String> b = { "a", "b" };
+
+    HashSet<Vector<String>> hash;
+
+    EXPECT_FALSE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    hash.add(a);
+
+    EXPECT_TRUE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    hash.add(a);
+
+    EXPECT_TRUE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    hash.add(b);
+
+    EXPECT_TRUE(hash.contains(a));
+    EXPECT_TRUE(hash.contains(b));
+
+    hash.remove(a);
+
+    EXPECT_FALSE(hash.contains(a));
+    EXPECT_TRUE(hash.contains(b));
+
+    hash.remove(b);
+
+    EXPECT_FALSE(hash.contains(a));
+    EXPECT_FALSE(hash.contains(b));
+
+    for (auto i : { "a", "b", "c", "d", "e" }) {
+        for (auto j : { "a", "b", "c", "d", "e" }) {
+            hash.add({ i, j });
+            hash.add({ i, j, i, j });
+        }
+    }
+
+    for (auto i : { "a", "b", "c", "d", "e" }) {
+        for (auto j : { "a", "b", "c", "d", "e" }) {
+            EXPECT_TRUE(hash.contains({ i, j }));
+            EXPECT_TRUE(hash.contains({ i, j, i, j }));
+            EXPECT_FALSE(hash.contains({ i, j, i }));
+            EXPECT_FALSE(hash.contains({ "x", j }));
+            EXPECT_FALSE(hash.contains({ i, j, "x", j }));
+        }
+    }
+
+    for (auto i : { "d", "e" }) {
+        for (auto j : { "a", "b", "c", "d", "e" }) {
+            hash.remove({ i, j });
+            hash.remove({ i, j, i, j });
+        }
+    }
+
+    for (auto i : { "d", "e" }) {
+        for (auto j : { "a", "b", "c", "d", "e" }) {
+            EXPECT_FALSE(hash.contains({ i, j }));
+            EXPECT_FALSE(hash.contains({ i, j, i, j }));
+        }
+    }
+
+    for (auto i : { "a", "b", "c" }) {
+        for (auto j : { "a", "b", "c", "d", "e" }) {
+            EXPECT_TRUE(hash.contains({ i, j }));
+            EXPECT_TRUE(hash.contains({ i, j, i, j }));
+        }
+    }
+
+    for (auto i : { "a", "b", "c" }) {
+        for (auto j : { "a", "b", "c", "d", "e" }) {
+            hash.remove({ i, j });
+            hash.remove({ i, j, i, j });
+        }
+    }
+
+    EXPECT_TRUE(hash.isEmpty());
+}
     
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to