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> |
| 6 | static void QSort_Partition(T** first, T** last) |
| 7 | { |
| 8 | T** left = first; |
| 9 | T** rite = last; |
| 10 | T** pivot = left; |
| 11 | |
| 12 | while (left <= rite) { |
| 13 | while (left < last && **left < **pivot) |
| 14 | left += 1; |
| 15 | while (first < rite && **pivot < **rite) |
| 16 | rite -= 1; |
| 17 | if (left <= rite) { |
| 18 | if (left < rite) { |
| 19 | SkTSwap(*left, *rite); |
| 20 | } |
| 21 | left += 1; |
| 22 | rite -= 1; |
| 23 | } |
| 24 | } |
| 25 | if (first < rite) |
| 26 | QSort_Partition(first, rite); |
| 27 | if (left < last) |
| 28 | QSort_Partition(left, last); |
| 29 | } |
| 30 | |
| 31 | template <typename T> |
| 32 | void QSort(T** base, size_t count) |
| 33 | { |
| 34 | SkASSERT(base); |
| 35 | |
| 36 | if (count <= 1) { |
| 37 | return; |
| 38 | } |
| 39 | QSort_Partition(base, base + (count - 1)); |
| 40 | } |
| 41 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 42 | template <typename S, typename T> |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 43 | static T* QSort_Partition(S& context, T* left, T* right, T* pivot, |
| 44 | bool (*lessThan)(S&, const T, const T)) |
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 | T pivotValue = *pivot; |
| 47 | SkTSwap(*pivot, *right); |
| 48 | T* newPivot = left; |
| 49 | while (left < right) { |
| 50 | if (lessThan(context, *left, pivotValue)) { |
| 51 | SkTSwap(*left, *newPivot); |
| 52 | newPivot += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 53 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 54 | left += 1; |
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 | SkTSwap(*newPivot, *right); |
| 57 | return newPivot; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 58 | } |
| 59 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 60 | template <typename S, typename T> |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 61 | void QSort(S& context, T* left, T* right, |
| 62 | bool (*lessThan)(S& , const T, const T)) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 63 | { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 64 | if (left >= right) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 65 | return; |
| 66 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 67 | T* pivot = left + (right - left >> 1); |
| 68 | pivot = QSort_Partition(context, left, right, pivot, lessThan); |
| 69 | QSort(context, left, pivot - 1, lessThan); |
| 70 | QSort(context, pivot + 1, right, lessThan); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 71 | } |