time-to-botec

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

mskrange.js (2006B)


      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 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Computes the range of a strided array according to a mask.
     30 *
     31 * @param {PositiveInteger} N - number of indexed elements
     32 * @param {NumericArray} x - input array
     33 * @param {integer} strideX - `x` stride length
     34 * @param {NumericArray} mask - mask array
     35 * @param {integer} strideMask - `mask` stride length
     36 * @returns {number} range
     37 *
     38 * @example
     39 * var x = [ 1.0, -2.0, 4.0, 2.0 ];
     40 * var mask = [ 0, 0, 1, 0 ];
     41 *
     42 * var v = mskrange( x.length, x, 1, mask, 1 );
     43 * // returns 4.0
     44 */
     45 function mskrange( N, x, strideX, mask, strideMask ) {
     46 	var max;
     47 	var min;
     48 	var ix;
     49 	var im;
     50 	var v;
     51 	var i;
     52 
     53 	if ( N <= 0 ) {
     54 		return NaN;
     55 	}
     56 	if ( strideX < 0 ) {
     57 		ix = (1-N) * strideX;
     58 	} else {
     59 		ix = 0;
     60 	}
     61 	if ( strideMask < 0 ) {
     62 		im = (1-N) * strideMask;
     63 	} else {
     64 		im = 0;
     65 	}
     66 	for ( i = 0; i < N; i++ ) {
     67 		if ( mask[ im ] === 0 ) {
     68 			break;
     69 		}
     70 		ix += strideX;
     71 		im += strideMask;
     72 	}
     73 	if ( i === N ) {
     74 		return NaN;
     75 	}
     76 	min = x[ ix ];
     77 	if ( isnan( min ) ) {
     78 		return min;
     79 	}
     80 	max = min;
     81 	i += 1;
     82 	for ( i; i < N; i++ ) {
     83 		ix += strideX;
     84 		im += strideMask;
     85 		if ( mask[ im ] ) {
     86 			continue;
     87 		}
     88 		v = x[ ix ];
     89 		if ( isnan( v ) ) {
     90 			return v;
     91 		}
     92 		if ( v < min ) {
     93 			min = v;
     94 		} else if ( v > max ) {
     95 			max = v;
     96 		}
     97 	}
     98 	return max - min;
     99 }
    100 
    101 
    102 // EXPORTS //
    103 
    104 module.exports = mskrange;