Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
heapsort.c File Reference
#include <stddef.h>
#include <stdlib.h>
Include dependency graph for heapsort.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 (void *vbase, size_t nmemb, size_t size, 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.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(child, child + size) < 0) \
{ \
child += size; \
++child_i; \
} \
par = base + par_i * size; \
if(compar(child, par) <= 0) \
break; \
SWAP(par, child, count, size, tmp); \
} \
}

Definition at line 73 of file heapsort.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(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(k, par) < 0) \
{ \
COPY(child, k, count, size, tmp1, tmp2); \
break; \
} \
COPY(child, par, count, size, tmp1, tmp2); \
} \
}

Definition at line 107 of file heapsort.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.c.

Function Documentation

◆ heapsort()

int heapsort ( void*  vbase,
size_t  nmemb,
size_t  size,
int *  compar 
)

Definition at line 142 of file heapsort.c.

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

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