Bug 1370632 - Move LookupRemoveIf() to nsBaseHashtable instead so that it can be used on more hashtables types. r=froydnj
authorMats Palmgren <mats@mozilla.com>
Wed, 07 Jun 2017 15:22:41 +0200
changeset 410812 5c7be210a5c7298984a3a073a9edeac9370362df
parent 410811 7fc0cbe0d71cfd101e6e9f536c28b7f7830c4e5b
child 410813 514cd61f92a0970ba495ffe39f0252b50bd8d4ea
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1370632
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1370632 - Move LookupRemoveIf() to nsBaseHashtable instead so that it can be used on more hashtables types. r=froydnj MozReview-Commit-ID: 9kQSytPWok5
xpcom/ds/nsBaseHashtable.h
xpcom/ds/nsClassHashtable.h
--- a/xpcom/ds/nsBaseHashtable.h
+++ b/xpcom/ds/nsBaseHashtable.h
@@ -159,16 +159,51 @@ public:
   }
 
   /**
    * remove the data for the associated key
    * @param aKey the key to remove from the hashtable
    */
   void Remove(KeyType aKey) { this->RemoveEntry(aKey); }
 
+  /**
+   * Looks up aKey in the hashtable and if found calls the given callback
+   * aFunction with the value.  If the callback returns true then the entry
+   * is removed.  If aKey doesn't exist nothing happens.
+   * The hashtable must not be modified in the callback function.
+   *
+   * A typical usage of this API looks like this:
+   *
+   *   table.LookupRemoveIf(key, [](T* aValue) {
+   *     // ... do stuff using aValue ...
+   *     return aValue->IsEmpty(); // or some other condition to remove it
+   *   });
+   *
+   * This is useful for cases where you want to lookup and possibly modify
+   * the value and then maybe remove the entry but would like to avoid two
+   * hashtable lookups.
+   */
+  template<class F>
+  void LookupRemoveIf(KeyType aKey, F aFunction)
+  {
+#ifdef DEBUG
+    auto tableGeneration = GetGeneration();
+#endif
+    EntryType* ent = this->GetEntry(aKey);
+    if (!ent) {
+      return;
+    }
+    bool shouldRemove = aFunction(ent->mData);
+    MOZ_ASSERT(tableGeneration == GetGeneration(),
+               "hashtable was modified by the LookupRemoveIf callback!");
+    if (shouldRemove) {
+      this->RemoveEntry(ent);
+    }
+  }
+
   // This is an iterator that also allows entry removal. Example usage:
   //
   //   for (auto iter = table.Iter(); !iter.Done(); iter.Next()) {
   //     const KeyType key = iter.Key();
   //     const UserDataType data = iter.UserData();
   //     // or
   //     const DataType& data = iter.Data();
   //     // ... do stuff with |key| and/or |data| ...
--- a/xpcom/ds/nsClassHashtable.h
+++ b/xpcom/ds/nsClassHashtable.h
@@ -131,51 +131,16 @@ public:
    *   }
    *
    * We ensure that the hashtable isn't modified before OrInsert() is called.
    * This is useful for cases where you want to insert a new entry into the
    * hashtable if one doesn't exist before but would like to avoid two hashtable
    * lookups.
    */
   MOZ_MUST_USE EntryPtr LookupForAdd(KeyType aKey);
-
-  /**
-   * Looks up aKey in the hashtable and if found calls the given callback
-   * aFunction with the value.  If the callback returns true then the entry
-   * is removed.  If aKey doesn't exist nothing happens.
-   * The hashtable must not be modified in the callback function.
-   *
-   * A typical usage of this API looks like this:
-   *
-   *   table.LookupRemoveIf(key, [](T* aValue) {
-   *     // ... do stuff using aValue ...
-   *     return aValue->IsEmpty(); // or some other condition to remove it
-   *   });
-   *
-   * This is useful for cases where you want to lookup and possibly modify
-   * the value and then maybe remove the entry but would like to avoid two
-   * hashtable lookups.
-   */
-  template<class F>
-  void LookupRemoveIf(KeyType aKey, F aFunction)
-  {
-#ifdef DEBUG
-    auto tableGeneration = base_type::GetGeneration();
-#endif
-    typename base_type::EntryType* ent = this->GetEntry(aKey);
-    if (!ent) {
-      return;
-    }
-    bool shouldRemove = aFunction(ent->mData);
-    MOZ_ASSERT(tableGeneration == base_type::GetGeneration(),
-               "hashtable was modified by the LookupRemoveIf callback!");
-    if (shouldRemove) {
-      this->RemoveEntry(ent);
-    }
-  }
 };
 
 //
 // nsClassHashtable definitions
 //
 
 template<class KeyClass, class T>
 template<typename... Args>