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