time-to-botec

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

iget.js (2585B)


      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 * Returns an array element located a specified linear view index.
     25 *
     26 * ## Notes
     27 *
     28 * -   For zero-dimensional arrays, the input argument is ignored and, for clarity, should not be provided.
     29 *
     30 * @private
     31 * @param {integer} [idx] - linear view index
     32 * @returns {*} array element
     33 */
     34 function iget( idx ) {
     35 	/* eslint-disable no-invalid-this */
     36 	var strides;
     37 	var shape;
     38 	var ndims;
     39 	var ind;
     40 	var s;
     41 	var i;
     42 
     43 	ndims = this._ndims;
     44 	if ( ndims === 0 ) {
     45 		if ( this._accessors ) {
     46 			return this._buffer.get( this._offset );
     47 		}
     48 		return this._buffer[ this._offset ];
     49 	}
     50 	if ( this._flags.ROW_MAJOR_CONTIGUOUS || this._flags.COLUMN_MAJOR_CONTIGUOUS ) { // eslint-disable-line max-len
     51 		// Trivial case where we have all positive strides...
     52 		if ( this._iterationOrder === 1 ) {
     53 			if ( this._accessors ) {
     54 				return this._buffer.get( this._offset+idx );
     55 			}
     56 			return this._buffer[ this._offset+idx ];
     57 		}
     58 		// Trivial case where we have all negative strides...
     59 		if ( this._iterationOrder === -1 ) {
     60 			if ( this._accessors ) {
     61 				return this._buffer.get( this.offset-idx );
     62 			}
     63 			return this._buffer[ this._offset-idx ];
     64 		}
     65 	}
     66 	// The approach which follows is to resolve a view index to its subscripts and then plug the subscripts into the standard formula for computing the linear index in the underlying data buffer...
     67 	shape = this._shape;
     68 	strides = this._strides;
     69 	ind = this._offset;
     70 	if ( this._order === 'column-major' ) {
     71 		for ( i = 0; i < ndims; i++ ) {
     72 			s = idx % shape[ i ];
     73 			idx -= s;
     74 			idx /= shape[ i ];
     75 			ind += s * strides[ i ];
     76 		}
     77 		if ( this._accessors ) {
     78 			return this._buffer.get( ind );
     79 		}
     80 		return this._buffer[ ind ];
     81 	}
     82 	// Case: row-major
     83 	for ( i = ndims-1; i >= 0; i-- ) {
     84 		s = idx % shape[ i ];
     85 		idx -= s;
     86 		idx /= shape[ i ];
     87 		ind += s * strides[ i ];
     88 	}
     89 	if ( this._accessors ) {
     90 		return this._buffer.get( ind );
     91 	}
     92 	return this._buffer[ ind ];
     93 }
     94 
     95 
     96 // EXPORTS //
     97 
     98 module.exports = iget;