]> git.saurik.com Git - redis.git/blob - pqsort.c
59dedcbc3b4d59115db2a89d6621e86c3b11160d
[redis.git] / pqsort.c
1 /* The following is the NetBSD libc qsort implementation modified in order to
2 * support partial sorting of ranges for Redis.
3 *
4 * Copyright(C) 2009 Salvatore Sanfilippo. All rights reserved.
5 *
6 * The original copyright notice follows. */
7
8
9 /* $NetBSD: qsort.c,v 1.19 2009/01/30 23:38:44 lukem 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 #ifndef __P
41 #define __P(protos) protos
42 #endif
43
44 #include <sys/cdefs.h>
45 #if defined(LIBC_SCCS) && !defined(lint)
46 #if 0
47 static char sccsid[] = "@(#)qsort.c 8.1 (Berkeley) 6/4/93";
48 #else
49 __RCSID("$NetBSD: qsort.c,v 1.19 2009/01/30 23:38:44 lukem Exp $");
50 #endif
51 #endif /* LIBC_SCCS and not lint */
52
53 #include <sys/types.h>
54
55 #include <assert.h>
56 #include <errno.h>
57 #include <stdlib.h>
58
59 static inline char *med3 __P((char *, char *, char *,
60 int (*)(const void *, const void *)));
61 static inline void swapfunc __P((char *, char *, size_t, int));
62
63 #define min(a, b) (a) < (b) ? a : b
64
65 /*
66 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
67 */
68 #define swapcode(TYPE, parmi, parmj, n) { \
69 size_t i = (n) / sizeof (TYPE); \
70 TYPE *pi = (TYPE *)(void *)(parmi); \
71 TYPE *pj = (TYPE *)(void *)(parmj); \
72 do { \
73 TYPE t = *pi; \
74 *pi++ = *pj; \
75 *pj++ = t; \
76 } while (--i > 0); \
77 }
78
79 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
80 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
81
82 static inline void
83 swapfunc(char *a, char *b, size_t n, int swaptype)
84 {
85
86 if (swaptype <= 1)
87 swapcode(long, a, b, n)
88 else
89 swapcode(char, a, b, n)
90 }
91
92 #define swap(a, b) \
93 if (swaptype == 0) { \
94 long t = *(long *)(void *)(a); \
95 *(long *)(void *)(a) = *(long *)(void *)(b); \
96 *(long *)(void *)(b) = t; \
97 } else \
98 swapfunc(a, b, es, swaptype)
99
100 #define vecswap(a, b, n) if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
101
102 static inline char *
103 med3(char *a, char *b, char *c,
104 int (*cmp) __P((const void *, const void *)))
105 {
106
107 return cmp(a, b) < 0 ?
108 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
109 :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
110 }
111
112 static void
113 _pqsort(void *a, size_t n, size_t es,
114 int (*cmp) __P((const void *, const void *)), void *lrange, void *rrange)
115 {
116 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
117 size_t d, r;
118 int swaptype, swap_cnt, cmp_result;
119
120 loop: SWAPINIT(a, es);
121 swap_cnt = 0;
122 if (n < 7) {
123 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
124 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
125 pl -= es)
126 swap(pl, pl - es);
127 return;
128 }
129 pm = (char *) a + (n / 2) * es;
130 if (n > 7) {
131 pl = (char *) a;
132 pn = (char *) a + (n - 1) * es;
133 if (n > 40) {
134 d = (n / 8) * es;
135 pl = med3(pl, pl + d, pl + 2 * d, cmp);
136 pm = med3(pm - d, pm, pm + d, cmp);
137 pn = med3(pn - 2 * d, pn - d, pn, cmp);
138 }
139 pm = med3(pl, pm, pn, cmp);
140 }
141 swap(a, pm);
142 pa = pb = (char *) a + es;
143
144 pc = pd = (char *) a + (n - 1) * es;
145 for (;;) {
146 while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
147 if (cmp_result == 0) {
148 swap_cnt = 1;
149 swap(pa, pb);
150 pa += es;
151 }
152 pb += es;
153 }
154 while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
155 if (cmp_result == 0) {
156 swap_cnt = 1;
157 swap(pc, pd);
158 pd -= es;
159 }
160 pc -= es;
161 }
162 if (pb > pc)
163 break;
164 swap(pb, pc);
165 swap_cnt = 1;
166 pb += es;
167 pc -= es;
168 }
169 if (swap_cnt == 0) { /* Switch to insertion sort */
170 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
171 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
172 pl -= es)
173 swap(pl, pl - es);
174 return;
175 }
176
177 pn = (char *) a + n * es;
178 r = min(pa - (char *) a, pb - pa);
179 vecswap(a, pb - r, r);
180 r = min((size_t)(pd - pc), pn - pd - es);
181 vecswap(pb, pn - r, r);
182 if ((r = pb - pa) > es) {
183 void *_l = a, *_r = ((unsigned char*)a)+r-1;
184 if (!((lrange < _l && rrange < _l) ||
185 (lrange > _r && rrange > _r)))
186 _pqsort(a, r / es, es, cmp, lrange, rrange);
187 }
188 if ((r = pd - pc) > es) {
189 void *_l, *_r;
190
191 /* Iterate rather than recurse to save stack space */
192 a = pn - r;
193 n = r / es;
194
195 _l = a;
196 _r = ((unsigned char*)a)+r-1;
197 if (!((lrange < _l && rrange < _l) ||
198 (lrange > _r && rrange > _r)))
199 goto loop;
200 }
201 /* qsort(pn - r, r / es, es, cmp);*/
202 }
203
204 void
205 pqsort(void *a, size_t n, size_t es,
206 int (*cmp) __P((const void *, const void *)), size_t lrange, size_t rrange)
207 {
208 _pqsort(a,n,es,cmp,((unsigned char*)a)+(lrange*es),
209 ((unsigned char*)a)+((rrange+1)*es)-1);
210 }