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 226 227 228 229 230 231 232 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 23x 23x 23x 23x 23x 23x 76x 76x 23x 23x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 106x 106x 106x 106x 106x 106x 83x 106x 10x 10x 106x 10x 10x 106x 9x 9x 106x 13x 13x 106x 11x 11x 106x 11x 11x 42x 106x 1x 1x 106x 9x 9x 106x 19x 19x 3x 3x 106x 13x 13x 3x 3x 13x 106x 3x 3x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 5x 2x 2x 3x 3x 48x 48x 53x 85x 85x 21x 11x 21x 10x 10x 21x 64x 64x 64x 27x 27x 27x 27x 53x 3x 3x 24x 53x 14x 53x 10x 10x 24x 53x 13x 53x 11x 11x 53x 6x 6x 53x 9x 9x 9x 53x 106x 2x 2x 2x 2x 2x | /** * @license Apache-2.0 * * Copyright (c) 2021 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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var isFunctionArray = require( '@stdlib/assert/is-function-array' ); var isFunction = require( '@stdlib/assert/is-function' ); var isCollection = require( '@stdlib/assert/is-collection' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var format = require( '@stdlib/string/format' ); var getDType = require( '@stdlib/ndarray/dtype' ); var resolveEnum = require( '@stdlib/ndarray/base/dtype-resolve-enum' ); var indexOfTypes = require( './index_of_types.js' ); // FUNCTIONS // /** * Returns a list of data type enumeration constants. * * @private * @param {Collection} types - list of types * @returns {IntegerArray} list of data type enumeration constants */ function types2enums( types ) { var out; var i; out = []; for ( i = 0; i < types.length; i++ ) { out.push( resolveEnum( types[ i ] ) ); } return out; } // MAIN // /** * Returns an ndarray function interface which performs multiple dispatch. * * @param {(FunctionArray|Function)} fcns - list of ndarray functions * @param {Collection} types - one-dimensional list of ndarray argument data types * @param {(Collection|null)} data - ndarray function data (e.g., callbacks) * @param {PositiveInteger} nargs - total number of ndarray function interface arguments * @param {NonNegativeInteger} nin - number of input ndarrays * @param {NonNegativeInteger} nout - number of output ndarrays * @throws {TypeError} first argument must be either a function or an array of functions * @throws {TypeError} second argument must be an array-like object * @throws {TypeError} third argument must be an array-like object or `null` * @throws {Error} third and first arguments must have the same number of elements * @throws {TypeError} fourth argument must be a positive integer * @throws {TypeError} fifth argument must be a nonnegative integer * @throws {TypeError} sixth argument must be a nonnegative integer * @throws {Error} fourth argument must equal the specified number of input and output arrays * @throws {Error} number of types must match the number of functions times the total number of array arguments for each function * @throws {Error} interface must accept at least one input and/or output ndarray * @returns {Function} ndarray function interface * * @example * var unary = require( '@stdlib/ndarray/base/unary' ); * var abs = require( '@stdlib/math/base/special/abs' ); * var Float64Array = require( '@stdlib/array/float64' ); * var ndarray = require( '@stdlib/ndarray/ctor' ); * * var types = [ * 'float64', 'float64' * ]; * * var data = [ * abs * ]; * * var absolute = dispatch( unary, types, data, 2, 1, 1 ); * * // ... * * var xbuf = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] ); * var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); * * var x = ndarray( 'float64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' ); * var y = ndarray( 'float64', ybuf, [ 5 ], [ 1 ], 0, 'row-major' ); * * absolute( x, y ); * // ybuf => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ] */ function dispatch( fcns, types, data, nargs, nin, nout ) { var narrays; var nfcns; var fcn; if ( isFunction( fcns ) ) { fcn = fcns; } else if ( !isFunctionArray( fcns ) ) { throw new TypeError( format( 'invalid argument. First argument must be either a function or an array of functions. Value: `%s`.', fcns ) ); } if ( !isCollection( types ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', types ) ); } if ( !isCollection( data ) && data !== null ) { throw new TypeError( format( 'invalid argument. Third argument must be an array-like object or null. Value: `%s`.', data ) ); } if ( !isPositiveInteger( nargs ) ) { throw new TypeError( format( 'invalid argument. Fourth argument must be a positive integer. Value: `%s`.', nargs ) ); } if ( !isNonNegativeInteger( nin ) ) { throw new TypeError( format( 'invalid argument. Fifth argument must be a nonnegative integer. Value: `%s`.', nin ) ); } if ( !isNonNegativeInteger( nout ) ) { throw new TypeError( format( 'invalid argument. Sixth argument must be a nonnegative integer. Value: `%s`.', nout ) ); } narrays = nin + nout; if ( narrays === 0 ) { throw new Error( 'invalid arguments. Interface must accept at least one input and/or output ndarray. Based on the provided arguments, `nin+nout` equals `0`.' ); } if ( nargs !== narrays ) { throw new Error( 'invalid arguments. Fourth argument does not equal the number of input and output ndarrays.' ); } if ( fcn ) { nfcns = types.length / narrays; if ( !isInteger( nfcns ) ) { throw new Error( 'invalid argument. Unexpected number of types. A type must be specified for each input and output ndarray for each provided ndarray function.' ); } } else { nfcns = fcns.length; if ( types.length !== nfcns*narrays ) { throw new Error( 'invalid argument. Unexpected number of types. A type must be specified for each input and output ndarray for each provided ndarray function.' ); } } if ( data && data.length !== nfcns ) { throw new Error( 'invalid argument. The third argument must have the same number of elements as the first argument.' ); } types = types2enums( types ); return dispatcher; /** * ndarray function interface which performs multiple dispatch. * * @private * @param {ndarrayLike} x - ndarray * @param {...ndarrayLike} args - ndarray arguments * @throws {Error} insufficient arguments * @throws {Error} too many arguments * @throws {TypeError} input array arguments must be ndarray-like objects * @throws {TypeError} output array arguments must be ndarray-like objects * @throws {TypeError} unable to resolve an ndarray function supporting the provided ndarray argument data types * @returns {(ndarrayLike|Array<ndarrayLike>|void)} destination array(s) */ function dispatcher() { var arrays; var dtypes; var argc; var idx; var v; var f; var i; argc = arguments.length; if ( argc !== nargs ) { if ( argc < nargs ) { throw new Error( 'invalid invocation. Insufficient arguments.' ); } throw new Error( 'invalid invocation. Too many arguments.' ); } arrays = []; dtypes = []; for ( i = 0; i < nargs; i++ ) { v = arguments[ i ]; if ( !isndarrayLike( v ) ) { if ( i < nin ) { throw new TypeError( format( 'invalid argument. Input array must be an ndarray-like object. Value: `%s`.', v ) ); } else { throw new TypeError( format( 'invalid argument. Output array must be an ndarray-like object. Value: `%s`.', v ) ); } } arrays.push( v ); dtypes.push( resolveEnum( getDType( v ) ) ); } // Resolve the ndarray function satisfying the input array types: idx = indexOfTypes( nfcns, narrays, types, narrays, 1, 0, dtypes, 1, 0 ); // eslint-disable-line max-len // Check whether we were able to successfully resolve an ndarray function: if ( idx < 0 ) { throw new TypeError( 'invalid arguments. Unable to resolve an ndarray function supporting the provided array argument data types.' ); } // Retrieve the ndarray function: if ( fcn ) { f = fcn; } else { f = fcns[ idx ]; } // Evaluate the ndarray function: if ( data ) { f( arrays, data[ idx ] ); } else { f( arrays ); } if ( nout === 1 ) { return arrays[ narrays-1 ]; } if ( nout === 0 ) { return; } return arrays.slice( nin ); } } // EXPORTS // module.exports = dispatch; |