diff --git a/applications/test/sort/Test-sortList.C b/applications/test/sort/Test-sortList.C index 9e511eb07f0e212597b95b4e7260f9c1b3248a1b..4cadeeb65ca72fc00fa8b5692371b538270f597c 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) 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 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 096dd6444ebc9ffa0c1071a307d2f20f589905c6..21611844ad728a802ad652a1824a92d7c0bdcce4 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) 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 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 25244db46aa7f3b8ddcf14d0cf87ec657b2f4bbf..6a506fac083ca3e1e8102203f9ad285c8f380ae9 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) 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 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);