time-to-botec

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

object_mode.js (3175B)


      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 isObject = require( '@stdlib/assert/is-plain-object' );
     24 var copy = require( '@stdlib/utils/copy' );
     25 var RandomStream = require( './main.js' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns an "objectMode" readable stream for generating pseudorandom numbers drawn from a hypergeometric distribution.
     32 *
     33 * @param {NonNegativeInteger} [N] - population size
     34 * @param {NonNegativeInteger} [K] - subpopulation size
     35 * @param {NonNegativeInteger} [n] - number of draws
     36 * @param {Options} [options] - stream options
     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 objects to store in an internal buffer before ceasing to generate additional pseudorandom numbers
     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 * @throws {TypeError} `N` must be a nonnegative integer
     46 * @throws {TypeError} `K` must be a nonnegative integer
     47 * @throws {TypeError} `n` must be a nonnegative integer
     48 * @throws {RangeError} `n` must be less than or equal to `N`
     49 * @throws {RangeError} `K` must be less than or equal to `N`
     50 * @throws {TypeError} options argument must be an object
     51 * @throws {TypeError} must provide valid options
     52 * @throws {Error} must provide a valid state
     53 * @returns {RandomStream} Stream instance
     54 *
     55 * @example
     56 * var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
     57 *
     58 * function log( v ) {
     59 *    console.log( v );
     60 * }
     61 *
     62 * var opts = {
     63 *     'iter': 10
     64 * };
     65 *
     66 * var stream = objectMode( 20, 10, 7, opts );
     67 *
     68 * stream.pipe( inspectStream.objectMode( log ) );
     69 */
     70 function objectMode( N, K, n, options ) {
     71 	var opts;
     72 	if ( arguments.length > 3 ) {
     73 		opts = options;
     74 		if ( !isObject( opts ) ) {
     75 			throw new TypeError( 'invalid argument. Options must be an object. Value: `' + opts + '`.' );
     76 		}
     77 		opts = copy( options, 1 );
     78 	} else {
     79 		opts = {};
     80 	}
     81 	opts.objectMode = true;
     82 	return new RandomStream( N, K, n, opts );
     83 }
     84 
     85 
     86 // EXPORTS //
     87 
     88 module.exports = objectMode;