Bug 1212624 - Implement range-based iteration for LinkedList, r=Waldo
authorSteve Fink <sfink@mozilla.com>
Wed, 07 Oct 2015 14:19:42 -0700
changeset 303136 8b1831b24cea66b458a05b63013bf930b08f86df
parent 303135 3f2f87f5f18733e5b99423452d8cf65f3f53b888
child 303137 8aba07c1a690d6e5e6e339a10f1ed13198931931
push id1001
push userraliiev@mozilla.com
push dateMon, 18 Jan 2016 19:06:03 +0000
treeherdermozilla-release@8b89261f3ac4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs1212624
milestone44.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 1212624 - Implement range-based iteration for LinkedList, r=Waldo
mfbt/LinkedList.h
--- a/mfbt/LinkedList.h
+++ b/mfbt/LinkedList.h
@@ -294,16 +294,36 @@ private:
 
 template<typename T>
 class LinkedList
 {
 private:
   LinkedListElement<T> sentinel;
 
 public:
+  class Iterator {
+    T* mCurrent;
+
+  public:
+    explicit Iterator(T* aCurrent) : mCurrent(aCurrent) {}
+
+    T* operator *() const {
+      return mCurrent;
+    }
+
+    const Iterator& operator++() {
+      mCurrent = mCurrent->getNext();
+      return *this;
+    }
+
+    bool operator!=(Iterator& aOther) const {
+      return mCurrent != aOther.mCurrent;
+    }
+  };
+
   LinkedList() : sentinel(LinkedListElement<T>::NODE_KIND_SENTINEL) { }
 
   LinkedList(LinkedList<T>&& aOther)
     : sentinel(mozilla::Move(aOther.sentinel))
   { }
 
   ~LinkedList() { MOZ_ASSERT(isEmpty()); }
 
@@ -379,16 +399,28 @@ public:
   void clear()
   {
     while (popFirst()) {
       continue;
     }
   }
 
   /*
+   * Allow range-based iteration:
+   *
+   *     for (MyElementType* elt : myList) { ... }
+   */
+  Iterator begin() {
+    return Iterator(getFirst());
+  }
+  Iterator end() {
+    return Iterator(nullptr);
+  }
+
+  /*
    * Measures the memory consumption of the list excluding |this|.  Note that
    * it only measures the list elements themselves.  If the list elements
    * contain pointers to other memory blocks, those blocks must be measured
    * separately during a subsequent iteration over the list.
    */
   size_t sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
   {
     size_t n = 0;