diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C index 9eaf99c801..25137d47e2 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C @@ -24,53 +24,39 @@ License \*---------------------------------------------------------------------------*/ -#include "OSspecific.H" - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -// Construct from List template Foam::SortableList::SortableList(const UList& values) : - List(values), - indices_(values.size()) + List(values) { sort(); } -// Construct from List by transferring template Foam::SortableList::SortableList(const xfer >& values) : - List(), - indices_(values->size()) + List(values) { - List::transfer(values()); sort(); } -// Construct given size. Sort later on. template Foam::SortableList::SortableList(const label size) : - List(size), - indices_(size) + List(size) {} -// Construct given size and initial value. Sort later on. template Foam::SortableList::SortableList(const label size, const Type& val) : - List(size, val), - indices_(size) + List(size, val) {} -// Construct as copy. template Foam::SortableList::SortableList(const SortableList& lst) : @@ -85,29 +71,38 @@ template void Foam::SortableList::setSize(const label newSize) { List::setSize(newSize); - indices_.setSize(newSize); + indices_.setSize(newSize, -1); +} + + +template +void Foam::SortableList::clear() +{ + List::clear(); + indices_.clear(); } template void Foam::SortableList::sort() { + // list lengths must be identical + indices_.setSize(this->size()); + forAll(indices_, i) { indices_[i] = i; } - // Foam::sort(indices_, less(*this)); - Foam::stableSort(indices_, less(*this)); - - List tmpValues(this->size()); + Foam::stableSort(indices_, typename UList::less(*this)); + List lst(this->size()); forAll(indices_, i) { - tmpValues[i] = this->operator[](indices_[i]); + lst[i] = this->operator[](indices_[i]); } - List::transfer(tmpValues); + List::transfer(lst); } @@ -120,7 +115,6 @@ void Foam::SortableList::operator=(const SortableList& rhs) indices_ = rhs.indices(); } - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H index b2ac5eefcb..69360d0058 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H @@ -60,44 +60,22 @@ class SortableList //- Original indices labelList indices_; - public: - // Public classes - - //- Less function class used by the sort function - class less - { - const UList& values_; - - public: - - less(const UList& values) - : - values_(values) - {} - - bool operator()(const label a, const label b) - { - return values_[a] < values_[b]; - } - }; - - // Constructors - //- Construct from List, sorting the elements. - // Starts with indices set to index in argument + //- Construct from UList, sorting immediately. explicit SortableList(const UList&); - //- Construct from tranferred List, sorting the elements. - // Starts with indices set to index in argument + //- Construct from transferred List, sorting immediately. explicit SortableList(const xfer >&); //- Construct given size. Sort later on. + // The indices remain empty until the list is sorted explicit SortableList(const label size); //- Construct given size and initial value. Sort later on. + // The indices remain empty until the list is sorted SortableList(const label size, const Type&); //- Construct as copy. @@ -112,10 +90,20 @@ public: return indices_; } - //- Size the list. If grow can cause undefined indices (until next sort) + //- Return non-const access to the sorted indices. Updated every sort. + labelList& indices() + { + return indices_; + } + + //- Size the list. Growing can cause undefined indices (until next sort) void setSize(const label); + //- Clear the list and the indices + void clear(); + //- (stable) sort the list (if changed after construction time) + // also resizes the indices if required void sort(); // Member Operators diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index 94f157bb54..bd586ce67e 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -92,6 +92,26 @@ public: //- Declare friendship with the SubList class friend class SubList; + // Public classes + + //- Less function class that can be used for sorting + class less + { + const UList& values_; + + public: + + less(const UList& values) + : + values_(values) + {} + + bool operator()(const label a, const label b) + { + return values_[a] < values_[b]; + } + }; + // Constructors diff --git a/src/decompositionAgglomeration/decompositionMethods/simpleGeomDecomp/simpleGeomDecomp.C b/src/decompositionAgglomeration/decompositionMethods/simpleGeomDecomp/simpleGeomDecomp.C index 08b5952952..d940e3f3bf 100644 --- a/src/decompositionAgglomeration/decompositionMethods/simpleGeomDecomp/simpleGeomDecomp.C +++ b/src/decompositionAgglomeration/decompositionMethods/simpleGeomDecomp/simpleGeomDecomp.C @@ -133,7 +133,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose(const pointField& points) sort ( pointIndices, - SortableList::less(rotatedPoints.component(vector::X)) + UList::less(rotatedPoints.component(vector::X)) ); assignToProcessorGroup(processorGroups, n_.x()); @@ -149,7 +149,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose(const pointField& points) sort ( pointIndices, - SortableList::less(rotatedPoints.component(vector::Y)) + UList::less(rotatedPoints.component(vector::Y)) ); assignToProcessorGroup(processorGroups, n_.y()); @@ -165,7 +165,7 @@ Foam::labelList Foam::simpleGeomDecomp::decompose(const pointField& points) sort ( pointIndices, - SortableList::less(rotatedPoints.component(vector::Z)) + UList::less(rotatedPoints.component(vector::Z)) ); assignToProcessorGroup(processorGroups, n_.z());