add missing reent_data; finish quicksort - untested
This commit is contained in:
parent
23ec64bf14
commit
4c0e79e173
67
thiersort.h
67
thiersort.h
@ -278,7 +278,8 @@ static inline void thiersort8_internal(
|
|||||||
void *reent_data),
|
void *reent_data),
|
||||||
const TSBOOL isint,
|
const TSBOOL isint,
|
||||||
const TSBOOL isunsigned,
|
const TSBOOL isunsigned,
|
||||||
const TSBOOL isfloat);
|
const TSBOOL isfloat,
|
||||||
|
void *reent_data);
|
||||||
|
|
||||||
/** The '<' operation used for int32 */
|
/** The '<' operation used for int32 */
|
||||||
static inline const int ts_lt_int(
|
static inline const int ts_lt_int(
|
||||||
@ -320,7 +321,8 @@ static inline void thiersort8_intkey(
|
|||||||
ts_lt_int,
|
ts_lt_int,
|
||||||
TSTRUE,
|
TSTRUE,
|
||||||
TSFALSE,
|
TSFALSE,
|
||||||
TSFALSE);
|
TSFALSE,
|
||||||
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -339,7 +341,8 @@ static inline void thiersort8_uintkey(
|
|||||||
ts_lt_uint,
|
ts_lt_uint,
|
||||||
TSFALSE,
|
TSFALSE,
|
||||||
TSTRUE,
|
TSTRUE,
|
||||||
TSFALSE);
|
TSFALSE,
|
||||||
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -435,13 +438,48 @@ static inline void ts_quicksort_inplace(
|
|||||||
const union tskey bk,
|
const union tskey bk,
|
||||||
const TSU32 ai,
|
const TSU32 ai,
|
||||||
const TSU32 bi,
|
const TSU32 bi,
|
||||||
void *reent_data)) {
|
void *reent_data),
|
||||||
|
void *reent_data) {
|
||||||
|
|
||||||
TSU32 len = (to - from);
|
TSU32 len = (to - from);
|
||||||
TSU32 mid = from + len / 2;
|
TSU32 mid = from + len / 2;
|
||||||
tskey pivotkey = src[mid].key;
|
tskey pivotkey = arr[mid].key;
|
||||||
TSU32 pivoti = src[mid].i;
|
TSU32 pivoti = arr[mid].i;
|
||||||
// TODO
|
|
||||||
|
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 union tskey bk,
|
||||||
const TSU32 ai,
|
const TSU32 ai,
|
||||||
const TSU32 bi,
|
const TSU32 bi,
|
||||||
void *reent_data)) {
|
void *reent_data),
|
||||||
|
void *reent_data) {
|
||||||
|
|
||||||
TSU32 len = (to - from);
|
TSU32 len = (to - from);
|
||||||
TSU32 mid = from + len / 2;
|
TSU32 mid = from + len / 2;
|
||||||
@ -470,7 +509,7 @@ static inline void ts_quicksort_fromto(
|
|||||||
TSU32 ri = to - 1;
|
TSU32 ri = to - 1;
|
||||||
for(TSU32 i = from; i < to; ++i) {
|
for(TSU32 i = from; i < to; ++i) {
|
||||||
tselem e = src[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;
|
dst[li++] = e;
|
||||||
} else {
|
} else {
|
||||||
dst[ri--] = e;
|
dst[ri--] = e;
|
||||||
@ -478,9 +517,8 @@ static inline void ts_quicksort_fromto(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Just simple recursion for now */
|
/* Just simple recursion for now */
|
||||||
/* If these calls not recurse further, its nonrecursive */
|
ts_quicksort_inplace(dst, from, mid, lt, reent_data);
|
||||||
ts_quicksort_inplace(dst, from, mid, lt);
|
ts_quicksort_inplace(dst, mid, to, lt, reent_data);
|
||||||
ts_quicksort_inplace(dst, mid, to, lt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Internal code reuse (type-branches should be optimized out) */
|
/** Internal code reuse (type-branches should be optimized out) */
|
||||||
@ -497,7 +535,8 @@ static inline void thiersort8_internal(
|
|||||||
void *reent_data),
|
void *reent_data),
|
||||||
const TSBOOL isint,
|
const TSBOOL isint,
|
||||||
const TSBOOL isunsigned,
|
const TSBOOL isunsigned,
|
||||||
const TSBOOL isfloat) {
|
const TSBOOL isfloat,
|
||||||
|
void *reent_data) {
|
||||||
|
|
||||||
/* Preparation */
|
/* Preparation */
|
||||||
uint32_t radics[256] = {0};
|
uint32_t radics[256] = {0};
|
||||||
@ -555,7 +594,7 @@ static inline void thiersort8_internal(
|
|||||||
TSU32 to = real_radics[i + 1];
|
TSU32 to = real_radics[i + 1];
|
||||||
|
|
||||||
/* 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);
|
ts_quicksort_fromto(arr2, arr1, from, to, lt, reent_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Cleanup */
|
/* Cleanup */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user