43 #define SWAP(a, b, count, size, tmp) \ 55 #define COPY(a, b, count, size, tmp1, tmp2) \ 73 #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) \ 75 for(par_i = initval; (child_i = par_i * 2) <= nmemb; par_i = child_i) \ 77 child = base + child_i * size; \ 78 if(child_i < nmemb && compar(thunk, child, child + size) < 0) \ 83 par = base + par_i * size; \ 84 if(compar(thunk, child, par) <= 0) \ 86 SWAP(par, child, count, size, tmp); \ 107 #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) \ 109 for(par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) \ 111 child = base + child_i * size; \ 112 if(child_i < nmemb && compar(thunk, child, child + size) < 0) \ 117 par = base + par_i * size; \ 118 COPY(par, child, count, size, tmp1, tmp2); \ 123 par_i = child_i / 2; \ 124 child = base + child_i * size; \ 125 par = base + par_i * size; \ 126 if(child_i == 1 || compar(thunk, k, par) < 0) \ 128 COPY(child, k, count, size, tmp1, tmp2); \ 131 COPY(child, par, count, size, tmp1, tmp2); \ 145 int (*compar)(
void*,
const void*,
const void*);
148 char tmp, *tmp1, *tmp2;
149 char *base, *k, *p, *t;
175 base = (
char*)vbase - size;
177 for(l = nmemb / 2 + 1; --l;)
178 CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
187 COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
188 COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
190 SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
#define COPY(a, b, count, size, tmp1, tmp2)
void free(void *ptr)
Deallocates allocated memory space.
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.
int heapsort_r(void *vbase, size_t nmemb, size_t size, void *thunk, int *compar)
#define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp)
#define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2)