Title: [252870] trunk
Revision
252870
Author
[email protected]
Date
2019-11-25 17:55:32 -0800 (Mon, 25 Nov 2019)

Log Message

Add DefaultHash<OptionSet<T>> and HashTrait<OptionSet<T>> specializations
https://bugs.webkit.org/show_bug.cgi?id=204562

Reviewed by Daniel Bates.

Source/WTF:

* WTF.xcodeproj/project.pbxproj: Added OptionSetHash.h
* wtf/CMakeLists.txt: Ditto.
* wtf/OptionSet.h: Made StorageType public, and use C++14 types.
* wtf/OptionSetHash.h: Added.
(WTF::DefaultHash<OptionSet<T>>::Hash::hash):
(WTF::DefaultHash<OptionSet<T>>::Hash::equal):
(WTF::HashTraits<OptionSet<T>>::emptyValue):
(WTF::HashTraits<OptionSet<T>>::constructDeletedValue):
(WTF::HashTraits<OptionSet<T>>::isDeletedValue):

Tools:

* TestWebKitAPI/Tests/WTF/OptionSet.cpp:
Added a new test WTF_OptionSet.HashSet.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (252869 => 252870)


--- trunk/Source/WTF/ChangeLog	2019-11-26 00:28:18 UTC (rev 252869)
+++ trunk/Source/WTF/ChangeLog	2019-11-26 01:55:32 UTC (rev 252870)
@@ -1,3 +1,20 @@
+2019-11-25  Fujii Hironori  <[email protected]>
+
+        Add DefaultHash<OptionSet<T>> and HashTrait<OptionSet<T>> specializations
+        https://bugs.webkit.org/show_bug.cgi?id=204562
+
+        Reviewed by Daniel Bates.
+
+        * WTF.xcodeproj/project.pbxproj: Added OptionSetHash.h
+        * wtf/CMakeLists.txt: Ditto.
+        * wtf/OptionSet.h: Made StorageType public, and use C++14 types.
+        * wtf/OptionSetHash.h: Added.
+        (WTF::DefaultHash<OptionSet<T>>::Hash::hash):
+        (WTF::DefaultHash<OptionSet<T>>::Hash::equal):
+        (WTF::HashTraits<OptionSet<T>>::emptyValue):
+        (WTF::HashTraits<OptionSet<T>>::constructDeletedValue):
+        (WTF::HashTraits<OptionSet<T>>::isDeletedValue):
+
 2019-11-25  Carlos Garcia Campos  <[email protected]>
 
         REGRESSION(r252770): [GTK][WPE] Remote inspector no longer works if debugger is in a different kind of system

Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (252869 => 252870)


--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2019-11-26 00:28:18 UTC (rev 252869)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2019-11-26 01:55:32 UTC (rev 252870)
@@ -330,6 +330,7 @@
 		26147B0815DDCCDC00DDB907 /* IntegerToStringConversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntegerToStringConversion.h; sourceTree = "<group>"; };
 		26299B6D17A9E5B800ADEBE5 /* Ref.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Ref.h; sourceTree = "<group>"; };
 		2684D4351C000D400081D663 /* IndexSparseSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IndexSparseSet.h; sourceTree = "<group>"; };
+		275DFB6B238BDF72001230E2 /* OptionSetHash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OptionSetHash.h; sourceTree = "<group>"; };
 		2C05385315BC819000F21B96 /* GregorianDateTime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GregorianDateTime.h; sourceTree = "<group>"; };
 		2CCD892915C0390200285083 /* GregorianDateTime.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GregorianDateTime.cpp; sourceTree = "<group>"; };
 		2CDED0EE18115C38004DBA70 /* RunLoopCF.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RunLoopCF.cpp; sourceTree = "<group>"; };
@@ -1069,6 +1070,7 @@
 				83A8AC3D1FABBE94002064AC /* ObjectIdentifier.h */,
 				1AFDE6521953B23D00C48FFA /* Optional.h */,
 				1A4656181C7FC68E00F5920F /* OptionSet.h */,
+				275DFB6B238BDF72001230E2 /* OptionSetHash.h */,
 				0F9495831C571CC900413A48 /* OrderMaker.h */,
 				A8A472D7151A825B004123FF /* OSAllocator.h */,
 				7CBBA07319BB7FDC00BBF025 /* OSObjectPtr.h */,

Modified: trunk/Source/WTF/wtf/CMakeLists.txt (252869 => 252870)


--- trunk/Source/WTF/wtf/CMakeLists.txt	2019-11-26 00:28:18 UTC (rev 252869)
+++ trunk/Source/WTF/wtf/CMakeLists.txt	2019-11-26 01:55:32 UTC (rev 252870)
@@ -151,6 +151,7 @@
     ObjCRuntimeExtras.h
     ObjectIdentifier.h
     OptionSet.h
+    OptionSetHash.h
     Optional.h
     OrderMaker.h
     Packed.h

Modified: trunk/Source/WTF/wtf/OptionSet.h (252869 => 252870)


