Commit 093e6705 authored by laurence's avatar laurence
Browse files

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

parent dc488579
......@@ -92,14 +92,23 @@ void inplaceMapKey(const labelUList& oldToNew, Container&);
template<class T>
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
template<class T>
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
template<class T>
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.
// eg, to extract all selected elements:
// subset<bool, labelList>(selectedElems, true, lst);
......
......@@ -180,6 +180,18 @@ void Foam::sortedOrder
const UList<T>& lst,
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
if (order.size() != lst.size())
......@@ -193,7 +205,7 @@ void Foam::sortedOrder
{
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,
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)
{
......@@ -210,7 +234,7 @@ void Foam::duplicateOrder
return;
}
sortedOrder(lst, order);
sortedOrder(lst, order, cmp);
label n = 0;
for (label i = 0; i < order.size() - 1; ++i)
......@@ -231,7 +255,19 @@ void Foam::uniqueOrder
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)
{
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -23,26 +23,7 @@ License
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
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));
}
#include "ListOps.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -113,7 +94,7 @@ Foam::List<T>& Foam::SortableList<T>::shrink()
template<class T>
void Foam::SortableList<T>::sort()
{
sortIndices(indices_);
sortedOrder(*this, indices_);
List<T> lst(this->size());
forAll(indices_, i)
......@@ -128,13 +109,12 @@ void Foam::SortableList<T>::sort()
template<class T>
void Foam::SortableList<T>::reverseSort()
{
sortIndices(indices_);
sortedOrder(*this, indices_, typename UList<T>::greater(*this));
List<T> lst(this->size());
label endI = indices_.size();
forAll(indices_, i)
{
lst[--endI] = this->operator[](indices_[i]);
lst[i] = this->operator[](indices_[i]);
}
List<T>::transfer(lst);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -59,8 +59,6 @@ class SortableList
//- Original indices
labelList indices_;
//- Resize, fill and sort the parameter according to the list values
void sortIndices(List<label>&) const;
public:
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment