Update radixsort module from NetBSD

Merge some interesting changes.
This commit is contained in:
Guillem Jover 2013-05-25 15:35:39 +02:00
parent 96a2dae352
commit 30349f8922
2 changed files with 47 additions and 48 deletions

View File

@ -1,3 +1,5 @@
.\" $NetBSD: radixsort.3,v 1.12 2003/04/16 13:34:46 wiz Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993 .\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved. .\" The Regents of the University of California. All rights reserved.
.\" .\"
@ -9,7 +11,7 @@
.\" 2. Redistributions in binary form must reproduce the above copyright .\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the .\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution. .\" documentation and/or other materials provided with the distribution.
.\" 4. Neither the name of the University nor the names of its contributors .\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software .\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission. .\" without specific prior written permission.
.\" .\"
@ -25,14 +27,14 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" @(#)radixsort.3 8.2 (Berkeley) 1/27/94 .\" from: @(#)radixsort.3 8.2 (Berkeley) 1/27/94
.\" $FreeBSD$
.\" .\"
.Dd January 27, 1994 .Dd January 27, 1994
.Dt RADIXSORT 3 .Dt RADIXSORT 3
.Os .Os
.Sh NAME .Sh NAME
.Nm radixsort , sradixsort .Nm radixsort ,
.Nm sradixsort
.Nd radix sort .Nd radix sort
.Sh LIBRARY .Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd) .ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
@ -52,12 +54,17 @@ and
functions functions
are implementations of radix sort. are implementations of radix sort.
.Pp .Pp
These functions sort an array of pointers to byte strings, the initial These functions sort an
member of which is referenced by .Fa nmemb
element array of pointers to byte strings, with
the initial member of which is referenced by
.Fa base . .Fa base .
The byte strings may contain any values; the end of each string The byte strings may contain any values.
is denoted by the user-specified value End of strings is denoted
by character which has same weight as user specified value
.Fa endbyte . .Fa endbyte .
.Fa endbyte
has to be between 0 and 255.
.Pp .Pp
Applications may specify a sort order by providing the Applications may specify a sort order by providing the
.Fa table .Fa table

View File

@ -1,3 +1,4 @@
/* $NetBSD: radixsort.c,v 1.18 2009/08/21 20:49:50 dsl Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -13,7 +14,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@ -30,11 +31,14 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); #if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: radixsort.c,v 1.18 2009/08/21 20:49:50 dsl Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
/* /*
* Radixsort routines. * Radixsort routines.
@ -59,11 +63,10 @@ typedef struct {
int sn, si; int sn, si;
} stack; } stack;
static inline void simplesort static inline void simplesort(const u_char **, int, int, const u_char *, u_int);
(const u_char **, int, int, const u_char *, u_int);
static void r_sort_a(const u_char **, int, int, const u_char *, u_int); static void r_sort_a(const u_char **, int, int, const u_char *, u_int);
static void r_sort_b(const u_char **, const u_char **, int, int, static void r_sort_b(const u_char **,
const u_char *, u_int); const u_char **, int, int, const u_char *, u_int);
#define THRESHOLD 20 /* Divert to simplesort(). */ #define THRESHOLD 20 /* Divert to simplesort(). */
#define SIZE 512 /* Default stack size. */ #define SIZE 512 /* Default stack size. */
@ -88,13 +91,10 @@ static void r_sort_b(const u_char **, const u_char **, int, int,
} }
int int
radixsort(a, n, tab, endch) radixsort(const u_char **a, int n, const u_char *tab, u_int endch)
const u_char **a, *tab;
int n;
u_int endch;
{ {
const u_char *tr; const u_char *tr;
int c; u_int c;
u_char tr0[256]; u_char tr0[256];
SETUP; SETUP;
@ -103,15 +103,17 @@ radixsort(a, n, tab, endch)
} }
int int
sradixsort(a, n, tab, endch) sradixsort(const u_char **a, int n, const u_char *tab, u_int endch)
const u_char **a, *tab;
int n;
u_int endch;
{ {
const u_char *tr, **ta; const u_char *tr, **ta;
int c; u_int c;
u_char tr0[256]; u_char tr0[256];
if (a == NULL) {
errno = EFAULT;
return (-1);
}
SETUP; SETUP;
if (n < THRESHOLD) if (n < THRESHOLD)
simplesort(a, n, 0, tr, endch); simplesort(a, n, 0, tr, endch);
@ -131,17 +133,13 @@ sradixsort(a, n, tab, endch)
/* Unstable, in-place sort. */ /* Unstable, in-place sort. */
static void static void
r_sort_a(a, n, i, tr, endch) r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch)
const u_char **a;
int n, i;
const u_char *tr;
u_int endch;
{ {
static int count[256], nc, bmin; static u_int count[256], nc, bmin;
int c; u_int c;
const u_char **ak, *r; const u_char **ak, *r;
stack s[SIZE], *sp, *sp0, *sp1, temp; stack s[SIZE], *sp, *sp0, *sp1, temp;
int *cp, bigc; u_int *cp, bigc;
const u_char **an, *t, **aj, **top[256]; const u_char **an, *t, **aj, **top[256];
/* Set up stack. */ /* Set up stack. */
@ -233,18 +231,15 @@ r_sort_a(a, n, i, tr, endch)
/* Stable sort, requiring additional memory. */ /* Stable sort, requiring additional memory. */
static void static void
r_sort_b(a, ta, n, i, tr, endch) r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr,
const u_char **a, **ta; u_int endch)
int n, i;
const u_char *tr;
u_int endch;
{ {
static int count[256], nc, bmin; static u_int count[256], nc, bmin;
int c; u_int c;
const u_char **ak, **ai; const u_char **ak, **ai;
stack s[512], *sp, *sp0, *sp1, temp; stack s[512], *sp, *sp0, *sp1, temp;
const u_char **top[256]; const u_char **top[256];
int *cp, bigc; u_int *cp, bigc;
sp = s; sp = s;
push(a, n, i); push(a, n, i);
@ -304,12 +299,9 @@ r_sort_b(a, ta, n, i, tr, endch)
} }
} }
/* insertion sort */
static inline void static inline void
simplesort(a, n, b, tr, endch) /* insertion sort */ simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch)
const u_char **a;
int n, b;
const u_char *tr;
u_int endch;
{ {
u_char ch; u_char ch;
const u_char **ak, **ai, *s, *t; const u_char **ak, **ai, *s, *t;