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 | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 15 | static T** QSort_Partition(T** left, T** right, T** pivot) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 16 | { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 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; |
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 | left += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 26 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 27 | SkTSwap(*newPivot, *right); |
| 28 | return newPivot; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | template <typename T> |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 32 | void QSort(T** left, T** right) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 33 | { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 34 | if (left >= right) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 35 | return; |
| 36 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 37 | T** pivot = left + (right - left >> 1); |
| 38 | pivot = QSort_Partition(left, right, pivot); |
| 39 | QSort(left, pivot - 1); |
| 40 | QSort(pivot + 1, right); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 41 | } |
| 42 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 43 | template <typename S, typename T> |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 44 | static T* QSort_Partition(S& context, T* left, T* right, T* pivot, |
| 45 | bool (*lessThan)(S&, const T, const T)) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 46 | { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 47 | T pivotValue = *pivot; |
| 48 | SkTSwap(*pivot, *right); |
| 49 | T* newPivot = left; |
| 50 | while (left < right) { |
| 51 | if (lessThan(context, *left, pivotValue)) { |
| 52 | SkTSwap(*left, *newPivot); |
| 53 | newPivot += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 54 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 55 | left += 1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 56 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 57 | SkTSwap(*newPivot, *right); |
| 58 | return newPivot; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 59 | } |
| 60 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 61 | template <typename S, typename T> |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 62 | void QSort(S& context, T* left, T* right, |
| 63 | bool (*lessThan)(S& , const T, const T)) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 64 | { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 65 | if (left >= right) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 66 | return; |
| 67 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 68 | T* pivot = left + (right - left >> 1); |
| 69 | pivot = QSort_Partition(context, left, right, pivot, lessThan); |
| 70 | QSort(context, left, pivot - 1, lessThan); |
| 71 | QSort(context, pivot + 1, right, lessThan); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 72 | } |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 73 | |
| 74 | #endif |