Added an overload of SkTQSort that sorts an array of values, rather than an array of pointers.

Also added some parentheses to all the QSort variants to get rid of a gcc warning.
Review URL: https://codereview.appspot.com/6492044

git-svn-id: http://skia.googlecode.com/svn/trunk@5311 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
rileya@google.com 2012-08-28 14:40:49 +00:00
parent d0f3f6825b
commit 5ee3f67ce3
2 changed files with 32 additions and 2 deletions

View File

@ -61,7 +61,33 @@ template <typename T> void SkTQSort(T** left, T** right) {
if (left >= right) {
return;
}
T** pivot = left + (right - left >> 1);
T** pivot = left + ((right - left) >> 1);
pivot = SkTQSort_Partition(left, right, pivot);
SkTQSort(left, pivot - 1);
SkTQSort(pivot + 1, right);
}
template <typename T>
static T* SkTQSort_Partition(T* left, T* right, T* pivot) {
T pivotValue = *pivot;
SkTSwap(*pivot, *right);
T* newPivot = left;
while (left < right) {
if (*left < pivotValue) {
SkTSwap(*left, *newPivot);
newPivot += 1;
}
left += 1;
}
SkTSwap(*newPivot, *right);
return newPivot;
}
template <typename T> void SkTQSort(T* left, T* right) {
if (left >= right) {
return;
}
T* pivot = left + ((right - left) >> 1);
pivot = SkTQSort_Partition(left, right, pivot);
SkTQSort(left, pivot - 1);
SkTQSort(pivot + 1, right);
@ -90,7 +116,7 @@ void SkQSort(S& context, T* left, T* right,
if (left >= right) {
return;
}
T* pivot = left + (right - left >> 1);
T* pivot = left + ((right - left) >> 1);
pivot = SkTQSort_Partition(context, left, right, pivot, lessThan);
SkQSort(context, left, pivot - 1, lessThan);
SkQSort(context, pivot + 1, right, lessThan);

View File

@ -69,6 +69,10 @@ static void TestSort(skiatest::Reporter* reporter) {
rand_array(rand, array, count);
SkTHeapSort<int>(array, count);
check_sort(reporter, "Heap", array, count);
rand_array(rand, array, count);
SkTQSort<int>(array, array + count - 1);
check_sort(reporter, "Quick", array, count);
}
if (false) { // avoid bit rot, suppress warning
compare_int(array, array);