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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | 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 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 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. */ /* eslint-disable no-restricted-syntax, no-invalid-this */ 'use strict'; // MODULES // var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var hasProp = require( '@stdlib/assert/has-property' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var isObject = require( '@stdlib/assert/is-object' ); var isFunction = require( '@stdlib/assert/is-function' ); var isCollection = require( '@stdlib/assert/is-collection' ); var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' ); var isFunctionArray = require( '@stdlib/assert/is-function-array' ); var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' ); var isOutputDataTypePolicy = require( '@stdlib/ndarray/base/assert/is-output-data-type-policy' ); var contains = require( '@stdlib/array/base/assert/contains' ); var nullaryStrided1d = require( '@stdlib/ndarray/base/nullary-strided1d' ); var resolveEnum = require( '@stdlib/ndarray/base/dtype-resolve-enum' ); var ndims = require( '@stdlib/ndarray/ndims' ); var getDType = require( '@stdlib/ndarray/base/dtype' ); var zeroTo = require( '@stdlib/array/base/zero-to' ); var join = require( '@stdlib/array/base/join' ); var copy = require( '@stdlib/array/base/copy' ); var everyBy = require( '@stdlib/array/base/every-by' ); var objectAssign = require( '@stdlib/object/assign' ); var format = require( '@stdlib/string/format' ); var defaults = require( './defaults.json' ); var validate = require( './validate.js' ); 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 ] ) ); // note: we're assuming that `types[i]` is a known data type; otherwise, the resolved enum will be `null` } return out; } // MAIN // /** * Constructor for applying a strided function to an output ndarray. * * @constructor * @param {Object} table - dispatch table * @param {Function} table.default - default strided function * @param {StringArray} [table.types=[]] - one-dimensional list of ndarray data types describing specialized output ndarray argument signatures * @param {ArrayLikeObject<Function>} [table.fcns=[]] - list of strided functions which are specific to specialized output ndarray argument signatures * @param {ArrayLikeObject<StringArray>} odtypes - list containing lists of supported output data types for each ndarray argument * @param {Object} policies - policies * @param {string} policies.output - output data type policy * @param {Options} [options] - function options * @param {boolean} [options.strictTraversalOrder=false] - boolean specifying whether to require that element traversal match the memory layout of an output ndarray * @throws {TypeError} first argument must be an object having valid properties * @throws {Error} first argument must be an object having valid properties * @throws {TypeError} second argument must be an array containing arrays of supported data types * @throws {TypeError} third argument must be an object having supported policies * @throws {TypeError} options argument must be an object * @throws {TypeError} must provide valid options * @returns {NullaryStrided1dDispatch} instance * * @example * var base = require( '@stdlib/blas/ext/base/ndarray/gsorthp' ); * var dtypes = require( '@stdlib/ndarray/dtypes' ); * var ndarray2array = require( '@stdlib/ndarray/to-array' ); * var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var ndarray = require( '@stdlib/ndarray/base/ctor' ); * * var odt = dtypes( 'all' ); * var policies = { * 'output': 'same' * }; * * var table = { * 'default': base * }; * var sorthp = new NullaryStrided1dDispatch( table, [ odt ], policies ); * * var xbuf = [ -1.0, 2.0, -3.0 ]; * var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); * * var o = scalar2ndarray( 1.0, { * 'dtype': 'generic' * }); * * sorthp.assign( x, o ); * * var arr = ndarray2array( x ); * // returns [ -3.0, -1.0, 2.0 ] */ function NullaryStrided1dDispatch( table, odtypes, policies, options ) { var dt; var i; if ( !( this instanceof NullaryStrided1dDispatch ) ) { if ( arguments.length > 3 ) { return new NullaryStrided1dDispatch( table, odtypes, policies, options ); // eslint-disable-line max-len } return new NullaryStrided1dDispatch( table, odtypes, policies ); } if ( !isObject( table ) ) { throw new TypeError( format( 'invalid argument. First argument must be an object. Value: `%s`.', table ) ); } if ( !isFunction( table.default ) ) { throw new TypeError( format( 'invalid argument. First argument must be an object having a "default" property and an associated method.' ) ); } if ( hasProp( table, 'types' ) && !isCollection( table.types ) && !isEmptyCollection( table.types ) ) { throw new TypeError( format( 'invalid argument. First argument must be an object having a "types" property whose associated value is an array-like object.' ) ); } if ( hasProp( table, 'fcns' ) && !isFunctionArray( table.fcns ) && !isEmptyCollection( table.fcns ) ) { throw new TypeError( format( 'invalid argument. First argument must be an object having a "fcns" property whose associated value is an array-like object containing functions.' ) ); } if ( !isCollection( odtypes ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', odtypes ) ); } for ( i = 0; i < odtypes.length; i++ ) { dt = odtypes[ i ]; if ( !isCollection( dt ) || dt.length < 1 || !everyBy( dt, isDataType ) ) { throw new TypeError( format( 'invalid argument. Second argument must contain arrays of data types. Value: `%s`.', odtypes ) ); } } if ( !isObject( policies ) ) { throw new TypeError( format( 'invalid argument. Third argument must be an object. Value: `%s`.', table ) ); } if ( !isOutputDataTypePolicy( policies.output ) ) { throw new TypeError( format( 'invalid argument. Third argument must be an object having a supported output data type policy. Value: `%s`.', policies.output ) ); } this._table = { 'default': table.default, 'types': ( table.types ) ? types2enums( table.types ) : [], // note: convert to enums (i.e., integers) to ensure faster comparisons 'fcns': ( table.fcns ) ? copy( table.fcns ) : [] }; if ( this._table.types.length !== this._table.fcns.length ) { throw new Error( 'invalid argument. First argument specifies an unexpected number of types. A pair of input and output ndarray data types must be specified for each provided strided function.' ); } this._odtypes = odtypes; this._policies = { 'output': policies.output, 'casting': 'none' }; if ( arguments.length > 3 ) { this._apply = nullaryStrided1d.factory( options ); // note: delegate options validation to factory method } else { this._apply = nullaryStrided1d; } return this; } /** * Applies a strided function and assigns results to a provided output ndarray. * * @name assign * @memberof NullaryStrided1dDispatch.prototype * @type {Function} * @param {ndarrayLike} x - output ndarray * @param {...ndarrayLike} [args] - additional ndarray arguments * @param {Options} [options] - function options * @param {IntegerArray} [options.dims] - list of dimensions over which to perform an operation * @throws {TypeError} first argument must be an ndarray * @throws {TypeError} first argument must have a supported data type * @throws {TypeError} options argument must be an object * @throws {RangeError} dimension indices must not exceed input ndarray bounds * @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions * @throws {Error} must provide valid options * @returns {ndarrayLike} output ndarray * * @example * var base = require( '@stdlib/blas/ext/base/ndarray/gsorthp' ); * var dtypes = require( '@stdlib/ndarray/dtypes' ); * var ndarray2array = require( '@stdlib/ndarray/to-array' ); * var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var ndarray = require( '@stdlib/ndarray/base/ctor' ); * * var odt = dtypes( 'all' ); * var policies = { * 'output': 'same' * }; * * var table = { * 'default': base * }; * var sorthp = new NullaryStrided1dDispatch( table, [ odt ], policies ); * * var xbuf = [ -1.0, 2.0, -3.0 ]; * var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); * * var o = scalar2ndarray( 1.0, { * 'dtype': 'generic' * }); * * var out = sorthp.assign( x, o, { * 'dims': [ 0 ] * }); * // returns <ndarray> * * var arr = ndarray2array( x ); * // returns [ -3.0, -1.0, 2.0 ] * * var bool = ( out === x ); * // returns true */ setReadOnly( NullaryStrided1dDispatch.prototype, 'assign', function assign( x ) { var options; var dtypes; var nargs; var opts; var args; var arr; var err; var flg; var xdt; var dt; var N; var f; var i; nargs = arguments.length; if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); } // Validate the output ndarray data type... xdt = getDType( x ); if ( !contains( this._odtypes[ 0 ], xdt ) ) { throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( this._odtypes[ 0 ], '", "' ), xdt ) ); } args = [ x ]; // Resolve additional ndarray arguments... for ( i = 1; i < nargs; i++ ) { arr = arguments[ i ]; if ( !isndarrayLike( arr ) ) { break; } args.push( arr ); } // If we processed all but the last argument, assume that the last argument is an options argument... if ( i === nargs-1 ) { options = arguments[ i ]; flg = true; } // Otherwise, if we have more than one argument remaining, then at least one argument is not an ndarray but should be... else if ( i < nargs-1 ) { throw new TypeError( format( 'invalid argument. Argument %d must be an ndarray-like object. Value: `%s`.', i, arguments[ i ] ) ); } // Verify that additional ndarray arguments have expected dtypes... for ( i = 1; i < args.length; i++ ) { dt = getDType( args[ i ] ); if ( !contains( this._odtypes[ 0 ], dt ) ) { throw new TypeError( format( 'invalid argument. Argument %d must have one of the following data types: "%s". Data type: `%s`.', i, join( this._odtypes[ i ], '", "' ), dt ) ); } } // Validate any provided options... N = ndims( x ); opts = objectAssign( {}, defaults ); if ( flg ) { err = validate( opts, N, this._odtypes, options ); if ( err ) { throw err; } } // When a list of dimensions is not provided, apply the strided function across all dimensions... if ( opts.dims === null ) { opts.dims = zeroTo( N ); } // Resolve the lower-level strided function satisfying the output ndarray data type: dtypes = [ resolveEnum( xdt ) ]; i = indexOfTypes( this._table.fcns.length, 1, this._table.types, 1, 1, 0, dtypes, 1, 0 ); // eslint-disable-line max-len if ( i >= 0 ) { f = this._table.fcns[ i ]; } else { f = this._table.default; } // Perform operation: this._apply( f, args, opts.dims ); // note: we assume that this lower-level function handles further validation of the output ndarray (e.g., expected shape, etc) return x; }); // EXPORTS // module.exports = NullaryStrided1dDispatch; |