Skip to content

Commit

Permalink
feat: add forEach method to array/complex64
Browse files Browse the repository at this point in the history
PR-URL: 	#1211
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Signed-off-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
Jaysukh-409 and kgryte authored Dec 24, 2023
1 parent 151d27d commit 3306285
Show file tree
Hide file tree
Showing 6 changed files with 523 additions and 0 deletions.
66 changes: 66 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,72 @@ var count = context.count;
// returns 3
```

<a name="method-for-each"></a>

#### 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
```

<a name="method-get"></a>

#### Complex64Array.prototype.get( i )
Expand Down
Original file line number Diff line number Diff line change
@@ -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' );
}
}
});
Original file line number Diff line number Diff line change
@@ -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();
73 changes: 73 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable max-lines */

/*
* @license Apache-2.0
*
Expand Down Expand Up @@ -105,6 +107,50 @@ type TernaryPredicate<U> = ( this: U, value: Complex64, index: number, arr: Comp
*/
type Predicate<U> = NullaryPredicate<U> | UnaryPredicate<U> | BinaryPredicate<U> | TernaryPredicate<U>;

/**
* Callback invoked for each element in an array.
*
* @returns undefined
*/
type NullaryCallback<U> = ( this: U ) => void;

/**
* Callback invoked for each element in an array.
*
* @param value - current array element
* @returns undefined
*/
type UnaryCallback<U> = ( 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<U> = ( 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<U> = ( 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<U> = NullaryCallback<U> | UnaryCallback<U> | BinaryCallback<U> | TernaryCallback<U>;

/**
* Class for creating a 64-bit complex number array.
*/
Expand Down Expand Up @@ -501,6 +547,33 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
findLastIndex<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): 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<U = unknown>( fcn: Callback, thisArg?: ThisParameterType<Callback<U>> ): void;

/**
* Returns an array element.
*
Expand Down
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading

1 comment on commit 3306285

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
array/complex64 $\color{red}2050/2051$
$\color{green}+99.95\%$
$\color{red}345/347$
$\color{green}+99.42\%$
$\color{green}34/34$
$\color{green}+100.00\%$
$\color{red}2050/2051$
$\color{green}+99.95\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.