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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 517x 517x 517x 517x 517x 517x 517x 517x 517x 439x 517x 10x 10x 517x 10x 10x 517x 9x 9x 517x 13x 13x 517x 11x 11x 517x 11x 11x 453x 517x 1x 1x 517x 384x 384x 3x 3x 517x 68x 68x 3x 3x 68x 517x 3x 3x 443x 517x 197x 517x 238x 246x 8x 8x 435x 517x 238x 431x 197x 197x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 435x 2658x 2658x 2658x 2658x 2658x 2658x 2658x 2658x 2658x 2658x 2658x 2658x 2658x 2658x 2658x 209x 149x 149x 60x 60x 2449x 2658x 120x 120x 2329x 2329x 2329x 2329x 2619x 9729x 9729x 2329x 2329x 2329x 2619x 9029x 9029x 420x 300x 400x 120x 120x 420x 8609x 8609x 2268x 1060x 1060x 1060x 4020x 4020x 231x 165x 220x 66x 66x 231x 3789x 3789x 1060x 1678x 1678x 2588x 4732x 4732x 420x 300x 400x 120x 120x 420x 4312x 4522x 2103x 2103x 462x 342x 420x 120x 120x 462x 4732x 462x 336x 420x 126x 126x 462x 3388x 3388x 334x 334x 334x 334x 2658x 56x 56x 278x 2658x 162x 2658x 116x 116x 278x 2658x 148x 74x 74x 74x 74x 2658x 65x 65x 65x 65x 2658x 80x 80x 2458x 108x 108x 90x 2658x 517x 13x 13x 13x 13x 13x | /** * @license Apache-2.0 * * Copyright (c) 2020 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 format = require( '@stdlib/string/format' ); var abs = require( '@stdlib/math/base/special/abs' ); var indexOfTypes = require( './index_of_types.js' ); // MAIN // /** * Returns a strided array function interface which performs multiple dispatch. * * @param {(FunctionArray|Function)} fcns - list of strided array functions * @param {Collection} types - one-dimensional list of strided array argument data types * @param {(Collection|null)} data - strided array function data (e.g., callbacks) * @param {PositiveInteger} nargs - total number of strided array function interface arguments (including data types, strides, and offsets) * @param {NonNegativeInteger} nin - number of input strided arrays * @param {NonNegativeInteger} nout - number of output strided arrays * @throws {TypeError} first argument must be either a function or an array of functions * @throws {TypeError} second argument must be an 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 be compatible with 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 strided input and/or output array * @returns {Function} strided array function interface * * @example * var unary = require( '@stdlib/strided/base/unary' ); * var abs = require( '@stdlib/math/base/special/abs' ); * var Float64Array = require( '@stdlib/array/float64' ); * * var types = [ * 'float64', 'float64' * ]; * * var data = [ * abs * ]; * * var strided = dispatch( unary, types, data, 7, 1, 1 ); * * // ... * * var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] ); * var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); * * strided( x.length, 'float64', x, 1, 'float64', y, 1 ); * // y => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ] */ function dispatch( fcns, types, data, nargs, nin, nout ) { var strideArgs; var hasOffsets; var narrays; var nfcns; var iout; 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 strided input and/or output array. Based on the provided arguments, `nin+nout` equals `0`.' ); } 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 strided input and output array for each provided strided array 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 strided input and output array for each provided strided array 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.' ); } // Determine whether the strided array interface includes offsets: if ( (narrays*3)+1 === nargs ) { hasOffsets = false; } else if ( (narrays*4)+1 === nargs ) { hasOffsets = true; } else { throw new Error( 'invalid argument. Fourth argument is incompatible with the number of strided input and output arrays.' ); } // Determine the "stride" for accessing related arguments: if ( hasOffsets ) { strideArgs = 4; } else { strideArgs = 3; } // Compute the index of the first output strided array argument: iout = ( nin*strideArgs ) + 1; return dispatcher; /** * Strided array function interface which performs multiple dispatch. * * @private * @param {integer} N - number of indexed elements * @param {*} dtypeX - data type for `x` * @param {Collection} x - strided array * @param {integer} strideX - index increment for `x` * @param {...(Collection|integer|NonNegativeInteger)} args - array arguments (data types, arrays, strides, and offsets) * @throws {Error} insufficient arguments * @throws {Error} too many arguments * @throws {TypeError} first argument must be an integer * @throws {TypeError} input array strides must be integers * @throws {TypeError} output array strides must be integers * @throws {TypeError} input array offsets must be nonnegative integers * @throws {TypeError} output array offsets must be nonnegative integers * @throws {TypeError} input array arguments must be array-like objects * @throws {TypeError} output array arguments must be array-like objects * @throws {RangeError} input array arguments must have sufficient elements based on the associated stride and the number of indexed elements * @throws {RangeError} output array arguments must have sufficient elements based on the associated stride and the number of indexed elements * @throws {TypeError} unable to resolve a strided array function supporting the provided array argument data types * @returns {(Collection|Array<Collection>|void)} destination array(s) */ function dispatcher() { var strides; var offsets; var arrays; var dtypes; var shape; var argc; var idx; var N; var v; var f; var i; var j; argc = arguments.length; if ( argc !== nargs ) { if ( argc < nargs ) { throw new Error( 'invalid invocation. Insufficient arguments.' ); } throw new Error( 'invalid invocation. Too many arguments.' ); } N = arguments[ 0 ]; if ( !isInteger( N ) ) { throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', N ) ); } shape = [ N ]; // data types for both input and output strided arrays are every `strideArgs` arguments beginning from the second argument... dtypes = []; for ( i = 1; i < nargs; i += strideArgs ) { dtypes.push( arguments[ i ] ); } // Strides for both input and output strided arrays are every `strideArgs` arguments beginning from the fourth argument... strides = []; for ( i = 3; i < nargs; i += strideArgs ) { v = arguments[ i ]; if ( !isInteger( v ) ) { if ( i < iout ) { throw new TypeError( format( 'invalid argument. Input array stride must be an integer. Value: `%s`.', v ) ); } else { throw new TypeError( format( 'invalid argument. Output array stride must be an integer. Value: `%s`.', v ) ); } } strides.push( v ); } if ( hasOffsets ) { // Offsets for both input and output strided arrays are every `strideArgs` arguments beginning from the fifth argument... offsets = []; for ( i = 4; i < nargs; i += strideArgs ) { v = arguments[ i ]; if ( !isNonNegativeInteger( v ) ) { if ( i < iout ) { throw new TypeError( format( 'invalid argument. Input array offset must be a nonnegative integer. Value: `%s`.', v ) ); } else { throw new TypeError( format( 'invalid argument. Output array offset must be a nonnegative integer. Value: `%s`.', v ) ); } } offsets.push( v ); } } // Input and output strided arrays are every `strideArgs` arguments beginning from the third argument... arrays = []; for ( i = 2; i < nargs; i += strideArgs ) { v = arguments[ i ]; if ( !isCollection( v ) ) { if ( i < iout ) { throw new TypeError( format( 'invalid argument. Input array must be an array-like object. Value: `%s`.', v ) ); } else { throw new TypeError( format( 'invalid argument. Output array must be an array-like object. Value: `%s`.', v ) ); } } j = (i-2) / strideArgs; if ( hasOffsets ) { idx = offsets[ j ] + ( (N-1)*strides[j] ); if ( N > 0 && (idx < 0 || idx >= v.length) ) { if ( i < iout ) { throw new RangeError( 'invalid argument. Input array has insufficient elements based on the associated stride and the number of indexed elements.' ); } else { throw new RangeError( 'invalid argument. Output array has insufficient elements based on the associated stride and the number of indexed elements.' ); } } } else if ( (N-1)*abs(strides[j]) >= v.length ) { if ( i < iout ) { throw new RangeError( 'invalid argument. Input array has insufficient elements based on the associated stride and the number of indexed elements.' ); } else { throw new RangeError( 'invalid argument. Output array has insufficient elements based on the associated stride and the number of indexed elements.' ); } } arrays.push( v ); } // Resolve the strided array 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 a strided array function: if ( idx < 0 ) { throw new TypeError( 'invalid arguments. Unable to resolve a strided array function supporting the provided array argument data types.' ); } // Retrieve the strided array function: if ( fcn ) { f = fcn; } else { f = fcns[ idx ]; } // Evaluate the strided array function: if ( data ) { if ( hasOffsets ) { f( arrays, shape, strides, offsets, data[ idx ] ); } else { f( arrays, shape, strides, data[ idx ] ); } } else if ( hasOffsets ) { f( arrays, shape, strides, offsets ); } else { f( arrays, shape, strides ); } if ( nout === 1 ) { return arrays[ narrays-1 ]; } if ( nout === 0 ) { return; } return arrays.slice( nin ); } } // EXPORTS // module.exports = dispatch; |