qsort.c revision 1.5 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.5 christos * $NetBSD: qsort.c,v 1.5 2019/03/31 20:08:45 christos 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.5 christos #define min(a, b) (int)(a) < (int)(b) ? (int)(a) : (int)(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.4 isaki register TYPE *pi = (TYPE *)(parmi); \
54 1.4 isaki 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.4 isaki } 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.4 isaki swapfunc(char *a, char *b, int n, int swaptype)
67 1.1 abs {
68 1.1 abs if(swaptype <= 1)
69 1.1 abs swapcode(long, a, b, n)
70 1.1 abs else
71 1.1 abs swapcode(char, a, b, n)
72 1.1 abs }
73 1.1 abs
74 1.1 abs #define swap(a, b) \
75 1.1 abs if (swaptype == 0) { \
76 1.1 abs long t = *(long *)(a); \
77 1.1 abs *(long *)(a) = *(long *)(b); \
78 1.1 abs *(long *)(b) = t; \
79 1.1 abs } else \
80 1.1 abs swapfunc(a, b, es, swaptype)
81 1.1 abs
82 1.1 abs #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
83 1.1 abs
84 1.3 perry static inline char *
85 1.4 isaki med3(char *a, char *b, char *c, cmp_t *cmp)
86 1.1 abs {
87 1.1 abs return cmp(a, b) < 0 ?
88 1.1 abs (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
89 1.4 isaki :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
90 1.1 abs }
91 1.1 abs
92 1.1 abs void
93 1.1 abs qsort(void *a, size_t n, size_t es, cmp_t *cmp)
94 1.1 abs {
95 1.1 abs char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
96 1.1 abs int d, r, swaptype, swap_cnt;
97 1.1 abs
98 1.4 isaki loop:
99 1.4 isaki SWAPINIT(a, es);
100 1.1 abs swap_cnt = 0;
101 1.1 abs if (n < 7) {
102 1.1 abs for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
103 1.1 abs for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0;
104 1.1 abs pl -= es)
105 1.1 abs swap(pl, pl - es);
106 1.1 abs return;
107 1.1 abs }
108 1.1 abs pm = (char *)a + (n / 2) * es;
109 1.1 abs if (n > 7) {
110 1.1 abs pl = a;
111 1.1 abs pn = (char *)a + (n - 1) * es;
112 1.1 abs if (n > 40) {
113 1.1 abs d = (n / 8) * es;
114 1.1 abs pl = med3(pl, pl + d, pl + 2 * d, cmp);
115 1.1 abs pm = med3(pm - d, pm, pm + d, cmp);
116 1.1 abs pn = med3(pn - 2 * d, pn - d, pn, cmp);
117 1.1 abs }
118 1.1 abs pm = med3(pl, pm, pn, cmp);
119 1.1 abs }
120 1.1 abs swap(a, pm);
121 1.1 abs pa = pb = (char *)a + es;
122 1.1 abs
123 1.1 abs pc = pd = (char *)a + (n - 1) * es;
124 1.1 abs for (;;) {
125 1.1 abs while (pb <= pc && (r = cmp(pb, a)) <= 0) {
126 1.1 abs if (r == 0) {
127 1.1 abs swap_cnt = 1;
128 1.1 abs swap(pa, pb);
129 1.1 abs pa += es;
130 1.1 abs }
131 1.1 abs pb += es;
132 1.1 abs }
133 1.1 abs while (pb <= pc && (r = cmp(pc, a)) >= 0) {
134 1.1 abs if (r == 0) {
135 1.1 abs swap_cnt = 1;
136 1.1 abs swap(pc, pd);
137 1.1 abs pd -= es;
138 1.1 abs }
139 1.1 abs pc -= es;
140 1.1 abs }
141 1.1 abs if (pb > pc)
142 1.1 abs break;
143 1.1 abs swap(pb, pc);
144 1.1 abs swap_cnt = 1;
145 1.1 abs pb += es;
146 1.1 abs pc -= es;
147 1.1 abs }
148 1.1 abs if (swap_cnt == 0) { /* Switch to insertion sort */
149 1.1 abs for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
150 1.1 abs for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0;
151 1.1 abs pl -= es)
152 1.1 abs swap(pl, pl - es);
153 1.1 abs return;
154 1.1 abs }
155 1.1 abs
156 1.1 abs pn = (char *)a + n * es;
157 1.1 abs r = min(pa - (char *)a, pb - pa);
158 1.1 abs vecswap(a, pb - r, r);
159 1.1 abs r = min(pd - pc, pn - pd - es);
160 1.1 abs vecswap(pb, pn - r, r);
161 1.5 christos if ((size_t)(r = pb - pa) > es)
162 1.1 abs qsort(a, r / es, es, cmp);
163 1.5 christos if ((size_t)(r = pd - pc) > es) {
164 1.1 abs /* Iterate rather than recurse to save stack space */
165 1.1 abs a = pn - r;
166 1.1 abs n = r / es;
167 1.1 abs goto loop;
168 1.1 abs }
169 1.1 abs /* qsort(pn - r, r / es, es, cmp);*/
170 1.1 abs }
171