rpm
4.5
|
00001 /*@-bounds -mustmod -sizeoftype @*/ 00002 #ifndef __APPLE__ 00003 /*- 00004 * Copyright (c) 1992, 1993 00005 * The Regents of the University of California. All rights reserved. 00006 * 00007 * This code is derived from software contributed to Berkeley by 00008 * Peter McIlroy. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions 00012 * are met: 00013 * 1. Redistributions of source code must retain the above copyright 00014 * notice, this list of conditions and the following disclaimer. 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in the 00017 * documentation and/or other materials provided with the distribution. 00018 * 3. All advertising materials mentioning features or use of this software 00019 * must display the following acknowledgement: 00020 * This product includes software developed by the University of 00021 * California, Berkeley and its contributors. 00022 * 4. Neither the name of the University nor the names of its contributors 00023 * may be used to endorse or promote products derived from this software 00024 * without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00027 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00030 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00031 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00032 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00033 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00034 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00035 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00036 * SUCH DAMAGE. 00037 */ 00038 00039 #if defined(LIBC_SCCS) && !defined(lint) 00040 static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94"; 00041 #endif /* LIBC_SCCS and not lint */ 00042 00043 /* 00044 * Hybrid exponential search/linear search merge sort with hybrid 00045 * natural/pairwise first pass. Requires about .3% more comparisons 00046 * for random data than LSMS with pairwise first pass alone. 00047 * It works for objects as small as two bytes. 00048 */ 00049 00050 #define NATURAL 00051 #define THRESHOLD 16 /* Best choice for natural merge cut-off. */ 00052 00053 /* #define NATURAL to get hybrid natural merge. 00054 * (The default is pairwise merging.) 00055 */ 00056 00057 #include "system.h" 00058 00059 #define ISIZE sizeof(int) 00060 #define PSIZE sizeof(unsigned char *) 00061 #define ICOPY_LIST(src, dst, last) \ 00062 do \ 00063 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ 00064 while(src < last) 00065 #define ICOPY_ELT(src, dst, i) \ 00066 do \ 00067 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ 00068 while (i -= ISIZE) 00069 00070 #define CCOPY_LIST(src, dst, last) \ 00071 do \ 00072 *dst++ = *src++; \ 00073 while (src < last) 00074 #define CCOPY_ELT(src, dst, i) \ 00075 do \ 00076 *dst++ = *src++; \ 00077 while (i -= 1) 00078 00079 /* 00080 * Find the next possible pointer head. (Trickery for forcing an array 00081 * to do double duty as a linked list when objects do not align with word 00082 * boundaries. 00083 */ 00084 /* Assumption: PSIZE is a power of 2. */ 00085 #define EVAL(p) (unsigned char **) \ 00086 ((unsigned char *)0 + \ 00087 (((unsigned char *)p + PSIZE - 1 - (unsigned char *) 0) & ~(PSIZE - 1))) 00088 00089 #define swap(a, b) { \ 00090 s = b; \ 00091 i = size; \ 00092 do { \ 00093 tmp = *a; *a++ = *s; *s++ = tmp; \ 00094 } while (--i); \ 00095 a -= size; \ 00096 } 00097 #define reverse(bot, top) { \ 00098 s = top; \ 00099 do { \ 00100 i = size; \ 00101 do { \ 00102 tmp = *bot; *bot++ = *s; *s++ = tmp; \ 00103 } while (--i); \ 00104 s -= size2; \ 00105 } while(bot < s); \ 00106 } 00107 00108 /* 00109 * This is to avoid out-of-bounds addresses in sorting the 00110 * last 4 elements. 00111 */ 00112 static void 00113 insertionsort(unsigned char *a, size_t n, size_t size, 00114 int (*cmp) (const void *, const void *)) 00115 /*@modifies *a @*/ 00116 { 00117 u_char *ai, *s, *t, *u, tmp; 00118 int i; 00119 00120 for (ai = a+size; --n >= 1; ai += size) 00121 for (t = ai; t > a; t -= size) { 00122 u = t - size; 00123 if (cmp(u, t) <= 0) 00124 /*@innerbreak@*/ break; 00125 swap(u, t); 00126 } 00127 } 00128 00129 /* 00130 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of 00131 * increasing order, list2 in a corresponding linked list. Checks for runs 00132 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL 00133 * is defined. Otherwise simple pairwise merging is used.) 00134 */ 00135 static void 00136 setup(unsigned char *list1, /*@out@*/ unsigned char *list2, 00137 size_t n, size_t size, int (*cmp) (const void *, const void *)) 00138 /*@modifies list1, list2 @*/ 00139 { 00140 int i, length, size2, tmp, sense; 00141 unsigned char *f1, *f2, *s, *l2, *last, *p2; 00142 00143 size2 = size*2; 00144 if (n <= 5) { 00145 insertionsort(list1, n, size, cmp); 00146 *EVAL(list2) = (unsigned char*) list2 + n*size; 00147 return; 00148 } 00149 /* 00150 * Avoid running pointers out of bounds; limit n to evens 00151 * for simplicity. 00152 */ 00153 i = 4 + (n & 1); 00154 insertionsort(list1 + (n - i) * size, i, size, cmp); 00155 last = list1 + size * (n - i); 00156 *EVAL(list2 + (last - list1)) = list2 + n * size; 00157 00158 #ifdef NATURAL 00159 p2 = list2; 00160 f1 = list1; 00161 sense = (cmp(f1, f1 + size) > 0); 00162 for (; f1 < last; sense = !sense) { 00163 length = 2; 00164 /* Find pairs with same sense. */ 00165 for (f2 = f1 + size2; f2 < last; f2 += size2) { 00166 if ((cmp(f2, f2+ size) > 0) != sense) 00167 /*@innerbreak@*/ break; 00168 length += 2; 00169 } 00170 if (length < THRESHOLD) { /* Pairwise merge */ 00171 do { 00172 p2 = *EVAL(p2) = f1 + size2 - list1 + list2; 00173 if (sense > 0) 00174 swap (f1, f1 + size); 00175 } while ((f1 += size2) < f2); 00176 } else { /* Natural merge */ 00177 l2 = f2; 00178 for (f2 = f1 + size2; f2 < l2; f2 += size2) { 00179 if ((cmp(f2-size, f2) > 0) != sense) { 00180 p2 = *EVAL(p2) = f2 - list1 + list2; 00181 if (sense > 0) 00182 reverse(f1, f2-size); 00183 f1 = f2; 00184 } 00185 } 00186 if (sense > 0) 00187 reverse (f1, f2-size); 00188 f1 = f2; 00189 if (f2 < last || cmp(f2 - size, f2) > 0) 00190 p2 = *EVAL(p2) = f2 - list1 + list2; 00191 else 00192 p2 = *EVAL(p2) = list2 + n*size; 00193 } 00194 } 00195 #else /* pairwise merge only. */ 00196 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { 00197 p2 = *EVAL(p2) = p2 + size2; 00198 if (cmp (f1, f1 + size) > 0) 00199 swap(f1, f1 + size); 00200 } 00201 #endif /* NATURAL */ 00202 } 00203 00204 /* 00205 * Arguments are as for qsort. 00206 */ 00207 int 00208 mergesort(void *base, size_t nmemb, size_t size, 00209 int (*cmp) (const void *, const void *)) 00210 { 00211 register int i, sense; 00212 int big, iflag; 00213 register unsigned char *f1, *f2, *t, *b, *q, *l1, *l2; 00214 /*@dependent@*/ 00215 register unsigned char *tp2; 00216 /*@owned@*/ 00217 unsigned char *list2; 00218 /*@dependent@*/ 00219 unsigned char *list1; 00220 unsigned char *p2, *p, *last, **p1; 00221 00222 if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ 00223 errno = EINVAL; 00224 return (-1); 00225 } 00226 00227 if (nmemb == 0) 00228 return (0); 00229 00230 /* 00231 * XXX 00232 * Stupid subtraction for the Cray. 00233 */ 00234 iflag = 0; 00235 if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) 00236 iflag = 1; 00237 00238 if ((list2 = malloc(nmemb * size + PSIZE)) == NULL) 00239 return (-1); 00240 00241 list1 = base; 00242 setup(list1, list2, nmemb, size, cmp); 00243 last = list2 + nmemb * size; 00244 i = big = 0; 00245 /*@-branchstate@*/ 00246 while (*EVAL(list2) != last) { 00247 l2 = list1; 00248 p1 = EVAL(list1); 00249 for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { 00250 p2 = *EVAL(p2); 00251 f1 = l2; 00252 f2 = l1 = list1 + (p2 - list2); 00253 if (p2 != last) 00254 p2 = *EVAL(p2); 00255 l2 = list1 + (p2 - list2); 00256 while (f1 < l1 && f2 < l2) { 00257 if ((*cmp)(f1, f2) <= 0) { 00258 q = f2; 00259 b = f1, t = l1; 00260 sense = -1; 00261 } else { 00262 q = f1; 00263 b = f2, t = l2; 00264 sense = 0; 00265 } 00266 if (!big) { /* here i = 0 */ 00267 while ((b += size) < t && cmp(q, b) >sense) 00268 if (++i == 6) { 00269 big = 1; 00270 goto EXPONENTIAL; 00271 } 00272 } else { 00273 /*@-shiftimplementation@*/ 00274 EXPONENTIAL: for (i = size; ; i <<= 1) 00275 if ((p = (b + i)) >= t) { 00276 if ((p = t - size) > b && 00277 (*cmp)(q, p) <= sense) 00278 t = p; 00279 else 00280 b = p; 00281 /*@innerbreak@*/ break; 00282 } else if ((*cmp)(q, p) <= sense) { 00283 t = p; 00284 if (i == size) 00285 big = 0; 00286 goto FASTCASE; 00287 } else 00288 b = p; 00289 /*@-infloopsuncon@*/ 00290 while (t > b+size) { 00291 i = (((t - b) / size) >> 1) * size; 00292 if ((*cmp)(q, p = b + i) <= sense) 00293 t = p; 00294 else 00295 b = p; 00296 } 00297 goto COPY; 00298 FASTCASE: while (i > size) 00299 if ((*cmp)(q, 00300 p = b + (i >>= 1)) <= sense) 00301 t = p; 00302 else 00303 b = p; 00304 /*@=infloopsuncon@*/ 00305 /*@=shiftimplementation@*/ 00306 COPY: b = t; 00307 } 00308 i = size; 00309 if (q == f1) { 00310 if (iflag) { 00311 ICOPY_LIST(f2, tp2, b); 00312 ICOPY_ELT(f1, tp2, i); 00313 } else { 00314 CCOPY_LIST(f2, tp2, b); 00315 CCOPY_ELT(f1, tp2, i); 00316 } 00317 } else { 00318 if (iflag) { 00319 ICOPY_LIST(f1, tp2, b); 00320 ICOPY_ELT(f2, tp2, i); 00321 } else { 00322 CCOPY_LIST(f1, tp2, b); 00323 CCOPY_ELT(f2, tp2, i); 00324 } 00325 } 00326 } 00327 if (f2 < l2) { 00328 if (iflag) 00329 ICOPY_LIST(f2, tp2, l2); 00330 else 00331 CCOPY_LIST(f2, tp2, l2); 00332 } else if (f1 < l1) { 00333 if (iflag) 00334 ICOPY_LIST(f1, tp2, l1); 00335 else 00336 CCOPY_LIST(f1, tp2, l1); 00337 } 00338 *p1 = l2; 00339 } 00340 /*@-dependenttrans@*/ 00341 tp2 = list1; /* swap list1, list2 */ 00342 list1 = list2; 00343 list2 = tp2; 00344 /*@=dependenttrans@*/ 00345 last = list2 + nmemb*size; 00346 } 00347 /*@=branchstate@*/ 00348 if (base == list2) { 00349 memmove(list2, list1, nmemb*size); 00350 list2 = list1; 00351 } 00352 /*@-usereleased@*/ 00353 free(list2); 00354 /*@=usereleased@*/ 00355 return (0); 00356 } 00357 #else 00358 /* mergesort is implemented in System on Mac OS X */ 00359 #endif /* __APPLE__ */ 00360 /*@=bounds =mustmod =sizeoftype @*/