1 /*****************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2012 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
4 * Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com> *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 *****************************************************************************/
22 #ifndef KFILEITEMMODELSORTALGORITHM_H
23 #define KFILEITEMMODELSORTALGORITHM_H
25 #include <QtConcurrentRun>
30 * Sorts the items using the merge sort algorithm is used to assure a
31 * worst-case of O(n * log(n)) and to keep the number of comparisons low.
33 * The implementation is based on qStableSortHelper() from qalgorithms.h
34 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
37 template <typename RandomAccessIterator
, typename LessThan
>
38 static void mergeSort(RandomAccessIterator begin
,
39 RandomAccessIterator end
,
40 const LessThan
& lessThan
)
42 // The implementation is based on qStableSortHelper() from qalgorithms.h
43 // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
45 const int span
= end
- begin
;
50 const RandomAccessIterator middle
= begin
+ span
/ 2;
51 mergeSort(begin
, middle
, lessThan
);
52 mergeSort(middle
, end
, lessThan
);
53 merge(begin
, middle
, end
, lessThan
);
57 * Uses up to \a numberOfThreads threads to sort the items between
58 * \a begin and \a end. Only item ranges longer than
59 * \a parallelMergeSortingThreshold are split to be sorted by two different
62 * The comparison function \a lessThan must be reentrant.
65 template <typename RandomAccessIterator
, typename LessThan
>
66 static void parallelMergeSort(RandomAccessIterator begin
,
67 RandomAccessIterator end
,
70 int parallelMergeSortingThreshold
= 100)
72 const int span
= end
- begin
;
74 if (numberOfThreads
> 1 && span
> parallelMergeSortingThreshold
) {
75 const int newNumberOfThreads
= numberOfThreads
/ 2;
76 const RandomAccessIterator middle
= begin
+ span
/ 2;
78 QFuture
<void> future
= QtConcurrent::run(parallelMergeSort
<RandomAccessIterator
, LessThan
>, begin
, middle
, lessThan
, newNumberOfThreads
, parallelMergeSortingThreshold
);
79 parallelMergeSort(middle
, end
, lessThan
, newNumberOfThreads
, parallelMergeSortingThreshold
);
81 future
.waitForFinished();
83 merge(begin
, middle
, end
, lessThan
);
85 mergeSort(begin
, end
, lessThan
);
90 * Merges the sorted item ranges between \a begin and \a pivot and
91 * between \a pivot and \a end into a single sorted range between
92 * \a begin and \a end.
94 * The implementation is based on qMerge() from qalgorithms.h
95 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
98 template <typename RandomAccessIterator
, typename LessThan
>
99 static void merge(RandomAccessIterator begin
,
100 RandomAccessIterator pivot
,
101 RandomAccessIterator end
,
102 const LessThan
& lessThan
)
104 // The implementation is based on qMerge() from qalgorithms.h
105 // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
107 const int len1
= pivot
- begin
;
108 const int len2
= end
- pivot
;
110 if (len1
== 0 || len2
== 0) {
114 if (len1
+ len2
== 2) {
115 if (lessThan(*(begin
+ 1), *(begin
))) {
116 qSwap(*begin
, *(begin
+ 1));
121 RandomAccessIterator firstCut
;
122 RandomAccessIterator secondCut
;
125 const int len1Half
= len1
/ 2;
126 firstCut
= begin
+ len1Half
;
127 secondCut
= std::lower_bound
<RandomAccessIterator
,
128 decltype(*firstCut
), const LessThan
&>(pivot
, end
, *firstCut
, lessThan
);
129 len2Half
= secondCut
- pivot
;
132 secondCut
= pivot
+ len2Half
;
133 firstCut
= std::upper_bound
<RandomAccessIterator
,
134 decltype(*secondCut
), const LessThan
&>(begin
, pivot
, *secondCut
, lessThan
);
137 std::rotate(firstCut
, pivot
, secondCut
);
139 RandomAccessIterator newPivot
= firstCut
+ len2Half
;
140 merge(begin
, firstCut
, newPivot
, lessThan
);
141 merge(newPivot
, secondCut
, end
, lessThan
);