time-to-botec

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

factory.js (3957B)


      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 gamma distribution.
     31 *
     32 * @param {PositiveNumber} [alpha] - shape parameter
     33 * @param {PositiveNumber} [beta] - rate parameter
     34 * @param {Options} [options] - stream options
     35 * @param {boolean} [options.objectMode=false] - specifies whether a stream should operate in object mode
     36 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to `strings`
     37 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in an internal buffer before ceasing to generate additional pseudorandom numbers
     38 * @param {string} [options.sep='\n'] - separator used to join streamed data
     39 * @param {NonNegativeInteger} [options.iter] - number of iterations
     40 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers
     41 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed
     42 * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
     43 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
     44 * @param {PositiveInteger} [options.siter] - number of iterations after which to emit the PRNG state
     45 * @returns {Function} stream factory
     46 *
     47 * @example
     48 * var opts = {
     49 *     'sep': ',',
     50 *     'objectMode': false,
     51 *     'encoding': 'utf8',
     52 *     'highWaterMark': 64
     53 * };
     54 *
     55 * var createStream = factory( opts );
     56 *
     57 * // Create 10 identically configured streams...
     58 * var streams = [];
     59 * var i;
     60 * for ( i = 0; i < 10; i++ ) {
     61 *     streams.push( createStream( 2.0, 5.0 ) );
     62 * }
     63 */
     64 function factory( alpha, beta, options ) {
     65 	var nargs;
     66 	var opts;
     67 	var fcn;
     68 
     69 	nargs = arguments.length;
     70 	if ( nargs === 1 ) {
     71 		opts = copy( alpha, 1 );
     72 	} else if ( nargs > 2 ) {
     73 		opts = copy( options, 1 );
     74 	} else {
     75 		opts = {};
     76 	}
     77 	if ( nargs < 2 ) {
     78 		fcn = createStream1;
     79 	} else {
     80 		fcn = createStream2;
     81 	}
     82 	return fcn;
     83 
     84 	/**
     85 	* Returns a readable stream for generating pseudorandom numbers drawn from a gamma distribution.
     86 	*
     87 	* @private
     88 	* @param {PositiveNumber} alpha - shape parameter
     89 	* @param {PositiveNumber} beta - rate parameter
     90 	* @throws {TypeError} `alpha` must be a positive number
     91 	* @throws {TypeError} `beta` must be a positive number
     92 	* @throws {TypeError} options argument must be an object
     93 	* @throws {TypeError} must provide valid options
     94 	* @throws {Error} must provide a valid state
     95 	* @returns {RandomStream} Stream instance
     96 	*/
     97 	function createStream1( alpha, beta ) {
     98 		return new RandomStream( alpha, beta, opts );
     99 	}
    100 
    101 	/**
    102 	* Returns a readable stream for generating pseudorandom numbers drawn from a gamma distribution.
    103 	*
    104 	* @private
    105 	* @throws {TypeError} `alpha` must be a positive number
    106 	* @throws {TypeError} `beta` must be a positive number
    107 	* @throws {TypeError} options argument must be an object
    108 	* @throws {TypeError} must provide valid options
    109 	* @throws {Error} must provide a valid state
    110 	* @returns {RandomStream} Stream instance
    111 	*/
    112 	function createStream2() {
    113 		return new RandomStream( alpha, beta, opts );
    114 	}
    115 }
    116 
    117 
    118 // EXPORTS //
    119 
    120 module.exports = factory;