25 #define _SORT_R_INLINE inline 27 #if (defined __APPLE__ || defined __MACH__ || defined __DARWIN__ || \ 28 (defined __FreeBSD__ && !defined(qsort_r)) || defined __DragonFly__) 30 #elif (defined __GLIBC__ || (defined (__FreeBSD__) && defined(qsort_r))) 31 # define _SORT_R_LINUX 32 #elif (defined _WIN32 || defined _WIN64 || defined __WINDOWS__ || \ 33 defined __MINGW32__ || defined __MINGW64__) 34 # define _SORT_R_WINDOWS 35 # undef _SORT_R_INLINE 36 # define _SORT_R_INLINE __inline 41 #if (defined NESTED_QSORT && NESTED_QSORT == 0) 45 #define SORT_R_SWAP(a,b,tmp) ((tmp) = (a), (a) = (b), (b) = (tmp)) 53 for(; a < end; a++, b++) {
SORT_R_SWAP(*a, *b, tmp); }
60 char *__restrict b,
size_t w,
61 int (*compar)(
const void *_a,
66 if(compar(a, b, arg) > 0) {
84 if(na > 0 && nb > 0) {
93 int (*compar)(
const void *_a,
98 char *b = (
char *)base, *end = b + nel*w;
106 for(pi = b+w; pi < end; pi += w) {
107 for(pj = pi; pj > b &&
sort_r_cmpswap(pj-w,pj,w,compar,arg); pj -= w) {}
115 char *pl, *ple, *pr, *pre, *pivot;
116 char *last = b+w*(nel-1), *tmp;
129 if(compar(l[0],l[1],arg) > 0) {
SORT_R_SWAP(l[0], l[1], tmp); }
130 if(compar(l[1],l[2],arg) > 0) {
132 if(compar(l[0],l[1],arg) > 0) {
SORT_R_SWAP(l[0], l[1], tmp); }
167 for(; pl < pr; pl += w) {
168 cmp = compar(pl, pivot, arg);
169 if(cmp > 0) {
break; }
176 if(pl >= pr) {
break; }
181 cmp = compar(pr, pivot, arg);
215 #if defined NESTED_QSORT 218 int (*compar)(
const void *_a,
223 int nested_cmp(
const void *a,
const void *b)
225 return compar(a, b, arg);
228 qsort(base, nel, width, nested_cmp);
235 #if defined _SORT_R_BSD 238 extern void qsort_r(
void *base,
size_t nel,
size_t width,
void *thunk,
239 int (*compar)(
void *_thunk,
240 const void *_a,
const void *_b));
244 #if defined _SORT_R_BSD || defined _SORT_R_WINDOWS 251 int (*compar)(
const void *_a,
const void *_b,
void *_arg);
255 const void *a,
const void *b)
257 struct sort_r_data *ss = (
struct sort_r_data*)s;
258 return (ss->compar)(a, b, ss->arg);
263 #if defined _SORT_R_LINUX 265 typedef int(* __compar_d_fn_t)(
const void *,
const void *,
void *);
266 extern void (qsort_r)(
void *base,
size_t nel,
size_t width,
267 __compar_d_fn_t __compar,
void *arg)
268 __attribute__((nonnull (1, 4)));
275 int (*compar)(
const void *_a,
276 const void *_b,
void *_arg),
279 #if defined _SORT_R_LINUX 281 #if defined __GLIBC__ && ((__GLIBC__ < 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 8)) 288 qsort_r(base, nel, width, compar, arg);
292 #elif defined _SORT_R_BSD 294 struct sort_r_data tmp;
297 qsort_r(base, nel, width, &tmp, sort_r_arg_swap);
299 #elif defined _SORT_R_WINDOWS 301 struct sort_r_data tmp;
304 qsort_s(base, nel, width, sort_r_arg_swap, &tmp);
316 #undef _SORT_R_INLINE 317 #undef _SORT_R_WINDOWS
static _SORT_R_INLINE int sort_r_cmpswap(char *__restrict a, char *__restrict b, size_t w, int(*compar)(const void *_a, const void *_b, void *_arg), void *arg)
static _SORT_R_INLINE void sort_r_swap_blocks(char *ptr, size_t na, size_t nb)
static _SORT_R_INLINE void sort_r_swap(char *__restrict a, char *__restrict b, size_t w)
#define SORT_R_SWAP(a, b, tmp)
static _SORT_R_INLINE void sort_r_simple(void *base, size_t nel, size_t w, int(*compar)(const void *_a, const void *_b, void *_arg), void *arg)
static _SORT_R_INLINE void sort_r(void *base, size_t nel, size_t width, int(*compar)(const void *_a, const void *_b, void *_arg), void *arg)