]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
43866e37 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
43866e37 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
43866e37 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /*- | |
26 | * Copyright (c) 1991, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * This code is derived from software contributed to Berkeley by | |
30 | * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | */ | |
60 | ||
61 | #if defined(LIBC_SCCS) && !defined(lint) | |
62 | static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 6/4/93"; | |
63 | #endif /* LIBC_SCCS and not lint */ | |
64 | ||
65 | ||
66 | #include <libsa/stdlib.h> | |
67 | ||
68 | ||
69 | /* | |
70 | * Swap two areas of size number of bytes. Although qsort(3) permits random | |
71 | * blocks of memory to be sorted, sorting pointers is almost certainly the | |
72 | * common case (and, were it not, could easily be made so). Regardless, it | |
73 | * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer | |
74 | * arithmetic gets lost in the time required for comparison function calls. | |
75 | */ | |
76 | #define SWAP(a, b, count, size, tmp) { \ | |
77 | count = size; \ | |
78 | do { \ | |
79 | tmp = *a; \ | |
80 | *a++ = *b; \ | |
81 | *b++ = tmp; \ | |
82 | } while (--count); \ | |
83 | } | |
84 | ||
85 | /* Copy one block of size size to another. */ | |
86 | #define COPY(a, b, count, size, tmp1, tmp2) { \ | |
87 | count = size; \ | |
88 | tmp1 = a; \ | |
89 | tmp2 = b; \ | |
90 | do { \ | |
91 | *tmp1++ = *tmp2++; \ | |
92 | } while (--count); \ | |
93 | } | |
94 | ||
95 | /* | |
96 | * Build the list into a heap, where a heap is defined such that for | |
97 | * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N. | |
98 | * | |
99 | * There two cases. If j == nmemb, select largest of Ki and Kj. If | |
100 | * j < nmemb, select largest of Ki, Kj and Kj+1. | |
101 | */ | |
102 | #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \ | |
103 | for (par_i = initval; (child_i = par_i * 2) <= nmemb; \ | |
104 | par_i = child_i) { \ | |
105 | child = base + child_i * size; \ | |
106 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | |
107 | child += size; \ | |
108 | ++child_i; \ | |
109 | } \ | |
110 | par = base + par_i * size; \ | |
111 | if (compar(child, par) <= 0) \ | |
112 | break; \ | |
113 | SWAP(par, child, count, size, tmp); \ | |
114 | } \ | |
115 | } | |
116 | ||
117 | /* | |
118 | * Select the top of the heap and 'heapify'. Since by far the most expensive | |
119 | * action is the call to the compar function, a considerable optimization | |
120 | * in the average case can be achieved due to the fact that k, the displaced | |
121 | * elememt, is ususally quite small, so it would be preferable to first | |
122 | * heapify, always maintaining the invariant that the larger child is copied | |
123 | * over its parent's record. | |
124 | * | |
125 | * Then, starting from the *bottom* of the heap, finding k's correct place, | |
126 | * again maintianing the invariant. As a result of the invariant no element | |
127 | * is 'lost' when k is assigned its correct place in the heap. | |
128 | * | |
129 | * The time savings from this optimization are on the order of 15-20% for the | |
130 | * average case. See Knuth, Vol. 3, page 158, problem 18. | |
131 | * | |
132 | * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset. | |
133 | */ | |
134 | #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \ | |
135 | for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \ | |
136 | child = base + child_i * size; \ | |
137 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | |
138 | child += size; \ | |
139 | ++child_i; \ | |
140 | } \ | |
141 | par = base + par_i * size; \ | |
142 | COPY(par, child, count, size, tmp1, tmp2); \ | |
143 | } \ | |
144 | for (;;) { \ | |
145 | child_i = par_i; \ | |
146 | par_i = child_i / 2; \ | |
147 | child = base + child_i * size; \ | |
148 | par = base + par_i * size; \ | |
149 | if (child_i == 1 || compar(k, par) < 0) { \ | |
150 | COPY(child, k, count, size, tmp1, tmp2); \ | |
151 | break; \ | |
152 | } \ | |
153 | COPY(child, par, count, size, tmp1, tmp2); \ | |
154 | } \ | |
155 | } | |
156 | ||
157 | /* Pass heapsort off as qsort for krld. -- Nik Gervae | |
158 | * | |
159 | * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average | |
160 | * and worst. While heapsort is faster than the worst case of quicksort, | |
161 | * the BSD quicksort does median selection so that the chance of finding | |
162 | * a data set that will trigger the worst case is nonexistent. Heapsort's | |
163 | * only advantage over quicksort is that it requires little additional memory. | |
164 | */ | |
165 | __private_extern__ | |
166 | void qsort(void * vbase, size_t nmemb, size_t size, | |
167 | int (*compar)(const void *, const void *)) { | |
168 | ||
169 | register int cnt, i, j, l; | |
170 | register char tmp, *tmp1, *tmp2; | |
171 | char *base, *k, *p, *t; | |
172 | ||
173 | if (nmemb <= 1) { | |
174 | return; | |
175 | } | |
176 | ||
177 | if (!size) { | |
178 | return; | |
179 | } | |
180 | ||
181 | if ((k = (char *)malloc(size)) == NULL) { | |
182 | // panic(); | |
183 | return; | |
184 | } | |
185 | ||
186 | /* | |
187 | * Items are numbered from 1 to nmemb, so offset from size bytes | |
188 | * below the starting address. | |
189 | */ | |
190 | base = (char *)vbase - size; | |
191 | ||
192 | for (l = nmemb / 2 + 1; --l;) | |
193 | CREATE(l, nmemb, i, j, t, p, size, cnt, tmp); | |
194 | ||
195 | /* | |
196 | * For each element of the heap, save the largest element into its | |
197 | * final slot, save the displaced element (k), then recreate the | |
198 | * heap. | |
199 | */ | |
200 | while (nmemb > 1) { | |
201 | COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2); | |
202 | COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2); | |
203 | --nmemb; | |
204 | SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2); | |
205 | } | |
206 | free(k); | |
207 | return; | |
208 | } |