blob: e76f5c93f7b64bc804f871c833f81f0da80ab9fe [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
9#ifndef SkTSort_DEFINED
10#define SkTSort_DEFINED
11
12#include "SkTypes.h"
halcanary4dbbd042016-06-07 17:21:10 -070013#include "SkMathPriv.h"
bungeman@google.come83e9942013-01-30 21:01:26 +000014
15/* A comparison functor which performs the comparison 'a < b'. */
16template <typename T> struct SkTCompareLT {
17 bool operator()(const T a, const T b) const { return a < b; }
18};
19
20/* A comparison functor which performs the comparison '*a < *b'. */
21template <typename T> struct SkTPointerCompareLT {
22 bool operator()(const T* a, const T* b) const { return *a < *b; }
23};
24
25///////////////////////////////////////////////////////////////////////////////
26
27/* Sifts a broken heap. The input array is a heap from root to bottom
bungeman@google.com59ed9102013-01-24 22:18:56 +000028 * except that the root entry may be out of place.
29 *
30 * Sinks a hole from array[root] to leaf and then sifts the original array[root] element
31 * from the leaf level up.
32 *
33 * This version does extra work, in that it copies child to parent on the way down,
34 * then copies parent to child on the way back up. When copies are inexpensive,
35 * this is an optimization as this sift variant should only be used when
36 * the potentially out of place root entry value is expected to be small.
37 *
38 * @param root the one based index into array of the out-of-place root of the heap.
39 * @param bottom the one based index in the array of the last entry in the heap.
40 */
bungeman@google.come83e9942013-01-30 21:01:26 +000041template <typename T, typename C>
42void SkTHeapSort_SiftUp(T array[], size_t root, size_t bottom, C lessThan) {
bungeman@google.com59ed9102013-01-24 22:18:56 +000043 T x = array[root-1];
44 size_t start = root;
45 size_t j = root << 1;
46 while (j <= bottom) {
bungeman@google.come83e9942013-01-30 21:01:26 +000047 if (j < bottom && lessThan(array[j-1], array[j])) {
bungeman@google.com59ed9102013-01-24 22:18:56 +000048 ++j;
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 }
bungeman@google.com59ed9102013-01-24 22:18:56 +000050 array[root-1] = array[j-1];
51 root = j;
52 j = root << 1;
53 }
54 j = root >> 1;
55 while (j >= start) {
bungeman@google.come83e9942013-01-30 21:01:26 +000056 if (lessThan(array[j-1], x)) {
bungeman@google.com59ed9102013-01-24 22:18:56 +000057 array[root-1] = array[j-1];
58 root = j;
59 j = root >> 1;
reed@android.comeff416b2009-03-18 03:08:15 +000060 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 break;
reed@android.comeff416b2009-03-18 03:08:15 +000062 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 }
bungeman@google.com59ed9102013-01-24 22:18:56 +000064 array[root-1] = x;
reed@android.com8a1c16f2008-12-17 15:59:43 +000065}
66
bungeman@google.come83e9942013-01-30 21:01:26 +000067/* Sifts a broken heap. The input array is a heap from root to bottom
bungeman@google.com59ed9102013-01-24 22:18:56 +000068 * except that the root entry may be out of place.
69 *
70 * Sifts the array[root] element from the root down.
71 *
72 * @param root the one based index into array of the out-of-place root of the heap.
73 * @param bottom the one based index in the array of the last entry in the heap.
74 */
bungeman@google.come83e9942013-01-30 21:01:26 +000075template <typename T, typename C>
76void SkTHeapSort_SiftDown(T array[], size_t root, size_t bottom, C lessThan) {
bungeman@google.com59ed9102013-01-24 22:18:56 +000077 T x = array[root-1];
78 size_t child = root << 1;
79 while (child <= bottom) {
bungeman@google.come83e9942013-01-30 21:01:26 +000080 if (child < bottom && lessThan(array[child-1], array[child])) {
bungeman@google.com59ed9102013-01-24 22:18:56 +000081 ++child;
82 }
bungeman@google.come83e9942013-01-30 21:01:26 +000083 if (lessThan(x, array[child-1])) {
bungeman@google.com59ed9102013-01-24 22:18:56 +000084 array[root-1] = array[child-1];
85 root = child;
86 child = root << 1;
87 } else {
88 break;
89 }
reed@android.comeff416b2009-03-18 03:08:15 +000090 }
bungeman@google.com59ed9102013-01-24 22:18:56 +000091 array[root-1] = x;
92}
93
bsalomon@google.comff436612013-02-27 19:07:32 +000094/** Sorts the array of size count using comparator lessThan using a Heap Sort algorithm. Be sure to
95 * specialize SkTSwap if T has an efficient swap operation.
bungeman@google.come83e9942013-01-30 21:01:26 +000096 *
97 * @param array the array to be sorted.
98 * @param count the number of elements in the array.
99 * @param lessThan a functor with bool operator()(T a, T b) which returns true if a comes before b.
100 */
101template <typename T, typename C> void SkTHeapSort(T array[], size_t count, C lessThan) {
bungeman@google.com59ed9102013-01-24 22:18:56 +0000102 for (size_t i = count >> 1; i > 0; --i) {
bungeman@google.come83e9942013-01-30 21:01:26 +0000103 SkTHeapSort_SiftDown(array, i, count, lessThan);
bungeman@google.com59ed9102013-01-24 22:18:56 +0000104 }
105
106 for (size_t i = count - 1; i > 0; --i) {
reed@android.comeff416b2009-03-18 03:08:15 +0000107 SkTSwap<T>(array[0], array[i]);
bungeman@google.come83e9942013-01-30 21:01:26 +0000108 SkTHeapSort_SiftUp(array, 1, i, lessThan);
109 }
110}
111
112/** Sorts the array of size count using comparator '<' using a Heap Sort algorithm. */
113template <typename T> void SkTHeapSort(T array[], size_t count) {
114 SkTHeapSort(array, count, SkTCompareLT<T>());
115}
116
117///////////////////////////////////////////////////////////////////////////////
118
119/** Sorts the array of size count using comparator lessThan using an Insertion Sort algorithm. */
120template <typename T, typename C> static void SkTInsertionSort(T* left, T* right, C lessThan) {
121 for (T* next = left + 1; next <= right; ++next) {
Ben Wagner8508f652016-10-26 17:48:51 -0400122 if (!lessThan(*next, *(next - 1))) {
123 continue;
124 }
bungeman6f4293a2016-10-26 12:11:28 -0700125 T insert = std::move(*next);
bungeman@google.come83e9942013-01-30 21:01:26 +0000126 T* hole = next;
Ben Wagner8508f652016-10-26 17:48:51 -0400127 do {
bungeman6f4293a2016-10-26 12:11:28 -0700128 *hole = std::move(*(hole - 1));
bungeman@google.come83e9942013-01-30 21:01:26 +0000129 --hole;
Ben Wagner8508f652016-10-26 17:48:51 -0400130 } while (left < hole && lessThan(insert, *(hole - 1)));
bungeman6f4293a2016-10-26 12:11:28 -0700131 *hole = std::move(insert);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 }
133}
134
reed@google.com92fde902012-05-07 18:10:15 +0000135///////////////////////////////////////////////////////////////////////////////
136
bungeman@google.come83e9942013-01-30 21:01:26 +0000137template <typename T, typename C>
138static T* SkTQSort_Partition(T* left, T* right, T* pivot, C lessThan) {
139 T pivotValue = *pivot;
reed@google.com92fde902012-05-07 18:10:15 +0000140 SkTSwap(*pivot, *right);
bungeman@google.come83e9942013-01-30 21:01:26 +0000141 T* newPivot = left;
reed@google.com92fde902012-05-07 18:10:15 +0000142 while (left < right) {
bungeman@google.come83e9942013-01-30 21:01:26 +0000143 if (lessThan(*left, pivotValue)) {
reed@google.com92fde902012-05-07 18:10:15 +0000144 SkTSwap(*left, *newPivot);
145 newPivot += 1;
146 }
147 left += 1;
148 }
149 SkTSwap(*newPivot, *right);
150 return newPivot;
151}
152
bungeman@google.come83e9942013-01-30 21:01:26 +0000153/* Intro Sort is a modified Quick Sort.
bungeman@google.come83e9942013-01-30 21:01:26 +0000154 * When the region to be sorted is a small constant size it uses Insertion Sort.
155 * When depth becomes zero, it switches over to Heap Sort.
bungeman@google.com4e5a8952013-02-04 19:54:15 +0000156 * This implementation recurses on the left region after pivoting and loops on the right,
157 * we already limit the stack depth by switching to heap sort,
158 * and cache locality on the data appears more important than saving a few stack frames.
159 *
160 * @param depth at this recursion depth, switch to Heap Sort.
161 * @param left the beginning of the region to be sorted.
162 * @param right the end of the region to be sorted (inclusive).
163 * @param lessThan a functor with bool operator()(T a, T b) which returns true if a comes before b.
bungeman@google.come83e9942013-01-30 21:01:26 +0000164 */
165template <typename T, typename C> void SkTIntroSort(int depth, T* left, T* right, C lessThan) {
bungeman@google.com4e5a8952013-02-04 19:54:15 +0000166 while (true) {
167 if (right - left < 32) {
168 SkTInsertionSort(left, right, lessThan);
169 return;
170 }
171
bungeman@google.come83e9942013-01-30 21:01:26 +0000172 if (depth == 0) {
173 SkTHeapSort<T>(left, right - left + 1, lessThan);
174 return;
175 }
176 --depth;
bungeman@google.combbec4e62013-01-25 18:25:17 +0000177
bungeman@google.come83e9942013-01-30 21:01:26 +0000178 T* pivot = left + ((right - left) >> 1);
179 pivot = SkTQSort_Partition(left, right, pivot, lessThan);
180
bungeman@google.com4e5a8952013-02-04 19:54:15 +0000181 SkTIntroSort(depth, left, pivot - 1, lessThan);
182 left = pivot + 1;
reed@google.com92fde902012-05-07 18:10:15 +0000183 }
rileya@google.com5ee3f672012-08-28 14:40:49 +0000184}
185
bsalomon@google.comff436612013-02-27 19:07:32 +0000186/** Sorts the region from left to right using comparator lessThan using a Quick Sort algorithm. Be
187 * sure to specialize SkTSwap if T has an efficient swap operation.
bungeman@google.come83e9942013-01-30 21:01:26 +0000188 *
189 * @param left the beginning of the region to be sorted.
190 * @param right the end of the region to be sorted (inclusive).
191 * @param lessThan a functor with bool operator()(T a, T b) which returns true if a comes before b.
192 */
193template <typename T, typename C> void SkTQSort(T* left, T* right, C lessThan) {
bungeman@google.com118c1a72013-01-30 21:23:13 +0000194 if (left >= right) {
195 return;
196 }
bungeman@google.com4e5a8952013-02-04 19:54:15 +0000197 // Limit Intro Sort recursion depth to no more than 2 * ceil(log2(n)).
198 int depth = 2 * SkNextLog2(SkToU32(right - left));
199 SkTIntroSort(depth, left, right, lessThan);
rileya@google.com5ee3f672012-08-28 14:40:49 +0000200}
201
bungeman@google.come83e9942013-01-30 21:01:26 +0000202/** Sorts the region from left to right using comparator '<' using a Quick Sort algorithm. */
rileya@google.com5ee3f672012-08-28 14:40:49 +0000203template <typename T> void SkTQSort(T* left, T* right) {
bungeman@google.come83e9942013-01-30 21:01:26 +0000204 SkTQSort(left, right, SkTCompareLT<T>());
reed@google.com92fde902012-05-07 18:10:15 +0000205}
206
bungeman@google.come83e9942013-01-30 21:01:26 +0000207/** Sorts the region from left to right using comparator '* < *' using a Quick Sort algorithm. */
208template <typename T> void SkTQSort(T** left, T** right) {
209 SkTQSort(left, right, SkTPointerCompareLT<T>());
reed@google.com92fde902012-05-07 18:10:15 +0000210}
211
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212#endif