| caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 7 | #ifndef TSearch_DEFINED |
| 8 | #define TSearch_DEFINED |
| 9 | |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 10 | #include "SkTypes.h" |
| 11 | |
| 12 | // FIXME: Move this templated version into SKTSearch.h |
| 13 | |
| 14 | template <typename T> |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame^] | 15 | static T* QSort_Partition(T* left, T* right, T* pivot) |
| 16 | { |
| 17 | T pivotValue = *pivot; |
| 18 | SkTSwap(*pivot, *right); |
| 19 | T* newPivot = left; |
| 20 | while (left < right) { |
| 21 | if (*left < pivotValue) { |
| 22 | SkTSwap(*left, *newPivot); |
| 23 | newPivot += 1; |
| 24 | } |
| 25 | left += 1; |
| 26 | } |
| 27 | SkTSwap(*newPivot, *right); |
| 28 | return newPivot; |
| 29 | } |
| 30 | |
| 31 | template <typename T> |
| 32 | void QSort(T* left, T* right) |
| 33 | { |
| 34 | if (left >= right) { |
| 35 | return; |
| 36 | } |
| 37 | T* pivot = left + (right - left >> 1); |
| 38 | pivot = QSort_Partition(left, right, pivot); |
| 39 | QSort(left, pivot - 1); |
| 40 | QSort(pivot + 1, right); |
| 41 | } |
| 42 | |
| 43 | template <typename T> |
| caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 44 | static T** QSort_Partition(T** left, T** right, T** pivot) |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 45 | { |
| caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 46 | T* pivotValue = *pivot; |
| 47 | SkTSwap(*pivot, *right); |
| 48 | T** newPivot = left; |
| 49 | while (left < right) { |
| 50 | if (**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 | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 54 | left += 1; |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 55 | } |
| caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +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 | |
| 60 | template <typename T> |
| caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 61 | void QSort(T** left, T** right) |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 62 | { |
| caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 63 | if (left >= right) { |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 64 | return; |
| 65 | } |
| caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 66 | T** pivot = left + (right - left >> 1); |
| 67 | pivot = QSort_Partition(left, right, pivot); |
| 68 | QSort(left, pivot - 1); |
| 69 | QSort(pivot + 1, right); |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 72 | template <typename S, typename T> |
| caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 73 | static T* QSort_Partition(S& context, T* left, T* right, T* pivot, |
| 74 | bool (*lessThan)(S&, const T, const T)) |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 75 | { |
| caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 76 | T pivotValue = *pivot; |
| 77 | SkTSwap(*pivot, *right); |
| 78 | T* newPivot = left; |
| 79 | while (left < right) { |
| 80 | if (lessThan(context, *left, pivotValue)) { |
| 81 | SkTSwap(*left, *newPivot); |
| 82 | newPivot += 1; |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 83 | } |
| caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 84 | left += 1; |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 85 | } |
| caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 86 | SkTSwap(*newPivot, *right); |
| 87 | return newPivot; |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 90 | template <typename S, typename T> |
| caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 91 | void QSort(S& context, T* left, T* right, |
| 92 | bool (*lessThan)(S& , const T, const T)) |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 93 | { |
| caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 94 | if (left >= right) { |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 95 | return; |
| 96 | } |
| caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 97 | T* pivot = left + (right - left >> 1); |
| 98 | pivot = QSort_Partition(context, left, right, pivot, lessThan); |
| 99 | QSort(context, left, pivot - 1, lessThan); |
| 100 | QSort(context, pivot + 1, right, lessThan); |
| caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 101 | } |
| caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 102 | |
| 103 | #endif |