Copy qsort_r(3) from libc to libkern.

Reviewed by:	phk
Approved by:	julian (mentor)
This commit is contained in:
Gleb Smirnoff 2004-07-15 23:58:23 +00:00
parent af341d82c4
commit b1e34c44ef
4 changed files with 52 additions and 24 deletions

View File

@ -1208,6 +1208,7 @@ libkern/index.c standard
libkern/inet_ntoa.c standard libkern/inet_ntoa.c standard
libkern/mcount.c optional profiling-routine libkern/mcount.c optional profiling-routine
libkern/qsort.c standard libkern/qsort.c standard
libkern/qsort_r.c standard
libkern/fnmatch.c standard libkern/fnmatch.c standard
libkern/random.c standard libkern/random.c standard
libkern/rindex.c standard libkern/rindex.c standard

View File

@ -30,10 +30,15 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/libkern.h> #include <sys/libkern.h>
typedef int cmp_t(const void *, const void *); #ifdef I_AM_QSORT_R
static __inline char *med3(char *, char *, char *, cmp_t *); typedef int cmp_t(void *, const void *, const void *);
#else
typedef int cmp_t(const void *, const void *);
#endif
static __inline char *med3(char *, char *, char *, cmp_t *, void *);
static __inline void swapfunc(char *, char *, int, int); static __inline void swapfunc(char *, char *, int, int);
#define min(a, b) (a) < (b) ? (a) : (b) #define min(a, b) (a) < (b) ? (a) : (b)
@ -56,9 +61,7 @@ static __inline void swapfunc(char *, char *, int, int);
es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
static __inline void static __inline void
swapfunc(a, b, n, swaptype) swapfunc(char *a, char *b, int n, int swaptype)
char *a, *b;
int n, swaptype;
{ {
if(swaptype <= 1) if(swaptype <= 1)
swapcode(long, a, b, n) swapcode(long, a, b, n)
@ -76,21 +79,32 @@ swapfunc(a, b, n, swaptype)
#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
#ifdef I_AM_QSORT_R
#define CMP(t, x, y) (cmp((t), (x), (y)))
#else
#define CMP(t, x, y) (cmp((x), (y)))
#endif
static __inline char * static __inline char *
med3(a, b, c, cmp) med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
char *a, *b, *c; #ifndef I_AM_QSORT_R
cmp_t *cmp; __unused
#endif
)
{ {
return cmp(a, b) < 0 ? return CMP(thunk, a, b) < 0 ?
(cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
:(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
} }
#ifdef I_AM_QSORT_R
void void
qsort(a, n, es, cmp) qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
void *a; #else
size_t n, es; #define thunk NULL
cmp_t *cmp; void
qsort(void *a, size_t n, size_t es, cmp_t *cmp)
#endif
{ {
char *pa, *pb, *pc, *pd, *pl, *pm, *pn; char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
int d, r, swaptype, swap_cnt; int d, r, swaptype, swap_cnt;
@ -99,7 +113,7 @@ loop: SWAPINIT(a, es);
swap_cnt = 0; swap_cnt = 0;
if (n < 7) { if (n < 7) {
for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0; for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
pl -= es) pl -= es)
swap(pl, pl - es); swap(pl, pl - es);
return; return;
@ -110,18 +124,18 @@ loop: SWAPINIT(a, es);
pn = (char *)a + (n - 1) * es; pn = (char *)a + (n - 1) * es;
if (n > 40) { if (n > 40) {
d = (n / 8) * es; d = (n / 8) * es;
pl = med3(pl, pl + d, pl + 2 * d, cmp); pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
pm = med3(pm - d, pm, pm + d, cmp); pm = med3(pm - d, pm, pm + d, cmp, thunk);
pn = med3(pn - 2 * d, pn - d, pn, cmp); pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
} }
pm = med3(pl, pm, pn, cmp); pm = med3(pl, pm, pn, cmp, thunk);
} }
swap(a, pm); swap(a, pm);
pa = pb = (char *)a + es; pa = pb = (char *)a + es;
pc = pd = (char *)a + (n - 1) * es; pc = pd = (char *)a + (n - 1) * es;
for (;;) { for (;;) {
while (pb <= pc && (r = cmp(pb, a)) <= 0) { while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) {
if (r == 0) { if (r == 0) {
swap_cnt = 1; swap_cnt = 1;
swap(pa, pb); swap(pa, pb);
@ -129,7 +143,7 @@ loop: SWAPINIT(a, es);
} }
pb += es; pb += es;
} }
while (pb <= pc && (r = cmp(pc, a)) >= 0) { while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) {
if (r == 0) { if (r == 0) {
swap_cnt = 1; swap_cnt = 1;
swap(pc, pd); swap(pc, pd);
@ -146,7 +160,7 @@ loop: SWAPINIT(a, es);
} }
if (swap_cnt == 0) { /* Switch to insertion sort */ if (swap_cnt == 0) { /* Switch to insertion sort */
for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0; for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
pl -= es) pl -= es)
swap(pl, pl - es); swap(pl, pl - es);
return; return;
@ -158,12 +172,15 @@ loop: SWAPINIT(a, es);
r = min(pd - pc, pn - pd - es); r = min(pd - pc, pn - pd - es);
vecswap(pb, pn - r, r); vecswap(pb, pn - r, r);
if ((r = pb - pa) > es) if ((r = pb - pa) > es)
#ifdef I_AM_QSORT_R
qsort_r(a, r / es, es, thunk, cmp);
#else
qsort(a, r / es, es, cmp); qsort(a, r / es, es, cmp);
#endif
if ((r = pd - pc) > es) { if ((r = pd - pc) > es) {
/* Iterate rather than recurse to save stack space */ /* Iterate rather than recurse to save stack space */
a = pn - r; a = pn - r;
n = r / es; n = r / es;
goto loop; goto loop;
} }
/* qsort(pn - r, r / es, es, cmp);*/
} }

8
sys/libkern/qsort_r.c Normal file
View File

@ -0,0 +1,8 @@
/*
* This file is in the public domain. Originally written by Garrett
* A. Wollman.
*
* $FreeBSD$
*/
#define I_AM_QSORT_R
#include "libkern/qsort.c"

View File

@ -86,6 +86,8 @@ int fnmatch(const char *, const char *, int);
int locc(int, char *, u_int); int locc(int, char *, u_int);
void qsort(void *base, size_t nmemb, size_t size, void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *)); int (*compar)(const void *, const void *));
void qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
int (*compar)(void *, const void *, const void *));
u_long random(void); u_long random(void);
char *index(const char *, int); char *index(const char *, int);
char *rindex(const char *, int); char *rindex(const char *, int);