time-to-botec

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

assign.js (2535B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 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 * Computes the minimum and maximum linear indices in an underlying data buffer which are accessible to an array view and assigns results to a provided output array.
     25 *
     26 * @param {NonNegativeIntegerArray} shape - array shape
     27 * @param {IntegerArray} strides - stride array
     28 * @param {NonNegativeInteger} offset - index offset
     29 * @param {(Array|TypedArray|Object)} out - output object
     30 * @returns {(Array|TypedArray|Object)} linear indices
     31 *
     32 * @example
     33 * var shape = [ 10, 10 ];
     34 * var strides = [ 10, 1 ];
     35 * var offset = 10;
     36 *
     37 * var out = [ 0, 0 ];
     38 * var idx = minmaxViewBufferIndex( shape, strides, offset, out );
     39 * // returns [ 10, 109 ]
     40 *
     41 * var bool = ( idx === out );
     42 * // returns true
     43 *
     44 * @example
     45 * var shape = [ 10, 10 ];
     46 * var strides = [ -10, -1 ];
     47 * var offset = 99;
     48 *
     49 * var out = [ 0, 0 ];
     50 * var idx = minmaxViewBufferIndex( shape, strides, offset, out );
     51 * // returns [ 0, 99 ]
     52 *
     53 * var bool = ( idx === out );
     54 * // returns true
     55 *
     56 * @example
     57 * var shape = [ 10, 10 ];
     58 * var strides = [ 1, 10 ];
     59 * var offset = 10;
     60 *
     61 * var out = [ 0, 0 ];
     62 * var idx = minmaxViewBufferIndex( shape, strides, offset, out );
     63 * // returns [ 10, 109 ]
     64 *
     65 * var bool = ( idx === out );
     66 * // returns true
     67 *
     68 * @example
     69 * var shape = [ 10, 10 ];
     70 * var strides = [ -1, -10 ];
     71 * var offset = 99;
     72 *
     73 * var out = [ 0, 0 ];
     74 * var idx = minmaxViewBufferIndex( shape, strides, offset, out );
     75 * // returns [ 0, 99 ]
     76 *
     77 * var bool = ( idx === out );
     78 * // returns true
     79 */
     80 function minmaxViewBufferIndex( shape, strides, offset, out ) {
     81 	var ndims;
     82 	var min;
     83 	var max;
     84 	var s;
     85 	var i;
     86 
     87 	ndims = shape.length;
     88 	min = offset;
     89 	max = offset;
     90 	for ( i = 0; i < ndims; i++ ) {
     91 		s = strides[ i ];
     92 		if ( s > 0 ) {
     93 			max += s * ( shape[i]-1 );
     94 		} else if ( s < 0 ) {
     95 			min += s * ( shape[i]-1 ); // decrements min
     96 		}
     97 	}
     98 	out[ 0 ] = min;
     99 	out[ 1 ] = max;
    100 	return out;
    101 }
    102 
    103 
    104 // EXPORTS //
    105 
    106 module.exports = minmaxViewBufferIndex;