caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1 | #include "SkTypes.h" |
| 2 | |
| 3 | // FIXME: Move this templated version into SKTSearch.h |
| 4 | |
| 5 | template <typename T> |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame^] | 6 | static T** QSort_Partition(T** left, T** right, T** pivot) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame^] | 8 | T* pivotValue = *pivot; |
| 9 | SkTSwap(*pivot, *right); |
| 10 | T** newPivot = left; |
| 11 | while (left < right) { |
| 12 | if (**left < *pivotValue) { |
| 13 | SkTSwap(*left, *newPivot); |
| 14 | newPivot += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 15 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame^] | 16 | left += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 17 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame^] | 18 | SkTSwap(*newPivot, *right); |
| 19 | return newPivot; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | template <typename T> |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame^] | 23 | void QSort(T** left, T** right) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 24 | { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame^] | 25 | if (left >= right) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 26 | return; |
| 27 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame^] | 28 | T** pivot = left + (right - left >> 1); |
| 29 | pivot = QSort_Partition(left, right, pivot); |
| 30 | QSort(left, pivot - 1); |
| 31 | QSort(pivot + 1, right); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 32 | } |
| 33 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 34 | template <typename S, typename T> |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 35 | static T* QSort_Partition(S& context, T* left, T* right, T* pivot, |
| 36 | bool (*lessThan)(S&, const T, const T)) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 37 | { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 38 | T pivotValue = *pivot; |
| 39 | SkTSwap(*pivot, *right); |
| 40 | T* newPivot = left; |
| 41 | while (left < right) { |
| 42 | if (lessThan(context, *left, pivotValue)) { |
| 43 | SkTSwap(*left, *newPivot); |
| 44 | newPivot += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 45 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 46 | left += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 47 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 48 | SkTSwap(*newPivot, *right); |
| 49 | return newPivot; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 50 | } |
| 51 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 52 | template <typename S, typename T> |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 53 | void QSort(S& context, T* left, T* right, |
| 54 | bool (*lessThan)(S& , const T, const T)) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 55 | { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 56 | if (left >= right) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 57 | return; |
| 58 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 59 | T* pivot = left + (right - left >> 1); |
| 60 | pivot = QSort_Partition(context, left, right, pivot, lessThan); |
| 61 | QSort(context, left, pivot - 1, lessThan); |
| 62 | QSort(context, pivot + 1, right, lessThan); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 63 | } |