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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/* Generate random permutations.
Copyright (C) 2006-2007, 2009-2011 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 "hash.h"
#include "randperm.h"
#include <limits.h>
#include <stdlib.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 _GL_ATTRIBUTE_CONST
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;
}
/* Swap elements I and J in array V. */
static void
swap (size_t *v, size_t i, size_t j)
{
size_t t = v[i];
v[i] = v[j];
v[j] = t;
}
/* Structures and functions for a sparse_map abstract data type that's
used to effectively swap elements I and J in array V like swap(),
but in a more memory efficient manner (when the number of permutations
performed is significantly less than the size of the input). */
struct sparse_ent_
{
size_t index;
size_t val;
};
static size_t
sparse_hash_ (void const *x, size_t table_size)
{
struct sparse_ent_ const *ent = x;
return ent->index % table_size;
}
static bool
sparse_cmp_ (void const *x, void const *y)
{
struct sparse_ent_ const *ent1 = x;
struct sparse_ent_ const *ent2 = y;
return ent1->index == ent2->index;
}
typedef Hash_table sparse_map;
/* Initialize the structure for the sparse map,
when a best guess as to the number of entries
specified with SIZE_HINT. */
static sparse_map *
sparse_new (size_t size_hint)
{
return hash_initialize (size_hint, NULL, sparse_hash_, sparse_cmp_, free);
}
/* Swap the values for I and J. If a value is not already present
then assume it's equal to the index. Update the value for
index I in array V. */
static void
sparse_swap (sparse_map *sv, size_t* v, size_t i, size_t j)
{
struct sparse_ent_ *v1 = hash_delete (sv, &(struct sparse_ent_) {i,0});
struct sparse_ent_ *v2 = hash_delete (sv, &(struct sparse_ent_) {j,0});
/* FIXME: reduce the frequency of these mallocs. */
if (!v1)
{
v1 = xmalloc (sizeof *v1);
v1->index = v1->val = i;
}
if (!v2)
{
v2 = xmalloc (sizeof *v2);
v2->index = v2->val = j;
}
size_t t = v1->val;
v1->val = v2->val;
v2->val = t;
if (!hash_insert (sv, v1))
xalloc_die ();
if (!hash_insert (sv, v2))
xalloc_die ();
v[i] = v1->val;
}
static void
sparse_free (sparse_map *sv)
{
hash_free (sv);
}
/* 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:
{
/* The algorithm is essentially the same in both
the sparse and non sparse case. In the sparse case we use
a hash to implement sparse storage for the set of n numbers
we're shuffling. When to use the sparse method was
determined with the help of this script:
#!/bin/sh
for n in $(seq 2 32); do
for h in $(seq 2 32); do
test $h -gt $n && continue
for s in o n; do
test $s = o && shuf=shuf || shuf=./shuf
num=$(env time -f "$s:${h},${n} = %e,%M" \
$shuf -i0-$((2**$n-2)) -n$((2**$h-2)) | wc -l)
test $num = $((2**$h-2)) || echo "$s:${h},${n} = failed" >&2
done
done
done
This showed that if sparseness = n/h, then:
sparseness = 128 => .125 mem used, and about same speed
sparseness = 64 => .25 mem used, but 1.5 times slower
sparseness = 32 => .5 mem used, but 2 times slower
Also the memory usage was only significant when n > 128Ki
*/
bool sparse = (n >= (128 * 1024)) && (n / h >= 32);
size_t i;
sparse_map *sv;
if (sparse)
{
sv = sparse_new (h * 2);
if (sv == NULL)
xalloc_die ();
v = xnmalloc (h, sizeof *v);
}
else
{
sv = NULL; /* To placate GCC's -Wuninitialized. */
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);
if (sparse)
sparse_swap (sv, v, i, j);
else
swap (v, i, j);
}
if (sparse)
sparse_free (sv);
else
v = xnrealloc (v, h, sizeof *v);
}
break;
}
return v;
}
|