time-to-botec

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

main.js (9741B)


      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 Readable = require( 'readable-stream' ).Readable;
     24 var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
     25 var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ).isPrimitive;
     26 var isError = require( '@stdlib/assert/is-error' );
     27 var copy = require( '@stdlib/utils/copy' );
     28 var inherit = require( '@stdlib/utils/inherit' );
     29 var setNonEnumerable = require( '@stdlib/utils/define-nonenumerable-property' );
     30 var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     31 var setReadOnlyAccessor = require( '@stdlib/utils/define-read-only-accessor' );
     32 var setReadWriteAccessor = require( '@stdlib/utils/define-read-write-accessor' );
     33 var rerlang = require( './../../../base/erlang' ).factory;
     34 var string2buffer = require( '@stdlib/buffer/from-string' );
     35 var nextTick = require( '@stdlib/utils/next-tick' );
     36 var DEFAULTS = require( './defaults.json' );
     37 var validate = require( './validate.js' );
     38 var debug = require( './debug.js' );
     39 
     40 
     41 // FUNCTIONS //
     42 
     43 /**
     44 * Returns the PRNG seed.
     45 *
     46 * @private
     47 * @returns {(PRNGSeedMT19937|null)} seed
     48 */
     49 function getSeed() {
     50 	return this._prng.seed; // eslint-disable-line no-invalid-this
     51 }
     52 
     53 /**
     54 * Returns the PRNG seed length.
     55 *
     56 * @private
     57 * @returns {(PositiveInteger|null)} seed length
     58 */
     59 function getSeedLength() {
     60 	return this._prng.seedLength; // eslint-disable-line no-invalid-this
     61 }
     62 
     63 /**
     64 * Returns the PRNG state length.
     65 *
     66 * @private
     67 * @returns {(PositiveInteger|null)} state length
     68 */
     69 function getStateLength() {
     70 	return this._prng.stateLength; // eslint-disable-line no-invalid-this
     71 }
     72 
     73 /**
     74 * Returns the PRNG state size (in bytes).
     75 *
     76 * @private
     77 * @returns {(PositiveInteger|null)} state size (in bytes)
     78 */
     79 function getStateSize() {
     80 	return this._prng.byteLength; // eslint-disable-line no-invalid-this
     81 }
     82 
     83 /**
     84 * Returns the current PRNG state.
     85 *
     86 * @private
     87 * @returns {(PRNGStateMT19937|null)} current state
     88 */
     89 function getState() {
     90 	return this._prng.state; // eslint-disable-line no-invalid-this
     91 }
     92 
     93 /**
     94 * Sets the PRNG state.
     95 *
     96 * @private
     97 * @param {PRNGStateMT19937} s - generator state
     98 * @throws {Error} must provide a valid state
     99 */
    100 function setState( s ) {
    101 	this._prng.state = s; // eslint-disable-line no-invalid-this
    102 }
    103 
    104 /**
    105 * Implements the `_read` method.
    106 *
    107 * @private
    108 * @param {number} size - number (of bytes) to read
    109 * @returns {void}
    110 */
    111 function read() {
    112 	/* eslint-disable no-invalid-this */
    113 	var FLG;
    114 	var r;
    115 
    116 	if ( this._destroyed ) {
    117 		return;
    118 	}
    119 	FLG = true;
    120 	while ( FLG ) {
    121 		this._i += 1;
    122 		if ( this._i > this._iter ) {
    123 			debug( 'Finished generating pseudorandom numbers.' );
    124 			return this.push( null );
    125 		}
    126 		r = this._prng();
    127 
    128 		debug( 'Generated a new pseudorandom number. Value: %d. Iter: %d.', r, this._i );
    129 
    130 		if ( this._objectMode === false ) {
    131 			r = r.toString();
    132 			if ( this._i === 1 ) {
    133 				r = string2buffer( r );
    134 			} else {
    135 				r = string2buffer( this._sep+r );
    136 			}
    137 		}
    138 		FLG = this.push( r );
    139 		if ( this._i%this._siter === 0 ) {
    140 			this.emit( 'state', this.state );
    141 		}
    142 	}
    143 
    144 	/* eslint-enable no-invalid-this */
    145 }
    146 
    147 /**
    148 * Gracefully destroys a stream, providing backward compatibility.
    149 *
    150 * @private
    151 * @param {(string|Object|Error)} [error] - error
    152 * @returns {RandomStream} Stream instance
    153 */
    154 function destroy( error ) {
    155 	/* eslint-disable no-invalid-this */
    156 	var self;
    157 	if ( this._destroyed ) {
    158 		debug( 'Attempted to destroy an already destroyed stream.' );
    159 		return this;
    160 	}
    161 	self = this;
    162 	this._destroyed = true;
    163 
    164 	nextTick( close );
    165 
    166 	return this;
    167 
    168 	/**
    169 	* Closes a stream.
    170 	*
    171 	* @private
    172 	*/
    173 	function close() {
    174 		if ( error ) {
    175 			debug( 'Stream was destroyed due to an error. Error: %s.', ( isError( error ) ) ? error.message : JSON.stringify( error ) );
    176 			self.emit( 'error', error );
    177 		}
    178 		debug( 'Closing the stream...' );
    179 		self.emit( 'close' );
    180 	}
    181 
    182 	/* eslint-enable no-invalid-this */
    183 }
    184 
    185 
    186 // MAIN //
    187 
    188 /**
    189 * Stream constructor for generating a stream of pseudorandom numbers drawn from an Erlang distribution.
    190 *
    191 * @constructor
    192 * @param {PositiveInteger} k - shape parameter
    193 * @param {PositiveNumber} lambda - rate parameter
    194 * @param {Options} [options] - stream options
    195 * @param {boolean} [options.objectMode=false] - specifies whether the stream should operate in object mode
    196 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to strings
    197 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in an internal buffer before ceasing to generate additional pseudorandom numbers
    198 * @param {string} [options.sep='\n'] - separator used to join streamed data
    199 * @param {NonNegativeInteger} [options.iter] - number of iterations
    200 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers
    201 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed
    202 * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
    203 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
    204 * @param {PositiveInteger} [options.siter] - number of iterations after which to emit the PRNG state
    205 * @throws {TypeError} `k` must be a positive integer
    206 * @throws {TypeError} `lambda` must be a positive number
    207 * @throws {TypeError} options argument must be an object
    208 * @throws {TypeError} must provide valid options
    209 * @throws {Error} must provide a valid state
    210 * @returns {RandomStream} Stream instance
    211 *
    212 * @example
    213 * var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    214 *
    215 * function log( chunk ) {
    216 *    console.log( chunk.toString() );
    217 * }
    218 *
    219 * var opts = {
    220 *     'iter': 10
    221 * };
    222 *
    223 * var stream = new RandomStream( 2, 5.0, opts );
    224 *
    225 * stream.pipe( inspectStream( log )  );
    226 */
    227 function RandomStream( k, lambda, options ) {
    228 	var opts;
    229 	var err;
    230 	if ( !( this instanceof RandomStream ) ) {
    231 		if ( arguments.length > 2 ) {
    232 			return new RandomStream( k, lambda, options );
    233 		}
    234 		return new RandomStream( k, lambda );
    235 	}
    236 	if ( !isPositiveInteger( k ) ) {
    237 		throw new TypeError( 'invalid argument. First argument must be a positive integer. Value: `'+k+'`.' );
    238 	}
    239 	if ( !isPositiveNumber( lambda ) ) {
    240 		throw new TypeError( 'invalid argument. Second argument must be a positive number. Value: `'+lambda+'`.' );
    241 	}
    242 	opts = copy( DEFAULTS );
    243 	if ( arguments.length > 2 ) {
    244 		err = validate( opts, options );
    245 		if ( err ) {
    246 			throw err;
    247 		}
    248 	}
    249 	// Make the stream a readable stream:
    250 	debug( 'Creating a readable stream configured with the following options: %s.', JSON.stringify( opts ) );
    251 	Readable.call( this, opts );
    252 
    253 	// Destruction state:
    254 	setNonEnumerable( this, '_destroyed', false );
    255 
    256 	// Cache whether the stream is operating in object mode:
    257 	setNonEnumerableReadOnly( this, '_objectMode', opts.objectMode );
    258 
    259 	// Cache the separator:
    260 	setNonEnumerableReadOnly( this, '_sep', opts.sep );
    261 
    262 	// Cache the total number of iterations:
    263 	setNonEnumerableReadOnly( this, '_iter', opts.iter );
    264 
    265 	// Cache the number of iterations after which to emit the underlying PRNG state:
    266 	setNonEnumerableReadOnly( this, '_siter', opts.siter );
    267 
    268 	// Initialize an iteration counter:
    269 	setNonEnumerable( this, '_i', 0 );
    270 
    271 	// Create the underlying PRNG:
    272 	setNonEnumerableReadOnly( this, '_prng', rerlang( k, lambda, opts ) );
    273 	setNonEnumerableReadOnly( this, 'PRNG', this._prng.PRNG );
    274 
    275 	return this;
    276 }
    277 
    278 /*
    279 * Inherit from the `Readable` prototype.
    280 */
    281 inherit( RandomStream, Readable );
    282 
    283 /**
    284 * PRNG seed.
    285 *
    286 * @name seed
    287 * @memberof RandomStream.prototype
    288 * @type {(PRNGSeedMT19937|null)}
    289 */
    290 setReadOnlyAccessor( RandomStream.prototype, 'seed', getSeed );
    291 
    292 /**
    293 * PRNG seed length.
    294 *
    295 * @name seedLength
    296 * @memberof RandomStream.prototype
    297 * @type {(PositiveInteger|null)}
    298 */
    299 setReadOnlyAccessor( RandomStream.prototype, 'seedLength', getSeedLength );
    300 
    301 /**
    302 * PRNG state getter/setter.
    303 *
    304 * @name state
    305 * @memberof RandomStream.prototype
    306 * @type {(PRNGStateMT19937|null)}
    307 * @throws {Error} must provide a valid state
    308 */
    309 setReadWriteAccessor( RandomStream.prototype, 'state', getState, setState );
    310 
    311 /**
    312 * PRNG state length.
    313 *
    314 * @name stateLength
    315 * @memberof RandomStream.prototype
    316 * @type {(PositiveInteger|null)}
    317 */
    318 setReadOnlyAccessor( RandomStream.prototype, 'stateLength', getStateLength );
    319 
    320 /**
    321 * PRNG state size (in bytes).
    322 *
    323 * @name byteLength
    324 * @memberof RandomStream.prototype
    325 * @type {(PositiveInteger|null)}
    326 */
    327 setReadOnlyAccessor( RandomStream.prototype, 'byteLength', getStateSize );
    328 
    329 /**
    330 * Implements the `_read` method.
    331 *
    332 * @private
    333 * @name _read
    334 * @memberof RandomStream.prototype
    335 * @type {Function}
    336 * @param {number} size - number (of bytes) to read
    337 * @returns {void}
    338 */
    339 setNonEnumerableReadOnly( RandomStream.prototype, '_read', read );
    340 
    341 /**
    342 * Gracefully destroys a stream, providing backward compatibility.
    343 *
    344 * @name destroy
    345 * @memberof RandomStream.prototype
    346 * @type {Function}
    347 * @param {(string|Object|Error)} [error] - error
    348 * @returns {RandomStream} Stream instance
    349 */
    350 setNonEnumerableReadOnly( RandomStream.prototype, 'destroy', destroy );
    351 
    352 
    353 // EXPORTS //
    354 
    355 module.exports = RandomStream;