Title: [197123] trunk
Revision
197123
Author
[email protected]
Date
2016-02-25 11:28:42 -0800 (Thu, 25 Feb 2016)

Log Message

Source/WTF:
HashMap::ensure() should return an AddResult like all the other add-like functions.
https://bugs.webkit.org/show_bug.cgi?id=154680

Reviewed by Anders Carlsson.

While adopting HashMap::ensure(), I found it was useful in some circumstances to know
if the value was added or not. While I could discern this information by setting a bool
in the passed in lambda, it seemed clearer and more idiomatic to just have ensure return
an AddResult like all the other add-like functions do.

* wtf/HashMap.h:
Change return type of HashMap::ensure() to be an AddResult.

Tools:
HashMap::ensure() should return an AddResult like all the other add-like functions
https://bugs.webkit.org/show_bug.cgi?id=154680

Reviewed by Anders Carlsson.

* TestWebKitAPI/Tests/WTF/HashMap.cpp:
(TestWebKitAPI::TEST):
Update tests to use/test the new AddResult result.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (197122 => 197123)


--- trunk/Source/WTF/ChangeLog	2016-02-25 19:21:49 UTC (rev 197122)
+++ trunk/Source/WTF/ChangeLog	2016-02-25 19:28:42 UTC (rev 197123)
@@ -1,3 +1,18 @@
+2016-02-25  Sam Weinig  <[email protected]>
+
+        HashMap::ensure() should return an AddResult like all the other add-like functions.
+        https://bugs.webkit.org/show_bug.cgi?id=154680
+
+        Reviewed by Anders Carlsson.
+
+        While adopting HashMap::ensure(), I found it was useful in some circumstances to know
+        if the value was added or not. While I could discern this information by setting a bool
+        in the passed in lambda, it seemed clearer and more idiomatic to just have ensure return
+        an AddResult like all the other add-like functions do.
+
+        * wtf/HashMap.h:
+        Change return type of HashMap::ensure() to be an AddResult.
+
 2016-02-24  Nikos Andronikos  <[email protected]>
 
         [web-animations] Add AnimationTimeline, DocumentTimeline and add extensions to Document interface

Modified: trunk/Source/WTF/wtf/HashMap.h (197122 => 197123)


--- trunk/Source/WTF/wtf/HashMap.h	2016-02-25 19:21:49 UTC (rev 197122)
+++ trunk/Source/WTF/wtf/HashMap.h	2016-02-25 19:28:42 UTC (rev 197123)
@@ -118,8 +118,8 @@
     template<typename V> AddResult fastAdd(const KeyType&, V&&);
     template<typename V> AddResult fastAdd(KeyType&&, V&&);
 
-    template<typename Functor> MappedType& ensure(const KeyType&, const Functor&);
-    template<typename Functor> MappedType& ensure(KeyType&&, const Functor&);
+    template<typename Functor> AddResult ensure(const KeyType&, const Functor&);
+    template<typename Functor> AddResult ensure(KeyType&&, const Functor&);
 
     bool remove(const KeyType&);
     bool remove(iterator);
@@ -167,7 +167,7 @@
     AddResult inlineAdd(K&&, V&&);
 
     template<typename K, typename F>
-    MappedType& inlineEnsure(K&&, const F&);
+    AddResult inlineEnsure(K&&, const F&);
 
     HashTableType m_impl;
 };
@@ -315,9 +315,9 @@
 
 template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
 template<typename K, typename F>
-ALWAYS_INLINE auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::inlineEnsure(K&& key, const F& functor) -> MappedType&
+ALWAYS_INLINE auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::inlineEnsure(K&& key, const F& functor) -> AddResult
 {
-    return m_impl.template add<HashMapEnsureTranslator<KeyValuePairTraits, HashFunctions>>(std::forward<K>(key), functor).iterator->value;
+    return m_impl.template add<HashMapEnsureTranslator<KeyValuePairTraits, HashFunctions>>(std::forward<K>(key), functor);
 }
 
 template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
@@ -371,14 +371,14 @@
 
 template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
 template<typename Functor>
-auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::ensure(const KeyType& key, const Functor& functor) -> MappedType&
+auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::ensure(const KeyType& key, const Functor& functor) -> AddResult
 {
     return inlineEnsure(key, functor);
 }
 
 template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
 template<typename Functor>
-auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::ensure(KeyType&& key, const Functor& functor) -> MappedType&
+auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::ensure(KeyType&& key, const Functor& functor) -> AddResult
 {
     return inlineEnsure(WTFMove(key), functor);
 }

Modified: trunk/Tools/ChangeLog (197122 => 197123)


--- trunk/Tools/ChangeLog	2016-02-25 19:21:49 UTC (rev 197122)
+++ trunk/Tools/ChangeLog	2016-02-25 19:28:42 UTC (rev 197123)
@@ -1,3 +1,14 @@
+2016-02-25  Sam Weinig  <[email protected]>
+
+        HashMap::ensure() should return an AddResult like all the other add-like functions
+        https://bugs.webkit.org/show_bug.cgi?id=154680
+
+        Reviewed by Anders Carlsson.
+
+        * TestWebKitAPI/Tests/WTF/HashMap.cpp:
+        (TestWebKitAPI::TEST):
+        Update tests to use/test the new AddResult result.
+
 2016-02-25  Alexey Proskuryakov  <[email protected]>
 
         Enable MallocScribble when detecting leaks

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp (197122 => 197123)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp	2016-02-25 19:21:49 UTC (rev 197122)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp	2016-02-25 19:28:42 UTC (rev 197123)
@@ -512,10 +512,14 @@
 {
     HashMap<unsigned, unsigned> map;
     {
-        auto value = map.ensure(1, [] { return 1; });
-        EXPECT_EQ(1u, value);
-        value = map.ensure(1, [] { return 2; });
-        EXPECT_EQ(1u, value);
+        auto addResult = map.ensure(1, [] { return 1; });
+        EXPECT_EQ(1u, addResult.iterator->value);
+        EXPECT_EQ(1u, addResult.iterator->key);
+        EXPECT_TRUE(addResult.isNewEntry);
+        auto addResult2 = map.ensure(1, [] { return 2; });
+        EXPECT_EQ(1u, addResult2.iterator->value);
+        EXPECT_EQ(1u, addResult2.iterator->key);
+        EXPECT_FALSE(addResult2.isNewEntry);
     }
 }
 
@@ -523,10 +527,14 @@
 {
     HashMap<unsigned, MoveOnly> moveOnlyValues;
     {
-        auto& value = moveOnlyValues.ensure(1, [] { return MoveOnly(1); });
-        EXPECT_EQ(1u, value.value());
-        auto& value2 = moveOnlyValues.ensure(1, [] { return MoveOnly(2); });
-        EXPECT_EQ(1u, value2.value());
+        auto addResult = moveOnlyValues.ensure(1, [] { return MoveOnly(1); });
+        EXPECT_EQ(1u, addResult.iterator->value.value());
+        EXPECT_EQ(1u, addResult.iterator->key);
+        EXPECT_TRUE(addResult.isNewEntry);
+        auto addResult2 = moveOnlyValues.ensure(1, [] { return MoveOnly(2); });
+        EXPECT_EQ(1u, addResult2.iterator->value.value());
+        EXPECT_EQ(1u, addResult2.iterator->key);
+        EXPECT_FALSE(addResult2.isNewEntry);
     }
 }
 
@@ -534,12 +542,16 @@
 {
     HashMap<unsigned, std::unique_ptr<unsigned>> map;
     {
-        auto& value = map.ensure(1, [] { return std::make_unique<unsigned>(1); });
+        auto addResult = map.ensure(1, [] { return std::make_unique<unsigned>(1); });
         EXPECT_EQ(1u, *map.get(1));
-        EXPECT_EQ(1u, *value.get());
-        auto& value2 = map.ensure(1, [] { return std::make_unique<unsigned>(2); });
+        EXPECT_EQ(1u, *addResult.iterator->value.get());
+        EXPECT_EQ(1u, addResult.iterator->key);
+        EXPECT_TRUE(addResult.isNewEntry);
+        auto addResult2 = map.ensure(1, [] { return std::make_unique<unsigned>(2); });
         EXPECT_EQ(1u, *map.get(1));
-        EXPECT_EQ(1u, *value2.get());
+        EXPECT_EQ(1u, *addResult2.iterator->value.get());
+        EXPECT_EQ(1u, addResult2.iterator->key);
+        EXPECT_FALSE(addResult2.isNewEntry);
     }
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to