time-to-botec

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

cumax.js (2021B)


      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 isnan = require( '@stdlib/math/base/assert/is-nan' );
     24 var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Computes the cumulative maximum of a strided array.
     31 *
     32 * @param {PositiveInteger} N - number of indexed elements
     33 * @param {NumericArray} x - input array
     34 * @param {integer} strideX - `x` stride length
     35 * @param {NumericArray} y - output array
     36 * @param {integer} strideY - `y` stride length
     37 * @returns {NumericArray} output array
     38 *
     39 * @example
     40 * var x = [ 1.0, -2.0, 2.0 ];
     41 * var y = [ 0.0, 0.0, 0.0 ];
     42 * var N = x.length;
     43 *
     44 * var v = cumax( N, x, 1, y, 1 );
     45 * // returns [ 1.0, 1.0, 2.0 ]
     46 */
     47 function cumax( N, x, strideX, y, strideY ) {
     48 	var max;
     49 	var ix;
     50 	var iy;
     51 	var v;
     52 	var i;
     53 
     54 	if ( N <= 0 ) {
     55 		return y;
     56 	}
     57 	if ( strideX < 0 ) {
     58 		ix = (1-N) * strideX;
     59 	} else {
     60 		ix = 0;
     61 	}
     62 	if ( strideY < 0 ) {
     63 		iy = (1-N) * strideY;
     64 	} else {
     65 		iy = 0;
     66 	}
     67 	max = x[ ix ];
     68 	y[ iy ] = max;
     69 
     70 	iy += strideY;
     71 	i = 1;
     72 	if ( isnan( max ) === false ) {
     73 		for ( i; i < N; i++ ) {
     74 			ix += strideX;
     75 			v = x[ ix ];
     76 			if ( isnan( v ) ) {
     77 				max = v;
     78 				break;
     79 			}
     80 			if ( v > max || ( v === max && isPositiveZero( v ) ) ) {
     81 				max = v;
     82 			}
     83 			y[ iy ] = max;
     84 			iy += strideY;
     85 		}
     86 	}
     87 	if ( isnan( max ) ) {
     88 		for ( i; i < N; i++ ) {
     89 			y[ iy ] = max;
     90 			iy += strideY;
     91 		}
     92 	}
     93 	return y;
     94 }
     95 
     96 
     97 // EXPORTS //
     98 
     99 module.exports = cumax;