All files ndarray.js

100% Statements 225/225
100% Branches 35/35
100% Functions 2/2
100% Lines 225/225

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 2263x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 33x 33x 9x 9x 33x 9x 9x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 47x 47x 47x 47x 47x 47x 5x 5x 5x 42x 42x 42x 47x 4x 4x 47x 11x 11x 47x 11x 11x 47x 11x 11x 5x 5x 5x 47x 1x 47x 1x 4x 1x 3x 2x 2x 47x 1x 47x 1x 4x 1x 3x 2x 2x 5x 5x 5x 5x 47x 33x 3x 3x 3x 3x 3x  
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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.
*/
 
/* eslint-disable max-len */
 
'use strict';
 
// MODULES //
 
var isFunction = require( '@stdlib/assert/is-function' );
var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var resolve = require( '@stdlib/strided/base/dtype-resolve-enum' );
var reinterpretComplex64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var reinterpretComplex128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var offsetView = require( '@stdlib/strided/base/offset-view' );
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var format = require( '@stdlib/string/format' );
 
 
// VARIABLES //
 
var COMPLEX64 = resolve( 'complex64' );
var COMPLEX128 = resolve( 'complex128' );
var BOOLEAN = resolve( 'bool' );
 
 
// MAIN //
 
/**
* Returns a function which dispatches to a native add-on applying a unary function to an input strided array according to a mask strided array using alternative indexing semantics.
*
* ## Notes
*
* -   The returned function has the following signature:
*
*     ```text
*     f( N, dtypeX, x, strideX, offsetX, dtypeMask, mask, strideMask, offsetMask, dtypeY, y, strideY, offsetY )
*     ```
*
*     where
*
*     -   **N**: number of indexed elements.
*     -   **dtypeX**: `x` data type.
*     -   **x**: input array.
*     -   **strideX**: `x` stride length.
*     -   **offsetX**: starting `x` index.
*     -   **dtypeMask**: `mask` data type.
*     -   **mask**: mask array.
*     -   **strideMask**: `mask` stride length.
*     -   **offsetMask**: starting `mask` index.
*     -   **dtypeY**: `y` data type.
*     -   **y**: output array.
*     -   **strideY**: `y` stride length.
*     -   **offsetY**: starting `y` index.
*
* -   The add-on function should have the following signature:
*
*     ```text
*     f( N, dtypeX, x, strideX, dtypeMask, mask, strideMask, dtypeY, y, strideY )
*     ```
*
*     where
*
*     -   **N**: number of indexed elements.
*     -   **dtypeX**: `x` data type (enumeration constant).
*     -   **x**: input array.
*     -   **strideX**: `x` stride length.
*     -   **dtypeMask**: `mask` data type (enumeration constant).
*     -   **mask**: mask array.
*     -   **strideMask**: `mask` stride length.
*     -   **dtypeY**: `y` data type (enumeration constant).
*     -   **y**: output array.
*     -   **strideY**: `y` stride length.
*
* -   The fallback function should have the following signature:
*
*     ```text
*     f( N, dtypeX, x, strideX, offsetX, dtypeMask, mask, strideMask, offsetMask, dtypeY, y, strideY, offsetY )
*     ```
*
*     where
*
*     -   **N**: number of indexed elements.
*     -   **dtypeX**: `x` data type.
*     -   **x**: input array.
*     -   **strideX**: `x` stride length.
*     -   **offsetX**: starting `x` index.
*     -   **dtypeMask**: `mask` data type.
*     -   **mask**: mask array.
*     -   **strideMask**: `mask` stride length.
*     -   **offsetMask**: starting `mask` index.
*     -   **dtypeY**: `y` data type.
*     -   **y**: output array.
*     -   **strideY**: `y` stride length.
*     -   **offsetY**: starting `y` index.
*
* @param {Function} addon - add-on interface
* @param {Function} fallback - fallback function
* @throws {TypeError} first argument must be a function
* @throws {TypeError} second argument must be a function
* @returns {Function} dispatch function
*
* @example
* function addon( N, dtypeX, x, strideX, dtypeMask, mask, strideMask, dtypeY, y, strideY ) {
*     // Call into native add-on...
* }
*
* function fallback( N, dtypeX, x, strideX, offsetX, dtypeMask, mask, strideMask, offsetMask, dtypeY, y, strideY, offsetY ) {
*     // Fallback JavaScript implementation...
* }
*
* // Create a dispatch function:
* var f = dispatch( addon, fallback );
*
* // ...
*
* // Invoke the dispatch function with strided array arguments:
* f( 2, 'generic', [ 1, 2 ], 1, 0, 'generic', [ 0, 0 ], 1, 0, 'generic', [ 0, 0 ], 1, 0 );
*/
function dispatch( addon, fallback ) {
	if ( !isFunction( addon ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', addon ) );
	}
	if ( !isFunction( fallback ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', fallback ) );
	}
	return dispatcher;
 
	/**
	* Dispatches to a native add-on.
	*
	* @private
	* @param {integer} N - number of indexed elements
	* @param {*} dtypeX - `x` data type
	* @param {Collection} x - input array
	* @param {integer} strideX - `x` stride length
	* @param {NonNegativeInteger} offsetX - starting `x` index
	* @param {*} dtypeMask - `mask` data type
	* @param {Collection} mask - mask array
	* @param {integer} strideMask - `mask` stride length
	* @param {NonNegativeInteger} offsetMask - starting `mask` index
	* @param {*} dtypeY - `y` data type
	* @param {Collection} y - destination array
	* @param {integer} strideY - `y` stride length
	* @param {NonNegativeInteger} offsetY - starting `y` index
	* @throws {TypeError} fifth argument must be a nonnegative integer
	* @throws {TypeError} ninth argument must be a nonnegative integer
	* @throws {TypeError} thirteenth argument must be a nonnegative integer
	* @throws {TypeError} unable to resolve a strided array function supporting the provided array argument data types
	* @returns {Collection} `y`
	*/
	function dispatcher( N, dtypeX, x, strideX, offsetX, dtypeMask, mask, strideMask, offsetMask, dtypeY, y, strideY, offsetY ) { // eslint-disable-line max-params
		var viewMask;
		var viewX;
		var viewY;
 
		// WARNING: we assume that, if we're provided something resembling a typed array, we're provided a typed array; however, this can lead to potential unintended errors as the native add-on may not work with non-typed array objects (e.g., generic arrays)...
		if ( !isTypedArrayLike( x ) || !isTypedArrayLike( y ) || !isTypedArrayLike( mask ) ) {
			fallback( N, dtypeX, x, strideX, offsetX, dtypeMask, mask, strideMask, offsetMask, dtypeY, y, strideY, offsetY );
			return y;
		}
		dtypeX = resolve( dtypeX );
		dtypeY = resolve( dtypeY );
		dtypeMask = resolve( dtypeMask );
		if ( dtypeX === null || dtypeY === null || dtypeMask === null ) {
			throw new TypeError( 'invalid arguments. Unable to resolve a strided array function supporting the provided array argument data types.' );
		}
		if ( !isNonNegativeInteger( offsetX ) ) {
			throw new TypeError( format( 'invalid argument. Input array offset must be a nonnegative integer. Value: `%s`.', offsetX ) );
		}
		if ( !isNonNegativeInteger( offsetY ) ) {
			throw new TypeError( format( 'invalid argument. Output array offset must be a nonnegative integer. Value: `%s`.', offsetY ) );
		}
		if ( !isNonNegativeInteger( offsetMask ) ) {
			throw new TypeError( format( 'invalid argument. Mask array offset must be a nonnegative integer. Value: `%s`.', offsetMask ) );
		}
		offsetX = minViewBufferIndex( N, strideX, offsetX );
		offsetY = minViewBufferIndex( N, strideY, offsetY );
		offsetMask = minViewBufferIndex( N, strideMask, offsetMask );
		if ( dtypeX === COMPLEX64 ) {
			viewX = reinterpretComplex64( x, offsetX );
		} else if ( dtypeX === COMPLEX128 ) {
			viewX = reinterpretComplex128( x, offsetX );
		} else if ( dtypeX === BOOLEAN ) {
			viewX = reinterpretBoolean( x, offsetX );
		} else {
			viewX = offsetView( x, offsetX );
		}
		if ( dtypeY === COMPLEX64 ) {
			viewY = reinterpretComplex64( y, offsetY );
		} else if ( dtypeY === COMPLEX128 ) {
			viewY = reinterpretComplex128( y, offsetY );
		} else if ( dtypeY === BOOLEAN ) {
			viewY = reinterpretBoolean( y, offsetY );
		} else {
			viewY = offsetView( y, offsetY );
		}
		viewMask = offsetView( mask, offsetMask );
 
		addon( N, dtypeX, viewX, strideX, dtypeMask, viewMask, strideMask, dtypeY, viewY, strideY );
		return y;
	}
}
 
 
// EXPORTS //
 
module.exports = dispatch;