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