]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/private/kfileitemmodelsortalgorithm.cpp
Use std::rotate, rather than reversing three times
[dolphin.git] / src / kitemviews / private / kfileitemmodelsortalgorithm.cpp
index 9588d19bf135678b98ed7d1a808e4286c47284b3..a09d0cd80cd4afd953a375721421d544196b3051 100644 (file)
 #include <QThread>
 #include <QtCore>
 
+#include <algorithm>
+
 void KFileItemModelSortAlgorithm::sort(KFileItemModel* model,
                                        QList<KFileItemModel::ItemData*>::iterator begin,
                                        QList<KFileItemModel::ItemData*>::iterator end)
 {
-    static const int numberOfThreads = QThread::idealThreadCount();
-    parallelSort(model, begin, end, numberOfThreads);
+    if (model->sortRole() == model->roleForType(KFileItemModel::NameRole)) {
+        // Sorting by name can be expensive, in particular if natural sorting is
+        // enabled. Use all CPU cores to speed up the sorting process.
+        static const int numberOfThreads = QThread::idealThreadCount();
+        parallelSort(model, begin, end, numberOfThreads);
+    } else {
+        // Sorting by other roles is quite fast. Use only one thread to prevent
+        // problems caused by non-reentrant comparison functions, see
+        // https://bugs.kde.org/show_bug.cgi?id=312679
+        sequentialSort(model, begin, end);
+    }
 }
 
 void KFileItemModelSortAlgorithm::sequentialSort(KFileItemModel* model,
@@ -106,9 +117,7 @@ void KFileItemModelSortAlgorithm::merge(KFileItemModel* model,
         firstCut = upperBound(model, begin, pivot, *secondCut);
     }
 
-    reverse(firstCut, pivot);
-    reverse(pivot, secondCut);
-    reverse(firstCut, secondCut);
+    std::rotate(firstCut, pivot, secondCut);
 
     const QList<KFileItemModel::ItemData*>::iterator newPivot = firstCut + len2Half;
     merge(model, begin, firstCut, newPivot);
@@ -167,15 +176,3 @@ KFileItemModelSortAlgorithm::upperBound(KFileItemModel* model,
     }
     return begin;
 }
-
-void KFileItemModelSortAlgorithm::reverse(QList<KFileItemModel::ItemData*>::iterator begin,
-                                          QList<KFileItemModel::ItemData*>::iterator end)
-{
-    // The implementation is based on qReverse() from qalgorithms.h
-    // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-
-    --end;
-    while (begin < end) {
-        qSwap(*begin++, *end--);
-    }
-}