OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkTSort_DEFINED | 10 #ifndef SkTSort_DEFINED |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 array[root-1] = array[child-1]; | 85 array[root-1] = array[child-1]; |
86 root = child; | 86 root = child; |
87 child = root << 1; | 87 child = root << 1; |
88 } else { | 88 } else { |
89 break; | 89 break; |
90 } | 90 } |
91 } | 91 } |
92 array[root-1] = x; | 92 array[root-1] = x; |
93 } | 93 } |
94 | 94 |
95 /** Sorts the array of size count using comparator lessThan using a Heap Sort al
gorithm | 95 /** Sorts the array of size count using comparator lessThan using a Heap Sort al
gorithm. Be sure to |
| 96 * specialize SkTSwap if T has an efficient swap operation. |
96 * | 97 * |
97 * @param array the array to be sorted. | 98 * @param array the array to be sorted. |
98 * @param count the number of elements in the array. | 99 * @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 * @param lessThan a functor with bool operator()(T a, T b) which returns true
if a comes before b. |
100 */ | 101 */ |
101 template <typename T, typename C> void SkTHeapSort(T array[], size_t count, C le
ssThan) { | 102 template <typename T, typename C> void SkTHeapSort(T array[], size_t count, C le
ssThan) { |
102 for (size_t i = count >> 1; i > 0; --i) { | 103 for (size_t i = count >> 1; i > 0; --i) { |
103 SkTHeapSort_SiftDown(array, i, count, lessThan); | 104 SkTHeapSort_SiftDown(array, i, count, lessThan); |
104 } | 105 } |
105 | 106 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 --depth; | 174 --depth; |
174 | 175 |
175 T* pivot = left + ((right - left) >> 1); | 176 T* pivot = left + ((right - left) >> 1); |
176 pivot = SkTQSort_Partition(left, right, pivot, lessThan); | 177 pivot = SkTQSort_Partition(left, right, pivot, lessThan); |
177 | 178 |
178 SkTIntroSort(depth, left, pivot - 1, lessThan); | 179 SkTIntroSort(depth, left, pivot - 1, lessThan); |
179 left = pivot + 1; | 180 left = pivot + 1; |
180 } | 181 } |
181 } | 182 } |
182 | 183 |
183 /** Sorts the region from left to right using comparator lessThan using a Quick
Sort algorithm. | 184 /** Sorts the region from left to right using comparator lessThan using a Quick
Sort algorithm. Be |
| 185 * sure to specialize SkTSwap if T has an efficient swap operation. |
184 * | 186 * |
185 * @param left the beginning of the region to be sorted. | 187 * @param left the beginning of the region to be sorted. |
186 * @param right the end of the region to be sorted (inclusive). | 188 * @param right the end of the region to be sorted (inclusive). |
187 * @param lessThan a functor with bool operator()(T a, T b) which returns true
if a comes before b. | 189 * @param lessThan a functor with bool operator()(T a, T b) which returns true
if a comes before b. |
188 */ | 190 */ |
189 template <typename T, typename C> void SkTQSort(T* left, T* right, C lessThan) { | 191 template <typename T, typename C> void SkTQSort(T* left, T* right, C lessThan) { |
190 if (left >= right) { | 192 if (left >= right) { |
191 return; | 193 return; |
192 } | 194 } |
193 // Limit Intro Sort recursion depth to no more than 2 * ceil(log2(n)). | 195 // Limit Intro Sort recursion depth to no more than 2 * ceil(log2(n)). |
194 int depth = 2 * SkNextLog2(SkToU32(right - left)); | 196 int depth = 2 * SkNextLog2(SkToU32(right - left)); |
195 SkTIntroSort(depth, left, right, lessThan); | 197 SkTIntroSort(depth, left, right, lessThan); |
196 } | 198 } |
197 | 199 |
198 /** Sorts the region from left to right using comparator '<' using a Quick Sort
algorithm. */ | 200 /** Sorts the region from left to right using comparator '<' using a Quick Sort
algorithm. */ |
199 template <typename T> void SkTQSort(T* left, T* right) { | 201 template <typename T> void SkTQSort(T* left, T* right) { |
200 SkTQSort(left, right, SkTCompareLT<T>()); | 202 SkTQSort(left, right, SkTCompareLT<T>()); |
201 } | 203 } |
202 | 204 |
203 /** Sorts the region from left to right using comparator '* < *' using a Quick S
ort algorithm. */ | 205 /** Sorts the region from left to right using comparator '* < *' using a Quick S
ort algorithm. */ |
204 template <typename T> void SkTQSort(T** left, T** right) { | 206 template <typename T> void SkTQSort(T** left, T** right) { |
205 SkTQSort(left, right, SkTPointerCompareLT<T>()); | 207 SkTQSort(left, right, SkTPointerCompareLT<T>()); |
206 } | 208 } |
207 | 209 |
| 210 /** Adapts a tri-state SkTSearch comparison function to a bool less-than SkTSort
functor */ |
| 211 template <typename T, int (COMPARE)(const T*, const T*)> |
| 212 class SkTSearchCompareLTFunctor { |
| 213 public: |
| 214 bool operator()(const T& a, const T& b) { |
| 215 return COMPARE(&a, &b) < 0; |
| 216 } |
| 217 }; |
| 218 |
| 219 |
208 #endif | 220 #endif |
OLD | NEW |