123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- #include <bfc/platform/types.h>
- #include "sort.h"
- #include <assert.h>
- #pragma optimize("t", on)
- static void shortsort(uint8_t *lo, uint8_t *hi, size_t width, const void *context,
- int (__fastcall *comp)(const void *, const void *, const void *));
- static void swap(uint8_t *p, uint8_t *q, size_t width);
- #define CUTOFF 8
- #define STKSIZ (8*sizeof(void*) - 2)
- void __cdecl nu::qsort (
- void *base,
- size_t num,
- size_t width,
- const void *context,
- int (__fastcall *comp)(const void *, const void *, const void *)
- )
- {
-
- uint8_t *lo, *hi;
- uint8_t *mid;
- uint8_t *loguy, *higuy;
- size_t size;
- uint8_t *lostk[STKSIZ] = {0}, *histk[STKSIZ] = {0};
- int stkptr;
- assert((width % sizeof(void *)) == 0);
- if (num < 2 || width == 0)
- return;
- stkptr = 0;
- lo = static_cast<uint8_t *>(base);
- hi = (uint8_t *)base + width * (num-1);
-
- recurse:
- size = (hi - lo) / width + 1;
-
- if (size <= CUTOFF) {
- shortsort(lo, hi, width, context, comp);
- }
- else {
-
- mid = lo + (size / 2) * width;
-
- if (comp(lo, mid, context) > 0) {
- swap(lo, mid, width);
- }
- if (comp(lo, hi, context) > 0) {
- swap(lo, hi, width);
- }
- if (comp(mid, hi, context) > 0) {
- swap(mid, hi, width);
- }
-
- loguy = lo;
- higuy = hi;
-
- for (;;) {
-
-
- if (mid > loguy) {
- do {
- loguy += width;
- } while (loguy < mid && comp(loguy, mid, context) <= 0);
- }
- if (mid <= loguy) {
- do {
- loguy += width;
- } while (loguy <= hi && comp(loguy, mid, context) <= 0);
- }
-
- do {
- higuy -= width;
- } while (higuy > mid && comp(higuy, mid, context) > 0);
-
- if (higuy < loguy)
- break;
-
- swap(loguy, higuy, width);
-
- if (mid == higuy)
- mid = loguy;
-
- }
-
-
- higuy += width;
- if (mid < higuy) {
- do {
- higuy -= width;
- } while (higuy > mid && comp(higuy, mid, context) == 0);
- }
- if (mid >= higuy) {
- do {
- higuy -= width;
- } while (higuy > lo && comp(higuy, mid, context) == 0);
- }
-
-
- if ( higuy - lo >= hi - loguy ) {
- if (lo < higuy) {
- lostk[stkptr] = lo;
- histk[stkptr] = higuy;
- ++stkptr;
- }
- if (loguy < hi) {
- lo = loguy;
- goto recurse;
- }
- }
- else {
- if (loguy < hi) {
- lostk[stkptr] = loguy;
- histk[stkptr] = hi;
- ++stkptr;
- }
- if (lo < higuy) {
- hi = higuy;
- goto recurse;
- }
- }
- }
-
- --stkptr;
- if (stkptr >= 0) {
- lo = lostk[stkptr];
- hi = histk[stkptr];
- goto recurse;
- }
- else
- return;
- }
- static void __cdecl shortsort (
- uint8_t *lo,
- uint8_t *hi,
- size_t width,
- const void *context,
- int (__fastcall *comp)(const void *, const void *, const void *)
- )
- {
- uint8_t *p;
-
- while (hi > lo) {
-
- uint8_t *max = lo;
- for (p = lo+width; p <= hi; p += width) {
-
- if (comp(p, max, context) > 0) {
- max = p;
- }
-
- }
-
- swap(max, hi, width);
-
- hi -= width;
-
- }
-
- }
- static void swap (
- uint8_t *_a,
- uint8_t *_b,
- size_t width
- )
- {
- #if 1
- void *tmp;
- void **a = (void **)_a;
- void **b = (void **)_b;
- if ( a != b )
-
- do {
- tmp = *a;
- *a++ = *b;
- *b++ = tmp;
- width-=sizeof(void *);
- } while (width);
- #else
-
- memcpy(temp, a, width);
- memcpy(a, b, width);
- memcpy(b, temp, width);
- #endif
- }
|