diff --git a/applications/test/IndirectList/Test-IndirectList.C b/applications/test/IndirectList/Test-IndirectList.C index 6ea616c0ba..2f3a2e45d4 100644 --- a/applications/test/IndirectList/Test-IndirectList.C +++ b/applications/test/IndirectList/Test-IndirectList.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -196,19 +196,23 @@ int main(int argc, char *argv[]) SortList sorter1(list1); + Info<< nl << "Sorted: " << flatOutput(sorter1) << nl; Info<< nl << "Sort indices: " << flatOutput(sorter1.indices()) << nl; - Info<< nl << "Reverse indices: " << flatOutput(sorter1.indices()) << nl; - + // Reverses indices sorter1.reverse(); - Info<< nl << "Again indices: " << flatOutput(sorter1.indices()) << nl; + Info<< nl << "Reverse: " << flatOutput(sorter1.indices()) << nl; + + sorter1.reset(); + + Info<< nl << "Reset indices: " << flatOutput(sorter1.indices()) << nl; sorter1.reverseSort(); Info<< nl << "Reverse indices: " << flatOutput(sorter1.indices()) << nl; - Info<< nl << "Sorted : " << flatOutput(sorter1) << nl; + Info<< nl << "Reverse sorted: " << flatOutput(sorter1) << nl; sorter1.sort(std::greater()); diff --git a/src/OpenFOAM/containers/IndirectLists/SortList/SortList.H b/src/OpenFOAM/containers/IndirectLists/SortList/SortList.H index e074413300..54622fe51a 100644 --- a/src/OpenFOAM/containers/IndirectLists/SortList/SortList.H +++ b/src/OpenFOAM/containers/IndirectLists/SortList/SortList.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -37,8 +37,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef SortList_H -#define SortList_H +#ifndef Foam_SortList_H +#define Foam_SortList_H #include "IndirectList.H" @@ -72,13 +72,13 @@ public: // Member Functions - //- Return the list of sorted indices. Updated every sort + //- Return the list of sorted indices (updated every sort). // Same as addressing() - inline const labelUList& indices() const; + inline const labelUList& indices() const noexcept; - //- Return the list of indices. Updated every sort + //- Return the list of indices (updated every sort). // Same as addressing() - inline labelList& indices(); + inline labelList& indices() noexcept; //- Reverse the indices inline void reverse(); @@ -94,9 +94,13 @@ public: //- Functionally identical to sort with std::less\() inline void sort(); - //- Reverse (stable) sort the list + //- Reverse (stable) sort the list. + //- Functionally identical to sort with std::greater\() inline void reverseSort(); + //- Sort the list, only retaining unique entries + inline void uniqueSort(); + // Member Operators diff --git a/src/OpenFOAM/containers/IndirectLists/SortList/SortListI.H b/src/OpenFOAM/containers/IndirectLists/SortList/SortListI.H index 98df2a0ff3..1d01531742 100644 --- a/src/OpenFOAM/containers/IndirectLists/SortList/SortListI.H +++ b/src/OpenFOAM/containers/IndirectLists/SortList/SortListI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,7 +32,7 @@ License template inline Foam::SortList::SortList(const UList& values) : - IndirectList(values, identity(values.size())) + IndirectList(values, labelList()) { sort(); } @@ -42,7 +42,7 @@ template template inline Foam::SortList::SortList(const UList& values, const Compare& comp) : - IndirectList(values, identity(values.size())) + IndirectList(values, labelList()) { sort(comp); } @@ -51,14 +51,14 @@ inline Foam::SortList::SortList(const UList& values, const Compare& comp) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -inline const Foam::labelUList& Foam::SortList::indices() const +inline const Foam::labelUList& Foam::SortList::indices() const noexcept { return this->addressing(); } template -inline Foam::labelList& Foam::SortList::indices() +inline Foam::labelList& Foam::SortList::indices() noexcept { return this->addressing(); } @@ -74,10 +74,9 @@ inline void Foam::SortList::reverse() template inline void Foam::SortList::reset() { - const label len = this->values().size(); + auto& addr = this->indices(); - labelList& addr = this->indices(); - addr.resize(len); + addr.resize_nocopy(this->values().size()); ListOps::identity(addr); } @@ -86,14 +85,11 @@ template template inline void Foam::SortList::sort(const Compare& comp) { - UList& vals = this->values(); - labelList& addr = this->indices(); + auto& vals = this->values(); + auto& addr = this->indices(); - if (addr.size() != vals.size()) - { - addr.resize(vals.size()); - ListOps::identity(addr); - } + addr.resize_nocopy(vals.size()); + ListOps::identity(addr); std::stable_sort ( @@ -107,43 +103,26 @@ inline void Foam::SortList::sort(const Compare& comp) template inline void Foam::SortList::sort() { - UList& vals = this->values(); - labelList& addr = this->indices(); + Foam::sortedOrder(this->values(), this->indices()); +} - if (addr.size() != vals.size()) - { - addr.resize(vals.size()); - ListOps::identity(addr); - } - // Forward sort of indices - std::stable_sort - ( - addr.begin(), - addr.end(), - [&](label a, label b) -> bool { return vals[a] < vals[b]; } - ); +template +inline void Foam::SortList::uniqueSort() +{ + Foam::uniqueOrder(this->values(), this->indices()); } template inline void Foam::SortList::reverseSort() { - UList& vals = this->values(); - labelList& addr = this->indices(); - - if (addr.size() != vals.size()) - { - addr.resize(vals.size()); - ListOps::identity(addr); - } - - // Reverse sort of indices - std::stable_sort + // Reverse sorted order for indices + Foam::sortedOrder ( - addr.begin(), - addr.end(), - [&](label a, label b) -> bool { return vals[b] < vals[a]; } + this->values(), + this->indices(), + typename UList::greater(this->values()) ); } diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C index 20f9fd02e6..70cc75a575 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -143,7 +143,7 @@ void Foam::SortableList::reverseSort() template void Foam::SortableList::partialSort(label n, label start) { - indices_.resize(this->size()); + indices_.resize_nocopy(this->size()); ListOps::identity(indices_); // Forward partial sort of indices @@ -163,7 +163,7 @@ void Foam::SortableList::partialSort(label n, label start) template void Foam::SortableList::partialReverseSort(label n, label start) { - indices_.resize(this->size()); + indices_.resize_nocopy(this->size()); ListOps::identity(indices_); // Reverse partial sort of indices