2 * qsort_arg.c: qsort with a passthrough "void *" argument
4 * Modifications from vanilla NetBSD source:
5 * Add do ... while() macro fix
6 * Remove __inline, _DIAGASSERTs, __P
7 * Remove ill-considered "swap_cnt" switch to insertion sort,
8 * in favor of a simple check for presorted input.
10 * CAUTION: if you change this file, see also qsort.c
12 * $PostgreSQL: pgsql/src/port/qsort_arg.c,v 1.3 2006/10/12 15:04:55 tgl Exp $
15 /* $NetBSD: qsort.c,v 1.13 2003/08/07 16:43:42 agc Exp $ */
18 * Copyright (c) 1992, 1993
19 * The Regents of the University of California. All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 static char *med3(char *a, char *b, char *c,
50 qsort_arg_comparator cmp, void *arg);
51 static void swapfunc(char *, char *, size_t, int);
54 * Qsort routine based on J. L. Bentley and M. D. McIlroy,
55 * "Engineering a sort function",
56 * Software--Practice and Experience 23 (1993) 1249-1265.
57 * We have modified their original by adding a check for already-sorted input,
58 * which seems to be a win per discussions on pgsql-hackers around 2006-03-21.
60 #define swapcode(TYPE, parmi, parmj, n) \
62 size_t i = (n) / sizeof (TYPE); \
63 TYPE *pi = (TYPE *)(void *)(parmi); \
64 TYPE *pj = (TYPE *)(void *)(parmj); \
72 #define SWAPINIT(a, es) swaptype = ((char *)(a) - (char *)0) % sizeof(long) || \
73 (es) % sizeof(long) ? 2 : (es) == sizeof(long)? 0 : 1;
76 swapfunc(a, b, n, swaptype)
83 swapcode(long, a, b, n);
85 swapcode(char, a, b, n);
89 if (swaptype == 0) { \
90 long t = *(long *)(void *)(a); \
91 *(long *)(void *)(a) = *(long *)(void *)(b); \
92 *(long *)(void *)(b) = t; \
94 swapfunc(a, b, es, swaptype)
96 #define vecswap(a, b, n) if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
99 med3(char *a, char *b, char *c, qsort_arg_comparator cmp, void *arg)
101 return cmp(a, b, arg) < 0 ?
102 (cmp(b, c, arg) < 0 ? b : (cmp(a, c, arg) < 0 ? c : a))
103 : (cmp(b, c, arg) > 0 ? b : (cmp(a, c, arg) < 0 ? a : c));
107 qsort_arg(void *a, size_t n, size_t es, qsort_arg_comparator cmp, void *arg)
121 loop:SWAPINIT(a, es);
124 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
125 for (pl = pm; pl > (char *) a && cmp(pl - es, pl, arg) > 0;
131 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
133 if (cmp(pm - es, pm, arg) > 0)
141 pm = (char *) a + (n / 2) * es;
145 pn = (char *) a + (n - 1) * es;
149 pl = med3(pl, pl + d, pl + 2 * d, cmp, arg);
150 pm = med3(pm - d, pm, pm + d, cmp, arg);
151 pn = med3(pn - 2 * d, pn - d, pn, cmp, arg);
153 pm = med3(pl, pm, pn, cmp, arg);
156 pa = pb = (char *) a + es;
157 pc = pd = (char *) a + (n - 1) * es;
160 while (pb <= pc && (r = cmp(pb, a, arg)) <= 0)
169 while (pb <= pc && (r = cmp(pc, a, arg)) >= 0)
184 pn = (char *) a + n * es;
185 r = Min(pa - (char *) a, pb - pa);
186 vecswap(a, pb - r, r);
187 r = Min(pd - pc, pn - pd - es);
188 vecswap(pb, pn - r, r);
189 if ((r = pb - pa) > es)
190 qsort_arg(a, r / es, es, cmp, arg);
191 if ((r = pd - pc) > es)
193 /* Iterate rather than recurse to save stack space */
198 /* qsort_arg(pn - r, r / es, es, cmp, arg);*/