2 * Copied from NetBSD CVS, 2002-07-19, bjm
3 * Add do ... while() macro fix
4 * Remove __inline, _DIAGASSERTs, __P
6 * $PostgreSQL: pgsql/src/port/qsort.c,v 1.4 2003/11/29 19:52:13 pgsql Exp $
9 /* $NetBSD: qsort.c,v 1.12 1999/09/20 04:39:40 lukem Exp $ */
12 * Copyright (c) 1992, 1993
13 * The Regents of the University of California. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 #include <sys/types.h>
49 static char *med3(char *, char *, char *,
50 int (*) (const void *, const void *));
51 static void swapfunc(char *, char *, size_t, int);
53 #define min(a, b) ((a) < (b) ? (a) : (b))
56 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
58 #define swapcode(TYPE, parmi, parmj, n) \
60 size_t i = (n) / sizeof (TYPE); \
61 TYPE *pi = (TYPE *)(void *)(parmi); \
62 TYPE *pj = (TYPE *)(void *)(parmj); \
70 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
71 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
74 swapfunc(a, b, n, swaptype)
81 swapcode(long, a, b, n);
83 swapcode(char, a, b, n);
87 if (swaptype == 0) { \
88 long t = *(long *)(void *)(a); \
89 *(long *)(void *)(a) = *(long *)(void *)(b); \
90 *(long *)(void *)(b) = t; \
92 swapfunc(a, b, es, swaptype)
94 #define vecswap(a, b, n) if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
101 int (*cmp) (const void *, const void *);
103 return cmp(a, b) < 0 ?
104 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a))
105 : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c));
113 int (*cmp) (const void *, const void *);
127 loop:SWAPINIT(a, es);
131 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
132 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
137 pm = (char *) a + (n / 2) * es;
141 pn = (char *) a + (n - 1) * es;
145 pl = med3(pl, pl + d, pl + 2 * d, cmp);
146 pm = med3(pm - d, pm, pm + d, cmp);
147 pn = med3(pn - 2 * d, pn - d, pn, cmp);
149 pm = med3(pl, pm, pn, cmp);
152 pa = pb = (char *) a + es;
154 pc = pd = (char *) a + (n - 1) * es;
157 while (pb <= pc && (r = cmp(pb, a)) <= 0)
167 while (pb <= pc && (r = cmp(pc, a)) >= 0)
185 { /* Switch to insertion sort */
186 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
187 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
193 pn = (char *) a + n * es;
194 r = min(pa - (char *) a, pb - pa);
195 vecswap(a, pb - r, r);
196 r = min(pd - pc, pn - pd - es);
197 vecswap(pb, pn - r, r);
198 if ((r = pb - pa) > es)
199 qsort(a, r / es, es, cmp);
200 if ((r = pd - pc) > es)
202 /* Iterate rather than recurse to save stack space */
207 /* qsort(pn - r, r / es, es, cmp);*/