time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

factory.js (4440B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var copy = require( '@stdlib/utils/copy' );
     24 var RandomStream = require( './main.js' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Returns a function for creating readable streams which generate pseudorandom numbers drawn from a hypergeometric distribution.
     31 *
     32 * @param {NonNegativeInteger} [N] - population size
     33 * @param {NonNegativeInteger} [K] - subpopulation size
     34 * @param {NonNegativeInteger} [n] - number of draws
     35 * @param {Options} [options] - stream options
     36 * @param {boolean} [options.objectMode=false] - specifies whether a stream should operate in object mode
     37 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to `strings`
     38 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in an internal buffer before ceasing to generate additional pseudorandom numbers
     39 * @param {string} [options.sep='\n'] - separator used to join streamed data
     40 * @param {NonNegativeInteger} [options.iter] - number of iterations
     41 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers
     42 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed
     43 * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
     44 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
     45 * @param {PositiveInteger} [options.siter] - number of iterations after which to emit the PRNG state
     46 * @returns {Function} stream factory
     47 *
     48 * @example
     49 * var opts = {
     50 *     'sep': ',',
     51 *     'objectMode': false,
     52 *     'encoding': 'utf8',
     53 *     'highWaterMark': 64
     54 * };
     55 *
     56 * var createStream = factory( opts );
     57 *
     58 * // Create 10 identically configured streams...
     59 * var streams = [];
     60 * var i;
     61 * for ( i = 0; i < 10; i++ ) {
     62 *     streams.push( createStream( 5, 3, 2 ) );
     63 * }
     64 */
     65 function factory( N, K, n, options ) {
     66 	var nargs;
     67 	var opts;
     68 	var fcn;
     69 
     70 	nargs = arguments.length;
     71 	if ( nargs === 1 ) {
     72 		opts = copy( N, 1 );
     73 	} else if ( nargs > 3 ) {
     74 		opts = copy( options, 1 );
     75 	} else {
     76 		opts = {};
     77 	}
     78 	if ( nargs < 3 ) {
     79 		fcn = createStream1;
     80 	} else {
     81 		fcn = createStream2;
     82 	}
     83 	return fcn;
     84 
     85 	/**
     86 	* Returns a readable stream for generating pseudorandom numbers drawn from a hypergeometric distribution.
     87 	*
     88 	* @private
     89 	* @param {NonNegativeInteger} N - population size
     90 	* @param {NonNegativeInteger} K - subpopulation size
     91 	* @param {NonNegativeInteger} n - number of draws
     92 	* @throws {TypeError} `N` must be a nonnegative integer
     93 	* @throws {TypeError} `K` must be a nonnegative integer
     94 	* @throws {TypeError} `n` must be a nonnegative integer
     95 	* @throws {RangeError} `n` must be less than or equal to `N`
     96 	* @throws {RangeError} `K` must be less than or equal to `N`
     97 	* @throws {TypeError} options argument must be an object
     98 	* @throws {TypeError} must provide valid options
     99 	* @throws {Error} must provide a valid state
    100 	* @returns {RandomStream} Stream instance
    101 	*/
    102 	function createStream1( N, K, n ) {
    103 		return new RandomStream( N, K, n, opts );
    104 	}
    105 
    106 	/**
    107 	* Returns a readable stream for generating pseudorandom numbers drawn from a hypergeometric distribution.
    108 	*
    109 	* @private
    110 	* @throws {TypeError} `N` must be a nonnegative integer
    111 	* @throws {TypeError} `K` must be a nonnegative integer
    112 	* @throws {TypeError} `n` must be a nonnegative integer
    113 	* @throws {RangeError} `n` must be less than or equal to `N`
    114 	* @throws {RangeError} `K` must be less than or equal to `N`
    115 	* @throws {TypeError} options argument must be an object
    116 	* @throws {TypeError} must provide valid options
    117 	* @throws {Error} must provide a valid state
    118 	* @returns {RandomStream} Stream instance
    119 	*/
    120 	function createStream2() {
    121 		return new RandomStream( N, K, n, opts );
    122 	}
    123 }
    124 
    125 
    126 // EXPORTS //
    127 
    128 module.exports = factory;