<commit_before>'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/types/ndarray/base/shape2strides' );
var getIndex = require( '@stdlib/types/ndarray/base/sub2ind' );
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`: throws 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.
*
*
* @param {NonNegativeIntegerArray} shape - array shape
* @param {...integer} i - subscripts
* @param {Options} [options] - function options
* @param {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( 'invalid input argument. First argument must be an array-like object containing nonnegative integers. Value: `' + shape + '`.' );
	}
	len = arguments.length;
	ndims = shape.length;

	opts = {};
	opts.mode = defaults.mode;
	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( 'invalid input argument. Number of provided subscripts must match the number of dimensions. ndims: ' + ndims + '. Number of subscripts: ' + (j-i) + '.' );
	}
	args = new Array( ndims+4 );
	args[ 0 ] = shape;
	args[ 1 ] = shape2strides( shape, opts.order );
	args[ 2 ] = 0;
	for ( ; i < j; i++ ) {
		if ( !isInteger( arguments[ i ] ) ) {
			throw new TypeError( 'invalid input argument. Subscripts must be integer valued. Argument: ' + i + '. Value: `' + arguments[ i ] + '`.' );
		}
		args[ i+2 ] = arguments[ i ];
	}
	args[ i+2 ] = opts.mode; // i+2 == args.length-1
	return getIndex.apply( null, args );
} // end FUNCTION sub2ind()


// EXPORTS //

module.exports = sub2ind;<commit_msg>Add comment<commit_after>'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/types/ndarray/base/shape2strides' );
var getIndex = require( '@stdlib/types/ndarray/base/sub2ind' );
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`: throws 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.
*
*
* @param {NonNegativeIntegerArray} shape - array shape
* @param {...integer} i - subscripts
* @param {Options} [options] - function options
* @param {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( 'invalid input argument. First argument must be an array-like object containing nonnegative integers. Value: `' + shape + '`.' );
	}
	len = arguments.length;
	ndims = shape.length;

	opts = {};
	opts.mode = defaults.mode;
	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( 'invalid input argument. Number of provided subscripts must match the number of dimensions. ndims: ' + ndims + '. Number of subscripts: ' + (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( 'invalid input argument. Subscripts must be integer valued. Argument: ' + i + '. Value: `' + arguments[ i ] + '`.' );
		}
		args[ i+2 ] = arguments[ i ];
	}
	args[ i+2 ] = opts.mode; // i+2 == args.length-1
	return getIndex.apply( null, args );
} // end FUNCTION sub2ind()


// EXPORTS //

module.exports = sub2ind;
{'fraction_non_alphanumeric': 0.13559587112918361, 'fraction_numerical': 0.009383797309978105, 'mean_word_length': 3.6040316774658026, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6099842', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2008, 'n_words': 708}