Added an overload of SkTQSort that sorts an array of values, rather than an array...
authorrileya@google.com <rileya@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 28 Aug 2012 14:40:49 +0000 (14:40 +0000)
committerrileya@google.com <rileya@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 28 Aug 2012 14:40:49 +0000 (14:40 +0000)
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

src/core/SkTSort.h
tests/SortTest.cpp

index 86cc23a..db961d0 100644 (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);
index d1a61d4..65c4863 100644 (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);