heapsort.c revision 1.2 1 1.2 jnemeth /* $NetBSD: heapsort.c,v 1.2 2008/11/17 09:56:48 jnemeth Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 1991, 1993
5 1.1 ad * The Regents of the University of California. All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to Berkeley by
8 1.1 ad * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad * 3. Neither the name of the University nor the names of its contributors
19 1.1 ad * may be used to endorse or promote products derived from this software
20 1.1 ad * without specific prior written permission.
21 1.1 ad *
22 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 ad * SUCH DAMAGE.
33 1.1 ad */
34 1.1 ad
35 1.1 ad #if HAVE_NBTOOL_CONFIG_H
36 1.1 ad #include "nbtool_config.h"
37 1.1 ad /*
38 1.1 ad * XXX Undefine the renames of these functions so that we don't
39 1.1 ad * XXX rename the versions found in the host's headers by mistake!
40 1.1 ad */
41 1.1 ad #undef heapsort
42 1.1 ad #endif
43 1.1 ad
44 1.1 ad #include <sys/cdefs.h>
45 1.1 ad #if defined(LIBC_SCCS) && !defined(lint)
46 1.1 ad #if 0
47 1.1 ad static char sccsid[] = "from: @(#)heapsort.c 8.1 (Berkeley) 6/4/93";
48 1.1 ad #else
49 1.2 jnemeth __RCSID("$NetBSD: heapsort.c,v 1.2 2008/11/17 09:56:48 jnemeth Exp $");
50 1.1 ad #endif
51 1.1 ad #endif /* LIBC_SCCS and not lint */
52 1.1 ad
53 1.2 jnemeth #if defined(_KERNEL) || defined(_STANDALONE)
54 1.1 ad #include <sys/types.h>
55 1.1 ad
56 1.1 ad #include <lib/libkern/libkern.h>
57 1.2 jnemeth #else /* _KERNEL || _STANDALONE */
58 1.1 ad #include "namespace.h"
59 1.1 ad #include <sys/types.h>
60 1.1 ad
61 1.1 ad #include <assert.h>
62 1.1 ad #include <errno.h>
63 1.1 ad #include <stdlib.h>
64 1.1 ad
65 1.1 ad #if HAVE_NBTOOL_CONFIG_H
66 1.1 ad /* XXX Now, re-apply the renaming that we undid above. */
67 1.1 ad #define heapsort __nbcompat_heapsort
68 1.1 ad #endif
69 1.1 ad
70 1.1 ad #ifdef __weak_alias
71 1.1 ad __weak_alias(heapsort,_heapsort)
72 1.1 ad #endif
73 1.2 jnemeth #endif /* _KERNEL || _STANDALONE */
74 1.1 ad
75 1.1 ad /*
76 1.1 ad * Swap two areas of size number of bytes. Although qsort(3) permits random
77 1.1 ad * blocks of memory to be sorted, sorting pointers is almost certainly the
78 1.1 ad * common case (and, were it not, could easily be made so). Regardless, it
79 1.1 ad * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
80 1.1 ad * arithmetic gets lost in the time required for comparison function calls.
81 1.1 ad */
82 1.1 ad #define SWAP(a, b, count, size, tmp) { \
83 1.1 ad count = size; \
84 1.1 ad do { \
85 1.1 ad tmp = *a; \
86 1.1 ad *a++ = *b; \
87 1.1 ad *b++ = tmp; \
88 1.1 ad } while (--count); \
89 1.1 ad }
90 1.1 ad
91 1.1 ad /* Copy one block of size size to another. */
92 1.1 ad #define COPY(a, b, count, size, tmp1, tmp2) { \
93 1.1 ad count = size; \
94 1.1 ad tmp1 = a; \
95 1.1 ad tmp2 = b; \
96 1.1 ad do { \
97 1.1 ad *tmp1++ = *tmp2++; \
98 1.1 ad } while (--count); \
99 1.1 ad }
100 1.1 ad
101 1.1 ad /*
102 1.1 ad * Build the list into a heap, where a heap is defined such that for
103 1.1 ad * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
104 1.1 ad *
105 1.1 ad * There are two cases. If j == nmemb, select largest of Ki and Kj. If
106 1.1 ad * j < nmemb, select largest of Ki, Kj and Kj+1.
107 1.1 ad */
108 1.1 ad #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
109 1.1 ad for (par_i = initval; (child_i = par_i * 2) <= nmemb; \
110 1.1 ad par_i = child_i) { \
111 1.1 ad child = base + child_i * size; \
112 1.1 ad if (child_i < nmemb && compar(child, child + size) < 0) { \
113 1.1 ad child += size; \
114 1.1 ad ++child_i; \
115 1.1 ad } \
116 1.1 ad par = base + par_i * size; \
117 1.1 ad if (compar(child, par) <= 0) \
118 1.1 ad break; \
119 1.1 ad SWAP(par, child, count, size, tmp); \
120 1.1 ad } \
121 1.1 ad }
122 1.1 ad
123 1.1 ad /*
124 1.1 ad * Select the top of the heap and 'heapify'. Since by far the most expensive
125 1.1 ad * action is the call to the compar function, a considerable optimization
126 1.1 ad * in the average case can be achieved due to the fact that k, the displaced
127 1.1 ad * element, is usually quite small, so it would be preferable to first
128 1.1 ad * heapify, always maintaining the invariant that the larger child is copied
129 1.1 ad * over its parent's record.
130 1.1 ad *
131 1.1 ad * Then, starting from the *bottom* of the heap, finding k's correct place,
132 1.1 ad * again maintaining the invariant. As a result of the invariant no element
133 1.1 ad * is 'lost' when k is assigned its correct place in the heap.
134 1.1 ad *
135 1.1 ad * The time savings from this optimization are on the order of 15-20% for the
136 1.1 ad * average case. See Knuth, Vol. 3, page 158, problem 18.
137 1.1 ad *
138 1.1 ad * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset.
139 1.1 ad */
140 1.1 ad #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \
141 1.1 ad for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
142 1.1 ad child = base + child_i * size; \
143 1.1 ad if (child_i < nmemb && compar(child, child + size) < 0) { \
144 1.1 ad child += size; \
145 1.1 ad ++child_i; \
146 1.1 ad } \
147 1.1 ad par = base + par_i * size; \
148 1.1 ad COPY(par, child, count, size, tmp1, tmp2); \
149 1.1 ad } \
150 1.1 ad for (;;) { \
151 1.1 ad child_i = par_i; \
152 1.1 ad par_i = child_i / 2; \
153 1.1 ad child = base + child_i * size; \
154 1.1 ad par = base + par_i * size; \
155 1.1 ad if (child_i == 1 || compar(k, par) < 0) { \
156 1.1 ad COPY(child, k, count, size, tmp1, tmp2); \
157 1.1 ad break; \
158 1.1 ad } \
159 1.1 ad COPY(child, par, count, size, tmp1, tmp2); \
160 1.1 ad } \
161 1.1 ad }
162 1.1 ad
163 1.1 ad /*
164 1.1 ad * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average
165 1.1 ad * and worst. While heapsort is faster than the worst case of quicksort,
166 1.1 ad * the BSD quicksort does median selection so that the chance of finding
167 1.1 ad * a data set that will trigger the worst case is nonexistent. Heapsort's
168 1.1 ad * only advantage over quicksort is that it requires little additional memory.
169 1.1 ad */
170 1.2 jnemeth #if defined(_KERNEL) || defined(_STANDALONE)
171 1.1 ad int
172 1.1 ad kheapsort(void *vbase, size_t nmemb, size_t size,
173 1.1 ad int (*compar)(const void *, const void *), void *k)
174 1.1 ad #else
175 1.1 ad int
176 1.1 ad heapsort(void *vbase, size_t nmemb, size_t size,
177 1.1 ad int (*compar)(const void *, const void *))
178 1.1 ad #endif
179 1.1 ad {
180 1.1 ad size_t cnt, i, j, l;
181 1.1 ad char tmp, *tmp1, *tmp2;
182 1.1 ad char *base, *p, *t;
183 1.2 jnemeth #if defined(_KERNEL) || defined(_STANDALONE)
184 1.1 ad char *k;
185 1.1 ad #endif
186 1.1 ad
187 1.1 ad _DIAGASSERT(vbase != NULL);
188 1.1 ad _DIAGASSERT(compar != NULL);
189 1.1 ad
190 1.1 ad if (nmemb <= 1)
191 1.1 ad return (0);
192 1.1 ad
193 1.1 ad if (!size) {
194 1.2 jnemeth #if defined(_KERNEL) || defined(_STANDALONE)
195 1.1 ad errno = EINVAL;
196 1.1 ad #endif
197 1.1 ad return (-1);
198 1.1 ad }
199 1.1 ad
200 1.2 jnemeth #if defined(_KERNEL) || defined(_STANDALONE)
201 1.1 ad if ((k = malloc(size)) == NULL)
202 1.1 ad return (-1);
203 1.1 ad #endif
204 1.1 ad
205 1.1 ad /*
206 1.1 ad * Items are numbered from 1 to nmemb, so offset from size bytes
207 1.1 ad * below the starting address.
208 1.1 ad */
209 1.1 ad base = (char *)vbase - size;
210 1.1 ad
211 1.1 ad for (l = nmemb / 2 + 1; --l;)
212 1.1 ad CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
213 1.1 ad
214 1.1 ad /*
215 1.1 ad * For each element of the heap, save the largest element into its
216 1.1 ad * final slot, save the displaced element (k), then recreate the
217 1.1 ad * heap.
218 1.1 ad */
219 1.1 ad while (nmemb > 1) {
220 1.1 ad COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
221 1.1 ad COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
222 1.1 ad --nmemb;
223 1.1 ad SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
224 1.1 ad }
225 1.2 jnemeth #if defined(_KERNEL) || defined(_STANDALONE)
226 1.1 ad free(k);
227 1.1 ad #endif
228 1.1 ad return (0);
229 1.1 ad }
230