diff --git a/thiersort.h b/thiersort.h index 94f1e48..6702a2d 100644 --- a/thiersort.h +++ b/thiersort.h @@ -278,7 +278,8 @@ static inline void thiersort8_internal( void *reent_data), const TSBOOL isint, const TSBOOL isunsigned, - const TSBOOL isfloat); + const TSBOOL isfloat, + void *reent_data); /** The '<' operation used for int32 */ static inline const int ts_lt_int( @@ -320,7 +321,8 @@ static inline void thiersort8_intkey( ts_lt_int, TSTRUE, TSFALSE, - TSFALSE); + TSFALSE, + NULL); } /** @@ -339,7 +341,8 @@ static inline void thiersort8_uintkey( ts_lt_uint, TSFALSE, TSTRUE, - TSFALSE); + TSFALSE, + NULL); } /** @@ -435,13 +438,48 @@ static inline void ts_quicksort_inplace( const union tskey bk, const TSU32 ai, const TSU32 bi, - void *reent_data)) { + void *reent_data), + void *reent_data) { TSU32 len = (to - from); TSU32 mid = from + len / 2; - tskey pivotkey = src[mid].key; - TSU32 pivoti = src[mid].i; - // TODO + tskey pivotkey = arr[mid].key; + TSU32 pivoti = arr[mid].i; + + TSU32 left = from; + TSU32 right = to - 1; + while(left < right) { + // Step over rightly positioned elems from left + tskey leftkey = arr[left].key; + TSU32 lefti = arr[left].i; + while((left < right) && lt(leftkey, pivotkey, lefti, pivoti, reent_data)) { + ++left; + leftkey = arr[left].key; + lefti = arr[left].i; + } + + // Step over rightly positioned elems from right + tskey rightkey = arr[right].key; + TSU32 righti = arr[right].i; + while((left < right) && lt(pivotkey, rightkey, pivoti, righti, reent_data)) { + --right; + rightkey = arr[right].key; + righti = arr[right].i; + } + + // Swap wrongly positioned element + if(left < right) { + tselem tmp = arr[left]; + arr[left] = arr[right]; + arr[right] = tmp; + ++left; + --right; + } + } + + /* Just simple recursion for now */ + ts_quicksort_inplace(arr, from, mid, lt, reent_data); + ts_quicksort_inplace(arr, mid, to, lt, reent_data); } /** @@ -459,7 +497,8 @@ static inline void ts_quicksort_fromto( const union tskey bk, const TSU32 ai, const TSU32 bi, - void *reent_data)) { + void *reent_data), + void *reent_data) { TSU32 len = (to - from); TSU32 mid = from + len / 2; @@ -470,7 +509,7 @@ static inline void ts_quicksort_fromto( TSU32 ri = to - 1; for(TSU32 i = from; i < to; ++i) { tselem e = src[i]; - if(lt(e.key, pivotkey, e.i, pivoti)) { + if(lt(e.key, pivotkey, e.i, pivoti, reent_data)) { dst[li++] = e; } else { dst[ri--] = e; @@ -478,9 +517,8 @@ static inline void ts_quicksort_fromto( } /* Just simple recursion for now */ - /* If these calls not recurse further, its nonrecursive */ - ts_quicksort_inplace(dst, from, mid, lt); - ts_quicksort_inplace(dst, mid, to, lt); + ts_quicksort_inplace(dst, from, mid, lt, reent_data); + ts_quicksort_inplace(dst, mid, to, lt, reent_data); } /** Internal code reuse (type-branches should be optimized out) */ @@ -497,7 +535,8 @@ static inline void thiersort8_internal( void *reent_data), const TSBOOL isint, const TSBOOL isunsigned, - const TSBOOL isfloat) { + const TSBOOL isfloat, + void *reent_data) { /* Preparation */ uint32_t radics[256] = {0}; @@ -555,7 +594,7 @@ static inline void thiersort8_internal( TSU32 to = real_radics[i + 1]; /* Quicksort this bucket in O(mlogm), where m << n usually */ - ts_quicksort_fromto(arr2, arr1, from, to, lt); + ts_quicksort_fromto(arr2, arr1, from, to, lt, reent_data); } /* Cleanup */