added access to STL stable_sort algorithm

This commit is contained in:
Mark Olesen
2008-05-08 14:15:31 +02:00
parent 365b6311b9
commit 52be85e2f4
4 changed files with 54 additions and 14 deletions

View File

@ -435,6 +435,20 @@ void sort(List<T>& a, const Cmp& cmp)
} }
template<class T>
void stableSort(List<T>& a)
{
std::stable_sort(a.begin(), a.end());
}
template<class T, class Cmp>
void stableSort(List<T>& a, const Cmp& cmp)
{
std::stable_sort(a.begin(), a.end(), cmp);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
// Assignment to UList operator. Takes linear time. // Assignment to UList operator. Takes linear time.

View File

@ -195,6 +195,12 @@ void sort(List<T>& a);
template<class T, class Cmp> template<class T, class Cmp>
void sort(List<T>& a, const Cmp&); void sort(List<T>& a, const Cmp&);
template<class T>
void stableSort(List<T>& a);
template<class T, class Cmp>
void stableSort(List<T>& a, const Cmp&);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -28,14 +28,11 @@ License
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Construct from List // Construct from List
template <class Type> template <class Type>
SortableList<Type>::SortableList(const List<Type>& values) Foam::SortableList<Type>::SortableList(const List<Type>& values)
: :
List<Type>(values), List<Type>(values),
indices_(values.size()) indices_(values.size())
@ -46,7 +43,7 @@ SortableList<Type>::SortableList(const List<Type>& values)
// Construct given size. Sort later on. // Construct given size. Sort later on.
template <class Type> template <class Type>
SortableList<Type>::SortableList(const label size) Foam::SortableList<Type>::SortableList(const label size)
: :
List<Type>(size), List<Type>(size),
indices_(size) indices_(size)
@ -55,7 +52,7 @@ SortableList<Type>::SortableList(const label size)
// Construct given size and initial value. Sort later on. // Construct given size and initial value. Sort later on.
template <class Type> template <class Type>
SortableList<Type>::SortableList(const label size, const Type& val) Foam::SortableList<Type>::SortableList(const label size, const Type& val)
: :
List<Type>(size, val), List<Type>(size, val),
indices_(size) indices_(size)
@ -64,7 +61,7 @@ SortableList<Type>::SortableList(const label size, const Type& val)
// Construct as copy. // Construct as copy.
template <class Type> template <class Type>
SortableList<Type>::SortableList(const SortableList<Type>& lst) Foam::SortableList<Type>::SortableList(const SortableList<Type>& lst)
: :
List<Type>(lst), List<Type>(lst),
indices_(lst.indices()) indices_(lst.indices())
@ -74,7 +71,7 @@ SortableList<Type>::SortableList(const SortableList<Type>& lst)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template <class Type> template <class Type>
void SortableList<Type>::setSize(const label newSize) void Foam::SortableList<Type>::setSize(const label newSize)
{ {
List<Type>::setSize(newSize); List<Type>::setSize(newSize);
indices_.setSize(newSize); indices_.setSize(newSize);
@ -82,7 +79,7 @@ void SortableList<Type>::setSize(const label newSize)
template <class Type> template <class Type>
void SortableList<Type>::sort() void Foam::SortableList<Type>::sort()
{ {
forAll(indices_, i) forAll(indices_, i)
{ {
@ -98,7 +95,29 @@ void SortableList<Type>::sort()
tmpValues[i] = this->operator[](indices_[i]); tmpValues[i] = this->operator[](indices_[i]);
} }
List<Type>::operator=(tmpValues); List<Type>::transfer(tmpValues);
}
template <class Type>
void Foam::SortableList<Type>::stableSort()
{
forAll(indices_, i)
{
indices_[i] = i;
}
Foam::stableSort(indices_, less(*this));
List<Type> tmpValues(this->size());
forAll(indices_, i)
{
tmpValues[i] = this->operator[](indices_[i]);
}
List<Type>::transfer(tmpValues);
} }
@ -114,6 +133,4 @@ void Foam::SortableList<Type>::operator=(const SortableList<Type>& rhs)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* // // ************************************************************************* //

View File

@ -100,7 +100,7 @@ public:
// Member Functions // Member Functions
//- Return the list of sorted point indices. Updated every sort. //- Return the list of sorted indices. Updated every sort.
const labelList& indices() const const labelList& indices() const
{ {
return indices_; return indices_;
@ -112,6 +112,9 @@ public:
//- Sort the list (if changed after construction time) //- Sort the list (if changed after construction time)
void sort(); void sort();
//- Sort the list (if changed after construction time)
void stableSort();
// Member Operators // Member Operators