]>
git.saurik.com Git - redis.git/blob - pqsort.c
8e2b12483be1f7f33ae9883996cf20c2b312b2cb
1 /* The following is the NetBSD libc qsort implementation modified in order to
2 * support partial sorting of ranges for Redis.
4 * Copyright(C) 2009 Salvatore Sanfilippo. All rights reserved.
6 * The original copyright notice follows. */
9 /* $NetBSD: qsort.c,v 1.19 2009/01/30 23:38:44 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. 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.
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
40 #define __P(protos) protos
42 #include <sys/cdefs.h>
43 #if defined(LIBC_SCCS) && !defined(lint)
45 static char sccsid
[] = "@(#)qsort.c 8.1 (Berkeley) 6/4/93";
47 __RCSID("$NetBSD: qsort.c,v 1.19 2009/01/30 23:38:44 lukem Exp $");
49 #endif /* LIBC_SCCS and not lint */
51 #include <sys/types.h>
57 static inline char *med3
__P((char *, char *, char *,
58 int (*)(const void *, const void *)));
59 static inline void swapfunc
__P((char *, char *, size_t, int));
61 #define min(a, b) (a) < (b) ? a : b
64 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
66 #define swapcode(TYPE, parmi, parmj, n) { \
67 size_t i = (n) / sizeof (TYPE); \
68 TYPE *pi = (TYPE *)(void *)(parmi); \
69 TYPE *pj = (TYPE *)(void *)(parmj); \
77 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
78 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
81 swapfunc(char *a
, char *b
, size_t n
, int swaptype
)
85 swapcode(long, a
, b
, n
)
87 swapcode(char, a
, b
, n
)
91 if (swaptype == 0) { \
92 long t = *(long *)(void *)(a); \
93 *(long *)(void *)(a) = *(long *)(void *)(b); \
94 *(long *)(void *)(b) = t; \
96 swapfunc(a, b, es, swaptype)
98 #define vecswap(a, b, n) if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
101 med3(char *a
, char *b
, char *c
,
102 int (*cmp
) __P((const void *, const void *)))
105 return cmp(a
, b
) < 0 ?
106 (cmp(b
, c
) < 0 ? b
: (cmp(a
, c
) < 0 ? c
: a
))
107 :(cmp(b
, c
) > 0 ? b
: (cmp(a
, c
) < 0 ? a
: c
));
111 _pqsort(void *a
, size_t n
, size_t es
,
112 int (*cmp
) __P((const void *, const void *)), void *lrange
, void *rrange
)
114 char *pa
, *pb
, *pc
, *pd
, *pl
, *pm
, *pn
;
116 int swaptype
, swap_cnt
, cmp_result
;
118 loop
: SWAPINIT(a
, es
);
121 for (pm
= (char *) a
+ es
; pm
< (char *) a
+ n
* es
; pm
+= es
)
122 for (pl
= pm
; pl
> (char *) a
&& cmp(pl
- es
, pl
) > 0;
127 pm
= (char *) a
+ (n
/ 2) * es
;
130 pn
= (char *) a
+ (n
- 1) * es
;
133 pl
= med3(pl
, pl
+ d
, pl
+ 2 * d
, cmp
);
134 pm
= med3(pm
- d
, pm
, pm
+ d
, cmp
);
135 pn
= med3(pn
- 2 * d
, pn
- d
, pn
, cmp
);
137 pm
= med3(pl
, pm
, pn
, cmp
);
140 pa
= pb
= (char *) a
+ es
;
142 pc
= pd
= (char *) a
+ (n
- 1) * es
;
144 while (pb
<= pc
&& (cmp_result
= cmp(pb
, a
)) <= 0) {
145 if (cmp_result
== 0) {
152 while (pb
<= pc
&& (cmp_result
= cmp(pc
, a
)) >= 0) {
153 if (cmp_result
== 0) {
167 if (swap_cnt
== 0) { /* Switch to insertion sort */
168 for (pm
= (char *) a
+ es
; pm
< (char *) a
+ n
* es
; pm
+= es
)
169 for (pl
= pm
; pl
> (char *) a
&& cmp(pl
- es
, pl
) > 0;
175 pn
= (char *) a
+ n
* es
;
176 r
= min(pa
- (char *) a
, pb
- pa
);
177 vecswap(a
, pb
- r
, r
);
178 r
= min((size_t)(pd
- pc
), pn
- pd
- es
);
179 vecswap(pb
, pn
- r
, r
);
180 if ((r
= pb
- pa
) > es
) {
181 void *_l
= a
, *_r
= ((unsigned char*)a
)+r
-1;
182 if (!((lrange
< _l
&& rrange
< _l
) ||
183 (lrange
> _r
&& rrange
> _r
)))
184 _pqsort(a
, r
/ es
, es
, cmp
, lrange
, rrange
);
186 if ((r
= pd
- pc
) > es
) {
189 /* Iterate rather than recurse to save stack space */
194 _r
= ((unsigned char*)a
)+r
-1;
195 if (!((lrange
< _l
&& rrange
< _l
) ||
196 (lrange
> _r
&& rrange
> _r
)))
199 /* qsort(pn - r, r / es, es, cmp);*/
203 pqsort(void *a
, size_t n
, size_t es
,
204 int (*cmp
) __P((const void *, const void *)), size_t lrange
, size_t rrange
)
206 _pqsort(a
,n
,es
,cmp
,((unsigned char*)a
)+(lrange
*es
),
207 ((unsigned char*)a
)+((rrange
+1)*es
)-1);