time-to-botec

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

main.js (2073B)


      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 and assigns results to elements in a strided output array.
     25 *
     26 * @param {ArrayLikeObject<Collection>} arrays - array-like object containing one input 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 input and output arrays
     29 * @param {Callback} fcn - unary callback
     30 * @returns {void}
     31 *
     32 * @example
     33 * var Float64Array = require( '@stdlib/array/float64' );
     34 *
     35 * function scale( x ) {
     36 *     return x * 10.0;
     37 * }
     38 *
     39 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     40 * var y = new Float64Array( x.length );
     41 *
     42 * var shape = [ x.length ];
     43 * var strides = [ 1, 1 ];
     44 *
     45 * unary( [ x, y ], shape, strides, scale );
     46 *
     47 * console.log( y );
     48 * // => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0 ]
     49 */
     50 function unary( arrays, shape, strides, fcn ) {
     51 	var sx;
     52 	var sy;
     53 	var ix;
     54 	var iy;
     55 	var x;
     56 	var y;
     57 	var N;
     58 	var i;
     59 
     60 	N = shape[ 0 ];
     61 	if ( N <= 0 ) {
     62 		return;
     63 	}
     64 	sx = strides[ 0 ];
     65 	sy = strides[ 1 ];
     66 	if ( sx < 0 ) {
     67 		ix = (1-N) * sx;
     68 	} else {
     69 		ix = 0;
     70 	}
     71 	if ( sy < 0 ) {
     72 		iy = (1-N) * sy;
     73 	} else {
     74 		iy = 0;
     75 	}
     76 	x = arrays[ 0 ];
     77 	y = arrays[ 1 ];
     78 	for ( i = 0; i < N; i++ ) {
     79 		y[ iy ] = fcn( x[ ix ] );
     80 		ix += sx;
     81 		iy += sy;
     82 	}
     83 }
     84 
     85 
     86 // EXPORTS //
     87 
     88 module.exports = unary;