Bug 617947 - Merge 'class nsQuickSortComparator' into 'nsTArray'. r=bsmedberg

This commit is contained in:
Matias Juntunen 2012-03-21 19:52:24 -04:00
parent 75b8ad96ec
commit 2cefe6b3a9

View File

@ -381,24 +381,6 @@ public:
}
};
// This class exists because VC6 cannot handle static template functions.
// Otherwise, the Compare method would be defined directly on nsTArray.
template <class E, class Comparator>
class nsQuickSortComparator
{
public:
typedef E elem_type;
// This function is meant to be used with the NS_QuickSort function. It
// maps the callback API expected by NS_QuickSort to the Comparator API
// used by nsTArray. See nsTArray::Sort.
static int Compare(const void* e1, const void* e2, void *data) {
const Comparator* c = reinterpret_cast<const Comparator*>(data);
const elem_type* a = static_cast<const elem_type*>(e1);
const elem_type* b = static_cast<const elem_type*>(e2);
return c->LessThan(*a, *b) ? -1 : (c->Equals(*a, *b) ? 0 : 1);
}
};
// The default comparator used by nsTArray
template<class A, class B>
class nsDefaultComparator
@ -1131,6 +1113,17 @@ public:
//
// Sorting
//
// This function is meant to be used with the NS_QuickSort function. It
// maps the callback API expected by NS_QuickSort to the Comparator API
// used by nsTArray. See nsTArray::Sort.
template<class Comparator>
static int Compare(const void* e1, const void* e2, void *data) {
const Comparator* c = reinterpret_cast<const Comparator*>(data);
const elem_type* a = static_cast<const elem_type*>(e1);
const elem_type* b = static_cast<const elem_type*>(e2);
return c->LessThan(*a, *b) ? -1 : (c->Equals(*a, *b) ? 0 : 1);
}
// This method sorts the elements of the array. It uses the LessThan
// method defined on the given Comparator object to collate elements.
@ -1138,8 +1131,7 @@ public:
template<class Comparator>
void Sort(const Comparator& comp) {
NS_QuickSort(Elements(), Length(), sizeof(elem_type),
nsQuickSortComparator<elem_type, Comparator>::Compare,
const_cast<Comparator*>(&comp));
Compare<Comparator>, const_cast<Comparator*>(&comp));
}
// A variation on the Sort method defined above that assumes that