Bug 1139993 - Add VectorBase::ConstRange that gives out |const T&| entries. r=Waldo
authorNick Fitzgerald <fitzgen@gmail.com>
Fri, 06 Mar 2015 15:37:00 -0500
changeset 232607 9da55484cc2d0fa2efe1c6dd719a87830e5f93b0
parent 232606 282e743394dc8349d9e26fa69cf142866f8ca4b3
child 232608 150dbaea5d3fb4ab6ef526fd73473f9f825c1fbb
push id11667
push userkwierso@gmail.com
push dateTue, 10 Mar 2015 02:14:18 +0000
treeherderfx-team@a1e59f81736a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs1139993
milestone39.0a1
Bug 1139993 - Add VectorBase::ConstRange that gives out |const T&| entries. r=Waldo
mfbt/Vector.h
mfbt/tests/TestVector.cpp
--- a/mfbt/Vector.h
+++ b/mfbt/Vector.h
@@ -468,25 +468,45 @@ public:
     Range(T* aCur, T* aEnd)
       : mCur(aCur)
       , mEnd(aEnd)
     {
       MOZ_ASSERT(aCur <= aEnd);
     }
 
   public:
-    Range() {}
     bool empty() const { return mCur == mEnd; }
     size_t remain() const { return PointerRangeSize(mCur, mEnd); }
     T& front() const { MOZ_ASSERT(!empty()); return *mCur; }
     void popFront() { MOZ_ASSERT(!empty()); ++mCur; }
     T popCopyFront() { MOZ_ASSERT(!empty()); return *mCur++; }
   };
 
+  class ConstRange
+  {
+    friend class VectorBase;
+    const T* mCur;
+    const T* mEnd;
+    ConstRange(const T* aCur, const T* aEnd)
+      : mCur(aCur)
+      , mEnd(aEnd)
+    {
+      MOZ_ASSERT(aCur <= aEnd);
+    }
+
+  public:
+    bool empty() const { return mCur == mEnd; }
+    size_t remain() const { return PointerRangeSize(mCur, mEnd); }
+    const T& front() const { MOZ_ASSERT(!empty()); return *mCur; }
+    void popFront() { MOZ_ASSERT(!empty()); ++mCur; }
+    T popCopyFront() { MOZ_ASSERT(!empty()); return *mCur++; }
+  };
+
   Range all() { return Range(begin(), end()); }
+  ConstRange all() const { return ConstRange(begin(), end()); }
 
   /* mutators */
 
   /**
    * Given that the vector is empty and has no inline storage, grow to
    * |capacity|.
    */
   bool initCapacity(size_t aRequest);
--- a/mfbt/tests/TestVector.cpp
+++ b/mfbt/tests/TestVector.cpp
@@ -9,16 +9,17 @@
 
 using mozilla::detail::VectorTesting;
 using mozilla::Move;
 using mozilla::Vector;
 
 struct mozilla::detail::VectorTesting
 {
   static void testReserved();
+  static void testConstRange();
 };
 
 void
 mozilla::detail::VectorTesting::testReserved()
 {
 #ifdef DEBUG
   Vector<bool> bv;
   MOZ_RELEASE_ASSERT(bv.reserved() == 0);
@@ -70,13 +71,36 @@ mozilla::detail::VectorTesting::testRese
   MOZ_RELEASE_ASSERT(iv.reserved() == 0);
   MOZ_RELEASE_ASSERT(iv2.reserved() == 55);
 
   iv2.clearAndFree();
   MOZ_RELEASE_ASSERT(iv2.reserved() == 0);
 #endif
 }
 
+void
+mozilla::detail::VectorTesting::testConstRange()
+{
+#ifdef DEBUG
+  Vector<int> vec;
+
+  for (int i = 0; i < 10; i++) {
+    MOZ_RELEASE_ASSERT(vec.append(i));
+  }
+
+  const auto &vecRef = vec;
+
+  Vector<int>::ConstRange range = vecRef.all();
+  for (int i = 0; i < 10; i++) {
+    MOZ_RELEASE_ASSERT(!range.empty());
+    MOZ_RELEASE_ASSERT(range.front() == i);
+    range.popFront();
+  }
+#endif
+}
+
+
 int
 main()
 {
   VectorTesting::testReserved();
+  VectorTesting::testConstRange();
 }