From: rileya@google.com Date: Tue, 28 Aug 2012 14:40:49 +0000 (+0000) Subject: Added an overload of SkTQSort that sorts an array of values, rather than an array... X-Git-Tag: accepted/tizen/5.0/unified/20181102.025319~15012 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5ee3f67ce35f19f6e5ef44b67db62e964f77d69d;p=platform%2Fupstream%2FlibSkiaSharp.git 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 --- diff --git a/src/core/SkTSort.h b/src/core/SkTSort.h index 86cc23a..db961d0 100644 --- a/src/core/SkTSort.h +++ b/src/core/SkTSort.h @@ -61,7 +61,33 @@ template 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 +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 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); diff --git a/tests/SortTest.cpp b/tests/SortTest.cpp index d1a61d4..65c4863 100644 --- a/tests/SortTest.cpp +++ b/tests/SortTest.cpp @@ -69,6 +69,10 @@ static void TestSort(skiatest::Reporter* reporter) { rand_array(rand, array, count); SkTHeapSort(array, count); check_sort(reporter, "Heap", array, count); + + rand_array(rand, array, count); + SkTQSort(array, array + count - 1); + check_sort(reporter, "Quick", array, count); } if (false) { // avoid bit rot, suppress warning compare_int(array, array);