time-to-botec

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

factory.js (4130B)


      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 triangular distribution.
     31 *
     32 * @param {number} [a] - minimum support
     33 * @param {number} [b] - maximum support
     34 * @param {number} [c] - mode
     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( 2.0, 5.0, 4.0 ) );
     63 * }
     64 */
     65 function factory( a, b, c, options ) {
     66 	var nargs;
     67 	var opts;
     68 	var fcn;
     69 
     70 	nargs = arguments.length;
     71 	if ( nargs === 1 ) {
     72 		opts = copy( a, 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 triangular distribution.
     87 	*
     88 	* @private
     89 	* @param {number} a - minimum support
     90 	* @param {number} b - maximum support
     91 	* @param {number} c - mode
     92 	* @throws {TypeError} `a` must be a number
     93 	* @throws {TypeError} `b` must be a number
     94 	* @throws {TypeError} `c` must be a number
     95 	* @throws {RangeError} arguments must satisfy `a <= c <= b`
     96 	* @throws {TypeError} options argument must be an object
     97 	* @throws {TypeError} must provide valid options
     98 	* @throws {Error} must provide a valid state
     99 	* @returns {RandomStream} Stream instance
    100 	*/
    101 	function createStream1( a, b, c ) {
    102 		return new RandomStream( a, b, c, opts );
    103 	}
    104 
    105 	/**
    106 	* Returns a readable stream for generating pseudorandom numbers drawn from a triangular distribution.
    107 	*
    108 	* @private
    109 	* @throws {TypeError} `a` must be a number
    110 	* @throws {TypeError} `b` must be a number
    111 	* @throws {TypeError} `c` must be a number
    112 	* @throws {RangeError} arguments must satisfy `a <= c <= b`
    113 	* @throws {TypeError} options argument must be an object
    114 	* @throws {TypeError} must provide valid options
    115 	* @throws {Error} must provide a valid state
    116 	* @returns {RandomStream} Stream instance
    117 	*/
    118 	function createStream2() {
    119 		return new RandomStream( a, b, c, opts );
    120 	}
    121 }
    122 
    123 
    124 // EXPORTS //
    125 
    126 module.exports = factory;