Bug 1322843 part 1 - Add Reverse method to nsTArray. r=froydnj draft
authorXidorn Quan <me@upsuper.org>
Fri, 30 Dec 2016 18:23:26 +1100
changeset 454987 4f1ef1bca974045168d6e33e2f079ae1f7b44be9
parent 454954 977c2b8777e98c950b6826851330c992f0d2a3ab
child 454988 5ca01565f607241df0c63a7cd64c35ac7ff7648f
push id40100
push userxquan@mozilla.com
push dateSat, 31 Dec 2016 01:09:41 +0000
reviewersfroydnj
bugs1322843
milestone53.0a1
Bug 1322843 part 1 - Add Reverse method to nsTArray. r=froydnj MozReview-Commit-ID: 8VNEFzHn4dC
xpcom/glue/nsTArray.h
--- a/xpcom/glue/nsTArray.h
+++ b/xpcom/glue/nsTArray.h
@@ -2012,16 +2012,26 @@ public:
     NS_QuickSort(Elements(), Length(), sizeof(elem_type),
                  Compare<Comparator>, const_cast<Comparator*>(&aComp));
   }
 
   // A variation on the Sort method defined above that assumes that
   // 'operator<' is defined for elem_type.
   void Sort() { Sort(nsDefaultComparator<elem_type, elem_type>()); }
 
+  // This method reverses the array in place.
+  void Reverse()
+  {
+    elem_type* elements = Elements();
+    const size_type len = Length();
+    for (index_type i = 0, iend = len / 2; i < iend; ++i) {
+      mozilla::Swap(elements[i], elements[len - i - 1]);
+    }
+  }
+
 protected:
   using base_type::Hdr;
   using base_type::ShrinkCapacity;
 
   // This method invokes elem_type's destructor on a range of elements.
   // @param aStart The index of the first element to destroy.
   // @param aCount The number of elements to destroy.
   void DestructRange(index_type aStart, size_type aCount)