diff --git a/src/OpenFOAM/containers/Lists/List/List.C b/src/OpenFOAM/containers/Lists/List/List.C index 3eecd8c055..6884f8de40 100644 --- a/src/OpenFOAM/containers/Lists/List/List.C +++ b/src/OpenFOAM/containers/Lists/List/List.C @@ -435,6 +435,20 @@ void sort(List& a, const Cmp& cmp) } +template +void stableSort(List& a) +{ + std::stable_sort(a.begin(), a.end()); +} + + +template +void stableSort(List& a, const Cmp& cmp) +{ + std::stable_sort(a.begin(), a.end(), cmp); +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // Assignment to UList operator. Takes linear time. diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index 50db82f0a3..52e285418e 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -28,7 +28,7 @@ Class Description A 1D array of objects of type \, where the size of the vector is known and used for subscript bounds checking, etc. - + Storage is allocated on free-store during construction. SourceFiles @@ -195,6 +195,12 @@ void sort(List& a); template void sort(List& a, const Cmp&); +template +void stableSort(List& a); + +template +void stableSort(List& a, const Cmp&); + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C index b20baf4645..78e9553d96 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C @@ -28,14 +28,11 @@ License // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -namespace Foam -{ - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // Construct from List template -SortableList::SortableList(const List& values) +Foam::SortableList::SortableList(const List& values) : List(values), indices_(values.size()) @@ -46,7 +43,7 @@ SortableList::SortableList(const List& values) // Construct given size. Sort later on. template -SortableList::SortableList(const label size) +Foam::SortableList::SortableList(const label size) : List(size), indices_(size) @@ -55,7 +52,7 @@ SortableList::SortableList(const label size) // Construct given size and initial value. Sort later on. template -SortableList::SortableList(const label size, const Type& val) +Foam::SortableList::SortableList(const label size, const Type& val) : List(size, val), indices_(size) @@ -64,7 +61,7 @@ SortableList::SortableList(const label size, const Type& val) // Construct as copy. template -SortableList::SortableList(const SortableList& lst) +Foam::SortableList::SortableList(const SortableList& lst) : List(lst), indices_(lst.indices()) @@ -74,7 +71,7 @@ SortableList::SortableList(const SortableList& lst) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -void SortableList::setSize(const label newSize) +void Foam::SortableList::setSize(const label newSize) { List::setSize(newSize); indices_.setSize(newSize); @@ -82,7 +79,7 @@ void SortableList::setSize(const label newSize) template -void SortableList::sort() +void Foam::SortableList::sort() { forAll(indices_, i) { @@ -98,7 +95,29 @@ void SortableList::sort() tmpValues[i] = this->operator[](indices_[i]); } - List::operator=(tmpValues); + List::transfer(tmpValues); +} + + + +template +void Foam::SortableList::stableSort() +{ + forAll(indices_, i) + { + indices_[i] = i; + } + + Foam::stableSort(indices_, less(*this)); + + List tmpValues(this->size()); + + forAll(indices_, i) + { + tmpValues[i] = this->operator[](indices_[i]); + } + + List::transfer(tmpValues); } @@ -114,6 +133,4 @@ void Foam::SortableList::operator=(const SortableList& rhs) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H index 6a242180c2..389f98215e 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H @@ -100,7 +100,7 @@ public: // 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 { return indices_; @@ -112,6 +112,9 @@ public: //- Sort the list (if changed after construction time) void sort(); + //- Sort the list (if changed after construction time) + void stableSort(); + // Member Operators