--- trunk/Source/WTF/wtf/OptionSet.h	2019-11-26 00:28:18 UTC (rev 252869)
+++ trunk/Source/WTF/wtf/OptionSet.h	2019-11-26 01:55:32 UTC (rev 252870)
@@ -39,9 +39,10 @@
 template<typename T> class OptionSet {
     WTF_MAKE_FAST_ALLOCATED;
     static_assert(std::is_enum<T>::value, "T is not an enum type");
-    typedef typename std::make_unsigned<typename std::underlying_type<T>::type>::type StorageType;
 
 public:
+    using StorageType = std::make_unsigned_t<std::underlying_type_t<T>>;
+
     template<typename StorageType> class Iterator {
         WTF_MAKE_FAST_ALLOCATED;
     public:

Added: trunk/Source/WTF/wtf/OptionSetHash.h (0 => 252870)


--- trunk/Source/WTF/wtf/OptionSetHash.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/OptionSetHash.h	2019-11-26 01:55:32 UTC (rev 252870)
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2019 Sony Interactive Entertainment Inc.
+ *
+ * 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. AND ITS CONTRIBUTORS ``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 ITS 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 <wtf/HashTraits.h>
+#include <wtf/OptionSet.h>
+
+namespace WTF {
+
+template<typename T> struct DefaultHash<OptionSet<T>> {
+    struct Hash {
+        static unsigned hash(OptionSet<T> key)
+        {
+            return IntHash<typename OptionSet<T>::StorageType>::hash(key.toRaw());
+        }
+
+        static bool equal(OptionSet<T> a, OptionSet<T> b)
+        {
+            return a == b;
+        }
+
+        static constexpr bool safeToCompareToEmptyOrDeleted = true;
+    };
+};
+
+template<typename T> struct HashTraits<OptionSet<T>> : GenericHashTraits<OptionSet<T>> {
+    using StorageTraits = UnsignedWithZeroKeyHashTraits<typename OptionSet<T>::StorageType>;
+
+    static OptionSet<T> emptyValue()
+    {
+        return OptionSet<T>::fromRaw(StorageTraits::emptyValue());
+    }
+
+    static void constructDeletedValue(OptionSet<T>& slot)
+    {
+        typename OptionSet<T>::StorageType storage;
+        StorageTraits::constructDeletedValue(storage);
+        slot = OptionSet<T>::fromRaw(storage);
+    }
+
+    static bool isDeletedValue(OptionSet<T> value)
+    {
+        return StorageTraits::isDeletedValue(value.toRaw());
+    }
+};
+
+} // namespace WTF

Modified: trunk/Tools/ChangeLog (252869 => 252870)


--- trunk/Tools/ChangeLog	2019-11-26 00:28:18 UTC (rev 252869)
+++ trunk/Tools/ChangeLog	2019-11-26 01:55:32 UTC (rev 252870)
@@ -1,3 +1,13 @@
+2019-11-25  Fujii Hironori  <[email protected]>
+
+        Add DefaultHash<OptionSet<T>> and HashTrait<OptionSet<T>> specializations
+        https://bugs.webkit.org/show_bug.cgi?id=204562
+
+        Reviewed by Daniel Bates.
+
+        * TestWebKitAPI/Tests/WTF/OptionSet.cpp:
+        Added a new test WTF_OptionSet.HashSet.
+
 2019-11-25  Philippe Normand  <[email protected]>
 
         [JHBuild] xserver build fails

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/OptionSet.cpp (252869 => 252870)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/OptionSet.cpp	2019-11-26 00:28:18 UTC (rev 252869)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/OptionSet.cpp	2019-11-26 01:55:32 UTC (rev 252870)
@@ -26,7 +26,9 @@
 #include "config.h"
 
 #include "Test.h"
+#include <wtf/HashSet.h>
 #include <wtf/OptionSet.h>
+#include <wtf/OptionSetHash.h>
 
 namespace TestWebKitAPI {
 
@@ -436,4 +438,21 @@
     EXPECT_FALSE(set.containsAll({ ExampleFlags::A, ExampleFlags::B, ExampleFlags::C }));
 }
 
+TEST(WTF_OptionSet, HashSet)
+{
+    HashSet<OptionSet<ExampleFlags>> hashSet;
+    EXPECT_TRUE(hashSet.add(OptionSet<ExampleFlags>()).isNewEntry);
+    EXPECT_TRUE(hashSet.add({ ExampleFlags::A }).isNewEntry);
+    EXPECT_TRUE(hashSet.add({ ExampleFlags::A, ExampleFlags::B }).isNewEntry);
+    EXPECT_FALSE(hashSet.add(OptionSet<ExampleFlags>()).isNewEntry);
+    EXPECT_FALSE(hashSet.add({ ExampleFlags::A }).isNewEntry);
+    EXPECT_FALSE(hashSet.add({ ExampleFlags::A, ExampleFlags::B }).isNewEntry);
+    EXPECT_TRUE(hashSet.remove(OptionSet<ExampleFlags>()));
+    EXPECT_TRUE(hashSet.remove({ ExampleFlags::A }));
+    EXPECT_TRUE(hashSet.remove({ ExampleFlags::A, ExampleFlags::B }));
+    EXPECT_TRUE(hashSet.add(OptionSet<ExampleFlags>()).isNewEntry);
+    EXPECT_TRUE(hashSet.add({ ExampleFlags::A }).isNewEntry);
+    EXPECT_TRUE(hashSet.add({ ExampleFlags::A, ExampleFlags::B }).isNewEntry);
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to