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