1/*************************************************************************** 2 * LPRng - An Extended Print Spooler System 3 * 4 * Copyright 1988-2003, Patrick Powell, San Diego, CA 5 * papowell@lprng.com 6 * See LICENSE for conditions of use. 7 * 8 ***************************************************************************/ 9 10 static char *const _id = 11"$Id: merge.c,v 1.1.1.1 2008/10/15 03:28:27 james26_jang Exp $"; 12 13 14/*- 15 * copyright (c) 1992, 1993 16 * The Regents of the University of California. All rights reserved. 17 * 18 * This code is derived from software contributed to Berkeley by 19 * Peter McIlroy. 20 * 21 * Redistribution and use in source and binary forms, with or without 22 * modification, are permitted provided that the following conditions 23 * are met: 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 3. All advertising materials mentioning features or use of this software 30 * must display the following acknowledgement: 31 * This product includes software developed by the University of 32 * California, Berkeley and its contributors. 33 * 4. Neither the name of the University nor the names of its contributors 34 * may be used to endorse or promote products derived from this software 35 * without specific prior written permission. 36 * 37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 */ 49 50#if defined(LIBC_SCCS) && !defined(lint) 51 static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94"; 52#endif /* LIBC_SCCS and not lint */ 53 54/* 55 * Hybrid exponential search/linear search merge sort with hybrid 56 * natural/pairwise first pass. Requires about .3% more comparisons 57 * for random data than LSMS with pairwise first pass alone. 58 * It works for objects as small as two bytes. 59 */ 60 61#define NATURAL 62#define THRESHOLD 16 /* Best choice for natural merge cut-off. */ 63 64/* #define NATURAL to get hybrid natural merge. 65 * (The default is pairwise merging.) 66 */ 67 68#include "lp.h" 69#include "merge.h" 70 71 static void 72 setup(unsigned char *list1, unsigned char *list2, size_t n, size_t size, 73 int (*cmp)(const void *, const void *, const void *), const void * arg); 74 static void 75 insertionsort(unsigned char *a, size_t n, size_t size, 76 int (*cmp)(const void *, const void *, const void *), const void * arg); 77 78#define ISIZE sizeof(int) 79#define PSIZE sizeof(unsigned char *) 80#define ICOPY_LIST(src, dst, last) \ 81 do \ 82 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ 83 while(src < last) 84#define ICOPY_ELT(src, dst, i) \ 85 do \ 86 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ 87 while (i -= ISIZE) 88 89#define CCOPY_LIST(src, dst, last) \ 90 do \ 91 *dst++ = *src++; \ 92 while (src < last) 93#define CCOPY_ELT(src, dst, i) \ 94 do \ 95 *dst++ = *src++; \ 96 while (i -= 1) 97 98/* 99 * Find the next possible pointer head. (Trickery for forcing an array 100 * to do double duty as a linked list when objects do not align with word 101 * boundaries. 102 */ 103/* Assumption: PSIZE is a power of 2. */ 104#define EVAL(p) (unsigned char **) \ 105 ((unsigned char *)0 + \ 106 (((unsigned char *)p + PSIZE - 1 - (unsigned char *) 0) & ~(PSIZE - 1))) 107 108/* 109 * We pass an additional arguement to the comparison routines. 110 * This lack of a third argument is a real defect in the qsort, etc. 111 * code 112 */ 113int 114Mergesort(void *base, size_t nmemb, size_t size, 115 int (*cmp)(const void *, const void *, const void *), const void * arg) 116{ 117 int i, sense; 118 int big, iflag; 119 unsigned char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; 120 unsigned char *list2, *list1, *p2, *p, *last, **p1; 121 122 if( nmemb < 2 ){ 123 return(0); 124 } 125 if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ 126 errno = EINVAL; 127 return (-1); 128 } 129 130 /* 131 * XXX 132 * Stupid subtraction for the Cray. 133 */ 134 iflag = 0; 135 if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) 136 iflag = 1; 137 138 list2 = malloc_or_die( (nmemb * size + PSIZE),__FILE__,__LINE__); 139 140 list1 = base; 141 setup(list1, list2, nmemb, size, cmp, arg); 142 last = list2 + nmemb * size; 143 i = big = 0; 144 while (*EVAL(list2) != last) { 145 l2 = list1; 146 p1 = EVAL(list1); 147 for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { 148 p2 = *EVAL(p2); 149 f1 = l2; 150 f2 = l1 = list1 + (p2 - list2); 151 if (p2 != last) 152 p2 = *EVAL(p2); 153 l2 = list1 + (p2 - list2); 154 while (f1 < l1 && f2 < l2) { 155 if ((*cmp)(f1, f2, arg) <= 0) { 156 q = f2; 157 b = f1, t = l1; 158 sense = -1; 159 } else { 160 q = f1; 161 b = f2, t = l2; 162 sense = 0; 163 } 164 if (!big) { /* here i = 0 */ 165/*LINEAR:*/ while ((b += size) < t && cmp(q, b, arg) >sense) 166 if (++i == 6) { 167 big = 1; 168 goto EXPONENTIAL; 169 } 170 } else { 171 EXPONENTIAL: for (i = size; ; i <<= 1) 172 if ((p = (b + i)) >= t) { 173 if ((p = t - size) > b && 174 (*cmp)(q, p, arg) <= sense) 175 t = p; 176 else 177 b = p; 178 break; 179 } else if ((*cmp)(q, p, arg) <= sense) { 180 t = p; 181 if (i == (int)size) 182 big = 0; 183 goto FASTCASE; 184 } else 185 b = p; 186/*SLOWCASE:*/ while (t > b+size) { 187 i = (((t - b) / size) >> 1) * size; 188 if ((*cmp)(q, p = b + i, arg) <= sense) 189 t = p; 190 else 191 b = p; 192 } 193 goto COPY; 194 FASTCASE: while (i > (int)size) 195 if ((*cmp)(q, 196 p = b + (i >>= 1), arg) <= sense) 197 t = p; 198 else 199 b = p; 200 COPY: b = t; 201 } 202 i = size; 203 if (q == f1) { 204 if (iflag) { 205 ICOPY_LIST(f2, tp2, b); 206 ICOPY_ELT(f1, tp2, i); 207 } else { 208 CCOPY_LIST(f2, tp2, b); 209 CCOPY_ELT(f1, tp2, i); 210 } 211 } else { 212 if (iflag) { 213 ICOPY_LIST(f1, tp2, b); 214 ICOPY_ELT(f2, tp2, i); 215 } else { 216 CCOPY_LIST(f1, tp2, b); 217 CCOPY_ELT(f2, tp2, i); 218 } 219 } 220 } 221 if (f2 < l2) { 222 if (iflag) 223 ICOPY_LIST(f2, tp2, l2); 224 else 225 CCOPY_LIST(f2, tp2, l2); 226 } else if (f1 < l1) { 227 if (iflag) 228 ICOPY_LIST(f1, tp2, l1); 229 else 230 CCOPY_LIST(f1, tp2, l1); 231 } 232 *p1 = l2; 233 } 234 tp2 = list1; /* swap list1, list2 */ 235 list1 = list2; 236 list2 = tp2; 237 last = list2 + nmemb*size; 238 } 239 if (base == list2) { 240 memmove(list2, list1, nmemb*size); 241 list2 = list1; 242 } 243 free(list2); 244 return (0); 245} 246 247#define swap(a, b) { \ 248 s = b; \ 249 i = size; \ 250 do { \ 251 tmp = *a; *a++ = *s; *s++ = tmp; \ 252 } while (--i); \ 253 a -= size; \ 254 } 255#define reverse(bot, top) { \ 256 s = top; \ 257 do { \ 258 i = size; \ 259 do { \ 260 tmp = *bot; *bot++ = *s; *s++ = tmp; \ 261 } while (--i); \ 262 s -= size2; \ 263 } while(bot < s); \ 264} 265 266/* 267 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of 268 * increasing order, list2 in a corresponding linked list. Checks for runs 269 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL 270 * is defined. Otherwise simple pairwise merging is used.) 271 */ 272 static void 273 setup(unsigned char *list1, unsigned char *list2, size_t n, size_t size, 274 int (*cmp)(const void *, const void *, const void *), const void * arg) 275{ 276 int i, length, size2, tmp, sense; 277 unsigned char *f1, *f2, *s, *l2, *last, *p2; 278 279 size2 = size*2; 280 if (n <= 5) { 281 insertionsort(list1, n, size, cmp, arg); 282 *EVAL(list2) = (unsigned char*) list2 + n*size; 283 return; 284 } 285 /* 286 * Avoid running pointers out of bounds; limit n to evens 287 * for simplicity. 288 */ 289 i = 4 + (n & 1); 290 insertionsort(list1 + (n - i) * size, i, size, cmp, arg); 291 last = list1 + size * (n - i); 292 *EVAL(list2 + (last - list1)) = list2 + n * size; 293 294#ifdef NATURAL 295 p2 = list2; 296 f1 = list1; 297 sense = (cmp(f1, f1 + size, arg) > 0); 298 for (; f1 < last; sense = !sense) { 299 length = 2; 300 /* Find pairs with same sense. */ 301 for (f2 = f1 + size2; f2 < last; f2 += size2) { 302 if ((cmp(f2, f2+ size, arg) > 0) != sense) 303 break; 304 length += 2; 305 } 306 if (length < THRESHOLD) { /* Pairwise merge */ 307 do { 308 p2 = *EVAL(p2) = f1 + size2 - list1 + list2; 309 if (sense > 0) 310 swap (f1, f1 + size); 311 } while ((f1 += size2) < f2); 312 } else { /* Natural merge */ 313 l2 = f2; 314 for (f2 = f1 + size2; f2 < l2; f2 += size2) { 315 if ((cmp(f2-size, f2, arg) > 0) != sense) { 316 p2 = *EVAL(p2) = f2 - list1 + list2; 317 if (sense > 0) 318 reverse(f1, f2-size); 319 f1 = f2; 320 } 321 } 322 if (sense > 0) 323 reverse (f1, f2-size); 324 f1 = f2; 325 if (f2 < last || cmp(f2 - size, f2, arg) > 0) 326 p2 = *EVAL(p2) = f2 - list1 + list2; 327 else 328 p2 = *EVAL(p2) = list2 + n*size; 329 } 330 } 331#else /* pairwise merge only. */ 332 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { 333 p2 = *EVAL(p2) = p2 + size2; 334 if (cmp (f1, f1 + size, arg) > 0) 335 swap(f1, f1 + size); 336 } 337#endif /* NATURAL */ 338} 339 340/* 341 * This is to avoid out-of-bounds addresses in sorting the 342 * last 4 elements. 343 */ 344 static void 345 insertionsort(unsigned char *a, size_t n, size_t size, 346 int (*cmp)(const void *, const void *, const void *), const void * arg) 347{ 348 unsigned char *ai, *s, *t, *u, tmp; 349 int i; 350 351 for (ai = a+size; --n >= 1; ai += size) 352 for (t = ai; t > a; t -= size) { 353 u = t - size; 354 if (cmp(u, t, arg) <= 0) 355 break; 356 swap(u, t); 357 } 358} 359