qsort.c revision 1.3 1 1.1 abs /*-
2 1.1 abs * Copyright (c) 1992, 1993
3 1.1 abs * The Regents of the University of California. All rights reserved.
4 1.1 abs *
5 1.1 abs * Redistribution and use in source and binary forms, with or without
6 1.1 abs * modification, are permitted provided that the following conditions
7 1.1 abs * are met:
8 1.1 abs * 1. Redistributions of source code must retain the above copyright
9 1.1 abs * notice, this list of conditions and the following disclaimer.
10 1.1 abs * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 abs * notice, this list of conditions and the following disclaimer in the
12 1.1 abs * documentation and/or other materials provided with the distribution.
13 1.1 abs * 3. All advertising materials mentioning features or use of this software
14 1.1 abs * must display the following acknowledgement:
15 1.1 abs * This product includes software developed by the University of
16 1.1 abs * California, Berkeley and its contributors.
17 1.1 abs * 4. Neither the name of the University nor the names of its contributors
18 1.1 abs * may be used to endorse or promote products derived from this software
19 1.1 abs * without specific prior written permission.
20 1.1 abs *
21 1.1 abs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 abs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 abs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 abs * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 abs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 abs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 abs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 abs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 abs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 abs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 abs * SUCH DAMAGE.
32 1.1 abs *
33 1.1 abs * $FreeBSD: src/sys/libkern/qsort.c,v 1.12 2002/11/09 12:55:06 alfred Exp $
34 1.3 perry * $NetBSD: qsort.c,v 1.3 2005/12/24 20:45:09 perry Exp $
35 1.1 abs */
36 1.1 abs
37 1.1 abs #include <sys/cdefs.h>
38 1.1 abs #include <sys/types.h>
39 1.1 abs
40 1.1 abs typedef int cmp_t(const void *, const void *);
41 1.3 perry static inline char *med3(char *, char *, char *, cmp_t *);
42 1.3 perry static inline void swapfunc(char *, char *, int, int);
43 1.1 abs
44 1.1 abs #define min(a, b) (a) < (b) ? (a) : (b)
45 1.1 abs
46 1.1 abs void qsort(void *a, size_t n, size_t es, cmp_t *cmp);
47 1.1 abs
48 1.1 abs /*
49 1.1 abs * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
50 1.1 abs */
51 1.1 abs #define swapcode(TYPE, parmi, parmj, n) { \
52 1.1 abs long i = (n) / sizeof (TYPE); \
53 1.1 abs register TYPE *pi = (TYPE *) (parmi); \
54 1.1 abs register TYPE *pj = (TYPE *) (parmj); \
55 1.1 abs do { \
56 1.1 abs register TYPE t = *pi; \
57 1.1 abs *pi++ = *pj; \
58 1.1 abs *pj++ = t; \
59 1.1 abs } while (--i > 0); \
60 1.1 abs }
61 1.1 abs
62 1.1 abs #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
63 1.1 abs es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
64 1.1 abs
65 1.3 perry static inline void
66 1.1 abs swapfunc(a, b, n, swaptype)
67 1.1 abs char *a, *b;
68 1.1 abs int n, swaptype;
69 1.1 abs {
70 1.1 abs if(swaptype <= 1)
71 1.1 abs swapcode(long, a, b, n)
72 1.1 abs else
73 1.1 abs swapcode(char, a, b, n)
74 1.1 abs }
75 1.1 abs
76 1.1 abs #define swap(a, b) \
77 1.1 abs if (swaptype == 0) { \
78 1.1 abs long t = *(long *)(a); \
79 1.1 abs *(long *)(a) = *(long *)(b); \
80 1.1 abs *(long *)(b) = t; \
81 1.1 abs } else \
82 1.1 abs swapfunc(a, b, es, swaptype)
83 1.1 abs
84 1.1 abs #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
85 1.1 abs
86 1.3 perry static inline char *
87 1.1 abs med3(a, b, c, cmp)
88 1.1 abs char *a, *b, *c;
89 1.1 abs cmp_t *cmp;
90 1.1 abs {
91 1.1 abs return cmp(a, b) < 0 ?
92 1.1 abs (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
93 1.1 abs :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
94 1.1 abs }
95 1.1 abs
96 1.1 abs void
97 1.1 abs qsort(void *a, size_t n, size_t es, cmp_t *cmp)
98 1.1 abs {
99 1.1 abs char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
100 1.1 abs int d, r, swaptype, swap_cnt;
101 1.1 abs
102 1.1 abs loop: SWAPINIT(a, es);
103 1.1 abs swap_cnt = 0;
104 1.1 abs if (n < 7) {
105 1.1 abs for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
106 1.1 abs for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0;
107 1.1 abs pl -= es)
108 1.1 abs swap(pl, pl - es);
109 1.1 abs return;
110 1.1 abs }
111 1.1 abs pm = (char *)a + (n / 2) * es;
112 1.1 abs if (n > 7) {
113 1.1 abs pl = a;
114 1.1 abs pn = (char *)a + (n - 1) * es;
115 1.1 abs if (n > 40) {
116 1.1 abs d = (n / 8) * es;
117 1.1 abs pl = med3(pl, pl + d, pl + 2 * d, cmp);
118 1.1 abs pm = med3(pm - d, pm, pm + d, cmp);
119 1.1 abs pn = med3(pn - 2 * d, pn - d, pn, cmp);
120 1.1 abs }
121 1.1 abs pm = med3(pl, pm, pn, cmp);
122 1.1 abs }
123 1.1 abs swap(a, pm);
124 1.1 abs pa = pb = (char *)a + es;
125 1.1 abs
126 1.1 abs pc = pd = (char *)a + (n - 1) * es;
127 1.1 abs for (;;) {
128 1.1 abs while (pb <= pc && (r = cmp(pb, a)) <= 0) {
129 1.1 abs if (r == 0) {
130 1.1 abs swap_cnt = 1;
131 1.1 abs swap(pa, pb);
132 1.1 abs pa += es;
133 1.1 abs }
134 1.1 abs pb += es;
135 1.1 abs }
136 1.1 abs while (pb <= pc && (r = cmp(pc, a)) >= 0) {
137 1.1 abs if (r == 0) {
138 1.1 abs swap_cnt = 1;
139 1.1 abs swap(pc, pd);
140 1.1 abs pd -= es;
141 1.1 abs }
142 1.1 abs pc -= es;
143 1.1 abs }
144 1.1 abs if (pb > pc)
145 1.1 abs break;
146 1.1 abs swap(pb, pc);
147 1.1 abs swap_cnt = 1;
148 1.1 abs pb += es;
149 1.1 abs pc -= es;
150 1.1 abs }
151 1.1 abs if (swap_cnt == 0) { /* Switch to insertion sort */
152 1.1 abs for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
153 1.1 abs for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0;
154 1.1 abs pl -= es)
155 1.1 abs swap(pl, pl - es);
156 1.1 abs return;
157 1.1 abs }
158 1.1 abs
159 1.1 abs pn = (char *)a + n * es;
160 1.1 abs r = min(pa - (char *)a, pb - pa);
161 1.1 abs vecswap(a, pb - r, r);
162 1.1 abs r = min(pd - pc, pn - pd - es);
163 1.1 abs vecswap(pb, pn - r, r);
164 1.1 abs if ((r = pb - pa) > es)
165 1.1 abs qsort(a, r / es, es, cmp);
166 1.1 abs if ((r = pd - pc) > es) {
167 1.1 abs /* Iterate rather than recurse to save stack space */
168 1.1 abs a = pn - r;
169 1.1 abs n = r / es;
170 1.1 abs goto loop;
171 1.1 abs }
172 1.1 abs /* qsort(pn - r, r / es, es, cmp);*/
173 1.1 abs }
174