]> git.saurik.com Git - apple/libc.git/blame_incremental - stdlib/heapsort_r-fbsd.c
Libc-583.tar.gz
[apple/libc.git] / stdlib / heapsort_r-fbsd.c
... / ...
CommitLineData
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#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 6/4/93";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: src/lib/libc/stdlib/heapsort.c,v 1.6 2008/01/13 02:11:10 das Exp $");
38
39#include <errno.h>
40#include <stddef.h>
41#include <stdlib.h>
42
43/*
44 * Swap two areas of size number of bytes. Although qsort(3) permits random
45 * blocks of memory to be sorted, sorting pointers is almost certainly the
46 * common case (and, were it not, could easily be made so). Regardless, it
47 * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
48 * arithmetic gets lost in the time required for comparison function calls.
49 */
50#define SWAP(a, b, count, size, tmp) { \
51 count = size; \
52 do { \
53 tmp = *a; \
54 *a++ = *b; \
55 *b++ = tmp; \
56 } while (--count); \
57}
58
59/* Copy one block of size size to another. */
60#define COPY(a, b, count, size, tmp1, tmp2) { \
61 count = size; \
62 tmp1 = a; \
63 tmp2 = b; \
64 do { \
65 *tmp1++ = *tmp2++; \
66 } while (--count); \
67}
68
69/*
70 * Build the list into a heap, where a heap is defined such that for
71 * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
72 *
73 * There two cases. If j == nmemb, select largest of Ki and Kj. If
74 * j < nmemb, select largest of Ki, Kj and Kj+1.
75 */
76#define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
77 for (par_i = initval; (child_i = par_i * 2) <= nmemb; \
78 par_i = child_i) { \
79 child = base + child_i * size; \
80 if (child_i < nmemb && compar(thunk, child, child + size) < 0) { \
81 child += size; \
82 ++child_i; \
83 } \
84 par = base + par_i * size; \
85 if (compar(thunk, child, par) <= 0) \
86 break; \
87 SWAP(par, child, count, size, tmp); \
88 } \
89}
90
91/*
92 * Select the top of the heap and 'heapify'. Since by far the most expensive
93 * action is the call to the compar function, a considerable optimization
94 * in the average case can be achieved due to the fact that k, the displaced
95 * elememt, is ususally quite small, so it would be preferable to first
96 * heapify, always maintaining the invariant that the larger child is copied
97 * over its parent's record.
98 *
99 * Then, starting from the *bottom* of the heap, finding k's correct place,
100 * again maintianing the invariant. As a result of the invariant no element
101 * is 'lost' when k is assigned its correct place in the heap.
102 *
103 * The time savings from this optimization are on the order of 15-20% for the
104 * average case. See Knuth, Vol. 3, page 158, problem 18.
105 *
106 * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset.
107 */
108#define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \
109 for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
110 child = base + child_i * size; \
111 if (child_i < nmemb && compar(thunk, child, child + size) < 0) { \
112 child += size; \
113 ++child_i; \
114 } \
115 par = base + par_i * size; \
116 COPY(par, child, count, size, tmp1, tmp2); \
117 } \
118 for (;;) { \
119 child_i = par_i; \
120 par_i = child_i / 2; \
121 child = base + child_i * size; \
122 par = base + par_i * size; \
123 if (child_i == 1 || compar(thunk, k, par) < 0) { \
124 COPY(child, k, count, size, tmp1, tmp2); \
125 break; \
126 } \
127 COPY(child, par, count, size, tmp1, tmp2); \
128 } \
129}
130
131/*
132 * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average
133 * and worst. While heapsort is faster than the worst case of quicksort,
134 * the BSD quicksort does median selection so that the chance of finding
135 * a data set that will trigger the worst case is nonexistent. Heapsort's
136 * only advantage over quicksort is that it requires little additional memory.
137 */
138__private_extern__ int
139__heapsort_r(vbase, nmemb, size, thunk, compar)
140 void *vbase;
141 size_t nmemb, size;
142 void *thunk;
143 int (*compar)(void *, const void *, const void *);
144{
145 size_t cnt, i, j, l;
146 char tmp, *tmp1, *tmp2;
147 char *base, *k, *p, *t;
148
149 if (nmemb <= 1)
150 return (0);
151
152 if (!size) {
153 errno = EINVAL;
154 return (-1);
155 }
156
157 if ((k = malloc(size)) == NULL)
158 return (-1);
159
160 /*
161 * Items are numbered from 1 to nmemb, so offset from size bytes
162 * below the starting address.
163 */
164 base = (char *)vbase - size;
165
166 for (l = nmemb / 2 + 1; --l;)
167 CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
168
169 /*
170 * For each element of the heap, save the largest element into its
171 * final slot, save the displaced element (k), then recreate the
172 * heap.
173 */
174 while (nmemb > 1) {
175 COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
176 COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
177 --nmemb;
178 SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
179 }
180 free(k);
181 return (0);
182}