time-to-botec

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

main.js (3101B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     24 var iteratorSymbol = require( '@stdlib/symbol/iterator' );
     25 var floor = require( './../../../../base/special/floor' );
     26 var sqrt = require( './../../../../base/special/sqrt' );
     27 var validate = require( './validate.js' );
     28 
     29 
     30 // VARIABLES //
     31 
     32 var MAX_ITER = 9007199349647256; // FLOAT64_MAX_SAFE_INTEGER + floor(sqrt(FLOAT64_MAX_SAFE_INTEGER))
     33 
     34 
     35 // MAIN //
     36 
     37 /**
     38 * Returns an iterator which generates a sequence of nonsquares.
     39 *
     40 * ## Notes
     41 *
     42 * -   If an environment supports `Symbol.iterator`, the returned iterator is iterable.
     43 *
     44 * @param {Options} [options] - function options
     45 * @param {NonNegativeInteger} [options.iter=9007199349647256] - number of iterations
     46 * @throws {TypeError} options argument must be an object
     47 * @throws {TypeError} must provide valid options
     48 * @returns {Iterator} iterator
     49 *
     50 * @example
     51 * var iter = iterNonSquaresSeq();
     52 *
     53 * var v = iter.next().value;
     54 * // returns 2
     55 *
     56 * v = iter.next().value;
     57 * // returns 3
     58 *
     59 * v = iter.next().value;
     60 * // returns 5
     61 *
     62 * // ...
     63 */
     64 function iterNonSquaresSeq( options ) {
     65 	var opts;
     66 	var iter;
     67 	var FLG;
     68 	var err;
     69 	var i;
     70 
     71 	opts = {
     72 		'iter': MAX_ITER
     73 	};
     74 	if ( arguments.length ) {
     75 		err = validate( opts, options );
     76 		if ( err ) {
     77 			throw err;
     78 		}
     79 	}
     80 	i = 0;
     81 
     82 	// Create an iterator protocol-compliant object:
     83 	iter = {};
     84 	setReadOnly( iter, 'next', next );
     85 	setReadOnly( iter, 'return', end );
     86 
     87 	// If an environment supports `Symbol.iterator`, make the iterator iterable:
     88 	if ( iteratorSymbol ) {
     89 		setReadOnly( iter, iteratorSymbol, factory );
     90 	}
     91 	return iter;
     92 
     93 	/**
     94 	* Returns an iterator protocol-compliant object containing the next iterated value.
     95 	*
     96 	* @private
     97 	* @returns {Object} iterator protocol-compliant object
     98 	*/
     99 	function next() {
    100 		i += 1;
    101 		if ( FLG || i > opts.iter ) {
    102 			return {
    103 				'done': true
    104 			};
    105 		}
    106 		return {
    107 			'value': i + floor( 0.5+sqrt(i) ),
    108 			'done': false
    109 		};
    110 	}
    111 
    112 	/**
    113 	* Finishes an iterator.
    114 	*
    115 	* @private
    116 	* @param {*} [value] - value to return
    117 	* @returns {Object} iterator protocol-compliant object
    118 	*/
    119 	function end( value ) {
    120 		FLG = true;
    121 		if ( arguments.length ) {
    122 			return {
    123 				'value': value,
    124 				'done': true
    125 			};
    126 		}
    127 		return {
    128 			'done': true
    129 		};
    130 	}
    131 
    132 	/**
    133 	* Returns a new iterator.
    134 	*
    135 	* @private
    136 	* @returns {Iterator} iterator
    137 	*/
    138 	function factory() {
    139 		return iterNonSquaresSeq( opts );
    140 	}
    141 }
    142 
    143 
    144 // EXPORTS //
    145 
    146 module.exports = iterNonSquaresSeq;