Add EnumerateEntries to nsCheapSet. Bug 809446, r=bsmedberg
authorSimon Montagu <smontagu@smontagu.org>
Tue, 20 Nov 2012 06:21:13 -0800
changeset 113766 ce04cb1af0a0eb1e510ef9db34a3593def8bc8a1
parent 113765 5662d8cc819b38e417c101088e113be35868bacf
child 113767 24ad2de34061eb8c7b19bf92264783e0d1628f95
push id18378
push usersmontagu@mozilla.com
push dateTue, 20 Nov 2012 14:21:51 +0000
treeherdermozilla-inbound@b7e492bf7c13 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs809446
milestone20.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
Add EnumerateEntries to nsCheapSet. Bug 809446, r=bsmedberg
xpcom/ds/nsCheapSets.h
--- a/xpcom/ds/nsCheapSets.h
+++ b/xpcom/ds/nsCheapSets.h
@@ -13,16 +13,17 @@
  * A set that takes up minimal size when there are 0 or 1 entries in the set.
  * Use for cases where sizes of 0 and 1 are even slightly common.
  */
 template<typename EntryType>
 class nsCheapSet
 {
 public:
   typedef typename EntryType::KeyType KeyType;
+  typedef PLDHashOperator (* Enumerator)(EntryType* aEntry, void* userArg);
 
   nsCheapSet() : mState(ZERO)
   {
   }
   ~nsCheapSet()
   {
     switch (mState) {
     case ZERO:
@@ -53,16 +54,35 @@ public:
     case MANY:
       return !!mUnion.table->GetEntry(aVal);
     default:
       NS_NOTREACHED("bogus state");
       return false;
     }
   }
 
+  uint32_t EnumerateEntries(Enumerator enumFunc, void* userArg)
+  {
+    switch (mState) {
+    case ZERO:
+      return 0;
+    case ONE:
+      if (enumFunc(GetSingleEntry(), userArg) == PL_DHASH_REMOVE) {
+        GetSingleEntry()->~EntryType();
+        mState = ZERO;
+      }
+      return 1;
+    case MANY:
+      return mUnion.table->EnumerateEntries(enumFunc, userArg);
+    default:
+      NS_NOTREACHED("bogus state");
+      return 0;
+    }
+  }
+
 private:
   EntryType* GetSingleEntry()
   {
     return reinterpret_cast<EntryType*>(&mUnion.singleEntry[0]);
   }
 
   enum SetState {
     ZERO,