time-to-botec

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

ndarray.js (2154B)


      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 isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Calculates the minimum value of a strided array via a callback function.
     31 *
     32 * @param {PositiveInteger} N - number of indexed elements
     33 * @param {Collection} x - input array/collection
     34 * @param {integer} stride - index increment
     35 * @param {NonNegativeInteger} offset - starting index
     36 * @param {Callback} clbk - callback
     37 * @param {*} [thisArg] - execution context
     38 * @returns {number} minimum value
     39 *
     40 * @example
     41 * var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
     42 *
     43 * function accessor( v ) {
     44 *     return v * 2.0;
     45 * }
     46 *
     47 * var v = minBy( x.length, x, 1, 0, accessor );
     48 * // returns -10.0
     49 */
     50 function minBy( N, x, stride, offset, clbk, thisArg ) {
     51 	var min;
     52 	var ix;
     53 	var v;
     54 	var i;
     55 
     56 	if ( N <= 0 ) {
     57 		return NaN;
     58 	}
     59 	if ( N === 1 || stride === 0 ) {
     60 		v = clbk.call( thisArg, x[ 0 ], 0, 0, x );
     61 		if ( v === void 0 ) {
     62 			return NaN;
     63 		}
     64 		return v;
     65 	}
     66 	ix = offset;
     67 	for ( i = 0; i < N; i++ ) {
     68 		min = clbk.call( thisArg, x[ ix ], i, ix, x );
     69 		if ( min !== void 0 ) {
     70 			break;
     71 		}
     72 		ix += stride;
     73 	}
     74 	if ( i === N ) {
     75 		return NaN;
     76 	}
     77 	i += 1;
     78 	for ( i; i < N; i++ ) {
     79 		ix += stride;
     80 		v = clbk.call( thisArg, x[ ix ], i, ix, x );
     81 		if ( v === void 0 ) {
     82 			continue;
     83 		}
     84 		if ( isnan( v ) ) {
     85 			return v;
     86 		}
     87 		if ( v < min || ( v === min && isNegativeZero( v ) ) ) {
     88 			min = v;
     89 		}
     90 	}
     91 	return min;
     92 }
     93 
     94 
     95 // EXPORTS //
     96 
     97 module.exports = minBy;