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