From 3306285e5cb9679c2a9a19e718b317be53e4c251 Mon Sep 17 00:00:00 2001 From: Jaysukh Makvana <111515433+Jaysukh-409@users.noreply.github.com> Date: Sun, 24 Dec 2023 15:35:28 +0530 Subject: [PATCH] feat: add `forEach` method to `array/complex64` PR-URL: https://github.com/stdlib-js/stdlib/pull/1211 Co-authored-by: Athan Reines Reviewed-by: Athan Reines Signed-off-by: Athan Reines --- .../@stdlib/array/complex64/README.md | 66 +++++++ .../complex64/benchmark/benchmark.for_each.js | 56 ++++++ .../benchmark/benchmark.for_each.length.js | 109 +++++++++++ .../array/complex64/docs/types/index.d.ts | 73 ++++++++ .../@stdlib/array/complex64/lib/main.js | 43 +++++ .../array/complex64/test/test.for_each.js | 176 ++++++++++++++++++ 6 files changed, 523 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.for_each.js create mode 100644 lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.for_each.length.js create mode 100644 lib/node_modules/@stdlib/array/complex64/test/test.for_each.js diff --git a/lib/node_modules/@stdlib/array/complex64/README.md b/lib/node_modules/@stdlib/array/complex64/README.md index 9cca8e5ea8c..12ea74992f7 100644 --- a/lib/node_modules/@stdlib/array/complex64/README.md +++ b/lib/node_modules/@stdlib/array/complex64/README.md @@ -1197,6 +1197,72 @@ var count = context.count; // returns 3 ``` + + +#### Complex64Array.prototype.forEach( predicate\[, thisArg] ) + +Invokes a function once for each array element. + +```javascript +var Complex64 = require( '@stdlib/complex/float32' ); + +function log( v, i ) { + console.log( '%s: %s', i, v.toString() ); +} + +var arr = new Complex64Array( 3 ); + +// Set the first three elements: +arr.set( [ 1.0, 1.0 ], 0 ); +arr.set( [ 2.0, 2.0 ], 1 ); +arr.set( [ 3.0, 3.0 ], 2 ); + +arr.forEach( log ); +/* => + 0: 1 + 1i + 1: 2 + 2i + 2: 3 + 3i +*/ +``` + +The invoked function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the function execution context, provide a `thisArg`. + +```javascript +var Complex64 = require( '@stdlib/complex/float32' ); + +function fcn( v, i ) { + this.count += 1; + console.log( '%s: %s', i, v.toString() ); +} + +var arr = new Complex64Array( 3 ); + +var context = { + 'count': 0 +}; + +// Set the first three elements: +arr.set( [ 1.0, -1.0 ], 0 ); +arr.set( [ 2.0, -2.0 ], 1 ); +arr.set( [ 3.0, -3.0 ], 2 ); + +arr.forEach( fcn, context ); +/* => + 0: 1 + 1i + 1: 2 + 2i + 2: 3 + 3i +*/ + +var count = context.count; +// returns 3 +``` + #### Complex64Array.prototype.get( i ) diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.for_each.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.for_each.js new file mode 100644 index 00000000000..07555a1877d --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.for_each.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 isComplex64 = require( '@stdlib/assert/is-complex64' ); +var pkg = require( './../package.json' ).name; +var Complex64Array = require( './../lib' ); + + +// MAIN // + +bench( pkg+':forEach', function benchmark( b ) { + var arr; + var i; + + arr = new Complex64Array( [ 1, 2, 3, 4 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr.forEach( check ); + if ( arr.length !== 2 ) { + b.fail( 'should not change an array length' ); + } + } + b.toc(); + if ( arr.length !== 2 ) { + b.fail( 'should not change an array length' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function check( v ) { + if ( !isComplex64( v ) ) { + b.fail( 'should be a complex number' ); + } + } +}); diff --git a/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.for_each.length.js b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.for_each.length.js new file mode 100644 index 00000000000..9f54a0fa2ad --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/benchmark/benchmark.for_each.length.js @@ -0,0 +1,109 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 Complex64 = require( '@stdlib/complex/float32' ); +var realf = require( '@stdlib/complex/realf' ); +var imagf = require( '@stdlib/complex/imagf' ); +var pkg = require( './../package.json' ).name; +var Complex64Array = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr; + var i; + + arr = []; + for ( i = 0; i < len; i++ ) { + arr.push( new Complex64( i, i ) ); + } + arr = new Complex64Array( arr ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr.forEach( callback ); + if ( arr.length !== len ) { + b.fail( 'should not change an array length' ); + } + } + b.toc(); + if ( arr.length !== len ) { + b.fail( 'should not change an array length' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function callback( value ) { + if ( realf( value ) !== imagf( value ) ) { + throw new Error( 'something went wrong' ); + } + } + } +} + + +// 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+':forEach:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts index 00fd0b35481..1eab9573fd5 100644 --- a/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts @@ -1,3 +1,5 @@ +/* eslint-disable max-lines */ + /* * @license Apache-2.0 * @@ -105,6 +107,50 @@ type TernaryPredicate = ( this: U, value: Complex64, index: number, arr: Comp */ type Predicate = NullaryPredicate | UnaryPredicate | BinaryPredicate | TernaryPredicate; +/** +* Callback invoked for each element in an array. +* +* @returns undefined +*/ +type NullaryCallback = ( this: U ) => void; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @returns undefined +*/ +type UnaryCallback = ( this: U, value: Complex64 ) => void; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @param index - current array element index +* @returns undefined +*/ +type BinaryCallback = ( this: U, value: Complex64, index: number ) => void; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @param index - current array element index +* @param arr - array on which the method was called +* @returns undefined +*/ +type TernaryCallback = ( this: U, value: Complex64, index: number, arr: Complex64Array ) => void; + +/** +* Callback invoked for each element in an array. +* +* @param value - current array element +* @param index - current array element index +* @param arr - array on which the method was called +* @returns undefined +*/ +type Callback = NullaryCallback | UnaryCallback | BinaryCallback | TernaryCallback; + /** * Class for creating a 64-bit complex number array. */ @@ -501,6 +547,33 @@ declare class Complex64Array implements Complex64ArrayInterface { */ findLastIndex( predicate: Predicate, thisArg?: ThisParameterType> ): number; + /** + * Invokes a function once for each array element. + * + * @param fcn - function to invoke + * @param thisArg - execution context + * @returns undefined + * + * @example + * var Complex64 = require( '@stdlib/complex/float32' ); + * + * function log( v, i ) { + * console.log( '%s: %s', i, v.toString() ); + * } + * + * var arr = new Complex64Array( 3 ); + * + * arr.set( [ 1.0, 1.0 ], 0 ); + * arr.set( [ 2.0, 2.0 ], 1 ); + * arr.set( [ 3.0, 3.0 ], 2 ); + * + * arr.forEach( log ); + * // => 0: 1 + 1i + * // => 1: 2 + 2i + * // => 2: 3 + 3i + */ + forEach( fcn: Callback, thisArg?: ThisParameterType> ): void; + /** * Returns an array element. * diff --git a/lib/node_modules/@stdlib/array/complex64/lib/main.js b/lib/node_modules/@stdlib/array/complex64/lib/main.js index 517024e685f..48a5e7f98d4 100644 --- a/lib/node_modules/@stdlib/array/complex64/lib/main.js +++ b/lib/node_modules/@stdlib/array/complex64/lib/main.js @@ -1192,6 +1192,49 @@ setReadOnly( Complex64Array.prototype, 'findLastIndex', function findLastIndex( return -1; }); +/** +* Invokes a function once for each array element. +* +* @name forEach +* @memberof Complex64Array.prototype +* @type {Function} +* @param {Function} fcn - function to invoke +* @param {*} [thisArg] - function invocation context +* @throws {TypeError} `this` must be a complex number array +* @throws {TypeError} first argument must be a function +* +* @example +* var Complex64 = require( '@stdlib/complex/float32' ); +* +* function log( v, i ) { +* console.log( '%s: %s', i, v.toString() ); +* } +* +* var arr = new Complex64Array( 3 ); +* +* arr.set( [ 1.0, 1.0 ], 0 ); +* arr.set( [ 2.0, 2.0 ], 1 ); +* arr.set( [ 3.0, 3.0 ], 2 ); +* +* arr.forEach( log ); +*/ +setReadOnly( Complex64Array.prototype, 'forEach', function forEach( fcn, thisArg ) { + var buf; + var i; + var z; + if ( !isComplexArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); + } + if ( !isFunction( fcn ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + z = getComplex64( buf, i ); + fcn.call( thisArg, z, i, this ); + } +}); + /** * Returns an array element. * diff --git a/lib/node_modules/@stdlib/array/complex64/test/test.for_each.js b/lib/node_modules/@stdlib/array/complex64/test/test.for_each.js new file mode 100644 index 00000000000..68316f2b30d --- /dev/null +++ b/lib/node_modules/@stdlib/array/complex64/test/test.for_each.js @@ -0,0 +1,176 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' ); +var isComplex64 = require('@stdlib/assert/is-complex64'); +var Complex64Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Complex64Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `forEach` method', function test( t ) { + t.strictEqual( hasOwnProp( Complex64Array.prototype, 'forEach' ), true, 'has property' ); + t.strictEqual( isFunction( Complex64Array.prototype.forEach ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a complex number array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Complex64Array( 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.forEach.call( value, fcn ); + }; + } + + function fcn( v ) { + if ( !isComplex64( v ) ) { + t.fail( 'should be a complex number ' ); + } + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new Complex64Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + 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.forEach( value ); + }; + } +}); + +tape( 'the method should not invoke a provided callback function if operating on an empty complex number array', function test( t ) { + var arr; + + arr = new Complex64Array(); + arr.forEach( fcn ); + + t.end(); + + function fcn() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns `undefined`', function test( t ) { + var arr; + var out; + + arr = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + out = arr.forEach( fcn ); + + t.strictEqual( out, void 0, 'returns expected value' ); + t.end(); + + function fcn( v ) { + if ( !isComplex64( v ) ) { + t.fail( 'should be a complex number' ); + } + } +}); + +tape( 'the method invokes a provided function for each element in an array', function test( t ) { + var expected; + var arr; + + expected = []; + arr = new Complex64Array( [ 1.0, 1.0, 2.0, -2.0 ] ); + arr.forEach( fcn ); + expected = new Complex64Array( expected ); + + t.deepEqual( reinterpret64( expected, 0 ), reinterpret64( arr, 0 ), 'returns expected value' ); + t.end(); + + function fcn( v ) { + expected.push( v ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctx; + var arr; + + ctx = { + 'count': 0 + }; + arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, 3.0 ] ); + arr.forEach( fcn, ctx ); + + t.strictEqual( ctx.count, 3, 'returns expected value' ); + + t.end(); + + function fcn() { + this.count += 1; // eslint-disable-line no-invalid-this + } +});