35 typedef int cmp_t(
void*,
const void*,
const void*);
37 typedef int cmp_t(
const void*,
const void*);
39 static inline char*
med3(
char* a,
char* b,
char* c,
cmp_t* cmd,
void*
thunk)
41 static inline void swapfunc(
char* a,
char* b,
size_t n,
size_t swaptype)
44 #define min(a, b) (a) < (b) ? a : b 49 #define swapcode(TYPE, parmi, parmj, n) \ 51 size_t i = (n) / sizeof(TYPE); \ 52 TYPE* pi = (TYPE*)(parmi); \ 53 TYPE* pj = (TYPE*)(parmj); \ 62 #define SWAPINIT(a, es) \ 63 swaptype = (uintptr_t)a % sizeof(long) || es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1; 65 static inline void swapfunc(
char* a,
char* b,
size_t n,
size_t swaptype)
69 long t = *(
long*)(
void*)(a);
70 *(
long*)(
void*)(a) = *(
long*)(
void*)(b);
71 *(
long*)(
void*)(b) = t;
73 else if(swaptype == 1)
75 swapcode(
long, (
void*)a, (
void*)b, n)
83 #define swap(a, b) swapfunc(a, b, (size_t)es, (size_t)swaptype) 88 #define vecswap(a, b, n) \ 90 swapfunc(a, b, n, swaptype ? (size_t)swaptype : 1) 93 #define CMP(t, x, y) (cmp((t), (x), (y))) 95 #define CMP(t, x, y) (cmp((x), (y))) 110 #define DEPTH(x) (2 * (flsl((long)(x)) - 1)) 112 #define DEPTH(x) (2 * (fls((int)(x)) - 1)) 115 static void _qsort(
void* a,
size_t n,
size_t es,
123 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
124 size_t d, r, swap_cnt;
129 if(depth_limit-- <= 0)
142 for(pm = (
char*)a + es; pm < (
char*)a + n * es; pm += es)
144 for(pl = pm; pl > (
char*)a &&
CMP(
thunk, pl - es, pl) > 0; pl -= es)
151 pm = (
char*)a + (n / 2) * es;
155 pn = (
char*)a + (n - 1) * es;
166 pa = pb = (
char*)a + es;
168 pc = pd = (
char*)a + (n - 1) * es;
171 while(pb <= pc && (cmp_result =
CMP(
thunk, pb, a)) <= 0)
181 while(pb <= pc && (cmp_result =
CMP(
thunk, pc, a)) >= 0)
201 pn = (
char*)a + n * es;
202 r =
min((uintptr_t)pa - (uintptr_t)a, (uintptr_t)pb - (uintptr_t)pa);
204 r =
min((uintptr_t)pd - (uintptr_t)pc, (uintptr_t)pn - (uintptr_t)pd - (uintptr_t)es);
210 for(pm = (
char*)a + es; pm < (
char*)a + n * es; pm += es)
212 for(pl = pm; pl > (
char*)a &&
CMP(
thunk, pl - es, pl) > 0; pl -= es)
225 if((r = (uintptr_t)pb - (uintptr_t)pa) > es)
233 if((r = (uintptr_t)pd - (uintptr_t)pc) > es)
__attribute__((noreturn, weak)) void __assert_fail(const char *expr
void qsort_r(void *a, size_t n, size_t es, void *thunk, int(*cmp)(void *, const void *, const void *))
Sorts the given array pointed to by ptr in ascending order.
void qsort(void *a, size_t n, size_t es, cmp_t *cmp)
int cmp_t(const void *, const void *)
static void swapfunc(char *a, char *b, size_t n, size_t swaptype) __attribute__((always_inline))
#define swapcode(TYPE, parmi, parmj, n)
static char * med3(char *a, char *b, char *c, cmp_t *cmd, void *thunk) __attribute__((always_inline))
int cmp(Bigint *a, Bigint *b)
int heapsort(void *vbase, size_t nmemb, size_t size, int(*compar)(const void *, const void *))
Sorts the given array pointed to by vbase in ascending order.
static void _qsort(void *a, size_t n, size_t es, #define thunk cmp_t *cmp, int depth_limit)
int heapsort_r(void *vbase, size_t nmemb, size_t size, void *thunk, int(*compar)(void *, const void *, const void *))
Sorts the given array pointed to by vbase in ascending order.