diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H index f878a6f969deba67665afa3aab561d7a2426448f..c322c7856faae1accc1c36db8790131b1eb3e5c5 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H @@ -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); diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C index ab6f9bf511e90a28abee4852d5e1d0ee4a40a53e..29ea65055121aedf96dc1ac262601134c97e6953 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C @@ -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) { diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C index e7613ab1ab2c6fff2bdbb724e2f1c1018d631b83..d610a56a04cdf408325dcb8a8b1b805d53945563 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C @@ -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); diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H index 1e62f39109e7952b9594441ba156db2bec227259..2d7b31a9e89ff315670d6608d23882528294d985 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H @@ -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: diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index 06ef4cbb61b4a320e4aebc02510d4b8bb6207dfb..e3bfe48f53c851c886fde848696faa8bc90104e1 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -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