xref: /xnu-12377.81.4/bsd/kern/qsort.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions /*
29*043036a2SApple OSS Distributions  * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
30*043036a2SApple OSS Distributions  *
31*043036a2SApple OSS Distributions  * Copyright (c) 1992, 1993
32*043036a2SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
33*043036a2SApple OSS Distributions  *
34*043036a2SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
35*043036a2SApple OSS Distributions  * modification, are permitted provided that the following conditions
36*043036a2SApple OSS Distributions  * are met:
37*043036a2SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
38*043036a2SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
39*043036a2SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
40*043036a2SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
41*043036a2SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
42*043036a2SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
43*043036a2SApple OSS Distributions  *    must display the following acknowledgement:
44*043036a2SApple OSS Distributions  *	This product includes software developed by the University of
45*043036a2SApple OSS Distributions  *	California, Berkeley and its contributors.
46*043036a2SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
47*043036a2SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
48*043036a2SApple OSS Distributions  *    without specific prior written permission.
49*043036a2SApple OSS Distributions  *
50*043036a2SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51*043036a2SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52*043036a2SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53*043036a2SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54*043036a2SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55*043036a2SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56*043036a2SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57*043036a2SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58*043036a2SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59*043036a2SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60*043036a2SApple OSS Distributions  * SUCH DAMAGE.
61*043036a2SApple OSS Distributions  *
62*043036a2SApple OSS Distributions  *	@(#)qsort.c	8.1 (Berkeley) 6/4/93
63*043036a2SApple OSS Distributions  */
64*043036a2SApple OSS Distributions 
65*043036a2SApple OSS Distributions 
66*043036a2SApple OSS Distributions #include <sys/types.h>
67*043036a2SApple OSS Distributions #include <sys/kpi_private.h>
68*043036a2SApple OSS Distributions #include <kern/qsort.h>
69*043036a2SApple OSS Distributions 
70*043036a2SApple OSS Distributions static inline char      *med3(char *, char *, char *, int (*)(const void *, const void *));
71*043036a2SApple OSS Distributions static inline void       swapfunc(char *, char *, long, int);
72*043036a2SApple OSS Distributions 
73*043036a2SApple OSS Distributions #define min(a, b)       ((a) < (b) ? (a) : (b))
74*043036a2SApple OSS Distributions 
75*043036a2SApple OSS Distributions /*
76*043036a2SApple OSS Distributions  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
77*043036a2SApple OSS Distributions  */
78*043036a2SApple OSS Distributions #define swapcode(TYPE, parmi, parmj, n)                 \
79*043036a2SApple OSS Distributions 	long i = (n) / sizeof (TYPE);                   \
80*043036a2SApple OSS Distributions 	TYPE *pi = (TYPE *) (parmi);                    \
81*043036a2SApple OSS Distributions 	TYPE *pj = (TYPE *) (parmj);                    \
82*043036a2SApple OSS Distributions 	do {                                            \
83*043036a2SApple OSS Distributions 	        TYPE	t = *pi;                        \
84*043036a2SApple OSS Distributions 	        *pi++ = *pj;                            \
85*043036a2SApple OSS Distributions 	        *pj++ = t;                              \
86*043036a2SApple OSS Distributions 	} while (--i > 0);
87*043036a2SApple OSS Distributions 
88*043036a2SApple OSS Distributions #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
89*043036a2SApple OSS Distributions 	es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
90*043036a2SApple OSS Distributions 
91*043036a2SApple OSS Distributions static inline void
swapfunc(char * a,char * b,long n,int swaptype)92*043036a2SApple OSS Distributions swapfunc(char *a, char *b, long n, int swaptype)
93*043036a2SApple OSS Distributions {
94*043036a2SApple OSS Distributions 	if (swaptype <= 1) {
95*043036a2SApple OSS Distributions 		swapcode(long, a, b, n);
96*043036a2SApple OSS Distributions 	} else {
97*043036a2SApple OSS Distributions 		swapcode(char, a, b, n);
98*043036a2SApple OSS Distributions 	}
99*043036a2SApple OSS Distributions }
100*043036a2SApple OSS Distributions 
101*043036a2SApple OSS Distributions #define swap(a, b)                                      \
102*043036a2SApple OSS Distributions 	if (swaptype == 0) {                            \
103*043036a2SApple OSS Distributions 	        long t = *(long *)(a);                  \
104*043036a2SApple OSS Distributions 	        *(long *)(a) = *(long *)(b);            \
105*043036a2SApple OSS Distributions 	        *(long *)(b) = t;                       \
106*043036a2SApple OSS Distributions 	} else                                          \
107*043036a2SApple OSS Distributions 	        swapfunc(a, b, es, swaptype)
108*043036a2SApple OSS Distributions 
109*043036a2SApple OSS Distributions #define vecswap(a, b, n)        if ((n) > 0) swapfunc(a, b, n, swaptype)
110*043036a2SApple OSS Distributions 
111*043036a2SApple OSS Distributions static inline char *
med3(char * a,char * b,char * c,int (* cmp)(const void *,const void *))112*043036a2SApple OSS Distributions med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
113*043036a2SApple OSS Distributions {
114*043036a2SApple OSS Distributions 	return cmp(a, b) < 0 ?
115*043036a2SApple OSS Distributions 	       (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a))
116*043036a2SApple OSS Distributions 	       :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c));
117*043036a2SApple OSS Distributions }
118*043036a2SApple OSS Distributions 
119*043036a2SApple OSS Distributions __private_extern__
120*043036a2SApple OSS Distributions void
qsort(void * a,size_t n,size_t es,int (* cmp)(const void *,const void *))121*043036a2SApple OSS Distributions qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *))
122*043036a2SApple OSS Distributions {
123*043036a2SApple OSS Distributions 	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
124*043036a2SApple OSS Distributions 	int swaptype, swap_cnt;
125*043036a2SApple OSS Distributions 	long d, r;
126*043036a2SApple OSS Distributions 
127*043036a2SApple OSS Distributions loop:   SWAPINIT(a, es);
128*043036a2SApple OSS Distributions 	swap_cnt = 0;
129*043036a2SApple OSS Distributions 	if (n < 7) {
130*043036a2SApple OSS Distributions 		for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) {
131*043036a2SApple OSS Distributions 			for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
132*043036a2SApple OSS Distributions 			    pl -= es) {
133*043036a2SApple OSS Distributions 				swap(pl, (char *)(pl - es));
134*043036a2SApple OSS Distributions 			}
135*043036a2SApple OSS Distributions 		}
136*043036a2SApple OSS Distributions 		return;
137*043036a2SApple OSS Distributions 	}
138*043036a2SApple OSS Distributions 	pm = (char *)a + (n / 2) * es;
139*043036a2SApple OSS Distributions 	if (n > 7) {
140*043036a2SApple OSS Distributions 		pl = a;
141*043036a2SApple OSS Distributions 		pn = (char *)a + (n - 1) * es;
142*043036a2SApple OSS Distributions 		if (n > 40) {
143*043036a2SApple OSS Distributions 			d = (n / 8) * es;
144*043036a2SApple OSS Distributions 			pl = med3(pl, pl + d, pl + 2 * d, cmp);
145*043036a2SApple OSS Distributions 			pm = med3(pm - d, pm, pm + d, cmp);
146*043036a2SApple OSS Distributions 			pn = med3(pn - 2 * d, pn - d, pn, cmp);
147*043036a2SApple OSS Distributions 		}
148*043036a2SApple OSS Distributions 		pm = med3(pl, pm, pn, cmp);
149*043036a2SApple OSS Distributions 	}
150*043036a2SApple OSS Distributions 	swap(a, pm);
151*043036a2SApple OSS Distributions 	pa = pb = (char *)a + es;
152*043036a2SApple OSS Distributions 
153*043036a2SApple OSS Distributions 	pc = pd = (char *)a + (n - 1) * es;
154*043036a2SApple OSS Distributions 	for (;;) {
155*043036a2SApple OSS Distributions 		while (pb <= pc && (r = cmp(pb, a)) <= 0) {
156*043036a2SApple OSS Distributions 			if (r == 0) {
157*043036a2SApple OSS Distributions 				swap_cnt = 1;
158*043036a2SApple OSS Distributions 				swap(pa, pb);
159*043036a2SApple OSS Distributions 				pa += es;
160*043036a2SApple OSS Distributions 			}
161*043036a2SApple OSS Distributions 			pb += es;
162*043036a2SApple OSS Distributions 		}
163*043036a2SApple OSS Distributions 		while (pb <= pc && (r = cmp(pc, a)) >= 0) {
164*043036a2SApple OSS Distributions 			if (r == 0) {
165*043036a2SApple OSS Distributions 				swap_cnt = 1;
166*043036a2SApple OSS Distributions 				swap(pc, pd);
167*043036a2SApple OSS Distributions 				pd -= es;
168*043036a2SApple OSS Distributions 			}
169*043036a2SApple OSS Distributions 			pc -= es;
170*043036a2SApple OSS Distributions 		}
171*043036a2SApple OSS Distributions 		if (pb > pc) {
172*043036a2SApple OSS Distributions 			break;
173*043036a2SApple OSS Distributions 		}
174*043036a2SApple OSS Distributions 		swap(pb, pc);
175*043036a2SApple OSS Distributions 		swap_cnt = 1;
176*043036a2SApple OSS Distributions 		pb += es;
177*043036a2SApple OSS Distributions 		pc -= es;
178*043036a2SApple OSS Distributions 	}
179*043036a2SApple OSS Distributions 	if (swap_cnt == 0) {  /* Switch to insertion sort */
180*043036a2SApple OSS Distributions 		for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) {
181*043036a2SApple OSS Distributions 			for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
182*043036a2SApple OSS Distributions 			    pl -= es) {
183*043036a2SApple OSS Distributions 				swap(pl, pl - es);
184*043036a2SApple OSS Distributions 			}
185*043036a2SApple OSS Distributions 		}
186*043036a2SApple OSS Distributions 		return;
187*043036a2SApple OSS Distributions 	}
188*043036a2SApple OSS Distributions 
189*043036a2SApple OSS Distributions 	pn = (char *)a + n * es;
190*043036a2SApple OSS Distributions 	r = min(pa - (char *)a, pb - pa);
191*043036a2SApple OSS Distributions 	vecswap(a, pb - r, r);
192*043036a2SApple OSS Distributions 	r = min(pd - pc, pn - pd - es);
193*043036a2SApple OSS Distributions 	vecswap(pb, pn - r, r);
194*043036a2SApple OSS Distributions 	if ((size_t)(r = pb - pa) > es) {
195*043036a2SApple OSS Distributions 		qsort(a, r / es, es, cmp);
196*043036a2SApple OSS Distributions 	}
197*043036a2SApple OSS Distributions 	if ((size_t)(r = pd - pc) > es) {
198*043036a2SApple OSS Distributions 		/* Iterate rather than recurse to save stack space */
199*043036a2SApple OSS Distributions 		a = pn - r;
200*043036a2SApple OSS Distributions 		n = r / es;
201*043036a2SApple OSS Distributions 		goto loop;
202*043036a2SApple OSS Distributions 	}
203*043036a2SApple OSS Distributions /*		qsort(pn - r, r / es, es, cmp);*/
204*043036a2SApple OSS Distributions }
205*043036a2SApple OSS Distributions 
206*043036a2SApple OSS Distributions /* private KPI */
207*043036a2SApple OSS Distributions void
kx_qsort(void * array,size_t nm,size_t member_size,int (* cmpf)(const void *,const void *))208*043036a2SApple OSS Distributions kx_qsort(void *array, size_t nm, size_t member_size, int (*cmpf)(const void *, const void *))
209*043036a2SApple OSS Distributions {
210*043036a2SApple OSS Distributions 	qsort(array, nm, member_size, cmpf);
211*043036a2SApple OSS Distributions }
212