Bug 1371928 - Add a new EnsureInserted() method that return true if a new entry was created, and EnsureRemoved() that return true if an existing entry was removed. r=froydnj
authorMats Palmgren <mats@mozilla.com>
Sat, 17 Jun 2017 00:06:04 +0200
changeset 364515 3cd8b9859ac2236ec4ac28db56eba0e51cba7dfa
parent 364514 02383934eea258ea88bf4b20a11e0dd9581a62ca
child 364516 ec5d2e57caa6285b0cc5c2913d3a4e17c3a48b7e
push id32043
push userarchaeopteryx@coole-files.de
push dateSat, 17 Jun 2017 20:28:42 +0000
treeherdermozilla-central@0ef3bfc9f4b8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1371928
milestone56.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 1371928 - Add a new EnsureInserted() method that return true if a new entry was created, and EnsureRemoved() that return true if an existing entry was removed. r=froydnj As opposed to PutEntry/RemoveEntry which do not indicate what happened. MozReview-Commit-ID: LeNKDqpSksR
xpcom/ds/nsTHashtable.h
--- a/xpcom/ds/nsTHashtable.h
+++ b/xpcom/ds/nsTHashtable.h
@@ -158,25 +158,62 @@ public:
   MOZ_MUST_USE
   EntryType* PutEntry(KeyType aKey, const fallible_t&)
   {
     return static_cast<EntryType*>(mTable.Add(EntryType::KeyToPointer(aKey),
                                               mozilla::fallible));
   }
 
   /**
+   * Get the entry associated with a key, or create a new entry using infallible
+   * allocation and insert that.
+   * @param     aKey the key to retrieve
+   * @param     aEntry will be assigned (if non-null) to the entry that was found
+   *            or created
+   * @return    true if a new entry was created, or false if an existing entry
+   *            was found
+   */
+  MOZ_MUST_USE
+  bool EnsureInserted(KeyType aKey, EntryType** aEntry = nullptr)
+  {
+    auto oldCount = Count();
+    EntryType* entry = PutEntry(aKey);
+    if (aEntry) {
+      *aEntry = entry;
+    }
+    return oldCount != Count();
+  }
+
+  /**
    * Remove the entry associated with a key.
    * @param     aKey of the entry to remove
    */
   void RemoveEntry(KeyType aKey)
   {
     mTable.Remove(EntryType::KeyToPointer(aKey));
   }
 
   /**
+   * Lookup the entry associated with aKey and remove it if found, otherwise
+   * do nothing.
+   * @param     aKey of the entry to remove
+   * @return    true if an entry was found and removed, or false if no entry
+   *            was found for aKey
+   */
+  bool EnsureRemoved(KeyType aKey)
+  {
+    auto* entry = GetEntry(aKey);
+    if (entry) {
+      RemoveEntry(entry);
+      return true;
+    }
+    return false;
+  }
+
+  /**
    * Remove the entry associated with a key.
    * @param aEntry   the entry-pointer to remove (obtained from GetEntry)
    */
   void RemoveEntry(EntryType* aEntry)
   {
     mTable.RemoveEntry(aEntry);
   }
 
@@ -488,29 +525,29 @@ public:
   explicit nsTHashtable(uint32_t aInitLength)
     : Base(aInitLength)
   {}
 
   ~nsTHashtable() = default;
 
   nsTHashtable(nsTHashtable&&) = default;
 
-  /* Wrapper functions */
   using Base::GetGeneration;
   using Base::Count;
   using Base::IsEmpty;
   using Base::Clear;
 
   using Base::ShallowSizeOfExcludingThis;
   using Base::ShallowSizeOfIncludingThis;
 
 #ifdef DEBUG
   using Base::MarkImmutable;
 #endif
 
+  /* Wrapper functions */
   EntryType* GetEntry(T* aKey) const
   {
     return reinterpret_cast<EntryType*>(Base::GetEntry(aKey));
   }
 
   bool Contains(T* aKey) const
   {
     return Base::Contains(aKey);
@@ -523,21 +560,32 @@ public:
 
   MOZ_MUST_USE
   EntryType* PutEntry(T* aKey, const mozilla::fallible_t&)
   {
     return reinterpret_cast<EntryType*>(
       Base::PutEntry(aKey, mozilla::fallible));
   }
 
+  MOZ_MUST_USE
+  bool EnsureInserted(T* aKey, EntryType** aEntry = nullptr)
+  {
+    return Base::EnsureInserted(aKey, reinterpret_cast<::detail::VoidPtrHashKey**>(aEntry));
+  }
+
   void RemoveEntry(T* aKey)
   {
     Base::RemoveEntry(aKey);
   }
 
+  bool EnsureRemoved(T* aKey)
+  {
+    return Base::EnsureRemoved(aKey);
+  }
+
   void RemoveEntry(EntryType* aEntry)
   {
     Base::RemoveEntry(reinterpret_cast<::detail::VoidPtrHashKey*>(aEntry));
   }
 
   void RawRemoveEntry(EntryType* aEntry)
   {
     Base::RawRemoveEntry(reinterpret_cast<::detail::VoidPtrHashKey*>(aEntry));