Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
heapsort_r.c File Reference
#include <stddef.h>
#include <stdlib.h>
Include dependency graph for heapsort_r.c:

Go to the source code of this file.

Macros

#define SWAP(a, b, count, size, tmp)
 
#define COPY(a, b, count, size, tmp1, tmp2)
 
#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)
 

Functions

int heapsort_r (void *vbase, size_t nmemb, size_t size, void *thunk, int *compar)
 

Macro Definition Documentation

◆ COPY

#define COPY (   a,
  b,
  count,
  size,
  tmp1,
  tmp2 
)
Value:
{ \
count = size; \
tmp1 = a; \
tmp2 = b; \
do \
{ \
*tmp1++ = *tmp2++; \
} while(--count); \
}

Definition at line 55 of file heapsort_r.c.

◆ CREATE

#define CREATE (   initval,
  nmemb,
  par_i,
  child_i,
  par,
  child,
  size,
  count,
  tmp 
)
Value:
{ \
for(par_i = initval; (child_i = par_i * 2) <= nmemb; par_i = child_i) \
{ \
child = base + child_i * size; \
if(child_i < nmemb && compar(thunk, child, child + size) < 0) \
{ \
child += size; \
++child_i; \
} \
par = base + par_i * size; \
if(compar(thunk, child, par) <= 0) \
break; \
SWAP(par, child, count, size, tmp); \
} \
}
#define thunk

Definition at line 73 of file heapsort_r.c.

◆ SELECT

#define SELECT (   par_i,
  child_i,
  nmemb,
  par,
  child,
  size,
  k,
  count,
  tmp1,
  tmp2 
)
Value:
{ \
for(par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) \
{ \
child = base + child_i * size; \
if(child_i < nmemb && compar(thunk, child, child + size) < 0) \
{ \
child += size; \
++child_i; \
} \
par = base + par_i * size; \
COPY(par, child, count, size, tmp1, tmp2); \
} \
for(;;) \
{ \
child_i = par_i; \
par_i = child_i / 2; \
child = base + child_i * size; \
par = base + par_i * size; \
if(child_i == 1 || compar(thunk, k, par) < 0) \
{ \
COPY(child, k, count, size, tmp1, tmp2); \
break; \
} \
COPY(child, par, count, size, tmp1, tmp2); \
} \
}
#define thunk

Definition at line 107 of file heapsort_r.c.

◆ SWAP

#define SWAP (   a,
  b,
  count,
  size,
  tmp 
)
Value:
{ \
count = size; \
do \
{ \
tmp = *a; \
*a++ = *b; \
*b++ = tmp; \
} while(--count); \
}

Definition at line 43 of file heapsort_r.c.

Function Documentation

◆ heapsort_r()

int heapsort_r ( void*  vbase,
size_t  nmemb,
size_t  size,
void*  thunk,
int *  compar 
)

Definition at line 142 of file heapsort_r.c.

146 {
147  size_t cnt, i, j, l;
148  char tmp, *tmp1, *tmp2;
149  char *base, *k, *p, *t;
150 
151  if(nmemb <= 1)
152  {
153  {
154  return (0);
155  }
156  }
157 
158  if(!size)
159  {
160  // errno = EINVAL;
161  return (-1);
162  }
163 
164  if((k = malloc(size)) == NULL)
165  {
166  {
167  return (-1);
168  }
169  }
170 
171  /*
172  * Items are numbered from 1 to nmemb, so offset from size bytes
173  * below the starting address.
174  */
175  base = (char*)vbase - size;
176 
177  for(l = nmemb / 2 + 1; --l;)
178  CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
179 
180  /*
181  * For each element of the heap, save the largest element into its
182  * final slot, save the displaced element (k), then recreate the
183  * heap.
184  */
185  while(nmemb > 1)
186  {
187  COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
188  COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
189  --nmemb;
190  SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
191  }
192  free(k);
193  return (0);
194 }
#define COPY(a, b, count, size, tmp1, tmp2)
Definition: heapsort_r.c:55
void free(void *ptr)
Deallocates allocated memory space.
#define NULL
Definition: stddef.h:15
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.
#define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp)
Definition: heapsort_r.c:73
#define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2)
Definition: heapsort_r.c:107

References COPY, CREATE, free(), malloc(), NULL, and SELECT.