time-to-botec

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

ndarray.js (2474B)


      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 {NonNegativeIntegerArray} offsets - array-like object containing the starting indices (i.e., index offsets) for the strided arrays
     30 * @param {Callback} fcn - unary callback
     31 * @returns {void}
     32 *
     33 * @example
     34 * var Float64Array = require( '@stdlib/array/float64' );
     35 * var Uint8Array = require( '@stdlib/array/uint8' );
     36 *
     37 * function scale( x ) {
     38 *     return x * 10.0;
     39 * }
     40 *
     41 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     42 * var m = new Uint8Array( [ 0, 0, 1, 0, 0 ] );
     43 * var y = new Float64Array( x.length );
     44 *
     45 * var shape = [ x.length ];
     46 * var strides = [ 1, 1, 1 ];
     47 * var offsets = [ 0, 0, 0 ];
     48 *
     49 * mskunary( [ x, m, y ], shape, strides, offsets, scale );
     50 *
     51 * console.log( y );
     52 * // => <Float64Array>[ 10.0, 20.0, 0.0, 40.0, 50.0 ]
     53 */
     54 function mskunary( arrays, shape, strides, offsets, fcn ) {
     55 	var sx;
     56 	var sm;
     57 	var sy;
     58 	var ix;
     59 	var im;
     60 	var iy;
     61 	var x;
     62 	var m;
     63 	var y;
     64 	var N;
     65 	var i;
     66 
     67 	N = shape[ 0 ];
     68 	if ( N <= 0 ) {
     69 		return;
     70 	}
     71 	ix = offsets[ 0 ];
     72 	im = offsets[ 1 ];
     73 	iy = offsets[ 2 ];
     74 	sx = strides[ 0 ];
     75 	sm = strides[ 1 ];
     76 	sy = strides[ 2 ];
     77 	x = arrays[ 0 ];
     78 	m = arrays[ 1 ];
     79 	y = arrays[ 2 ];
     80 	for ( i = 0; i < N; i++ ) {
     81 		if ( m[ im ] === 0 ) {
     82 			y[ iy ] = fcn( x[ ix ] );
     83 		}
     84 		ix += sx;
     85 		im += sm;
     86 		iy += sy;
     87 	}
     88 }
     89 
     90 
     91 // EXPORTS //
     92 
     93 module.exports = mskunary;