Bug 543001 Make nsTArray::GreatestIndexLtEq easier to use r=bsmedberg
authorNeil Rashbrook <neil@parkwaycc.co.uk>
Sat, 20 Feb 2010 15:26:12 +0000
changeset 38333 90e2907fc71f74d9291bb1946efd333f21e472f8
parent 38332 d88964885072221ebf97369f67c900ddb7906d3c
child 38334 900590ea829bdd6fbae9a21eb0fb606ae3b75d71
push idunknown
push userunknown
push dateunknown
reviewersbsmedberg
bugs543001
milestone1.9.3a2pre
Bug 543001 Make nsTArray::GreatestIndexLtEq easier to use r=bsmedberg
xpcom/glue/nsTArray.h
xpcom/tests/TestTArray.cpp
--- a/xpcom/glue/nsTArray.h
+++ b/xpcom/glue/nsTArray.h
@@ -584,16 +584,33 @@ class nsTArray : public nsTArray_base {
       }
       // low <= idx < high, so insert at high ("shifting" high up by
       // 1) to maintain invariant.
       // (or insert at low, since low==high; just a matter of taste here.)
       *idx = high;
       return PR_FALSE;
     }
 
+    // A variation on the GreatestIndexLtEq method defined above.
+    template<class Item, class Comparator>
+    PRBool
+    GreatestIndexLtEq(const Item& item,
+                      index_type& idx,
+                      const Comparator& comp) const {
+      return GreatestIndexLtEq(item, comp, &idx);
+    }
+
+    // A variation on the GreatestIndexLtEq method defined above.
+    template<class Item>
+    PRBool
+    GreatestIndexLtEq(const Item& item,
+                      index_type& idx) const {
+      return GreatestIndexLtEq(item, nsDefaultComparator<elem_type, Item>(), &idx);
+    }
+
     // Inserts |item| at such an index to guarantee that if the array
     // was previously sorted, it will remain sorted after this
     // insertion.
     template<class Item, class Comparator>
     elem_type *InsertElementSorted(const Item& item, const Comparator& comp) {
       index_type index;
       GreatestIndexLtEq(item, comp, &index);
       return InsertElementAt(index, item);
--- a/xpcom/tests/TestTArray.cpp
+++ b/xpcom/tests/TestTArray.cpp
@@ -77,16 +77,26 @@ static PRBool test_basic_array(ElementTy
     if (ary.SafeElementAt(i, extra) != data[i])
       return PR_FALSE;
   }
   if (ary.SafeElementAt(ary.Length(), extra) != extra ||
       ary.SafeElementAt(ary.Length() * 10, extra) != extra)
     return PR_FALSE;
   // ensure sort results in ascending order
   ary.Sort();
+  PRUint32 j = 0, k;
+  if (ary.GreatestIndexLtEq(extra, k))
+    return PR_FALSE;
+  for (i = 0; i < ary.Length(); ++i) {
+    if (!ary.GreatestIndexLtEq(ary[i], k))
+      return PR_FALSE;
+    if (k < j)
+      return PR_FALSE;
+    j = k;
+  }
   for (i = ary.Length(); --i; ) {
     if (ary[i] < ary[i - 1])
       return PR_FALSE;
     if (ary[i] == ary[i - 1])
       ary.RemoveElementAt(i);
   }
   for (i = 0; i < ary.Length(); ++i) {
     if (ary.BinaryIndexOf(ary[i]) != i)