time-to-botec

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

assign.js (3530B)


      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 // MODULES //
     22 
     23 var trunc = require( '@stdlib/math/base/special/trunc' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Converts a linear index to an array of subscripts.
     30 *
     31 * @param {NonNegativeIntegerArray} shape - array shape
     32 * @param {IntegerArray} strides - stride array
     33 * @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array
     34 * @param {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
     35 * @param {integer} idx - linear index
     36 * @param {string} mode - specifies how to handle a linear index which exceeds array dimensions
     37 * @param {(Array|TypedArray|Object)} out - destination object
     38 * @throws {RangeError} linear index must not exceed array dimensions
     39 * @returns {(Array|TypedArray|Object)} subscripts
     40 *
     41 * @example
     42 * var shape = [ 3, 3, 3 ];
     43 * var strides = [ 9, 6, 1 ];
     44 * var offset = 0;
     45 * var order = 'row-major';
     46 *
     47 * var s = [ 0, 0, 0 ];
     48 * var out = ind2sub( shape, strides, offset, order, 17, 'throw', s );
     49 * // returns [ 1, 2, 2 ]
     50 *
     51 * var bool = ( out === s );
     52 * // returns true
     53 */
     54 function ind2sub( shape, strides, offset, order, idx, mode, out ) {
     55 	var ndims;
     56 	var len;
     57 	var k;
     58 	var s;
     59 	var i;
     60 
     61 	ndims = shape.length;
     62 	len = 1;
     63 	for ( i = 0; i < ndims; i++ ) {
     64 		len *= shape[ i ];
     65 	}
     66 	if ( mode === 'clamp' ) {
     67 		if ( idx < 0 ) {
     68 			idx = 0;
     69 		} else if ( idx >= len ) {
     70 			idx = len - 1;
     71 		}
     72 	} else if ( mode === 'wrap' ) {
     73 		if ( idx < 0 ) {
     74 			idx += len; // slight optimization to avoid modulo arithmetic when |idx| <= len
     75 			if ( idx < 0 ) {
     76 				idx %= len;
     77 				if ( idx !== 0 ) {
     78 					idx += len;
     79 				}
     80 			}
     81 		} else if ( idx >= len ) {
     82 			idx -= len; // slight optimization to avoid modulo arithmetic when len < idx <= 2*len
     83 			if ( idx >= len ) {
     84 				idx %= len;
     85 			}
     86 		}
     87 	} else if ( idx < 0 || idx >= len ) {
     88 		throw new RangeError( 'invalid argument. Linear index must not exceed array dimensions. Number of array elements: ' + len + '. Value: `' + idx + '`.' );
     89 	}
     90 	if ( offset === 0 ) {
     91 		if ( order === 'column-major' ) {
     92 			for ( i = 0; i < ndims; i++ ) {
     93 				s = idx % shape[ i ];
     94 				idx -= s;
     95 				idx /= shape[ i ];
     96 				out[ i ] = s;
     97 			}
     98 			return out;
     99 		}
    100 		// Case: row-major
    101 		for ( i = ndims-1; i >= 0; i-- ) {
    102 			s = idx % shape[ i ];
    103 			idx -= s;
    104 			idx /= shape[ i ];
    105 			out[ i ] = s;
    106 		}
    107 		return out;
    108 	}
    109 	if ( order === 'column-major' ) {
    110 		for ( i = ndims-1; i >= 0; i-- ) {
    111 			s = strides[ i ];
    112 			if ( s < 0 ) {
    113 				k = trunc( idx/s );
    114 				idx -= k * s;
    115 				out[ i ] = shape[ i ] - 1 + k;
    116 			} else {
    117 				k = trunc( idx/s );
    118 				idx -= k * s;
    119 				out[ i ] = k;
    120 			}
    121 		}
    122 		return out;
    123 	}
    124 	// Case: row-major
    125 	for ( i = 0; i < ndims; i++ ) {
    126 		s = strides[ i ];
    127 		if ( s < 0 ) {
    128 			k = trunc( idx/s );
    129 			idx -= k * s;
    130 			out[ i ] = shape[ i ] - 1 + k;
    131 		} else {
    132 			k = trunc( idx/s );
    133 			idx -= k * s;
    134 			out[ i ] = k;
    135 		}
    136 	}
    137 	return out;
    138 }
    139 
    140 
    141 // EXPORTS //
    142 
    143 module.exports = ind2sub;