/* Sifts a broken heap. The input array is a heap from root to bottom *exceptthattherootentrymaybeoutofplace. * *Sinksaholefromarray[root]toleafandthensiftstheoriginalarray[root]element *fromtheleaflevelup. * *Thisversiondoesextrawork,inthatitcopieschildtoparentonthewaydown, *thencopiesparenttochildonthewaybackup.Whencopiesareinexpensive, *thisisanoptimizationasthissiftvariantshouldonlybeusedwhen *thepotentiallyoutofplacerootentryvalueisexpectedtobesmall. * *@paramroottheonebasedindexintoarrayoftheout-of-placerootoftheheap. *@parambottomtheonebasedindexinthearrayofthelastentryintheheap.
*/ template <typename T, typename C> void SkTHeapSort_SiftUp(T array[], size_t root, size_t bottom, const C& lessThan) {
T x = array[root-1];
size_t start = root;
size_t j = root << 1; while (j <= bottom) { if (j < bottom && lessThan(array[j-1], array[j])) {
++j;
}
array[root-1] = array[j-1];
root = j;
j = root << 1;
}
j = root >> 1; while (j >= start) { if (lessThan(array[j-1], x)) {
array[root-1] = array[j-1];
root = j;
j = root >> 1;
} else { break;
}
}
array[root-1] = x;
}
/* Sifts a broken heap. The input array is a heap from root to bottom *exceptthattherootentrymaybeoutofplace. * *Siftsthearray[root]elementfromtherootdown. * *@paramroottheonebasedindexintoarrayoftheout-of-placerootoftheheap. *@parambottomtheonebasedindexinthearrayofthelastentryintheheap.
*/ template <typename T, typename C> void SkTHeapSort_SiftDown(T array[], size_t root, size_t bottom, const C& lessThan) {
T x = array[root-1];
size_t child = root << 1; while (child <= bottom) { if (child < bottom && lessThan(array[child-1], array[child])) {
++child;
} if (lessThan(x, array[child-1])) {
array[root-1] = array[child-1];
root = child;
child = root << 1;
} else { break;
}
}
array[root-1] = x;
}
/** Sorts the array of size count using comparator lessThan using a Heap Sort algorithm. Be sure to *specializeswapifThasanefficientswapoperation. * *@paramarraythearraytobesorted. *@paramcountthenumberofelementsinthearray. *@paramlessThanafunctorwithbooloperator()(Ta,Tb)whichreturnstrueifacomesbeforeb.
*/ template <typename T, typename C> void SkTHeapSort(T array[], size_t count, const C& lessThan) { for (size_t i = count >> 1; i > 0; --i) {
SkTHeapSort_SiftDown(array, i, count, lessThan);
}
for (size_t i = count - 1; i > 0; --i) {
using std::swap;
swap(array[0], array[i]);
SkTHeapSort_SiftUp(array, 1, i, lessThan);
}
}
/** Sorts the array of size count using comparator '<' using a Heap Sort algorithm. */ template <typename T> void SkTHeapSort(T array[], size_t count) {
SkTHeapSort(array, count, [](const T& a, const T& b) { return a < b; });
}
/** Sorts the region from left to right using comparator lessThan using Introsort. *Besuretospecialize`swap`ifThasanefficientswapoperation. * *@parambeginpointstothebeginningoftheregiontobesorted *@paramendpointspasttheendoftheregiontobesorted *@paramlessThanafunctor/lambdawhichreturnstrueifacomesbeforeb.
*/ template <typename T, typename C> void SkTQSort(T* begin, T* end, const C& lessThan) { int n = SkToInt(end - begin); if (n <= 1) { return;
} // Limit Introsort recursion depth to no more than 2 * ceil(log2(n-1)). int depth = 2 * SkNextLog2(n - 1);
SkTIntroSort(depth, begin, n, lessThan);
}
/** Sorts the region from left to right using comparator 'a < b' using Introsort. */ template <typename T> void SkTQSort(T* begin, T* end) {
SkTQSort(begin, end, [](const T& a, const T& b) { return a < b; });
}
/** Sorts the region from left to right using comparator '*a < *b' using Introsort. */ template <typename T> void SkTQSort(T** begin, T** end) {
SkTQSort(begin, end, [](const T* a, const T* b) { return *a < *b; });
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.