time-to-botec

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

main.js (4095B)


      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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
     24 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     25 var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
     26 var NINF = require( '@stdlib/constants/float64/ninf' );
     27 var Float64Array = require( '@stdlib/array/float64' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns an accumulator function which incrementally computes a moving maximum value.
     34 *
     35 * @param {PositiveInteger} W - window size
     36 * @throws {TypeError} must provide a positive integer
     37 * @returns {Function} accumulator function
     38 *
     39 * @example
     40 * var accumulator = incrmmax( 3 );
     41 *
     42 * var m = accumulator();
     43 * // returns null
     44 *
     45 * m = accumulator( 2.0 );
     46 * // returns 2.0
     47 *
     48 * m = accumulator( -5.0 );
     49 * // returns 2.0
     50 *
     51 * m = accumulator( 3.0 );
     52 * // returns 3.0
     53 *
     54 * m = accumulator( 5.0 );
     55 * // returns 5.0
     56 *
     57 * m = accumulator();
     58 * // returns 5.0
     59 */
     60 function incrmmax( W ) {
     61 	var buf;
     62 	var max;
     63 	var N;
     64 	var i;
     65 	if ( !isPositiveInteger( W ) ) {
     66 		throw new TypeError( 'invalid argument. Must provide a positive integer. Value: `' + W + '`.' );
     67 	}
     68 	buf = new Float64Array( W );
     69 	max = NINF;
     70 	i = -1;
     71 	N = 0;
     72 
     73 	return accumulator;
     74 
     75 	/**
     76 	* If provided a value, the accumulator function returns an updated maximum. If not provided a value, the accumulator function returns the current maximum.
     77 	*
     78 	* @private
     79 	* @param {number} [x] - input value
     80 	* @returns {(number|null)} maximum value or null
     81 	*/
     82 	function accumulator( x ) {
     83 		var v;
     84 		var k;
     85 		if ( arguments.length === 0 ) {
     86 			if ( N === 0 ) {
     87 				return null;
     88 			}
     89 			return max;
     90 		}
     91 		// Update the index for managing the circular buffer:
     92 		i = (i+1) % W;
     93 
     94 		// Case: update initial window...
     95 		if ( N < W ) {
     96 			N += 1;
     97 			if (
     98 				isnan( x ) ||
     99 				x > max ||
    100 				( x === max && isPositiveZero( x ) )
    101 			) {
    102 				max = x;
    103 			}
    104 		}
    105 		// Case: incoming value is NaN or greater than current maximum value...
    106 		else if ( isnan( x ) || x > max ) {
    107 			max = x;
    108 		}
    109 		// Case: outgoing value is the current maximum and the new value is less than the maximum, and, thus, we need to find a new maximum among the current values...
    110 		else if ( ( buf[ i ] === max && x < max ) || isnan( buf[ i ] ) ) {
    111 			max = x;
    112 			for ( k = 0; k < W; k++ ) {
    113 				if ( k !== i ) {
    114 					v = buf[ k ];
    115 					if ( isnan( v ) ) {
    116 						max = v;
    117 						break; // no need to continue searching
    118 					}
    119 					if ( v > max || ( v === max && isPositiveZero( v ) ) ) {
    120 						max = v;
    121 					}
    122 				}
    123 			}
    124 		}
    125 		// Case: outgoing value is the current maximum, which is zero, and the new value is also zero, and, thus, we need to correctly handle signed zeros...
    126 		else if ( buf[ i ] === max && x === max && x === 0.0 ) {
    127 			if ( isPositiveZero( x ) ) {
    128 				max = x;
    129 			} else if ( isPositiveZero( buf[ i ] ) ) {
    130 				// Because the outgoing and incoming are different signs (+,-), we need to search the buffer to see if it contains a positive zero. If so, the maximum value remains positive zero; otherwise, the maximum value is incoming value...
    131 				max = x;
    132 				for ( k = 0; k < W; k++ ) {
    133 					if ( k !== i && isPositiveZero( buf[ k ] ) ) {
    134 						max = buf[ k ];
    135 						break;
    136 					}
    137 				}
    138 			}
    139 			// Case: the outgoing and incoming values are both negative zero, so nothing changes
    140 		}
    141 		// Case: updating existing window; however, the maximum value does not change so nothing to do but update our buffer...
    142 
    143 		buf[ i ] = x;
    144 		return max;
    145 	}
    146 }
    147 
    148 
    149 // EXPORTS //
    150 
    151 module.exports = incrmmax;