Bug 1448563 - Part 3a: Add iterator to js::Fifo. r=luke
authorTed Campbell <tcampbell@mozilla.com>
Tue, 10 Apr 2018 22:53:14 -0400
changeset 468669 0d3b241f3634887b37db68b2b0114743dc27e28f
parent 468668 8f922357b90d4bc358480c4ac62ca4b3d12a3acb
child 468670 7cf8b356100c8afc5ff6f839e0d6769ea41361d8
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs1448563
milestone61.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 1448563 - Part 3a: Add iterator to js::Fifo. r=luke MozReview-Commit-ID: CoYdOBNnORg
js/src/ds/Fifo.h
--- a/js/src/ds/Fifo.h
+++ b/js/src/ds/Fifo.h
@@ -80,16 +80,51 @@ class Fifo
         return front_.length() + rear_.length();
     }
 
     bool empty() const {
         MOZ_ASSERT_IF(rear_.length() > 0, front_.length() > 0); // Invariant 4.
         return front_.empty();
     }
 
+    // Iterator from oldest to yongest element.
+    struct ConstIterator
+    {
+        const Fifo& self_;
+        size_t idx_;
+
+        ConstIterator(const Fifo& self, size_t idx)
+            : self_(self), idx_(idx)
+        { }
+
+        ConstIterator& operator++() {
+            ++idx_;
+            return *this;
+        }
+
+        const T& operator*() const {
+            // Iterate front in reverse, then rear.
+            size_t split = self_.front_.length();
+            return (idx_ < split) ? self_.front_[(split - 1) - idx_]
+                                  : self_.rear_[idx_ - split];
+        }
+
+        bool operator!=(const ConstIterator& other) const {
+            return (&self_ != &other.self_) || (idx_ != other.idx_);
+        }
+    };
+
+    ConstIterator begin() const {
+        return ConstIterator(*this, 0);
+    }
+
+    ConstIterator end() const {
+        return ConstIterator(*this, length());
+    }
+
     // Push an element to the back of the queue. This method can take either a
     // |const T&| or a |T&&|.
     template <typename U>
     MOZ_MUST_USE bool pushBack(U&& u) {
         if (!rear_.append(mozilla::Forward<U>(u)))
             return false;
         fixup();
         return true;