time-to-botec

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

main.js (2404B)


      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 // MAIN //
     22 
     23 /**
     24 * Applies a unary callback to elements in a strided input array according to elements in a strided mask array and assigns results to elements in a strided output array.
     25 *
     26 * @param {ArrayLikeObject<Collection>} arrays - array-like object containing one input array, a mask array, and one output array
     27 * @param {NonNegativeIntegerArray} shape - array-like object containing a single element, the number of indexed elements
     28 * @param {IntegerArray} strides - array-like object containing the stride lengths for the strided arrays
     29 * @param {Callback} fcn - unary callback
     30 * @returns {void}
     31 *
     32 * @example
     33 * var Float64Array = require( '@stdlib/array/float64' );
     34 * var Uint8Array = require( '@stdlib/array/uint8' );
     35 *
     36 * function scale( x ) {
     37 *     return x * 10.0;
     38 * }
     39 *
     40 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     41 * var m = new Uint8Array( [ 0, 0, 1, 0, 0 ] );
     42 * var y = new Float64Array( x.length );
     43 *
     44 * var shape = [ x.length ];
     45 * var strides = [ 1, 1, 1 ];
     46 *
     47 * mskunary( [ x, m, y ], shape, strides, scale );
     48 *
     49 * console.log( y );
     50 * // => <Float64Array>[ 10.0, 20.0, 0.0, 40.0, 50.0 ]
     51 */
     52 function mskunary( arrays, shape, strides, fcn ) {
     53 	var sx;
     54 	var sm;
     55 	var sy;
     56 	var ix;
     57 	var im;
     58 	var iy;
     59 	var x;
     60 	var m;
     61 	var y;
     62 	var N;
     63 	var i;
     64 
     65 	N = shape[ 0 ];
     66 	if ( N <= 0 ) {
     67 		return;
     68 	}
     69 	sx = strides[ 0 ];
     70 	sm = strides[ 1 ];
     71 	sy = strides[ 2 ];
     72 	if ( sx < 0 ) {
     73 		ix = (1-N) * sx;
     74 	} else {
     75 		ix = 0;
     76 	}
     77 	if ( sm < 0 ) {
     78 		im = (1-N) * sm;
     79 	} else {
     80 		im = 0;
     81 	}
     82 	if ( sy < 0 ) {
     83 		iy = (1-N) * sy;
     84 	} else {
     85 		iy = 0;
     86 	}
     87 	x = arrays[ 0 ];
     88 	m = arrays[ 1 ];
     89 	y = arrays[ 2 ];
     90 	for ( i = 0; i < N; i++ ) {
     91 		if ( m[ im ] === 0 ) {
     92 			y[ iy ] = fcn( x[ ix ] );
     93 		}
     94 		ix += sx;
     95 		im += sm;
     96 		iy += sy;
     97 	}
     98 }
     99 
    100 
    101 // EXPORTS //
    102 
    103 module.exports = mskunary;