diff --git a/applications/test/sort/Test-sortList.C b/applications/test/sort/Test-sortList.C index 65df041c2bff3f0cad73e64a6685318fa0e73230..713021e41b2c5bf75838cd8572f37a6a621586b6 100644 --- a/applications/test/sort/Test-sortList.C +++ b/applications/test/sort/Test-sortList.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010, 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2010, 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011 OpenFOAM Foundation @@ -67,6 +67,10 @@ int main(int argc, char *argv[]) Info<< "reverse ..." << nl; printInfo(list1r); + list1r.partialSort(list1r.size()/2); + Info<< "partial sorted ..." << nl; + printInfo(list1r); + SortableList<label> list2(orig); Info<< "unsorted: " << orig << nl; printInfo(list2); diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C index 362fa047743c86a52ce63f6d342002530c8d1a58..09456bd48bda4d00c80c8460678c21e8afbde939 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) 2004-2010, 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2010, 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -136,7 +136,7 @@ Foam::List<T>& Foam::SortableList<T>::shrink() template<class T> void Foam::SortableList<T>::sort() { - Foam::sortedOrder(*this, indices_); + Foam::sortedOrder(*this, indices_, typename UList<T>::less(*this)); List<T> list(*this, indices_); // Copy with indices for mapping List<T>::transfer(list); @@ -148,8 +148,48 @@ void Foam::SortableList<T>::reverseSort() { Foam::sortedOrder(*this, indices_, typename UList<T>::greater(*this)); - List<T> lst(*this, indices_); // Copy with indices for mapping - List<T>::transfer(lst); + List<T> list(*this, indices_); // Copy with indices for mapping + List<T>::transfer(list); +} + + +template<class T> +void Foam::SortableList<T>::partialSort(label n, label start) +{ + indices_.resize(this->size()); + ListOps::identity(indices_); + + // Forward partial sort of indices + std::partial_sort + ( + indices_.begin() + start, + indices_.begin() + start + n, + indices_.end(), + typename UList<T>::less(*this) + ); + + List<T> list(*this, indices_); // Copy with indices for mapping + List<T>::transfer(list); +} + + +template<class T> +void Foam::SortableList<T>::partialReverseSort(label n, label start) +{ + indices_.resize(this->size()); + ListOps::identity(indices_); + + // Reverse partial sort of indices + std::partial_sort + ( + indices_.begin() + start, + indices_.begin() + start + n, + indices_.end(), + typename UList<T>::greater(*this) + ); + + List<T> list(*this, indices_); // Copy with indices for mapping + List<T>::transfer(list); } diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H index 1146f9d78e2231f64499c5ab1edf248be6ac5ce8..3a1245c84e0477028f445e89d065fd4a5191db71 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) 2004-2010, 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2010, 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -130,6 +130,12 @@ public: // Resizes the indices as required void reverseSort(); + //- Forward partial sort the list until the middle point + void partialSort(label n, label start=0); + + //- Reverse partial sort the list until the middle point + void partialReverseSort(label n, label start=0); + //- Swap content with another SortableList in constant time inline void swap(SortableList<T>& lst);