bug 1316119 - add a GetOrInsert to nsBaseHashtable that returns a reference to the value r=froydnj
authorTrevor Saunders <tbsaunde@tbsaunde.org>
Tue, 08 Nov 2016 15:54:45 -0500
changeset 322910 e943ad944554ce274a83d8e940d8aa259f18da23
parent 322909 791cc18c90be2f6ecb12de64a344feb86b34f191
child 322911 7a9358cf3143e0dc7ca5c0081e7dc4a6899ff43f
push id84000
push usertsaunders@mozilla.com
push dateThu, 17 Nov 2016 04:39:30 +0000
treeherdermozilla-inbound@387d3acae9e9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1316119
milestone53.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 1316119 - add a GetOrInsert to nsBaseHashtable that returns a reference to the value r=froydnj This intends to expose basically the same functionality as operator[] on unordered_map.
xpcom/glue/nsBaseHashtable.h
--- a/xpcom/glue/nsBaseHashtable.h
+++ b/xpcom/glue/nsBaseHashtable.h
@@ -111,16 +111,32 @@ public:
     if (!ent) {
       return UserDataType{};
     }
 
     return ent->mData;
   }
 
   /**
+   * Add key to the table if not already present, and return a reference to its
+   * value.  If key is not already in the table then the value is default
+   * constructed.
+   */
+  DataType& GetOrInsert(const KeyType& aKey)
+  {
+    EntryType* ent = this->GetEntry(aKey);
+    if (ent) {
+      return ent->mData;
+    }
+
+    ent = this->PutEntry(aKey);
+    return ent->mData;
+  }
+
+  /**
    * put a new value for the associated key
    * @param aKey the key to put
    * @param aData the new data
    * @return always true, unless memory allocation failed
    */
   void Put(KeyType aKey, const UserDataType& aData)
   {
     if (!Put(aKey, aData, mozilla::fallible)) {