1
2 /*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10 #ifndef SkTSort_DEFINED
11 #define SkTSort_DEFINED
12
13 #include "SkTypes.h"
14 #include "SkMath.h"
15 #include <stddef.h>
16
17 /* A comparison functor which performs the comparison 'a < b'. */
18 template <typename T> struct SkTCompareLT {
operatorSkTCompareLT19 bool operator()(const T a, const T b) const { return a < b; }
20 };
21
22 /* A comparison functor which performs the comparison '*a < *b'. */
23 template <typename T> struct SkTPointerCompareLT {
operatorSkTPointerCompareLT24 bool operator()(const T* a, const T* b) const { return *a < *b; }
25 };
26
27 ///////////////////////////////////////////////////////////////////////////////
28
29 /* Sifts a broken heap. The input array is a heap from root to bottom
30 * except that the root entry may be out of place.
31 *
32 * Sinks a hole from array[root] to leaf and then sifts the original array[root] element
33 * from the leaf level up.
34 *
35 * This version does extra work, in that it copies child to parent on the way down,
36 * then copies parent to child on the way back up. When copies are inexpensive,
37 * this is an optimization as this sift variant should only be used when
38 * the potentially out of place root entry value is expected to be small.
39 *
40 * @param root the one based index into array of the out-of-place root of the heap.
41 * @param bottom the one based index in the array of the last entry in the heap.
42 */
43 template <typename T, typename C>
SkTHeapSort_SiftUp(T array[],size_t root,size_t bottom,C lessThan)44 void SkTHeapSort_SiftUp(T array[], size_t root, size_t bottom, C lessThan) {
45 T x = array[root-1];
46 size_t start = root;
47 size_t j = root << 1;
48 while (j <= bottom) {
49 if (j < bottom && lessThan(array[j-1], array[j])) {
50 ++j;
51 }
52 array[root-1] = array[j-1];
53 root = j;
54 j = root << 1;
55 }
56 j = root >> 1;
57 while (j >= start) {
58 if (lessThan(array[j-1], x)) {
59 array[root-1] = array[j-1];
60 root = j;
61 j = root >> 1;
62 } else {
63 break;
64 }
65 }
66 array[root-1] = x;
67 }
68
69 /* Sifts a broken heap. The input array is a heap from root to bottom
70 * except that the root entry may be out of place.
71 *
72 * Sifts the array[root] element from the root down.
73 *
74 * @param root the one based index into array of the out-of-place root of the heap.
75 * @param bottom the one based index in the array of the last entry in the heap.
76 */
77 template <typename T, typename C>
SkTHeapSort_SiftDown(T array[],size_t root,size_t bottom,C lessThan)78 void SkTHeapSort_SiftDown(T array[], size_t root, size_t bottom, C lessThan) {
79 T x = array[root-1];
80 size_t child = root << 1;
81 while (child <= bottom) {
82 if (child < bottom && lessThan(array[child-1], array[child])) {
83 ++child;
84 }
85 if (lessThan(x, array[child-1])) {
86 array[root-1] = array[child-1];
87 root = child;
88 child = root << 1;
89 } else {
90 break;
91 }
92 }
93 array[root-1] = x;
94 }
95
96 /** Sorts the array of size count using comparator lessThan using a Heap Sort algorithm
97 *
98 * @param array the array to be sorted.
99 * @param count the number of elements in the array.
100 * @param lessThan a functor with bool operator()(T a, T b) which returns true if a comes before b.
101 */
SkTHeapSort(T array[],size_t count,C lessThan)102 template <typename T, typename C> void SkTHeapSort(T array[], size_t count, C lessThan) {
103 for (size_t i = count >> 1; i > 0; --i) {
104 SkTHeapSort_SiftDown(array, i, count, lessThan);
105 }
106
107 for (size_t i = count - 1; i > 0; --i) {
108 SkTSwap<T>(array[0], array[i]);
109 SkTHeapSort_SiftUp(array, 1, i, lessThan);
110 }
111 }
112
113 /** Sorts the array of size count using comparator '<' using a Heap Sort algorithm. */
SkTHeapSort(T array[],size_t count)114 template <typename T> void SkTHeapSort(T array[], size_t count) {
115 SkTHeapSort(array, count, SkTCompareLT<T>());
116 }
117
118 ///////////////////////////////////////////////////////////////////////////////
119
120 /** Sorts the array of size count using comparator lessThan using an Insertion Sort algorithm. */
SkTInsertionSort(T * left,T * right,C lessThan)121 template <typename T, typename C> static void SkTInsertionSort(T* left, T* right, C lessThan) {
122 for (T* next = left + 1; next <= right; ++next) {
123 T insert = *next;
124 T* hole = next;
125 while (left < hole && lessThan(insert, *(hole - 1))) {
126 *hole = *(hole - 1);
127 --hole;
128 }
129 *hole = insert;
130 }
131 }
132
133 ///////////////////////////////////////////////////////////////////////////////
134
135 template <typename T, typename C>
SkTQSort_Partition(T * left,T * right,T * pivot,C lessThan)136 static T* SkTQSort_Partition(T* left, T* right, T* pivot, C lessThan) {
137 T pivotValue = *pivot;
138 SkTSwap(*pivot, *right);
139 T* newPivot = left;
140 while (left < right) {
141 if (lessThan(*left, pivotValue)) {
142 SkTSwap(*left, *newPivot);
143 newPivot += 1;
144 }
145 left += 1;
146 }
147 SkTSwap(*newPivot, *right);
148 return newPivot;
149 }
150
151 /* Intro Sort is a modified Quick Sort.
152 * It recurses on the smaller region after pivoting and loops on the larger.
153 * When the region to be sorted is a small constant size it uses Insertion Sort.
154 * When depth becomes zero, it switches over to Heap Sort.
155 */
SkTIntroSort(int depth,T * left,T * right,C lessThan)156 template <typename T, typename C> void SkTIntroSort(int depth, T* left, T* right, C lessThan) {
157 while (left < right) {
158 if (depth == 0) {
159 SkTHeapSort<T>(left, right - left + 1, lessThan);
160 return;
161 }
162 --depth;
163
164 T* pivot = left + ((right - left) >> 1);
165 pivot = SkTQSort_Partition(left, right, pivot, lessThan);
166
167 ptrdiff_t leftSize = pivot - left;
168 ptrdiff_t rightSize = right - pivot;
169 if (leftSize < rightSize) {
170 if (leftSize < 8) {
171 SkTInsertionSort(left, pivot - 1, lessThan);
172 } else {
173 SkTIntroSort(depth, left, pivot - 1, lessThan);
174 }
175 left = pivot + 1;
176 } else {
177 if (rightSize < 8) {
178 SkTInsertionSort(pivot + 1, right, lessThan);
179 } else {
180 SkTIntroSort(depth, pivot + 1, right, lessThan);
181 }
182 right = pivot - 1;
183 }
184 }
185 }
186
187 /** Sorts the region from left to right using comparator lessThan using a Quick Sort algorithm.
188 *
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 */
SkTQSort(T * left,T * right,C lessThan)193 template <typename T, typename C> void SkTQSort(T* left, T* right, C lessThan) {
194 if (left >= right) {
195 return;
196 }
197 ptrdiff_t size = right - left;
198 int depth = SkNextLog2(SkToU32(size));
199 SkTIntroSort(depth * 2, left, right, lessThan);
200 }
201
202 /** Sorts the region from left to right using comparator '<' using a Quick Sort algorithm. */
SkTQSort(T * left,T * right)203 template <typename T> void SkTQSort(T* left, T* right) {
204 SkTQSort(left, right, SkTCompareLT<T>());
205 }
206
207 /** Sorts the region from left to right using comparator '* < *' using a Quick Sort algorithm. */
SkTQSort(T ** left,T ** right)208 template <typename T> void SkTQSort(T** left, T** right) {
209 SkTQSort(left, right, SkTPointerCompareLT<T>());
210 }
211
212 #endif
213