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/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