time-to-botec

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

main.js (2499B)


      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 ternary callback to strided input array elements and assigns results to elements in a strided output array.
     25 *
     26 * @param {ArrayLikeObject<Collection>} arrays - array-like object containing three input arrays 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 - ternary callback
     30 * @returns {void}
     31 *
     32 * @example
     33 * var Float64Array = require( '@stdlib/array/float64' );
     34 *
     35 * function add( x, y, z ) {
     36 *     return x + y + z;
     37 * }
     38 *
     39 * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     40 * var y = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     41 * var z = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     42 * var w = new Float64Array( x.length );
     43 *
     44 * var shape = [ x.length ];
     45 * var strides = [ 1, 1, 1, 1 ];
     46 *
     47 * ternary( [ x, y, z, w ], shape, strides, add );
     48 *
     49 * console.log( w );
     50 * // => <Float64Array>[ 3.0, 6.0, 9.0, 12.0, 15.0 ]
     51 */
     52 function ternary( arrays, shape, strides, fcn ) {
     53 	var sx;
     54 	var sy;
     55 	var sz;
     56 	var sw;
     57 	var ix;
     58 	var iy;
     59 	var iz;
     60 	var iw;
     61 	var x;
     62 	var y;
     63 	var z;
     64 	var w;
     65 	var N;
     66 	var i;
     67 
     68 	N = shape[ 0 ];
     69 	if ( N <= 0 ) {
     70 		return;
     71 	}
     72 	sx = strides[ 0 ];
     73 	sy = strides[ 1 ];
     74 	sz = strides[ 2 ];
     75 	sw = strides[ 3 ];
     76 	if ( sx < 0 ) {
     77 		ix = (1-N) * sx;
     78 	} else {
     79 		ix = 0;
     80 	}
     81 	if ( sy < 0 ) {
     82 		iy = (1-N) * sy;
     83 	} else {
     84 		iy = 0;
     85 	}
     86 	if ( sz < 0 ) {
     87 		iz = (1-N) * sz;
     88 	} else {
     89 		iz = 0;
     90 	}
     91 	if ( sw < 0 ) {
     92 		iw = (1-N) * sw;
     93 	} else {
     94 		iw = 0;
     95 	}
     96 	x = arrays[ 0 ];
     97 	y = arrays[ 1 ];
     98 	z = arrays[ 2 ];
     99 	w = arrays[ 3 ];
    100 	for ( i = 0; i < N; i++ ) {
    101 		w[ iw ] = fcn( x[ ix ], y[ iy ], z[ iz ] );
    102 		ix += sx;
    103 		iy += sy;
    104 		iz += sz;
    105 		iw += sw;
    106 	}
    107 }
    108 
    109 
    110 // EXPORTS //
    111 
    112 module.exports = ternary;