diff --git a/thiersort.h b/thiersort.h index d3f7a85..94f1e48 100644 --- a/thiersort.h +++ b/thiersort.h @@ -425,6 +425,64 @@ static inline TSU8 ts_radixi( } } +/** Simple inplace quicksort for tselems */ +static inline void ts_quicksort_inplace( + struct tselem *arr, + TSU32 from, + TSU32 to, + const TSBOOL (*lt)( + const union tskey ak, + const union tskey bk, + const TSU32 ai, + const TSU32 bi, + void *reent_data)) { + + TSU32 len = (to - from); + TSU32 mid = from + len / 2; + tskey pivotkey = src[mid].key; + TSU32 pivoti = src[mid].i; + // TODO +} + +/** + * Simple quicksort implementation that also moves data. + * + * Decidedly not inplace to avoid non inpleace algs arr copy. + */ +static inline void ts_quicksort_fromto( + struct tselem *src, + struct tselem *dst, + TSU32 from, + TSU32 to, + const TSBOOL (*lt)( + const union tskey ak, + const union tskey bk, + const TSU32 ai, + const TSU32 bi, + void *reent_data)) { + + TSU32 len = (to - from); + TSU32 mid = from + len / 2; + tskey pivotkey = src[mid].key; + TSU32 pivoti = src[mid].i; + + TSU32 li = from; + TSU32 ri = to - 1; + for(TSU32 i = from; i < to; ++i) { + tselem e = src[i]; + if(lt(e.key, pivotkey, e.i, pivoti)) { + dst[li++] = e; + } else { + dst[ri--] = e; + } + } + + /* 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); +} + /** Internal code reuse (type-branches should be optimized out) */ static inline void thiersort8_internal( struct tselem *arr, @@ -496,7 +554,8 @@ static inline void thiersort8_internal( /* non-inclusive */ TSU32 to = real_radics[i + 1]; - /* TODO: Quicksort this bucket in O(mlogm), where m << n usually */ + /* Quicksort this bucket in O(mlogm), where m << n usually */ + ts_quicksort_fromto(arr2, arr1, from, to, lt); } /* Cleanup */