]>
git.saurik.com Git - apple/libc.git/blob - stdlib/merge.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1992, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
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.
70 #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
72 /* #define NATURAL to get hybrid natural merge.
73 * (The default is pairwise merging.)
76 #include <sys/types.h>
82 static void setup
__P((u_char
*, u_char
*, size_t, size_t, int (*)()));
83 static void insertionsort
__P((u_char
*, size_t, size_t, int (*)()));
85 #define ISIZE sizeof(int)
86 #define PSIZE sizeof(u_char *)
87 #define ICOPY_LIST(src, dst, last) \
89 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
91 #define ICOPY_ELT(src, dst, i) \
93 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
96 #define CCOPY_LIST(src, dst, last) \
100 #define CCOPY_ELT(src, dst, i) \
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
110 /* Assumption: PSIZE is a power of 2. */
111 #define EVAL(p) (u_char **) \
113 (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
116 * Arguments are as for qsort.
119 mergesort(base
, nmemb
, size
, cmp
)
122 register size_t size
;
123 int (*cmp
) __P((const void *, const void *));
125 register int i
, sense
;
127 register u_char
*f1
, *f2
, *t
, *b
, *tp2
, *q
, *l1
, *l2
;
128 u_char
*list2
, *list1
, *p2
, *p
, *last
, **p1
;
130 if (size
< PSIZE
/ 2) { /* Pointers must fit into 2 * size. */
137 * Stupid subtraction for the Cray.
140 if (!(size
% ISIZE
) && !(((char *)base
- (char *)0) % ISIZE
))
143 if ((list2
= malloc(nmemb
* size
+ PSIZE
)) == NULL
)
147 setup(list1
, list2
, nmemb
, size
, cmp
);
148 last
= list2
+ nmemb
* size
;
150 while (*EVAL(list2
) != last
) {
153 for (tp2
= p2
= list2
; p2
!= last
; p1
= EVAL(l2
)) {
156 f2
= l1
= list1
+ (p2
- list2
);
159 l2
= list1
+ (p2
- list2
);
160 while (f1
< l1
&& f2
< l2
) {
161 if ((*cmp
)(f1
, f2
) <= 0) {
170 if (!big
) { /* here i = 0 */
171 LINEAR
: while ((b
+= size
) < t
&& cmp(q
, b
) >sense
)
177 EXPONENTIAL
: for (i
= size
; ; i
<<= 1)
178 if ((p
= (b
+ i
)) >= t
) {
179 if ((p
= t
- size
) > b
&&
180 (*cmp
)(q
, p
) <= sense
)
185 } else if ((*cmp
)(q
, p
) <= sense
) {
192 SLOWCASE
: while (t
> b
+size
) {
193 i
= (((t
- b
) / size
) >> 1) * size
;
194 if ((*cmp
)(q
, p
= b
+ i
) <= sense
)
200 FASTCASE
: while (i
> size
)
202 p
= b
+ (i
>>= 1)) <= sense
)
211 ICOPY_LIST(f2
, tp2
, b
);
212 ICOPY_ELT(f1
, tp2
, i
);
214 CCOPY_LIST(f2
, tp2
, b
);
215 CCOPY_ELT(f1
, tp2
, i
);
219 ICOPY_LIST(f1
, tp2
, b
);
220 ICOPY_ELT(f2
, tp2
, i
);
222 CCOPY_LIST(f1
, tp2
, b
);
223 CCOPY_ELT(f2
, tp2
, i
);
229 ICOPY_LIST(f2
, tp2
, l2
);
231 CCOPY_LIST(f2
, tp2
, l2
);
232 } else if (f1
< l1
) {
234 ICOPY_LIST(f1
, tp2
, l1
);
236 CCOPY_LIST(f1
, tp2
, l1
);
240 tp2
= list1
; /* swap list1, list2 */
243 last
= list2
+ nmemb
*size
;
246 memmove(list2
, list1
, nmemb
*size
);
253 #define swap(a, b) { \
257 tmp = *a; *a++ = *s; *s++ = tmp; \
261 #define reverse(bot, top) { \
266 tmp = *bot; *bot++ = *s; *s++ = tmp; \
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.)
279 setup(list1
, list2
, n
, size
, cmp
)
281 int (*cmp
) __P((const void *, const void *));
282 u_char
*list1
, *list2
;
284 int i
, length
, size2
, tmp
, sense
;
285 u_char
*f1
, *f2
, *s
, *l2
, *last
, *p2
;
289 insertionsort(list1
, n
, size
, cmp
);
290 *EVAL(list2
) = (u_char
*) list2
+ n
*size
;
294 * Avoid running pointers out of bounds; limit n to evens
298 insertionsort(list1
+ (n
- i
) * size
, i
, size
, cmp
);
299 last
= list1
+ size
* (n
- i
);
300 *EVAL(list2
+ (last
- list1
)) = list2
+ n
* size
;
305 sense
= (cmp(f1
, f1
+ size
) > 0);
306 for (; f1
< last
; sense
= !sense
) {
308 /* Find pairs with same sense. */
309 for (f2
= f1
+ size2
; f2
< last
; f2
+= size2
) {
310 if ((cmp(f2
, f2
+ size
) > 0) != sense
)
314 if (length
< THRESHOLD
) { /* Pairwise merge */
316 p2
= *EVAL(p2
) = f1
+ size2
- list1
+ list2
;
318 swap (f1
, f1
+ size
);
319 } while ((f1
+= size2
) < f2
);
320 } else { /* Natural merge */
322 for (f2
= f1
+ size2
; f2
< l2
; f2
+= size2
) {
323 if ((cmp(f2
-size
, f2
) > 0) != sense
) {
324 p2
= *EVAL(p2
) = f2
- list1
+ list2
;
326 reverse(f1
, f2
-size
);
331 reverse (f1
, f2
-size
);
333 if (f2
< last
|| cmp(f2
- size
, f2
) > 0)
334 p2
= *EVAL(p2
) = f2
- list1
+ list2
;
336 p2
= *EVAL(p2
) = list2
+ n
*size
;
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)
349 * This is to avoid out-of-bounds addresses in sorting the
353 insertionsort(a
, n
, size
, cmp
)
356 int (*cmp
) __P((const void *, const void *));
358 u_char
*ai
, *s
, *t
, *u
, tmp
;
361 for (ai
= a
+size
; --n
>= 1; ai
+= size
)
362 for (t
= ai
; t
> a
; t
-= size
) {