Xen Test Framework
heapsort.c
Go to the documentation of this file.
1 #include <xtf/types.h>
2 
3 static void siftdown(void *base, size_t _size,
4  int (*compar)(const void *, const void *),
5  void (*swap)(void *, void *),
6  long start, long stop)
7 {
8  long size = _size, root, child;
9 
10  for ( root = start; root * 2 + size < stop; root = child )
11  {
12  child = root * 2 + size;
13 
14  if ( (child + size < stop) &&
15  (compar(base + child, base + child + size) < 0) )
16  child += size;
17 
18  if ( compar(base + root, base + child) >= 0 )
19  return;
20 
21  swap(base + root, base + child);
22  }
23 }
24 
25 void heapsort(void *base, size_t nmemb, size_t _size,
26  int (*compar)(const void *, const void *),
27  void (*swap)(void *, void *))
28 {
29  long size = _size, total = size * nmemb, idx;
30 
31  for ( idx = (nmemb/2 - 1) * size; idx >= 0; idx -= size )
32  siftdown(base, _size, compar, swap, idx, total);
33 
34  for ( idx = total - size; idx > 0; idx -= size )
35  {
36  swap(base, base + idx);
37  siftdown(base, _size, compar, swap, 0, idx);
38  }
39 }
40 
41 /*
42  * Local variables:
43  * mode: C
44  * c-file-style: "BSD"
45  * c-basic-offset: 4
46  * tab-width: 4
47  * indent-tabs-mode: nil
48  * End:
49  */
Common declarations for all tests.
uint16_t size
Definition: memory.h:24
void heapsort(void *base, size_t nmemb, size_t _size, int(*compar)(const void *, const void *), void(*swap)(void *, void *))
Definition: heapsort.c:25
static void siftdown(void *base, size_t _size, int(*compar)(const void *, const void *), void(*swap)(void *, void *), long start, long stop)
Definition: heapsort.c:3
unsigned long idx
Definition: memory.h:34