diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index ab659bbc55ba..ac5714e862d8 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -958,6 +958,65 @@ var v = out.get( 0 ); * * * + + +#### TypedArrayFE.prototype.fill( value\[, start\[, end]] ) + +Returns a modified typed array filled with a fill value. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] ); + +// Set all elements to the same value: +arr.fill( 6.0 ); + +var z = arr.get( 0 ); +// returns 6.0 + +z = arr.get( 1 ); +// returns 6.0 + +z = arr.get( 2 ); +// returns 6.0 + +// Fill all elements starting from the second element: +arr.fill( 7.0, 1 ); + +z = arr.get( 1 ); +// returns 7.0 + +z = arr.get( 2 ); +// returns 7.0 + +// Fill all elements from first element until the second-to-last element: +arr.fill( 8.0, 0, 2 ); + +z = arr.get( 0 ); +// returns 8.0 + +z = arr.get( 1 ); +// returns 8.0 +``` + +When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] ); + +// Set all array elements, except the last element, to the same value: +arr.fill( 6.0, 0, -1 ); + +var z = arr.get( 0 ); +// returns 6.0 + +z = arr.get( arr.length - 1 ); +// returns 3.0 +``` + ## Notes - A returned constructor supports the following byte orders: diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.fill.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.fill.js new file mode 100644 index 000000000000..cd537d05ff53 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.fill.js @@ -0,0 +1,57 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// MAIN // + +bench( pkg+':fill', function benchmark( b ) { + var values; + var arr; + var out; + var i; + + arr = new Float64ArrayFE( 'little-endian', 5); + values = [ 6.0, 7.0, 10.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.fill( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !( out instanceof Float64ArrayFE ) ) { + b.fail( 'should return a TypedArray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.fill.length.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.fill.length.js new file mode 100644 index 000000000000..3089f11c3a2c --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.fill.length.js @@ -0,0 +1,101 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float64ArrayFE( 'little-endian', len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var values; + var out; + var i; + + values = [ 6.0, 7.0, 10.2 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = arr.fill( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !( out instanceof Float64ArrayFE ) ) { + b.fail( 'should return a TypedArray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':fill:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index 079902a6c333..0358a5fceaa3 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -1153,6 +1153,94 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli return out; }); + /** + * Returns a modified typed array filled with a fill value. + * + * @name fill + * @memberof Complex128Array.prototype + * @type {Function} + * @param {ComplexLike} value - fill value + * @param {integer} [start=0] - starting index (inclusive) + * @param {integer} [end] - ending index (exclusive) + * @throws {TypeError} `this` must be a complex number array + * @throws {TypeError} first argument must be a complex number + * @throws {TypeError} second argument must be an integer + * @throws {TypeError} third argument must be an integer + * @returns {Complex128Array} modified array + * + * @example + * var real = require( '@stdlib/complex/float64/real' ); + * var imag = require( '@stdlib/complex/float64/imag' ); + * + * var arr = new Complex128Array( 3 ); + * + * arr.fill( new Complex128( 1.0, 1.0 ), 1 ); + * + * var z = arr.get( 1 ); + * // returns + * + * var re = real( z ); + * // returns 1.0 + * + * var im = imag( z ); + * // returns 1.0 + * + * z = arr.get( 2 ); + * // returns + * + * re = real( z ); + * // returns 1.0 + * + * im = imag( z ); + * // returns 1.0 + */ + setReadOnly( TypedArray.prototype, 'fill', function fill( value, start, end ) { + var isLE; + var buf; + var len; + var i; + if ( !isTypedArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a TypedArray array.' ); + } + buf = this._buffer; + len = this._length; + isLE = this._isLE; + if ( arguments.length > 1 ) { + if ( !isInteger( start ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', start ) ); + } + if ( start < 0 ) { + start += len; + if ( start < 0 ) { + start = 0; + } + } + if ( arguments.length > 2 ) { + if ( !isInteger( end ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', end ) ); + } + if ( end < 0 ) { + end += len; + if ( end < 0 ) { + end = 0; + } + } + if ( end > len ) { + end = len; + } + } else { + end = len; + } + } else { + start = 0; + end = len; + } + for ( i = start; i < end; i++ ) { + buf[ SETTER ]( i*BYTES_PER_ELEMENT, value, isLE ); + } + return this; + }); + return TypedArray; /** diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.fill.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.fill.js new file mode 100644 index 000000000000..a042b25da645 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.fill.js @@ -0,0 +1,300 @@ +/** +* @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 tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var factory = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( isFunction( ctor ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'attached to the prototype of the returned function is an `fill` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'fill' ), true, 'returns expected value' ); + t.strictEqual( isFunction( ctor.prototype.at ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.fill.call( value, 0 ); + }; + } +}); + +tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 5 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.fill( 5, value ); + }; + } +}); + +tape( 'the method throws an error if provided a third argument which is not an integer', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 5 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.fill( 6.0, 0, value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty array', function test( t ) { + var ctor; + var arr; + var out; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 0 ); + out = arr.fill( 6.0 ); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method does not change the array length', function test( t ) { + var ctor; + var arr; + var out; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + out = arr.fill( 6.0, 2, 5 ); + + t.strictEqual( out, arr, 'returns expected value' ); + t.strictEqual( arr.length, 5, 'returns expected value' ); + t.end(); +}); + +tape( 'if called with one argument, the method sets each array element to the provided value', function test( t ) { + var expected; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + expected = [ 6.0, 6.0, 6.0, 6.0, 6.0]; + + arr.fill( 6.0); + + for ( i = 0; i < arr.length; i++) { + t.strictEqual( arr.get(i), expected[i], 'expected value'); + } + t.end(); +}); + +tape( 'if called with two arguments, the method sets each array element to the provided value starting from a start index (inclusive)', function test( t ) { + var expected; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + expected = [ 1.0, 2.0, 6.0, 6.0, 6.0 ]; + + arr.fill( 6.0, 2 ); + + for ( i = 0; i < arr.length; i++ ) { + t.strictEqual( arr.get( i ), expected[ i ], 'expected value'); + } + t.end(); +}); + +tape( 'if called with three arguments, the method sets each array element to the provided value starting from a start index (inclusive) until a specified end index (exclusive)', function test( t ) { + var expected; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + expected = [ 1.0, 2.0, 6.0, 6.0, 5.0 ]; + + arr.fill( 6.0, 2, 4 ); + + for ( i = 0; i < arr.length; i++ ) { + t.strictEqual( arr.get( i ), expected[ i ], 'expected value' ); + } + t.end(); +}); + +tape( 'the method supports negative indices', function test( t ) { + var expected; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + expected = [ 1.0, 2.0, 6.0, 6.0, 5.0 ]; + + arr.fill( 6.0, -3, -1 ); + + for ( i = 0; i < arr.length; i++ ) { + t.strictEqual( arr.get( i ), expected[ i ], 'expected value' ); + } + t.end(); +}); + +tape( 'if a provided start index resolves to a negative index, the method fills an array starting from the first element', function test( t ) { + var expected; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + expected = [ 6.0, 6.0, 6.0, 6.0, 6.0 ]; + + arr.fill( 6.0, -10 ); + + for ( i = 0; i < arr.length; i++ ) { + t.strictEqual( arr.get( i ), expected[ i ], 'expected value' ); + } + t.end(); +}); + +tape( 'if a provided end index resolves to an index exceeding the last array element index, the method fills an array until the last element (inclusive)', function test( t ) { + var expected; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + expected = [ 1.0, 6.0, 6.0, 6.0, 6.0 ]; + + arr.fill( 6.0, 1, 10 ); + + for ( i = 0; i < arr.length; i++ ) { + t.strictEqual( arr.get( i ), expected[ i ], 'expected value' ); + } + t.end(); +}); + +tape( 'if a provided start index resolves to an index which is greater than or equal to a resolved end index, the method does not fill an array', function test( t ) { + var expected; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + + arr.fill( 6.0, 2, 1 ); + + for ( i = 0; i < arr.length; i++ ) { + t.strictEqual( arr.get( i ), expected[ i ], 'expected value' ); + } + t.end(); +});