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 | // FIXME: Move this templated version into SKTSearch.h |
| 11 | |
| 12 | template <typename T> |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 13 | static T* QSort_Partition(T* left, T* right, T* pivot) |
| 14 | { |
| 15 | T pivotValue = *pivot; |
| 16 | SkTSwap(*pivot, *right); |
| 17 | T* newPivot = left; |
| 18 | while (left < right) { |
| 19 | if (*left < pivotValue) { |
| 20 | SkTSwap(*left, *newPivot); |
| 21 | newPivot += 1; |
| 22 | } |
| 23 | left += 1; |
| 24 | } |
| 25 | SkTSwap(*newPivot, *right); |
| 26 | return newPivot; |
| 27 | } |
| 28 | |
| 29 | template <typename T> |
| 30 | void QSort(T* left, T* right) |
| 31 | { |
| 32 | if (left >= right) { |
| 33 | return; |
| 34 | } |
| 35 | T* pivot = left + (right - left >> 1); |
| 36 | pivot = QSort_Partition(left, right, pivot); |
| 37 | QSort(left, pivot - 1); |
| 38 | QSort(pivot + 1, right); |
| 39 | } |
| 40 | |
| 41 | template <typename T> |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 42 | static T** QSort_Partition(T** left, T** right, T** pivot) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 43 | { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 44 | T* pivotValue = *pivot; |
| 45 | SkTSwap(*pivot, *right); |
| 46 | T** newPivot = left; |
| 47 | while (left < right) { |
| 48 | if (**left < *pivotValue) { |
| 49 | SkTSwap(*left, *newPivot); |
| 50 | newPivot += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 51 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 52 | left += 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 | SkTSwap(*newPivot, *right); |
| 55 | return newPivot; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | template <typename T> |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 59 | void QSort(T** left, T** right) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 60 | { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 61 | if (left >= right) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 62 | return; |
| 63 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 64 | T** pivot = left + (right - left >> 1); |
| 65 | pivot = QSort_Partition(left, right, pivot); |
| 66 | QSort(left, pivot - 1); |
| 67 | QSort(pivot + 1, right); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 68 | } |
| 69 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 70 | template <typename S, typename T> |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 71 | static T* QSort_Partition(S& context, T* left, T* right, T* pivot, |
| 72 | bool (*lessThan)(S&, const T, const T)) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 73 | { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 74 | T pivotValue = *pivot; |
| 75 | SkTSwap(*pivot, *right); |
| 76 | T* newPivot = left; |
| 77 | while (left < right) { |
| 78 | if (lessThan(context, *left, pivotValue)) { |
| 79 | SkTSwap(*left, *newPivot); |
| 80 | newPivot += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 81 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 82 | left += 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 | SkTSwap(*newPivot, *right); |
| 85 | return newPivot; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 86 | } |
| 87 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 88 | template <typename S, typename T> |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 89 | void QSort(S& context, T* left, T* right, |
| 90 | bool (*lessThan)(S& , const T, const T)) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 91 | { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 92 | if (left >= right) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 93 | return; |
| 94 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 95 | T* pivot = left + (right - left >> 1); |
| 96 | pivot = QSort_Partition(context, left, right, pivot, lessThan); |
| 97 | QSort(context, left, pivot - 1, lessThan); |
| 98 | QSort(context, pivot + 1, right, lessThan); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 99 | } |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 100 | |
| 101 | #endif |