All files / ndarray/base/unary-strided1d-dispatch/lib index_of_types.js

77.77% Statements 84/108
100% Branches 1/1
0% Functions 0/1
77.77% Lines 84/108

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 1091x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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';
 
// MAIN //
 
/**
* Returns the first row index at which a given one-dimensional array of types can be found in a two-dimensional reference array of types (or `-1` if not found).
*
* ## Notes
*
* -   The intended use case for this function is for type dispatch (i.e., given a set of array data types, find a matching interface according the interface's accepted array data types).
* -   The function assumes that `x` is stored in row-major order.
* -   The function assumes that the number of indexed elements in `y` equals the number of columns in `x`.
* -   The function returns a row index. To convert to a linear index, multiply `strideX1` by the return value.
*
* @private
* @param {NonNegativeInteger} N - number of rows in `x` (size of first dimension)
* @param {NonNegativeInteger} M - number of columns in `x` (size of second dimension)
* @param {ArrayLikeObject} x - input two-dimensional reference array
* @param {integer} strideX1 - `x` stride length along first dimension
* @param {integer} strideX2 - `x` stride length along second dimension
* @param {NonNegativeInteger} offsetX - `x` starting index
* @param {ArrayLikeObject} y - search array
* @param {integer} strideY - `y` stride length
* @param {NonNegativeInteger} offsetY - `y` starting index
* @returns {integer} row index (if found) and `-1` otherwise
*
* @example
* // Define a reference array to search:
* var types = [
*     'float64', 'float64', 'float64',
*     'float32', 'float32', 'float32',
*     'uint32', 'uint32', 'float64',
*     'int32', 'int32', 'float64',
*     'uint16', 'uint16', 'float64',
*     'int16', 'int16', 'float64',
*     'uint8', 'uint8', 'float64',
*     'int8', 'int8', 'float64'
* ];
*
* // Define reference array dimensions:
* var N = 8; // rows
* var M = 3; // columns
*
* // Define a search array:
* y1 = [
*     'float32', 'float32', 'float32',
* ];
*
* // Find the list of types:
* var r1 = indexOfTypes( N, M, types, M, 1, 0, y1, 1, 0 );
* // returns 1
*
// Define a search array:
* y2 = [
*     'float32', 'float32', 'float64',
* ];
*
* // Find the list of types:
* var r2 = indexOfTypes( N, M, types, M, 1, 0, y2, 1, 0 );
* // returns -1
*/
function indexOfTypes( N, M, x, strideX1, strideX2, offsetX, y, strideY, offsetY ) { // eslint-disable-line max-len
	var ix;
	var iy;
	var i;
	var j;

	// Search for the first row which matches `y`...
	ix = offsetX;
	for ( i = 0; i < N; i++ ) {
		iy = offsetY;
		for ( j = 0; j < M; j++ ) {
			if ( x[ ix+(j*strideX2) ] !== y[ iy ] ) {
				break;
			}
			iy += strideY;
		}
		// If we successfully iterated over all columns, then that means we've found a match...
		if ( j === M ) {
			return i;
		}
		ix += strideX1;
	}
	return -1;
}
 
 
// EXPORTS //
 
module.exports = indexOfTypes;