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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 3x 3x 3x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 21x 21x 21x 63x 63x 21x 21x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 656x 656x 656x 656x 656x 656x 656x 656x 656x 656x 656x 656x 656x 656x 1x 1x 1x 655x 655x 656x 48x 48x 19x 19x 29x 40x 16x 16x 13x 13x 13x 607x 656x 1x 1x 606x 606x 606x 606x 652x 282x 282x 3x 3x 3x 3x 3x 3x 1x 1x 282x 279x 279x 279x 282x 324x 324x 278x 278x 278x 278x 81x 81x 278x 46x 46x 46x 46x 478x 478x 656x 461x 461x 656x 17x 17x 478x 656x 192x 192x 286x 652x 110x 110x 74x 1x 74x 2x 2x 74x 3x 3x 74x 2x 2x 74x 39x 2x 2x 37x 20x 20x 17x 19x 16x 16x 1x 1x 30x 110x 4x 4x 32x 32x 592x 146x 146x 30x 146x 15x 116x 18x 101x 53x 2x 2x 51x 20x 20x 31x 33x 16x 16x 15x 83x 30x 30x 596x 15x 15x 15x 15x 536x 107x 21x 107x 86x 86x 107x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 313x 313x 656x 17x 17x 17x 17x 17x | /** * @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. */ 'use strict'; // MODULES // var isString = require( '@stdlib/assert/is-string' ).isPrimitive; var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' ); var isBooleanDataType = require( '@stdlib/array/base/assert/is-boolean-data-type' ); var isCollection = require( '@stdlib/assert/is-collection' ); var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); var isObject = require( '@stdlib/assert/is-object' ); var isFunction = require( '@stdlib/assert/is-function' ); var ctors = require( '@stdlib/array/ctors' ); var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' ); var filledArray = require( '@stdlib/array/base/filled-by' ); var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); var iterLength = require( '@stdlib/iter/length' ); var defaults = require( '@stdlib/array/defaults' ); var format = require( '@stdlib/string/format' ); // VARIABLES // var HAS_ITERATOR_SYMBOL = hasIteratorSymbolSupport(); var DEFAULT_DTYPE = defaults.get( 'dtypes.default' ); // FUNCTIONS // /** * Creates a filled "generic" array from an iterator. * * @private * @param {Iterable} it - iterator * @param {Callback} clbk - callback function * @param {*} thisArg - callback function execution context * @returns {Array} filled array */ function filledArrayIterator( it, clbk, thisArg ) { var arr; var i; var v; arr = []; i = -1; while ( true ) { v = it.next(); if ( v.done ) { break; } i += 1; arr.push( clbk.call( thisArg, i ) ); } return arr; } /** * Fills an array exposing accessors for getting and setting array elements. * * @private * @param {Collection} arr - input array * @param {Callback} clbk - callback function * @param {*} thisArg - callback function execution context * @returns {Collection} input array */ function filledAccessors( arr, clbk, thisArg ) { var i; for ( i = 0; i < arr.length; i++ ) { arr.set( clbk.call( thisArg, i ), i ); } return arr; } // MAIN // /** * Creates a filled array according to a provided callback function. * * @param {(NonNegativeInteger|TypedArray|ArrayLikeObject|ArrayBuffer|Iterable)} [arg] - a length, typed array, array-like object, buffer, or iterable * @param {NonNegativeInteger} [byteOffset=0] - byte offset * @param {NonNegativeInteger} [length] - view length * @param {string} [dtype="float64"] - data type * @param {Callback} [clbk] - callback to invoke * @param {*} [thisArg] - callback execution context * @throws {TypeError} must provide a recognized data type * @throws {TypeError} must provide a length, typed array, array-like object, buffer, or iterable * @throws {TypeError} callback argument must be a function. * @throws {Error} creating a generic array from an `ArrayBuffer` is not supported * @returns {(TypedArray|Array)} array or typed array * * @example * var arr = filledarrayBy(); * // returns <Float64Array> * * @example * function clbk() { * return 1.0; * } * * var arr = filledarrayBy( 2, clbk ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * function clbk() { * return 1.0; * } * * var arr = filledarrayBy( 2, 'float32', clbk ); * // returns <Float32Array>[ 1.0, 1.0 ] * * @example * function clbk() { * return 1.0; * } * * var arr = filledarrayBy( 2, 'generic', clbk ); * // returns [ 1.0, 1.0 ] * * @example * function clbk() { * return 1.0; * } * * var arr = filledarrayBy( [ 0.5, 0.5 ], clbk ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * function clbk() { * return 1; * } * * var arr = filledarrayBy( [ 5, -3 ], 'int32', clbk ); * // returns <Int32Array>[ 1, 1 ] * * @example * function clbk1() { * return 10; * } * * function clbk2() { * return 1.0; * } * * var arr1 = filledarrayBy( [ 5, 3 ], 'int32', clbk1 ); * var arr2 = filledarrayBy( arr1, clbk2 ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * function clbk1() { * return 1.0; * } * * function clbk2() { * return 2; * } * * var arr1 = filledarrayBy( [ 5, 3 ], 'int32', clbk1 ); * var arr2 = filledarrayBy( arr1, 'uint32', clbk2 ); * // returns <Uint32Array>[ 2, 2 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * function clbk() { * return 1.0; * } * * var buf = new ArrayBuffer( 16 ); * var arr = filledarrayBy( buf, clbk ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * function clbk() { * return 1.0; * } * * var buf = new ArrayBuffer( 16 ); * var arr = filledarrayBy( buf, 'float32', clbk ); * // returns <Float32Array>[ 1.0, 1.0, 1.0, 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * function clbk() { * return 1.0; * } * * var buf = new ArrayBuffer( 16 ); * var arr = filledarrayBy( buf, 8, clbk ); * // returns <Float64Array>[ 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * function clbk() { * return 1.0; * } * * var buf = new ArrayBuffer( 16 ); * var arr = filledarrayBy( buf, 8, 'float32', clbk ); * // returns <Float32Array>[ 1.0, 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * function clbk() { * return 1.0; * } * * var buf = new ArrayBuffer( 32 ); * var arr = filledarrayBy( buf, 8, 2, clbk ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * function clbk() { * return 1; * } * * var buf = new ArrayBuffer( 32 ); * var arr = filledarrayBy( buf, 8, 2, 'int32', clbk ); * // returns <Int32Array>[ 1, 1 ] */ function filledarrayBy() { var thisArg; var nargs; var dtype; var clbk; var ctor; var arr; var len; var arg; nargs = arguments.length; // If we weren't provided any arguments, return an empty array... if ( nargs === 0 ) { ctor = ctors( DEFAULT_DTYPE ); return new ctor( 0 ); } // Check if we were provided a dtype as the first argument... dtype = arguments[ 0 ]; if ( isString( dtype ) ) { // Invoking this function with arguments `f( dtype, clbk[, thisArg] )` is not allowed (otherwise, we'd need to also allow `f( clbk[, thisArg] )`)... if ( nargs > 1 ) { throw new TypeError( 'invalid arguments. Must provide a length, typed array, array-like object, or an iterable.' ); } ctor = ctors( dtype ); if ( ctor === null ) { throw new TypeError( format( 'invalid argument. Must provide a recognized data type. Value: `%s`.', dtype ) ); } // Return an empty array having the specified dtype: return new ctor( 0 ); } // For all other supported invocations, we need at least two arguments... if ( nargs < 2 ) { throw new TypeError( 'invalid arguments. Must provide a length, typed array, array-like object, or an iterable.' ); } // At this point, we need to do some argument juggling... nargs -= 1; // henceforth, the number of available arguments is `nargs+1` // Determine whether the last argument is a callback or "this" context... if ( isFunction( arguments[ nargs ] ) ) { // If the last argument is a function, we need to check the next-to-last argument, and, if the next-to-last argument is a function, assume that the next-to-last argument is the callback and the last argument is a "this" context... if ( isFunction( arguments[ nargs-1 ] ) ) { thisArg = arguments[ nargs ]; nargs -= 1; clbk = arguments[ nargs ]; // Check if we were provided only a callback and a "this" context.. if ( nargs === 0 ) { throw new TypeError( 'invalid arguments. Must provide a length, typed array, array-like object, or an iterable.' ); } } else { // "this" context is left undefined... clbk = arguments[ nargs ]; } } // If we were provided 3 or more arguments and the last argument was not a function, assume that we were provided a callback and a "this" context... else if ( nargs >= 2 ) { thisArg = arguments[ nargs ]; nargs -= 1; clbk = arguments[ nargs ]; if ( !isFunction( clbk ) ) { throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) ); } } // If were were only provided 2 arguments and the last argument was not a function, we've been provided an insufficient number of arguments... else { throw new TypeError( 'invalid arguments. Must provide a length, typed array, array-like object, or an iterable.' ); } // Now that we've processed the callback arguments, let's continue working backward to see if we've been provided a `dtype` argument... nargs -= 1; if ( nargs >= 0 && isString( arguments[ nargs ] ) ) { dtype = arguments[ nargs ]; nargs -= 1; } else { dtype = DEFAULT_DTYPE; } ctor = ctors( dtype ); if ( ctor === null ) { throw new TypeError( format( 'invalid argument. Must provide a recognized data type. Value: `%s`.', dtype ) ); } // At this point, we've resolved the output array data type, and now we can actually create the output array... if ( dtype === 'generic' ) { arg = arguments[ 0 ]; if ( nargs === 0 ) { if ( isNonNegativeInteger( arg ) ) { len = arg; } else if ( isCollection( arg ) ) { len = arg.length; } if ( len !== void 0 ) { return filledArray( len, clbk, thisArg ); } if ( isArrayBuffer( arg ) ) { throw new Error( 'invalid arguments. Creating a generic array from an ArrayBuffer is not supported.' ); } if ( isObject( arg ) ) { if ( HAS_ITERATOR_SYMBOL === false ) { throw new TypeError( format( 'invalid argument. Environment lacks Symbol.iterator support. Must provide a length, typed array, or array-like object. Value: `%s`.', arg ) ); } if ( !isFunction( arg[ ITERATOR_SYMBOL ] ) ) { throw new TypeError( format( 'invalid argument. Must provide a length, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); } arg = arg[ ITERATOR_SYMBOL ](); if ( !isFunction( arg.next ) ) { throw new TypeError( format( 'invalid argument. Must provide a length, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); } return filledArrayIterator( arg, clbk, thisArg ); } throw new TypeError( format( 'invalid argument. Must provide a length, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); } else if ( isArrayBuffer( arg ) ) { throw new Error( 'invalid arguments. Creating a generic array from an ArrayBuffer is not supported.' ); } throw new TypeError( format( 'invalid argument. Must provide a length, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); } if ( nargs === 0 ) { // length || array-like || ArrayBuffer || iterable arg = arguments[ 0 ]; if ( isCollection( arg ) ) { arr = new ctor( arg.length ); } else if ( isArrayBuffer( arg ) ) { arr = new ctor( arg ); } else if ( isNonNegativeInteger( arg ) ) { arr = new ctor( arg ); } else if ( isObject( arg ) ) { if ( HAS_ITERATOR_SYMBOL === false ) { throw new TypeError( format( 'invalid argument. Environment lacks Symbol.iterator support. Must provide a length, typed array, or array-like object. Value: `%s`.', arg ) ); } if ( !isFunction( arg[ ITERATOR_SYMBOL ] ) ) { throw new TypeError( format( 'invalid argument. Must provide a length, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); } arg = arg[ ITERATOR_SYMBOL ](); if ( !isFunction( arg.next ) ) { throw new TypeError( format( 'invalid argument. Must provide a length, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); } arr = new ctor( iterLength( arg ) ); } else { throw new TypeError( format( 'invalid argument. Must provide a length, typed array, array-like object, or an iterable. Value: `%s`.', arg ) ); } } else if ( nargs === 1 ) { arr = new ctor( arguments[0], arguments[1] ); // (ArrayBuffer, byteOffset) } else { arr = new ctor( arguments[0], arguments[1], arguments[2] ); // (ArrayBuffer, byteOffset, length) } if ( arr.length > 0 ) { if ( isComplexDataType( dtype ) || isBooleanDataType( dtype ) ) { filledAccessors( arr, clbk, thisArg ); } else { gfillBy( arr.length, arr, 1, callback ); } } return arr; /** * Callback which wraps a provided callback and is invoked for each array element. * * @private * @param {*} value - element value * @param {NonNegativeInteger} aidx - array index * @param {NonNegativeInteger} sidx - strided index * @param {Collection} array - input array/collection * @returns {*} callback return value */ function callback( value, aidx ) { return clbk.call( thisArg, aidx ); } } // EXPORTS // module.exports = filledarrayBy; |