time-to-botec

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

main.js (3870B)


      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 isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
     24 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
     25 var shape2strides = require( './../../base/shape2strides' );
     26 var getIndex = require( './../../base/sub2ind' );
     27 var defaults = require( './defaults.json' );
     28 var validate = require( './validate.js' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Converts subscripts to a linear index.
     35 *
     36 * ## Notes
     37 *
     38 * -   The function accepts the following "modes":
     39 *
     40 *     -   `throw`: throws an error when a subscript exceeds array dimensions.
     41 *     -   `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.
     42 *     -   `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.
     43 *
     44 * -   If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.
     45 *
     46 *
     47 * @param {NonNegativeIntegerArray} shape - array shape
     48 * @param {...integer} i - subscripts
     49 * @param {Options} [options] - function options
     50 * @param {(StringArray|string)} [options.mode=["throw"]] - specifies how to handle subscripts which exceed array dimensions
     51 * @param {string} [options.order="row-major"] - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
     52 * @throws {TypeError} first argument must be an array-like object containing nonnegative integers
     53 * @throws {TypeError} subscripts must be integer valued
     54 * @throws {TypeError} options argument must be an object
     55 * @throws {TypeError} must provide valid options
     56 * @throws {RangeError} must provide subscripts which do not exceed array dimensions
     57 * @throws {RangeError} number of subscripts much match the number of dimensions
     58 * @returns {NonNegativeInteger} linear index
     59 *
     60 * @example
     61 * var i = sub2ind( [ 3, 3, 3 ], 1, 2, 2 );
     62 * // returns 17
     63 */
     64 function sub2ind() {
     65 	var options;
     66 	var shape;
     67 	var ndims;
     68 	var args;
     69 	var opts;
     70 	var err;
     71 	var len;
     72 	var i;
     73 	var j;
     74 
     75 	shape = arguments[ 0 ];
     76 	if ( !isNonNegativeIntegerArray( shape ) ) {
     77 		throw new TypeError( 'invalid argument. First argument must be an array-like object containing nonnegative integers. Value: `' + shape + '`.' );
     78 	}
     79 	len = arguments.length;
     80 	ndims = shape.length;
     81 
     82 	opts = {};
     83 	opts.mode = defaults.mode.slice();
     84 	opts.order = defaults.order;
     85 
     86 	if ( len > ndims+1 ) {
     87 		j = len - 1;
     88 		options = arguments[ j ];
     89 		err = validate( opts, options );
     90 		if ( err ) {
     91 			throw err;
     92 		}
     93 	} else {
     94 		j = len;
     95 	}
     96 	i = 1;
     97 	if ( j-i !== ndims ) {
     98 		throw new RangeError( 'invalid argument. Number of provided subscripts must match the number of dimensions. ndims: ' + ndims + '. Number of subscripts: ' + (j-i) + '.' );
     99 	}
    100 	args = new Array( ndims+4 );
    101 	args[ 0 ] = shape;
    102 	args[ 1 ] = shape2strides( shape, opts.order );
    103 	args[ 2 ] = 0; // strides are positive, so offset is always zero
    104 	for ( ; i < j; i++ ) {
    105 		if ( !isInteger( arguments[ i ] ) ) {
    106 			throw new TypeError( 'invalid argument. Subscripts must be integer valued. Argument: ' + i + '. Value: `' + arguments[ i ] + '`.' );
    107 		}
    108 		args[ i+2 ] = arguments[ i ];
    109 	}
    110 	args[ i+2 ] = opts.mode; // i+2 == args.length-1
    111 	return getIndex.apply( null, args );
    112 }
    113 
    114 
    115 // EXPORTS //
    116 
    117 module.exports = sub2ind;