зеркало из https://github.com/mozilla/gecko-dev.git
Bug 617947 - Merge 'class nsQuickSortComparator' into 'nsTArray'. r=bsmedberg
This commit is contained in:
Родитель
b3820740d8
Коммит
702f8921b8
|
@ -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
|
||||
|
@ -1132,14 +1114,24 @@ 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.
|
||||
// @param comp The Comparator used to collate elements.
|
||||
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
|
||||
|
|
Загрузка…
Ссылка в новой задаче