time-to-botec

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

nd_accessors.js (4489B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2021 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 numel = require( './../../../base/numel' );
     24 var vind2bind = require( './../../../base/vind2bind' );
     25 
     26 
     27 // VARIABLES //
     28 
     29 var MODE = 'throw';
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Applies a unary callback to elements in an n-dimensional input ndarray and assigns results to elements in an equivalently shaped output ndarray.
     36 *
     37 * @private
     38 * @param {Object} x - object containing input ndarray meta data
     39 * @param {string} x.dtype - data type
     40 * @param {Collection} x.data - data buffer
     41 * @param {NonNegativeIntegerArray} x.shape - dimensions
     42 * @param {IntegerArray} x.strides - stride lengths
     43 * @param {NonNegativeInteger} x.offset - index offset
     44 * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
     45 * @param {Function} x.getter - callback for accessing `x` data buffer elements
     46 * @param {Object} y - object containing output ndarray meta data
     47 * @param {string} y.dtype - data type
     48 * @param {Collection} y.data - data buffer
     49 * @param {NonNegativeIntegerArray} y.shape - dimensions
     50 * @param {IntegerArray} y.strides - stride lengths
     51 * @param {NonNegativeInteger} y.offset - index offset
     52 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
     53 * @param {Function} y.setter - callback for setting `y` data buffer elements
     54 * @param {Callback} fcn - unary callback
     55 * @returns {void}
     56 *
     57 * @example
     58 * var Complex64Array = require( '@stdlib/array/complex64' );
     59 * var Complex64 = require( '@stdlib/complex/float32' );
     60 * var real = require( '@stdlib/complex/real' );
     61 * var imag = require( '@stdlib/complex/imag' );
     62 *
     63 * function scale( z ) {
     64 *     return new Complex64( real(z)*10.0, imag(z)*10.0 );
     65 * }
     66 *
     67 * // Create data buffers:
     68 * var xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
     69 * var ybuf = new Complex64Array( 4 );
     70 *
     71 * // Define the shape of the input and output arrays:
     72 * var shape = [ 2, 2 ];
     73 *
     74 * // Define the array strides:
     75 * var sx = [ 2, 1 ];
     76 * var sy = [ 2, 1 ];
     77 *
     78 * // Define the index offsets:
     79 * var ox = 0;
     80 * var oy = 0;
     81 *
     82 * // Define getters and setters:
     83 * function getter( buf, idx ) {
     84 *     return buf.get( idx );
     85 * }
     86 *
     87 * function setter( buf, idx, value ) {
     88 *     buf.set( value, idx );
     89 * }
     90 *
     91 * // Create the input and output ndarray-like objects:
     92 * var x = {
     93 *     'dtype': 'complex64',
     94 *     'data': xbuf,
     95 *     'shape': shape,
     96 *     'strides': sx,
     97 *     'offset': ox,
     98 *     'order': 'row-major',
     99 *     'getter': getter
    100 * };
    101 * var y = {
    102 *     'dtype': 'complex64',
    103 *     'data': ybuf,
    104 *     'shape': shape,
    105 *     'strides': sy,
    106 *     'offset': oy,
    107 *     'order': 'row-major',
    108 *     'setter': setter
    109 * };
    110 *
    111 * // Apply the unary function:
    112 * unarynd( x, y, scale );
    113 *
    114 * var v = y.data.get( 0 );
    115 *
    116 * var re = real( v );
    117 * // returns 10.0
    118 *
    119 * var im = imag( v );
    120 * // returns 20.0
    121 */
    122 function unarynd( x, y, fcn ) {
    123 	var xbuf;
    124 	var ybuf;
    125 	var ordx;
    126 	var ordy;
    127 	var len;
    128 	var get;
    129 	var set;
    130 	var sh;
    131 	var sx;
    132 	var sy;
    133 	var ox;
    134 	var oy;
    135 	var ix;
    136 	var iy;
    137 	var i;
    138 
    139 	sh = x.shape;
    140 
    141 	// Compute the total number of elements over which to iterate:
    142 	len = numel( sh );
    143 
    144 	// Cache references to the input and output ndarray data buffers:
    145 	xbuf = x.data;
    146 	ybuf = y.data;
    147 
    148 	// Cache references the respective stride arrays:
    149 	sx = x.strides;
    150 	sy = y.strides;
    151 
    152 	// Cache the indices of the first indexed elements in the respective ndarrays:
    153 	ox = x.offset;
    154 	oy = y.offset;
    155 
    156 	// Cache the respective array orders:
    157 	ordx = x.order;
    158 	ordy = y.order;
    159 
    160 	// Cache accessors:
    161 	get = x.getter;
    162 	set = y.setter;
    163 
    164 	// Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory...
    165 	for ( i = 0; i < len; i++ ) {
    166 		ix = vind2bind( sh, sx, ox, ordx, i, MODE );
    167 		iy = vind2bind( sh, sy, oy, ordy, i, MODE );
    168 		set( ybuf, iy, fcn( get( xbuf, ix ) ) );
    169 	}
    170 }
    171 
    172 
    173 // EXPORTS //
    174 
    175 module.exports = unarynd;