]> granicus.if.org Git - postgresql/blob - src/port/qsort_arg.c
Switch over to using our own qsort() all the time, as has been proposed
[postgresql] / src / port / qsort_arg.c
1 /*
2  *      qsort_arg.c: qsort with a passthrough "void *" argument
3  *
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.
9  *
10  *      CAUTION: if you change this file, see also qsort.c
11  *
12  *      $PostgreSQL: pgsql/src/port/qsort_arg.c,v 1.1 2006/10/03 22:18:23 tgl Exp $
13  */
14
15 /*      $NetBSD: qsort.c,v 1.13 2003/08/07 16:43:42 agc Exp $   */
16
17 /*-
18  * Copyright (c) 1992, 1993
19  *      The Regents of the University of California.  All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
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.
32  *
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
43  * SUCH DAMAGE.
44  */
45
46 #include "c.h"
47
48
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);
52
53 #define min(a, b)       ((a) < (b) ? (a) : (b))
54
55 /*
56  * Qsort routine based on J. L. Bentley and M. D. McIlroy,
57  * "Engineering a sort function",
58  * Software--Practice and Experience 23 (1993) 1249-1265.
59  * We have modified their original by adding a check for already-sorted input,
60  * which seems to be a win per discussions on pgsql-hackers around 2006-03-21.
61  */
62 #define swapcode(TYPE, parmi, parmj, n) \
63 do {            \
64         size_t i = (n) / sizeof (TYPE);                 \
65         TYPE *pi = (TYPE *)(void *)(parmi);                     \
66         TYPE *pj = (TYPE *)(void *)(parmj);                     \
67         do {                                            \
68                 TYPE    t = *pi;                        \
69                 *pi++ = *pj;                            \
70                 *pj++ = t;                              \
71                 } while (--i > 0);                              \
72 } while (0)
73
74 #define SWAPINIT(a, es) swaptype = ((char *)(a) - (char *)0) % sizeof(long) || \
75         (es) % sizeof(long) ? 2 : (es) == sizeof(long)? 0 : 1;
76
77 static void
78 swapfunc(a, b, n, swaptype)
79 char       *a,
80                    *b;
81 size_t          n;
82 int                     swaptype;
83 {
84         if (swaptype <= 1)
85                 swapcode(long, a, b, n);
86         else
87                 swapcode(char, a, b, n);
88 }
89
90 #define swap(a, b)                                              \
91         if (swaptype == 0) {                                    \
92                 long t = *(long *)(void *)(a);                  \
93                 *(long *)(void *)(a) = *(long *)(void *)(b);    \
94                 *(long *)(void *)(b) = t;                       \
95         } else                                                  \
96                 swapfunc(a, b, es, swaptype)
97
98 #define vecswap(a, b, n) if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
99
100 static char *
101 med3(char *a, char *b, char *c, qsort_arg_comparator cmp, void *arg)
102 {
103         return cmp(a, b, arg) < 0 ?
104                 (cmp(b, c, arg) < 0 ? b : (cmp(a, c, arg) < 0 ? c : a))
105                 : (cmp(b, c, arg) > 0 ? b : (cmp(a, c, arg) < 0 ? a : c));
106 }
107
108 void
109 qsort_arg(void *a, size_t n, size_t es, qsort_arg_comparator cmp, void *arg)
110 {
111         char       *pa,
112                            *pb,
113                            *pc,
114                            *pd,
115                            *pl,
116                            *pm,
117                            *pn;
118         int                     d,
119                                 r,
120                                 swaptype,
121                                 presorted;
122
123 loop:SWAPINIT(a, es);
124         if (n < 7)
125         {
126                 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
127                         for (pl = pm; pl > (char *) a && cmp(pl - es, pl, arg) > 0;
128                                  pl -= es)
129                                 swap(pl, pl - es);
130                 return;
131         }
132         presorted = 1;
133         for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
134         {
135                 if (cmp(pm - es, pm, arg) > 0)
136                 {
137                         presorted = 0;
138                         break;
139                 }
140         }
141         if (presorted)
142                 return;
143         pm = (char *) a + (n / 2) * es;
144         if (n > 7)
145         {
146                 pl = (char *) a;
147                 pn = (char *) a + (n - 1) * es;
148                 if (n > 40)
149                 {
150                         d = (n / 8) * es;
151                         pl = med3(pl, pl + d, pl + 2 * d, cmp, arg);
152                         pm = med3(pm - d, pm, pm + d, cmp, arg);
153                         pn = med3(pn - 2 * d, pn - d, pn, cmp, arg);
154                 }
155                 pm = med3(pl, pm, pn, cmp, arg);
156         }
157         swap(a, pm);
158         pa = pb = (char *) a + es;
159         pc = pd = (char *) a + (n - 1) * es;
160         for (;;)
161         {
162                 while (pb <= pc && (r = cmp(pb, a, arg)) <= 0)
163                 {
164                         if (r == 0)
165                         {
166                                 swap(pa, pb);
167                                 pa += es;
168                         }
169                         pb += es;
170                 }
171                 while (pb <= pc && (r = cmp(pc, a, arg)) >= 0)
172                 {
173                         if (r == 0)
174                         {
175                                 swap(pc, pd);
176                                 pd -= es;
177                         }
178                         pc -= es;
179                 }
180                 if (pb > pc)
181                         break;
182                 swap(pb, pc);
183                 pb += es;
184                 pc -= es;
185         }
186         pn = (char *) a + n * es;
187         r = min(pa - (char *) a, pb - pa);
188         vecswap(a, pb - r, r);
189         r = min(pd - pc, pn - pd - es);
190         vecswap(pb, pn - r, r);
191         if ((r = pb - pa) > es)
192                 qsort_arg(a, r / es, es, cmp, arg);
193         if ((r = pd - pc) > es)
194         {
195                 /* Iterate rather than recurse to save stack space */
196                 a = pn - r;
197                 n = r / es;
198                 goto loop;
199         }
200 /*              qsort_arg(pn - r, r / es, es, cmp, arg);*/
201 }