All files main.js

100% Statements 118/118
100% Branches 14/14
100% Functions 1/1
100% Lines 118/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 1191x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 153x 15x 15x 138x 138x 138x 138x 138x 138x 138x 153x 97x 97x 97x 97x 32x 32x 153x 41x 41x 106x 153x 2x 2x 104x 104x 104x 104x 153x 210x 66x 66x 144x 144x 38x 38x 153x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var getIndex = require( '@stdlib/ndarray/base/sub2ind' );
var format = require( '@stdlib/string/format' );
var defaults = require( './defaults.json' );
var validate = require( './validate.js' );
 
 
// MAIN //
 
/**
* Converts subscripts to a linear index.
*
* ## Notes
*
* -   The function accepts the following "modes":
*
*     -   **throw**: throw an error when a subscript exceeds array dimensions.
*     -   **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.
*     -   **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.
*     -   **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.
*
* -   If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.
*
* @param {NonNegativeIntegerArray} shape - array shape
* @param {...integer} i - subscripts
* @param {Options} [options] - function options
* @param {(StringArray|string)} [options.mode=["throw"]] - specifies how to handle subscripts which exceed array dimensions
* @param {string} [options.order="row-major"] - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
* @throws {TypeError} first argument must be an array-like object containing nonnegative integers
* @throws {TypeError} subscripts must be integer valued
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @throws {RangeError} must provide subscripts which do not exceed array dimensions
* @throws {RangeError} number of subscripts much match the number of dimensions
* @returns {NonNegativeInteger} linear index
*
* @example
* var i = sub2ind( [ 3, 3, 3 ], 1, 2, 2 );
* // returns 17
*/
function sub2ind() {
	var options;
	var shape;
	var ndims;
	var args;
	var opts;
	var err;
	var len;
	var i;
	var j;
 
	shape = arguments[ 0 ];
	if ( !isNonNegativeIntegerArray( shape ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an array-like object containing nonnegative integers. Value: `%s`.', shape ) );
	}
	len = arguments.length;
	ndims = shape.length;
 
	opts = {};
	opts.mode = defaults.mode.slice();
	opts.order = defaults.order;
 
	if ( len > ndims+1 ) {
		j = len - 1;
		options = arguments[ j ];
		err = validate( opts, options );
		if ( err ) {
			throw err;
		}
	} else {
		j = len;
	}
	i = 1;
	if ( j-i !== ndims ) {
		throw new RangeError( format( 'invalid argument. Number of provided subscripts must match the number of dimensions. ndims: `%u`. Number of subscripts: `%u`.', ndims, j-i ) );
	}
	args = new Array( ndims+4 );
	args[ 0 ] = shape;
	args[ 1 ] = shape2strides( shape, opts.order );
	args[ 2 ] = 0; // strides are positive, so offset is always zero
	for ( ; i < j; i++ ) {
		if ( !isInteger( arguments[ i ] ) ) {
			throw new TypeError( format( 'invalid argument. Subscripts must be integer valued. Argument: `%u`. Value: `%s`.', i, arguments[ i ] ) );
		}
		args[ i+2 ] = arguments[ i ];
	}
	args[ i+2 ] = opts.mode; // i+2 == args.length-1
	return getIndex.apply( null, args );
}
 
 
// EXPORTS //
 
module.exports = sub2ind;