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 | 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 3x 3x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 14x 14x 14x 32x 32x 14x 14x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 327x 327x 327x 327x 327x 327x 327x 327x 327x 327x 327x 327x 318x 318x 327x 9x 9x 327x 327x 112x 112x 327x 64x 1x 1x 63x 63x 64x 43x 2x 43x 2x 2x 43x 4x 4x 43x 1x 1x 43x 20x 1x 1x 19x 10x 10x 9x 19x 8x 8x 1x 1x 18x 64x 2x 2x 18x 18x 297x 13x 13x 297x 110x 110x 28x 110x 14x 82x 24x 68x 32x 1x 1x 31x 9x 9x 22x 22x 8x 8x 14x 44x 12x 12x 297x 14x 14x 14x 14x 108x 108x 108x 14x 108x 14x 94x 80x 80x 108x 108x 327x 9x 9x 9x 9x 9x | /** * @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 isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' ); var isBooleanDataType = require( '@stdlib/array/base/assert/is-boolean-data-type' ); var isString = require( '@stdlib/assert/is-string' ).isPrimitive; var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; 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 gfill = require( '@stdlib/blas/ext/base/gfill' ); var filled = require( '@stdlib/array/base/filled' ); var reinterpretBool = require( '@stdlib/strided/base/reinterpret-boolean' ); 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 {Iterator} it - iterator * @param {*} value - fill value * @returns {Array} filled array */ function filledIterator( it, value ) { var arr; var v; arr = []; while ( true ) { v = it.next(); if ( v.done ) { break; } arr.push( value ); } return arr; } /** * Fills an array exposing accessors for getting and setting array elements. * * @private * @param {Collection} arr - input array * @param {*} value - fill value * @returns {Collection} input array */ function filledAccessors( arr, value ) { var i; for ( i = 0; i < arr.length; i++ ) { arr.set( value, i ); } return arr; } // MAIN // /** * Creates a filled array. * * @param {*} [value] - fill value * @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 * @throws {TypeError} must provide a recognized data type * @throws {TypeError} must provide a length, typed array, array-like object, buffer, or iterable * @throws {Error} creating a generic array from an `ArrayBuffer` is not supported * @returns {(TypedArray|Array|ComplexArray|ComplexTypedArray)} array or typed array * * @example * var arr = filledarray(); * // returns <Float64Array> * * @example * var arr = filledarray( 1.0, 2 ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * var arr = filledarray( 1.0, 2, 'float32' ); * // returns <Float32Array>[ 1.0, 1.0 ] * * @example * var arr = filledarray( 1.0, 2, 'generic' ); * // returns [ 1.0, 1.0 ] * * @example * var arr = filledarray( 1.0, [ 0.5, 0.5 ] ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * var arr = filledarray( 1, [ 5, -3 ], 'int32' ); * // returns <Int32Array>[ 1, 1 ] * * @example * var arr1 = filledarray( 2, [ 5, 3 ], 'int32' ); * var arr2 = filledarray( 1.0, arr1 ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * var arr1 = filledarray( 2, [ 5, 3 ], 'int32' ); * var arr2 = filledarray( 1, arr1, 'uint32' ); * // returns <Uint32Array>[ 1, 1 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * var buf = new ArrayBuffer( 16 ); * var arr = filledarray( 1.0, buf ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * var buf = new ArrayBuffer( 16 ); * var arr = filledarray( 1.0, buf, 'float32' ); * // returns <Float32Array>[ 1.0, 1.0, 1.0, 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * var buf = new ArrayBuffer( 16 ); * var arr = filledarray( 1.0, buf, 8 ); * // returns <Float64Array>[ 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * var buf = new ArrayBuffer( 16 ); * var arr = filledarray( 1.0, buf, 8, 'float32' ); * // returns <Float32Array>[ 1.0, 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * var buf = new ArrayBuffer( 32 ); * var arr = filledarray( 1.0, buf, 8, 2 ); * // returns <Float64Array>[ 1.0, 1.0 ] * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * var buf = new ArrayBuffer( 32 ); * var arr = filledarray( 1, buf, 8, 2, 'int32' ); * // returns <Int32Array>[ 1, 1 ] */ function filledarray() { var value; var nargs; var dtype; var ctor; var arr; var len; var arg; nargs = arguments.length; 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 ) ); } if ( dtype === 'generic' ) { if ( nargs <= 0 ) { return []; } value = arguments[ 0 ]; arg = arguments[ 1 ]; if ( nargs === 1 ) { if ( isNonNegativeInteger( arg ) ) { len = arg; } else if ( isCollection( arg ) ) { len = arg.length; } if ( len !== void 0 ) { return filled( value, len ); } 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 filledIterator( arg, value ); } 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 ) { return new ctor( 0 ); } if ( nargs === 1 ) { // length || array-like || ArrayBuffer || iterable arg = arguments[ 1 ]; 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 === 2 ) { arr = new ctor( arguments[1], arguments[2] ); // (ArrayBuffer, byteOffset) } else { arr = new ctor( arguments[1], arguments[2], arguments[3] ); // (ArrayBuffer, byteOffset, length) } if ( arr.length > 0 ) { value = arguments[ 0 ]; if ( isComplexDataType( dtype ) ) { filledAccessors( arr, value ); } else if ( isBooleanDataType( dtype ) ) { gfill( arr.length, ( value ) ? 1 : 0, reinterpretBool( arr, 0 ), 1 ); // eslint-disable-line max-len } else { gfill( arr.length, value, arr, 1 ); } } return arr; } // EXPORTS // module.exports = filledarray; |