main.js (3542B)
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 var abs = require( '@stdlib/math/base/special/abs' ); 25 26 27 // MAIN // 28 29 /** 30 * Converts a linear index in an underlying data buffer to a linear index in an array view. 31 * 32 * @param {NonNegativeIntegerArray} shape - array shape 33 * @param {IntegerArray} strides - stride array 34 * @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array 35 * @param {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) 36 * @param {integer} idx - linear index in an underlying data buffer 37 * @param {string} mode - specifies how to handle a linear index which exceeds array dimensions 38 * @throws {RangeError} linear index must not exceed array dimensions 39 * @returns {NonNegativeInteger} linear index in an array view 40 * 41 * @example 42 * var shape = [ 3, 3 ]; 43 * var strides = [ -3, 1 ]; 44 * var offset = 6; 45 * var order = 'row-major'; 46 * var mode = 'throw'; 47 * 48 * var ind = bind2vind( shape, strides, offset, order, 7, mode ); 49 * // returns 1 50 */ 51 function bind2vind( shape, strides, offset, order, idx, mode ) { 52 var ndims; 53 var len; 54 var ind; 55 var k; 56 var s; 57 var i; 58 59 ndims = shape.length; 60 len = 1; 61 for ( i = 0; i < ndims; i++ ) { 62 len *= shape[ i ]; 63 } 64 if ( mode === 'clamp' ) { 65 if ( idx < 0 ) { 66 idx = 0; 67 } else if ( idx >= len ) { 68 idx = len - 1; 69 } 70 } else if ( mode === 'wrap' ) { 71 if ( idx < 0 ) { 72 idx += len; // slight optimization to avoid modulo arithmetic when |idx| <= len 73 if ( idx < 0 ) { 74 idx %= len; 75 if ( idx !== 0 ) { 76 idx += len; 77 } 78 } 79 } else if ( idx >= len ) { 80 idx -= len; // slight optimization to avoid modulo arithmetic when len < idx <= 2*len 81 if ( idx >= len ) { 82 idx %= len; 83 } 84 } 85 } else if ( idx < 0 || idx >= len ) { 86 throw new RangeError( 'invalid argument. Linear index must not exceed array dimensions. Number of array elements: ' + len + '. Value: `' + idx + '`.' ); 87 } 88 // Trivial case where the offset into the underlying data buffer is 0... 89 if ( offset === 0 ) { 90 return idx; 91 } 92 // The approach which follows is to resolve a buffer index to its subscripts and then plug the subscripts into the standard formula for computing the linear index in the array view (i.e., where all strides are positive and offset is 0)... 93 ind = 0; 94 if ( order === 'column-major' ) { 95 for ( i = ndims-1; i >= 0; i-- ) { 96 s = strides[ i ]; 97 if ( s < 0 ) { 98 k = trunc( idx/s ); 99 idx -= k * s; 100 k += shape[ i ] - 1; 101 } else { 102 k = trunc( idx/s ); 103 idx -= k * s; 104 } 105 ind += k * abs( s ); 106 } 107 return ind; 108 } 109 // Case: row-major 110 for ( i = 0; i < ndims; i++ ) { 111 s = strides[ i ]; 112 if ( s < 0 ) { 113 k = trunc( idx/s ); 114 idx -= k * s; 115 k += shape[ i ] - 1; 116 } else { 117 k = trunc( idx/s ); 118 idx -= k * s; 119 } 120 ind += k * abs( s ); 121 } 122 return ind; 123 } 124 125 126 // EXPORTS // 127 128 module.exports = bind2vind;