]> git.saurik.com Git - apple/libc.git/blame - stdlib/merge.c
Libc-262.2.12.tar.gz
[apple/libc.git] / stdlib / merge.c
CommitLineData
e9ce8d39
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
734aad71 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
e9ce8d39 7 *
734aad71
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
e9ce8d39
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
734aad71
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.
e9ce8d39
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * Copyright (c) 1992, 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 * Peter McIlroy.
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
62/*
63 * Hybrid exponential search/linear search merge sort with hybrid
64 * natural/pairwise first pass. Requires about .3% more comparisons
65 * for random data than LSMS with pairwise first pass alone.
66 * It works for objects as small as two bytes.
67 */
68
69#define NATURAL
70#define THRESHOLD 16 /* Best choice for natural merge cut-off. */
71
72/* #define NATURAL to get hybrid natural merge.
73 * (The default is pairwise merging.)
74 */
75
76#include <sys/types.h>
77
78#include <errno.h>
79#include <stdlib.h>
80#include <string.h>
81
82static void setup __P((u_char *, u_char *, size_t, size_t, int (*)()));
83static void insertionsort __P((u_char *, size_t, size_t, int (*)()));
84
85#define ISIZE sizeof(int)
86#define PSIZE sizeof(u_char *)
87#define ICOPY_LIST(src, dst, last) \
88 do \
89 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
90 while(src < last)
91#define ICOPY_ELT(src, dst, i) \
92 do \
93 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
94 while (i -= ISIZE)
95
96#define CCOPY_LIST(src, dst, last) \
97 do \
98 *dst++ = *src++; \
99 while (src < last)
100#define CCOPY_ELT(src, dst, i) \
101 do \
102 *dst++ = *src++; \
103 while (i -= 1)
104
105/*
106 * Find the next possible pointer head. (Trickery for forcing an array
107 * to do double duty as a linked list when objects do not align with word
108 * boundaries.
109 */
110/* Assumption: PSIZE is a power of 2. */
111#define EVAL(p) (u_char **) \
112 ((u_char *)0 + \
113 (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
114
115/*
116 * Arguments are as for qsort.
117 */
118int
119mergesort(base, nmemb, size, cmp)
120 void *base;
121 size_t nmemb;
122 register size_t size;
123 int (*cmp) __P((const void *, const void *));
124{
125 register int i, sense;
126 int big, iflag;
127 register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
128 u_char *list2, *list1, *p2, *p, *last, **p1;
129
130 if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
131 errno = EINVAL;
132 return (-1);
133 }
134
135 /*
136 * XXX
137 * Stupid subtraction for the Cray.
138 */
139 iflag = 0;
140 if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
141 iflag = 1;
142
143 if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
144 return (-1);
145
146 list1 = base;
147 setup(list1, list2, nmemb, size, cmp);
148 last = list2 + nmemb * size;
149 i = big = 0;
150 while (*EVAL(list2) != last) {
151 l2 = list1;
152 p1 = EVAL(list1);
153 for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
154 p2 = *EVAL(p2);
155 f1 = l2;
156 f2 = l1 = list1 + (p2 - list2);
157 if (p2 != last)
158 p2 = *EVAL(p2);
159 l2 = list1 + (p2 - list2);
160 while (f1 < l1 && f2 < l2) {
161 if ((*cmp)(f1, f2) <= 0) {
162 q = f2;
163 b = f1, t = l1;
164 sense = -1;
165 } else {
166 q = f1;
167 b = f2, t = l2;
168 sense = 0;
169 }
170 if (!big) { /* here i = 0 */
171LINEAR: while ((b += size) < t && cmp(q, b) >sense)
172 if (++i == 6) {
173 big = 1;
174 goto EXPONENTIAL;
175 }
176 } else {
177EXPONENTIAL: for (i = size; ; i <<= 1)
178 if ((p = (b + i)) >= t) {
179 if ((p = t - size) > b &&
180 (*cmp)(q, p) <= sense)
181 t = p;
182 else
183 b = p;
184 break;
185 } else if ((*cmp)(q, p) <= sense) {
186 t = p;
187 if (i == size)
188 big = 0;
189 goto FASTCASE;
190 } else
191 b = p;
192SLOWCASE: while (t > b+size) {
193 i = (((t - b) / size) >> 1) * size;
194 if ((*cmp)(q, p = b + i) <= sense)
195 t = p;
196 else
197 b = p;
198 }
199 goto COPY;
200FASTCASE: while (i > size)
201 if ((*cmp)(q,
202 p = b + (i >>= 1)) <= sense)
203 t = p;
204 else
205 b = p;
206COPY: b = t;
207 }
208 i = size;
209 if (q == f1) {
210 if (iflag) {
211 ICOPY_LIST(f2, tp2, b);
212 ICOPY_ELT(f1, tp2, i);
213 } else {
214 CCOPY_LIST(f2, tp2, b);
215 CCOPY_ELT(f1, tp2, i);
216 }
217 } else {
218 if (iflag) {
219 ICOPY_LIST(f1, tp2, b);
220 ICOPY_ELT(f2, tp2, i);
221 } else {
222 CCOPY_LIST(f1, tp2, b);
223 CCOPY_ELT(f2, tp2, i);
224 }
225 }
226 }
227 if (f2 < l2) {
228 if (iflag)
229 ICOPY_LIST(f2, tp2, l2);
230 else
231 CCOPY_LIST(f2, tp2, l2);
232 } else if (f1 < l1) {
233 if (iflag)
234 ICOPY_LIST(f1, tp2, l1);
235 else
236 CCOPY_LIST(f1, tp2, l1);
237 }
238 *p1 = l2;
239 }
240 tp2 = list1; /* swap list1, list2 */
241 list1 = list2;
242 list2 = tp2;
243 last = list2 + nmemb*size;
244 }
245 if (base == list2) {
246 memmove(list2, list1, nmemb*size);
247 list2 = list1;
248 }
249 free(list2);
250 return (0);
251}
252
253#define swap(a, b) { \
254 s = b; \
255 i = size; \
256 do { \
257 tmp = *a; *a++ = *s; *s++ = tmp; \
258 } while (--i); \
259 a -= size; \
260 }
261#define reverse(bot, top) { \
262 s = top; \
263 do { \
264 i = size; \
265 do { \
266 tmp = *bot; *bot++ = *s; *s++ = tmp; \
267 } while (--i); \
268 s -= size2; \
269 } while(bot < s); \
270}
271
272/*
273 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
274 * increasing order, list2 in a corresponding linked list. Checks for runs
275 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
276 * is defined. Otherwise simple pairwise merging is used.)
277 */
278void
279setup(list1, list2, n, size, cmp)
280 size_t n, size;
281 int (*cmp) __P((const void *, const void *));
282 u_char *list1, *list2;
283{
284 int i, length, size2, tmp, sense;
285 u_char *f1, *f2, *s, *l2, *last, *p2;
286
287 size2 = size*2;
288 if (n <= 5) {
289 insertionsort(list1, n, size, cmp);
290 *EVAL(list2) = (u_char*) list2 + n*size;
291 return;
292 }
293 /*
294 * Avoid running pointers out of bounds; limit n to evens
295 * for simplicity.
296 */
297 i = 4 + (n & 1);
298 insertionsort(list1 + (n - i) * size, i, size, cmp);
299 last = list1 + size * (n - i);
300 *EVAL(list2 + (last - list1)) = list2 + n * size;
301
302#ifdef NATURAL
303 p2 = list2;
304 f1 = list1;
305 sense = (cmp(f1, f1 + size) > 0);
306 for (; f1 < last; sense = !sense) {
307 length = 2;
308 /* Find pairs with same sense. */
309 for (f2 = f1 + size2; f2 < last; f2 += size2) {
310 if ((cmp(f2, f2+ size) > 0) != sense)
311 break;
312 length += 2;
313 }
314 if (length < THRESHOLD) { /* Pairwise merge */
315 do {
316 p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
317 if (sense > 0)
318 swap (f1, f1 + size);
319 } while ((f1 += size2) < f2);
320 } else { /* Natural merge */
321 l2 = f2;
322 for (f2 = f1 + size2; f2 < l2; f2 += size2) {
323 if ((cmp(f2-size, f2) > 0) != sense) {
324 p2 = *EVAL(p2) = f2 - list1 + list2;
325 if (sense > 0)
326 reverse(f1, f2-size);
327 f1 = f2;
328 }
329 }
330 if (sense > 0)
331 reverse (f1, f2-size);
332 f1 = f2;
333 if (f2 < last || cmp(f2 - size, f2) > 0)
334 p2 = *EVAL(p2) = f2 - list1 + list2;
335 else
336 p2 = *EVAL(p2) = list2 + n*size;
337 }
338 }
339#else /* pairwise merge only. */
340 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
341 p2 = *EVAL(p2) = p2 + size2;
342 if (cmp (f1, f1 + size) > 0)
343 swap(f1, f1 + size);
344 }
345#endif /* NATURAL */
346}
347
348/*
349 * This is to avoid out-of-bounds addresses in sorting the
350 * last 4 elements.
351 */
352static void
353insertionsort(a, n, size, cmp)
354 u_char *a;
355 size_t n, size;
356 int (*cmp) __P((const void *, const void *));
357{
358 u_char *ai, *s, *t, *u, tmp;
359 int i;
360
361 for (ai = a+size; --n >= 1; ai += size)
362 for (t = ai; t > a; t -= size) {
363 u = t - size;
364 if (cmp(u, t) <= 0)
365 break;
366 swap(u, t);
367 }
368}