Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 22x 5x 22x 17x 17x 17x 43x 15x 15x 15x 8x 8x 7x 7x 43x 7x 7x 43x 17x 17x 17x 43x 11x 11x 43x 16x 43x 12x 2x 12x 10x 10x 12x 12x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 44x 44x 44x 44x 44x 44x 44x 44x 8x 8x 44x 1x 1x 36x 44x 31x 31x 17x 17x 31x 44x 10x 44x 9x 9x 44x 5x 5x 44x 11x 44x 1x 8x 7x 7x 19x 44x 9x 44x 2x 2x 17x 44x 5x 3x 3x 2x 2x 12x 44x 7x 7x 5x 5x 44x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 27x 27x 27x 27x 27x 27x 27x 27x 2x 2x 25x 27x 21x 21x 9x 9x 21x 27x 13x 27x 3x 3x 27x 13x 27x 3x 3x 27x 9x 27x 3x 7x 4x 4x 16x 27x 10x 27x 2x 2x 27x 6x 6x 8x 27x 5x 5x 5x 8x 27x 43x 2x 2x 2x 2x 2x | /** * @license Apache-2.0 * * Copyright (c) 2018 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 'use strict'; // MODULES // var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var isArrayLike = require( '@stdlib/assert/is-array-like' ); var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' ); var isString = require( '@stdlib/assert/is-string' ).isPrimitive; var format = require( '@stdlib/string/format' ); var randu = require( '@stdlib/random/base/mt19937' ).factory; var copy = require( '@stdlib/utils/copy' ); var discreteUniform = require( './discrete_uniform.js' ); var renormalizing = require( './renormalizing.js' ); var fisherYates = require( './fisher_yates.js' ); var vose = require( './vose.js' ); var defaults = require( './defaults.json' ); var validate = require( './validate.js' ); // FUNCTIONS // var slice = Array.prototype.slice; // MAIN // /** * Returns a function to sample elements from an array-like object. * * @param {(ArrayLike|TypedArrayLike)} [pool] - array-like object from which to sample * @param {Options} [options] - function options * @param {PositiveInteger} [options.seed] - integer-valued seed * @param {NonNegativeInteger} [options.size] - sample size * @param {boolean} [options.replace=true] - boolean indicating whether to sample with replacement * @param {boolean} [options.mutate=false] - boolean indicating whether to mutate the `pool` when sampling without replacement * @throws {TypeError} `pool` must be an array-like object * @throws {TypeError} options argument must be an object * @throws {TypeError} must provide valid options * @returns {Function} function to sample elements from an array-like object * * @example * var sample = factory({ * 'seed': 232 * }); * var out = sample( 'abcdefg' ); * // e.g., returns [ 'g', 'd', 'g', 'f', 'c', 'e', 'f' ] * * @example * var sample = factory( [ 1, 2, 3, 4, 5, 6 ], { * 'seed': 232, * 'size': 2 * }); * var out = sample(); * // e.g., returns [ 6, 4 ] * * out = sample(); * // e.g., returns [ 6, 5 ] * * @example * var sample = factory( [ 1, 2, 3, 4, 5, 6 ], { * 'seed': 474, * 'size': 3, * 'mutate': true, * 'replace': false * }); * var out = sample(); * // e.g., returns [ 4, 3, 6 ] * * out = sample(); * // e.g., returns [ 1, 5, 2 ] * * out = sample(); * // returns null * * @example * var sample = factory( [ 0, 1 ], { * 'size': 2 * }); * * var out = sample(); * // e.g., returns [ 1, 1 ] * * out = sample({ * 'size': 10 * }); * // e.g., returns [ 0, 1, 1, 1, 0, 1, 0, 0, 1, 1 ] * * @example * var sample = factory( [ 0, 1 ], { * 'size': 2 * }); * * var out = sample(); * // e.g., returns [ 1, 1 ] * * out = sample({ * 'replace': false * }); * // e.g., returns [ 0, 1 ] or [ 1, 0 ] * * out = sample(); * // e.g., returns [ 1, 1 ] * * @example * var sample = factory( [ 0, 1 ], { * 'size': 2, * 'mutate': true * }); * * var out = sample(); * // e.g., returns [ 1, 1 ] * * out = sample({ * 'replace': false * }); * // e.g., returns [ 0, 1 ] or [ 1, 0 ] * * out = sample(); * // returns null */ function factory() { var config; var pool; var conf; var rand; var err; var fcn; conf = copy( defaults ); if ( arguments.length === 1 ) { if ( isArrayLike( arguments[ 0 ] ) || isTypedArrayLike( arguments[ 0 ] ) ) { // eslint-disable-line max-len pool = arguments[ 0 ]; } else { config = arguments[ 0 ]; err = validate( conf, config ); } } else if ( arguments.length > 1 ) { pool = arguments[ 0 ]; config = arguments[ 1 ]; if ( !( isArrayLike( pool ) || isTypedArrayLike( pool ) ) ) { throw new TypeError( format( 'invalid argument. `%s` argument must be array-like. Value: `%s`.', 'pool', pool ) ); } err = validate( conf, config ); } if ( err ) { throw err; } if ( config && config.seed ) { rand = randu({ 'seed': config.seed }); } else { rand = randu(); } if ( pool === void 0 ) { fcn = sample1; } else { if ( isString( pool ) ) { pool = pool.split( '' ); } else { pool = copy( pool ); } fcn = sample2; } setReadOnly( fcn, 'seed', rand.seed ); setReadOnly( fcn, 'PRNG', rand ); rand = rand.normalized; return fcn; /** * Samples elements from an array-like object. * * @private * @param {(ArrayLike|TypedArrayLike)} x - array-like object from which to sample elements * @param {Options} [options] - function options * @param {NonNegativeInteger} [options.size] - sample size * @param {ProbabilityArray} [options.probs] - element probabilities * @param {boolean} [options.replace=true] - boolean indicating whether to sample with replacement * @throws {TypeError} first argument must be array-like * @throws {TypeError} options argument must be an object * @throws {TypeError} must provide valid options * @throws {RangeError} `size` option must be less than or equal to the length of `x` when the `replace` option is `false` * @returns {Array} sample */ function sample1( x, options ) { var replace; var xcopy; var probs; var opts; var size; var err; if ( !( isArrayLike( x ) || isTypedArrayLike( x ) ) ) { throw new TypeError( format( 'invalid argument. First argument must be array-like. Value: `%s`.', x ) ); } if ( isString( x ) ) { x = x.split( '' ); } opts = {}; if ( arguments.length > 1 ) { err = validate( opts, options ); if ( err ) { throw err; } } if ( opts.replace === void 0 ) { replace = conf.replace; } else { replace = opts.replace; } if ( opts.probs !== void 0 ) { probs = opts.probs; } if ( opts.size ) { size = opts.size; } else if ( conf.size ) { size = conf.size; } else { size = x.length; } if ( replace === false && size > x.length ) { throw new RangeError( format( 'invalid option. `size` option must be less than or equal to the length of `x` when `replace` is `false`. Option: `%s`.', size ) ); } // Custom probabilities... if ( probs ) { if ( replace ) { return vose( x, size, rand, probs ); } return renormalizing( x, size, rand, probs ); } // All elements equally likely... if ( replace ) { return discreteUniform( x, size, rand ); } xcopy = slice.call( x ); return fisherYates( xcopy, size, rand ); } /** * Samples elements from a population. * * @private * @param {Options} [options] - function options * @param {NonNegativeInteger} [options.size] - sample size * @param {boolean} [options.replace=true] - boolean indicating whether to sample with replacement * @param {boolean} [options.mutate=false] - boolean indicating whether to mutate the `pool` when sampling without replacement * @throws {TypeError} options argument must be an object * @throws {TypeError} must provide valid options * @throws {RangeError} `size` option must be less than or equal to the population when the `replace` option is `false` * @returns {Array} sample */ function sample2( options ) { var replace; var mutate; var opts; var size; var err; var out; if ( pool.length === 0 ) { return null; } opts = {}; if ( arguments.length ) { err = validate( opts, options ); if ( err ) { throw err; } } if ( opts.mutate === void 0 ) { mutate = conf.mutate; } else { mutate = opts.mutate; } if ( opts.replace === void 0 ) { replace = conf.replace; } else { replace = opts.replace; } if ( opts.size ) { size = opts.size; } else if ( conf.size ) { size = conf.size; } else { size = pool.length; } if ( replace === false && size > pool.length ) { throw new RangeError( format( 'invalid option. `size` option must be less than or equal to the population size when `replace` is `false`. Option: `%s`.', size ) ); } if ( replace ) { return discreteUniform( pool, size, rand ); } out = fisherYates( pool, size, rand ); if ( mutate ) { // Remove the sample observations: pool = pool.slice( size, pool.length ); } return out; } } // EXPORTS // module.exports = factory; |