]> granicus.if.org Git - postgresql/blob - src/port/qsort.c
Standard pgindent run for 8.1.
[postgresql] / src / port / qsort.c
1 /*
2  *      Modifications from vanilla NetBSD source:
3  *        Add do ... while() macro fix
4  *        Remove __inline, _DIAGASSERTs, __P
5  *
6  *      $PostgreSQL: pgsql/src/port/qsort.c,v 1.8 2005/10/15 02:49:51 momjian Exp $
7  */
8
9 /*      $NetBSD: qsort.c,v 1.13 2003/08/07 16:43:42 agc Exp $   */
10
11 /*-
12  * Copyright (c) 1992, 1993
13  *      The Regents of the University of California.  All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
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. Neither the name of the University nor the names of its contributors
24  *        may be used to endorse or promote products derived from this software
25  *        without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.      IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39
40 #include "c.h"
41
42
43 static char *med3(char *, char *, char *,
44          int (*) (const void *, const void *));
45 static void swapfunc(char *, char *, size_t, int);
46
47 #define min(a, b)       ((a) < (b) ? (a) : (b))
48
49 /*
50  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
51  */
52 #define swapcode(TYPE, parmi, parmj, n) \
53 do {            \
54         size_t i = (n) / sizeof (TYPE);                 \
55         TYPE *pi = (TYPE *)(void *)(parmi);                     \
56         TYPE *pj = (TYPE *)(void *)(parmj);                     \
57         do {                                            \
58                 TYPE    t = *pi;                        \
59                 *pi++ = *pj;                            \
60                 *pj++ = t;                              \
61                 } while (--i > 0);                              \
62 } while (0)
63
64 #define SWAPINIT(a, es) swaptype = ((char *)(a) - (char *)0) % sizeof(long) || \
65         (es) % sizeof(long) ? 2 : (es) == sizeof(long)? 0 : 1;
66
67 static void
68 swapfunc(a, b, n, swaptype)
69 char       *a,
70                    *b;
71 size_t          n;
72 int                     swaptype;
73 {
74         if (swaptype <= 1)
75                 swapcode(long, a, b, n);
76         else
77                 swapcode(char, a, b, n);
78 }
79
80 #define swap(a, b)                                              \
81         if (swaptype == 0) {                                    \
82                 long t = *(long *)(void *)(a);                  \
83                 *(long *)(void *)(a) = *(long *)(void *)(b);    \
84                 *(long *)(void *)(b) = t;                       \
85         } else                                                  \
86                 swapfunc(a, b, es, swaptype)
87
88 #define vecswap(a, b, n) if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
89
90 static char *
91 med3(a, b, c, cmp)
92 char       *a,
93                    *b,
94                    *c;
95 int                     (*cmp) (const void *, const void *);
96 {
97         return cmp(a, b) < 0 ?
98                 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a))
99                 : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c));
100 }
101
102 void
103 qsort(a, n, es, cmp)
104 void       *a;
105 size_t          n,
106                         es;
107 int                     (*cmp) (const void *, const void *);
108 {
109         char       *pa,
110                            *pb,
111                            *pc,
112                            *pd,
113                            *pl,
114                            *pm,
115                            *pn;
116         int                     d,
117                                 r,
118                                 swaptype,
119                                 swap_cnt;
120
121 loop:SWAPINIT(a, es);
122         swap_cnt = 0;
123         if (n < 7)
124         {
125                 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
126                         for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
127                                  pl -= es)
128                                 swap(pl, pl - es);
129                 return;
130         }
131         pm = (char *) a + (n / 2) * es;
132         if (n > 7)
133         {
134                 pl = (char *) a;
135                 pn = (char *) a + (n - 1) * es;
136                 if (n > 40)
137                 {
138                         d = (n / 8) * es;
139                         pl = med3(pl, pl + d, pl + 2 * d, cmp);
140                         pm = med3(pm - d, pm, pm + d, cmp);
141                         pn = med3(pn - 2 * d, pn - d, pn, cmp);
142                 }
143                 pm = med3(pl, pm, pn, cmp);
144         }
145         swap(a, pm);
146         pa = pb = (char *) a + es;
147
148         pc = pd = (char *) a + (n - 1) * es;
149         for (;;)
150         {
151                 while (pb <= pc && (r = cmp(pb, a)) <= 0)
152                 {
153                         if (r == 0)
154                         {
155                                 swap_cnt = 1;
156                                 swap(pa, pb);
157                                 pa += es;
158                         }
159                         pb += es;
160                 }
161                 while (pb <= pc && (r = cmp(pc, a)) >= 0)
162                 {
163                         if (r == 0)
164                         {
165                                 swap_cnt = 1;
166                                 swap(pc, pd);
167                                 pd -= es;
168                         }
169                         pc -= es;
170                 }
171                 if (pb > pc)
172                         break;
173                 swap(pb, pc);
174                 swap_cnt = 1;
175                 pb += es;
176                 pc -= es;
177         }
178         if (swap_cnt == 0)
179         {                                                       /* Switch to insertion sort */
180                 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
181                         for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
182                                  pl -= es)
183                                 swap(pl, pl - es);
184                 return;
185         }
186
187         pn = (char *) a + n * es;
188         r = min(pa - (char *) a, pb - pa);
189         vecswap(a, pb - r, r);
190         r = min(pd - pc, pn - pd - es);
191         vecswap(pb, pn - r, r);
192         if ((r = pb - pa) > es)
193                 qsort(a, r / es, es, cmp);
194         if ((r = pd - pc) > es)
195         {
196                 /* Iterate rather than recurse to save stack space */
197                 a = pn - r;
198                 n = r / es;
199                 goto loop;
200         }
201 /*              qsort(pn - r, r / es, es, cmp);*/
202 }