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