зеркало из https://github.com/mozilla/pjs.git
Bug 330101 nsTArray binary search for when array is sorted r=bsmedberg a=schrep
This commit is contained in:
Родитель
be9de25cc3
Коммит
5eefaca9ec
|
@ -440,6 +440,36 @@ class nsTArray : public nsTArray_base {
|
|||
return LastIndexOf(item, start, nsDefaultComparator<elem_type, Item>());
|
||||
}
|
||||
|
||||
// This method searches for the offset for the element in this array
|
||||
// that is equal to the given element. The array is assumed to be sorted.
|
||||
// @param item The item to search for.
|
||||
// @param comp The Comparator used.
|
||||
// @return The index of the found element or NoIndex if not found.
|
||||
template<class Item, class Comparator>
|
||||
index_type BinaryIndexOf(const Item& item, const Comparator& comp) const {
|
||||
index_type low = 0, high = Length();
|
||||
while (high > low) {
|
||||
index_type mid = (high + low) >> 1;
|
||||
if (comp.Equals(ElementAt(mid), item))
|
||||
return mid;
|
||||
if (comp.LessThan(ElementAt(mid), item))
|
||||
low = mid + 1;
|
||||
else
|
||||
high = mid;
|
||||
}
|
||||
return NoIndex;
|
||||
}
|
||||
|
||||
// This method searches for the offset for the element in this array
|
||||
// that is equal to the given element. The array is assumed to be sorted.
|
||||
// This method assumes that 'operator==' and 'operator<' are defined.
|
||||
// @param item The item to search for.
|
||||
// @return The index of the found element or NoIndex if not found.
|
||||
template<class Item>
|
||||
index_type BinaryIndexOf(const Item& item) const {
|
||||
return BinaryIndexOf(item, nsDefaultComparator<elem_type, Item>());
|
||||
}
|
||||
|
||||
//
|
||||
// Mutation methods
|
||||
//
|
||||
|
|
|
@ -80,10 +80,18 @@ static PRBool test_basic_array(ElementType *data,
|
|||
return PR_FALSE;
|
||||
// ensure sort results in ascending order
|
||||
ary.Sort();
|
||||
for (i = 1; i < ary.Length(); ++i) {
|
||||
if (ary[i] < ary[i-1])
|
||||
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)
|
||||
return PR_FALSE;
|
||||
}
|
||||
if (ary.BinaryIndexOf(extra) != -1)
|
||||
return PR_FALSE;
|
||||
PRUint32 oldLen = ary.Length();
|
||||
ary.RemoveElement(data[dataLen / 2]);
|
||||
if (ary.Length() != (oldLen - 1))
|
||||
|
@ -285,10 +293,18 @@ static PRBool test_string_array() {
|
|||
|
||||
strArray.Sort();
|
||||
const char ksorted[] = "\0 dehllloorw";
|
||||
for (i = 0; i < NS_ARRAY_LENGTH(kdata)-1; ++i) {
|
||||
for (i = NS_ARRAY_LENGTH(kdata); i--; ) {
|
||||
if (strArray[i].CharAt(0) != ksorted[i])
|
||||
return PR_FALSE;
|
||||
if (i > 0 && strArray[i] == strArray[i - 1])
|
||||
strArray.RemoveElementAt(i);
|
||||
}
|
||||
for (i = 0; i < strArray.Length(); ++i) {
|
||||
if (strArray.BinaryIndexOf(strArray[i]) != i)
|
||||
return PR_FALSE;
|
||||
}
|
||||
if (strArray.BinaryIndexOf(EmptyCString()) != -1)
|
||||
return PR_FALSE;
|
||||
|
||||
nsCString rawArray[NS_ARRAY_LENGTH(kdata)-1];
|
||||
for (i = 0; i < NS_ARRAY_LENGTH(rawArray); ++i)
|
||||
|
|
Загрузка…
Ссылка в новой задаче