summaryrefslogtreecommitdiff
path: root/gl/lib/randperm.c
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2007-09-01 09:54:45 +0200
committerJim Meyering <meyering@redhat.com>2007-10-07 19:43:35 +0200
commit58a7ead41d056909784bfab9323c5f44f577d3d0 (patch)
treeea0f1849f46529be4cac93d09f1b4f6f85f53255 /gl/lib/randperm.c
parent696f4a8042e11f6caf479f729b09dcae2700e541 (diff)
downloadcoreutils-58a7ead41d056909784bfab9323c5f44f577d3d0.tar.xz
Convert coreutils' rand*.{c,h,m4} into modules.
First step: move these files to gl/lib: * lib/rand-isaac.c, lib/rand-isaac.h * lib/randint.c, lib/randint.h * lib/randperm.c, lib/randperm.h * lib/randread.c, lib/randread.h Step 2: add modules/rand* and remove now-unneeded .m4 files. * gl/modules/randint: New file. * gl/modules/randperm: New file. * gl/modules/randread: New file. * m4/randint.m4: Remove file. * m4/randperm.m4: Remove file. * m4/randread.m4: Remove file. Step 3: use the new modules * bootstrap.conf (gnulib_modules): Add randint and randperm. * m4/prereq.m4 (gl_RANDINT, gl_RANDREAD, gl_RANDPERM): Don't require; These have been removed. (gl_ROOT_DEV_INO): Don't require; already handled via bootstrap.conf.
Diffstat (limited to 'gl/lib/randperm.c')
-rw-r--r--gl/lib/randperm.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/gl/lib/randperm.c b/gl/lib/randperm.c
new file mode 100644
index 000000000..0aaa5e2ff
--- /dev/null
+++ b/gl/lib/randperm.c
@@ -0,0 +1,102 @@
+/* Generate random permutations.
+
+ Copyright (C) 2006, 2007 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Paul Eggert. */
+
+#include <config.h>
+
+#include "randperm.h"
+
+#include <limits.h>
+
+#include "xalloc.h"
+
+/* Return the ceiling of the log base 2 of N. If N is zero, return
+ an unspecified value. */
+
+static size_t
+ceil_lg (size_t n)
+{
+ size_t b = 0;
+ for (n--; n != 0; n /= 2)
+ b++;
+ return b;
+}
+
+/* Return an upper bound on the number of random bytes needed to
+ generate the first H elements of a random permutation of N
+ elements. H must not exceed N. */
+
+size_t
+randperm_bound (size_t h, size_t n)
+{
+ /* Upper bound on number of bits needed to generate the first number
+ of the permutation. */
+ size_t lg_n = ceil_lg (n);
+
+ /* Upper bound on number of bits needed to generated the first H elements. */
+ size_t ar = lg_n * h;
+
+ /* Convert the bit count to a byte count. */
+ size_t bound = (ar + CHAR_BIT - 1) / CHAR_BIT;
+
+ return bound;
+}
+
+/* From R, allocate and return a malloc'd array of the first H elements
+ of a random permutation of N elements. H must not exceed N.
+ Return NULL if H is zero. */
+
+size_t *
+randperm_new (struct randint_source *r, size_t h, size_t n)
+{
+ size_t *v;
+
+ switch (h)
+ {
+ case 0:
+ v = NULL;
+ break;
+
+ case 1:
+ v = xmalloc (sizeof *v);
+ v[0] = randint_choose (r, n);
+ break;
+
+ default:
+ {
+ size_t i;
+
+ v = xnmalloc (n, sizeof *v);
+ for (i = 0; i < n; i++)
+ v[i] = i;
+
+ for (i = 0; i < h; i++)
+ {
+ size_t j = i + randint_choose (r, n - i);
+ size_t t = v[i];
+ v[i] = v[j];
+ v[j] = t;
+ }
+
+ v = xnrealloc (v, h, sizeof *v);
+ }
+ break;
+ }
+
+ return v;
+}