Update radixsort module from NetBSD Merge some interesting changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
diff --git a/man/radixsort.3 b/man/radixsort.3
index 3884124..069e401 100644
--- a/man/radixsort.3
+++ b/man/radixsort.3
@@ -1,3 +1,5 @@
+.\" $NetBSD: radixsort.3,v 1.12 2003/04/16 13:34:46 wiz Exp $
+.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
@@ -9,7 +11,7 @@
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" 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
.\" without specific prior written permission.
.\"
@@ -25,14 +27,14 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" @(#)radixsort.3 8.2 (Berkeley) 1/27/94
-.\" $FreeBSD$
+.\" from: @(#)radixsort.3 8.2 (Berkeley) 1/27/94
.\"
.Dd January 27, 1994
.Dt RADIXSORT 3
.Os
.Sh NAME
-.Nm radixsort , sradixsort
+.Nm radixsort ,
+.Nm sradixsort
.Nd radix sort
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
@@ -52,12 +54,17 @@ and
functions
are implementations of radix sort.
.Pp
-These functions sort an array of pointers to byte strings, the initial
-member of which is referenced by
+These functions sort an
+.Fa nmemb
+element array of pointers to byte strings, with
+the initial member of which is referenced by
.Fa base .
-The byte strings may contain any values; the end of each string
-is denoted by the user-specified value
+The byte strings may contain any values.
+End of strings is denoted
+by character which has same weight as user specified value
.Fa endbyte .
+.Fa endbyte
+has to be between 0 and 255.
.Pp
Applications may specify a sort order by providing the
.Fa table
diff --git a/src/radixsort.c b/src/radixsort.c
index 82ff1bc..45e2477 100644
--- a/src/radixsort.c
+++ b/src/radixsort.c
@@ -1,3 +1,4 @@
+/* $NetBSD: radixsort.c,v 1.18 2009/08/21 20:49:50 dsl Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -13,7 +14,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 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
* without specific prior written permission.
*
@@ -30,11 +31,14 @@
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#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 */
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
/*
* Radixsort routines.
@@ -59,11 +63,10 @@ typedef struct {
int sn, si;
} stack;
-static inline void simplesort
-(const u_char **, int, int, const u_char *, u_int);
+static inline void simplesort(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,
- const u_char *, u_int);
+static void r_sort_b(const u_char **,
+ const u_char **, int, int, const u_char *, u_int);
#define THRESHOLD 20 /* Divert to simplesort(). */
#define SIZE 512 /* Default stack size. */
@@ -88,13 +91,10 @@ static void r_sort_b(const u_char **, const u_char **, int, int,
}
int
-radixsort(a, n, tab, endch)
- const u_char **a, *tab;
- int n;
- u_int endch;
+radixsort(const u_char **a, int n, const u_char *tab, u_int endch)
{
const u_char *tr;
- int c;
+ u_int c;
u_char tr0[256];
SETUP;
@@ -103,15 +103,17 @@ radixsort(a, n, tab, endch)
}
int
-sradixsort(a, n, tab, endch)
- const u_char **a, *tab;
- int n;
- u_int endch;
+sradixsort(const u_char **a, int n, const u_char *tab, u_int endch)
{
const u_char *tr, **ta;
- int c;
+ u_int c;
u_char tr0[256];
+ if (a == NULL) {
+ errno = EFAULT;
+ return (-1);
+ }
+
SETUP;
if (n < THRESHOLD)
simplesort(a, n, 0, tr, endch);
@@ -131,17 +133,13 @@ sradixsort(a, n, tab, endch)
/* Unstable, in-place sort. */
static void
-r_sort_a(a, n, i, tr, endch)
- const u_char **a;
- int n, i;
- const u_char *tr;
- u_int endch;
+r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch)
{
- static int count[256], nc, bmin;
- int c;
+ static u_int count[256], nc, bmin;
+ u_int c;
const u_char **ak, *r;
stack s[SIZE], *sp, *sp0, *sp1, temp;
- int *cp, bigc;
+ u_int *cp, bigc;
const u_char **an, *t, **aj, **top[256];
/* Set up stack. */
@@ -233,18 +231,15 @@ r_sort_a(a, n, i, tr, endch)
/* Stable sort, requiring additional memory. */
static void
-r_sort_b(a, ta, n, i, tr, endch)
- const u_char **a, **ta;
- int n, i;
- const u_char *tr;
- u_int endch;
+r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr,
+ u_int endch)
{
- static int count[256], nc, bmin;
- int c;
+ static u_int count[256], nc, bmin;
+ u_int c;
const u_char **ak, **ai;
stack s[512], *sp, *sp0, *sp1, temp;
const u_char **top[256];
- int *cp, bigc;
+ u_int *cp, bigc;
sp = s;
push(a, n, i);
@@ -304,12 +299,9 @@ r_sort_b(a, ta, n, i, tr, endch)
}
}
+/* insertion sort */
static inline void
-simplesort(a, n, b, tr, endch) /* insertion sort */
- const u_char **a;
- int n, b;
- const u_char *tr;
- u_int endch;
+simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch)
{
u_char ch;
const u_char **ak, **ai, *s, *t;