From bf30779b6465efb3d0cb9cb74320bba049b8e133 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 24 Apr 2019 19:03:00 +0200
Subject: [PATCH] ENH: add partial sorting to SortableList

---
 applications/test/sort/Test-sortList.C        |  6 ++-
 .../Lists/SortableList/SortableList.C         | 48 +++++++++++++++++--
 .../Lists/SortableList/SortableList.H         |  8 +++-
 3 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/applications/test/sort/Test-sortList.C b/applications/test/sort/Test-sortList.C
index 9e511eb07f0..4cadeeb65ca 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 096dd6444eb..21611844ad7 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 25244db46aa..6a506fac083 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);
 
-- 
GitLab