blob: e4c4e95c385531048ae8599c2b459783e2c6824a [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "SkTypes.h"
2
3// FIXME: Move this templated version into SKTSearch.h
4
5template <typename T>
caryclark@google.comcd4421d2012-03-01 19:16:31 +00006static T** QSort_Partition(T** left, T** right, T** pivot)
caryclark@google.comc6825902012-02-03 22:07:47 +00007{
caryclark@google.comcd4421d2012-03-01 19:16:31 +00008 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.comc6825902012-02-03 22:07:47 +000015 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +000016 left += 1;
caryclark@google.comc6825902012-02-03 22:07:47 +000017 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +000018 SkTSwap(*newPivot, *right);
19 return newPivot;
caryclark@google.comc6825902012-02-03 22:07:47 +000020}
21
22template <typename T>
caryclark@google.comcd4421d2012-03-01 19:16:31 +000023void QSort(T** left, T** right)
caryclark@google.comc6825902012-02-03 22:07:47 +000024{
caryclark@google.comcd4421d2012-03-01 19:16:31 +000025 if (left >= right) {
caryclark@google.comc6825902012-02-03 22:07:47 +000026 return;
27 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +000028 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.comc6825902012-02-03 22:07:47 +000032}
33
caryclark@google.comf8b000d2012-02-09 22:04:27 +000034template <typename S, typename T>
caryclark@google.comcef7e3f2012-02-28 16:57:05 +000035static T* QSort_Partition(S& context, T* left, T* right, T* pivot,
36 bool (*lessThan)(S&, const T, const T))
caryclark@google.comc6825902012-02-03 22:07:47 +000037{
caryclark@google.comcef7e3f2012-02-28 16:57:05 +000038 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.comc6825902012-02-03 22:07:47 +000045 }
caryclark@google.comcef7e3f2012-02-28 16:57:05 +000046 left += 1;
caryclark@google.comc6825902012-02-03 22:07:47 +000047 }
caryclark@google.comcef7e3f2012-02-28 16:57:05 +000048 SkTSwap(*newPivot, *right);
49 return newPivot;
caryclark@google.comc6825902012-02-03 22:07:47 +000050}
51
caryclark@google.comf8b000d2012-02-09 22:04:27 +000052template <typename S, typename T>
caryclark@google.comcef7e3f2012-02-28 16:57:05 +000053void QSort(S& context, T* left, T* right,
54 bool (*lessThan)(S& , const T, const T))
caryclark@google.comc6825902012-02-03 22:07:47 +000055{
caryclark@google.comcef7e3f2012-02-28 16:57:05 +000056 if (left >= right) {
caryclark@google.comc6825902012-02-03 22:07:47 +000057 return;
58 }
caryclark@google.comcef7e3f2012-02-28 16:57:05 +000059 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.comc6825902012-02-03 22:07:47 +000063}