Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
heapsort_r.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 1991, 1993
3  * The Regents of the University of California. All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <stddef.h>
34 #include <stdlib.h>
35 
36 /*
37  * Swap two areas of size number of bytes. Although qsort(3) permits random
38  * blocks of memory to be sorted, sorting pointers is almost certainly the
39  * common case (and, were it not, could easily be made so). Regardless, it
40  * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
41  * arithmetic gets lost in the time required for comparison function calls.
42  */
43 #define SWAP(a, b, count, size, tmp) \
44  { \
45  count = size; \
46  do \
47  { \
48  tmp = *a; \
49  *a++ = *b; \
50  *b++ = tmp; \
51  } while(--count); \
52  }
53 
54 /* Copy one block of size size to another. */
55 #define COPY(a, b, count, size, tmp1, tmp2) \
56  { \
57  count = size; \
58  tmp1 = a; \
59  tmp2 = b; \
60  do \
61  { \
62  *tmp1++ = *tmp2++; \
63  } while(--count); \
64  }
65 
66 /*
67  * Build the list into a heap, where a heap is defined such that for
68  * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
69  *
70  * There two cases. If j == nmemb, select largest of Ki and Kj. If
71  * j < nmemb, select largest of Ki, Kj and Kj+1.
72  */
73 #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) \
74  { \
75  for(par_i = initval; (child_i = par_i * 2) <= nmemb; par_i = child_i) \
76  { \
77  child = base + child_i * size; \
78  if(child_i < nmemb && compar(thunk, child, child + size) < 0) \
79  { \
80  child += size; \
81  ++child_i; \
82  } \
83  par = base + par_i * size; \
84  if(compar(thunk, child, par) <= 0) \
85  break; \
86  SWAP(par, child, count, size, tmp); \
87  } \
88  }
89 
90 /*
91  * Select the top of the heap and 'heapify'. Since by far the most expensive
92  * action is the call to the compar function, a considerable optimization
93  * in the average case can be achieved due to the fact that k, the displaced
94  * elememt, is ususally quite small, so it would be preferable to first
95  * heapify, always maintaining the invariant that the larger child is copied
96  * over its parent's record.
97  *
98  * Then, starting from the *bottom* of the heap, finding k's correct place,
99  * again maintianing the invariant. As a result of the invariant no element
100  * is 'lost' when k is assigned its correct place in the heap.
101  *
102  * The time savings from this optimization are on the order of 15-20% for the
103  * average case. See Knuth, Vol. 3, page 158, problem 18.
104  *
105  * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset.
106  */
107 #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) \
108  { \
109  for(par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) \
110  { \
111  child = base + child_i * size; \
112  if(child_i < nmemb && compar(thunk, child, child + size) < 0) \
113  { \
114  child += size; \
115  ++child_i; \
116  } \
117  par = base + par_i * size; \
118  COPY(par, child, count, size, tmp1, tmp2); \
119  } \
120  for(;;) \
121  { \
122  child_i = par_i; \
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) \
127  { \
128  COPY(child, k, count, size, tmp1, tmp2); \
129  break; \
130  } \
131  COPY(child, par, count, size, tmp1, tmp2); \
132  } \
133  }
134 
135 /*
136  * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average
137  * and worst. While heapsort is faster than the worst case of quicksort,
138  * the BSD quicksort does median selection so that the chance of finding
139  * a data set that will trigger the worst case is nonexistent. Heapsort's
140  * only advantage over quicksort is that it requires little additional memory.
141  */
142 int heapsort_r(vbase, nmemb, size, thunk, compar) void* vbase;
143 size_t nmemb, size;
144 void* thunk;
145 int (*compar)(void*, const void*, const void*);
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.
int heapsort_r(void *vbase, size_t nmemb, size_t size, void *thunk, int *compar)
Definition: heapsort_r.c:142
#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
#define thunk