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