ENH: Update SortableList to use ListOps. Add "greater" functor to UList.

This commit is contained in:
laurence
2013-05-01 17:32:40 +01:00
parent dc488579ca
commit 093e6705aa
5 changed files with 73 additions and 32 deletions

View File

@ -92,14 +92,23 @@ void inplaceMapKey(const labelUList& oldToNew, Container&);
template<class T> template<class T>
void sortedOrder(const UList<T>&, labelList& order); void sortedOrder(const UList<T>&, labelList& order);
template<class T, class Cmp>
void sortedOrder(const UList<T>&, labelList& order, const Cmp& cmp);
//- Generate (sorted) indices corresponding to duplicate list values //- Generate (sorted) indices corresponding to duplicate list values
template<class T> template<class T>
void duplicateOrder(const UList<T>&, labelList& order); void duplicateOrder(const UList<T>&, labelList& order);
template<class T, class Cmp>
void duplicateOrder(const UList<T>&, labelList& order, const Cmp& cmp);
//- Generate (sorted) indices corresponding to unique list values //- Generate (sorted) indices corresponding to unique list values
template<class T> template<class T>
void uniqueOrder(const UList<T>&, labelList& order); void uniqueOrder(const UList<T>&, labelList& order);
template<class T, class Cmp>
void uniqueOrder(const UList<T>&, labelList& order, const Cmp& cmp);
//- Extract elements of List when select is a certain value. //- Extract elements of List when select is a certain value.
// eg, to extract all selected elements: // eg, to extract all selected elements:
// subset<bool, labelList>(selectedElems, true, lst); // subset<bool, labelList>(selectedElems, true, lst);

View File

@ -180,6 +180,18 @@ void Foam::sortedOrder
const UList<T>& lst, const UList<T>& lst,
labelList& order labelList& order
) )
{
sortedOrder(lst, order, typename UList<T>::less(lst));
}
template<class T, class Cmp>
void Foam::sortedOrder
(
const UList<T>& lst,
labelList& order,
const Cmp& cmp
)
{ {
// list lengths must be identical // list lengths must be identical
if (order.size() != lst.size()) if (order.size() != lst.size())
@ -193,7 +205,7 @@ void Foam::sortedOrder
{ {
order[elemI] = elemI; order[elemI] = elemI;
} }
Foam::stableSort(order, typename UList<T>::less(lst)); Foam::stableSort(order, cmp);
} }
@ -203,6 +215,18 @@ void Foam::duplicateOrder
const UList<T>& lst, const UList<T>& lst,
labelList& order labelList& order
) )
{
duplicateOrder(lst, order, typename UList<T>::less(lst));
}
template<class T, class Cmp>
void Foam::duplicateOrder
(
const UList<T>& lst,
labelList& order,
const Cmp& cmp
)
{ {
if (lst.size() < 2) if (lst.size() < 2)
{ {
@ -210,7 +234,7 @@ void Foam::duplicateOrder
return; return;
} }
sortedOrder(lst, order); sortedOrder(lst, order, cmp);
label n = 0; label n = 0;
for (label i = 0; i < order.size() - 1; ++i) for (label i = 0; i < order.size() - 1; ++i)
@ -231,7 +255,19 @@ void Foam::uniqueOrder
labelList& order labelList& order
) )
{ {
sortedOrder(lst, order); uniqueOrder(lst, order, typename UList<T>::less(lst));
}
template<class T, class Cmp>
void Foam::uniqueOrder
(
const UList<T>& lst,
labelList& order,
const Cmp& cmp
)
{
sortedOrder(lst, order, cmp);
if (order.size() > 1) if (order.size() > 1)
{ {

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -23,26 +23,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // #include "ListOps.H"
template<class T>
void Foam::SortableList<T>::sortIndices(List<label>& order) const
{
// list lengths must be identical
if (order.size() != this->size())
{
// avoid copying any elements, they are overwritten anyhow
order.clear();
order.setSize(this->size());
}
forAll(order, elemI)
{
order[elemI] = elemI;
}
Foam::stableSort(order, typename UList<T>::less(*this));
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -113,7 +94,7 @@ Foam::List<T>& Foam::SortableList<T>::shrink()
template<class T> template<class T>
void Foam::SortableList<T>::sort() void Foam::SortableList<T>::sort()
{ {
sortIndices(indices_); sortedOrder(*this, indices_);
List<T> lst(this->size()); List<T> lst(this->size());
forAll(indices_, i) forAll(indices_, i)
@ -128,13 +109,12 @@ void Foam::SortableList<T>::sort()
template<class T> template<class T>
void Foam::SortableList<T>::reverseSort() void Foam::SortableList<T>::reverseSort()
{ {
sortIndices(indices_); sortedOrder(*this, indices_, typename UList<T>::greater(*this));
List<T> lst(this->size()); List<T> lst(this->size());
label endI = indices_.size();
forAll(indices_, i) forAll(indices_, i)
{ {
lst[--endI] = this->operator[](indices_[i]); lst[i] = this->operator[](indices_[i]);
} }
List<T>::transfer(lst); List<T>::transfer(lst);

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -59,8 +59,6 @@ class SortableList
//- Original indices //- Original indices
labelList indices_; labelList indices_;
//- Resize, fill and sort the parameter according to the list values
void sortIndices(List<label>&) const;
public: public:

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -112,6 +112,24 @@ public:
} }
}; };
//- Greater function class that can be used for sorting
class greater
{
const UList<T>& values_;
public:
greater(const UList<T>& values)
:
values_(values)
{}
bool operator()(const label a, const label b)
{
return values_[a] > values_[b];
}
};
// Constructors // Constructors