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
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=132228
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/mcount.c optional profiling-routine
libkern/qsort.c standard
libkern/qsort_r.c standard
libkern/fnmatch.c standard
libkern/random.c standard
libkern/rindex.c standard

View File

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