diff options
| author | Garrett Wollman <wollman@FreeBSD.org> | 2002-09-10 02:04:49 +0000 | 
|---|---|---|
| committer | Garrett Wollman <wollman@FreeBSD.org> | 2002-09-10 02:04:49 +0000 | 
| commit | eca67d510483ad9565b7b816e16fc72ebe3b2864 (patch) | |
| tree | a88678f5332186a1bd2a23ff99d0b5dbfd9fed66 /lib/libc/stdlib/qsort.c | |
| parent | 0855f655f80cd9c90e3e60f0a5fb011e1af6a995 (diff) | |
Notes
Diffstat (limited to 'lib/libc/stdlib/qsort.c')
| -rw-r--r-- | lib/libc/stdlib/qsort.c | 59 | 
1 files changed, 40 insertions, 19 deletions
| diff --git a/lib/libc/stdlib/qsort.c b/lib/libc/stdlib/qsort.c index 8e6cd619c25c..47e563ad703e 100644 --- a/lib/libc/stdlib/qsort.c +++ b/lib/libc/stdlib/qsort.c @@ -39,8 +39,12 @@ __FBSDID("$FreeBSD$");  #include <stdlib.h> +#ifdef I_AM_QSORT_R +typedef int		 cmp_t(void *, const void *, const void *); +#else  typedef int		 cmp_t(const void *, const void *); -static inline char	*med3(char *, char *, char *, cmp_t *); +#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 @@ -83,21 +87,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_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp) +#else +#define thunk NULL  void -qsort(a, n, es, cmp) -	void *a; -	size_t n, es; -	cmp_t *cmp; +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; @@ -106,7 +121,8 @@ 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; @@ -117,18 +133,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); @@ -136,7 +152,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); @@ -153,7 +169,8 @@ 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; @@ -165,7 +182,11 @@ 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; | 
