Skip to content

Commit abe21e8

Browse files
committed
Auto-generated commit
1 parent 73dc5ea commit abe21e8

8 files changed

Lines changed: 527 additions & 4 deletions

File tree

complex64/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,72 @@ var count = context.count;
11971197
// returns 3
11981198
```
11991199

1200+
<a name="method-for-each"></a>
1201+
1202+
#### Complex64Array.prototype.forEach( predicate\[, thisArg] )
1203+
1204+
Invokes a function once for each array element.
1205+
1206+
```javascript
1207+
var Complex64 = require( '@stdlib/complex/float32' );
1208+
1209+
function log( v, i ) {
1210+
console.log( '%s: %s', i, v.toString() );
1211+
}
1212+
1213+
var arr = new Complex64Array( 3 );
1214+
1215+
// Set the first three elements:
1216+
arr.set( [ 1.0, 1.0 ], 0 );
1217+
arr.set( [ 2.0, 2.0 ], 1 );
1218+
arr.set( [ 3.0, 3.0 ], 2 );
1219+
1220+
arr.forEach( log );
1221+
/* =>
1222+
0: 1 + 1i
1223+
1: 2 + 2i
1224+
2: 3 + 3i
1225+
*/
1226+
```
1227+
1228+
The invoked function is provided three arguments:
1229+
1230+
- **value**: current array element.
1231+
- **index**: current array element index.
1232+
- **arr**: the array on which this method was called.
1233+
1234+
To set the function execution context, provide a `thisArg`.
1235+
1236+
```javascript
1237+
var Complex64 = require( '@stdlib/complex/float32' );
1238+
1239+
function fcn( v, i ) {
1240+
this.count += 1;
1241+
console.log( '%s: %s', i, v.toString() );
1242+
}
1243+
1244+
var arr = new Complex64Array( 3 );
1245+
1246+
var context = {
1247+
'count': 0
1248+
};
1249+
1250+
// Set the first three elements:
1251+
arr.set( [ 1.0, -1.0 ], 0 );
1252+
arr.set( [ 2.0, -2.0 ], 1 );
1253+
arr.set( [ 3.0, -3.0 ], 2 );
1254+
1255+
arr.forEach( fcn, context );
1256+
/* =>
1257+
0: 1 + 1i
1258+
1: 2 + 2i
1259+
2: 3 + 3i
1260+
*/
1261+
1262+
var count = context.count;
1263+
// returns 3
1264+
```
1265+
12001266
<a name="method-get"></a>
12011267

12021268
#### Complex64Array.prototype.get( i )
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isComplex64 = require( '@stdlib/assert/is-complex64' );
25+
var pkg = require( './../package.json' ).name;
26+
var Complex64Array = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+':forEach', function benchmark( b ) {
32+
var arr;
33+
var i;
34+
35+
arr = new Complex64Array( [ 1, 2, 3, 4 ] );
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
arr.forEach( check );
40+
if ( arr.length !== 2 ) {
41+
b.fail( 'should not change an array length' );
42+
}
43+
}
44+
b.toc();
45+
if ( arr.length !== 2 ) {
46+
b.fail( 'should not change an array length' );
47+
}
48+
b.pass( 'benchmark finished' );
49+
b.end();
50+
51+
function check( v ) {
52+
if ( !isComplex64( v ) ) {
53+
b.fail( 'should be a complex number' );
54+
}
55+
}
56+
});
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var Complex64 = require( '@stdlib/complex/float32' );
26+
var realf = require( '@stdlib/complex/realf' );
27+
var imagf = require( '@stdlib/complex/imagf' );
28+
var pkg = require( './../package.json' ).name;
29+
var Complex64Array = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var arr;
43+
var i;
44+
45+
arr = [];
46+
for ( i = 0; i < len; i++ ) {
47+
arr.push( new Complex64( i, i ) );
48+
}
49+
arr = new Complex64Array( arr );
50+
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
arr.forEach( callback );
65+
if ( arr.length !== len ) {
66+
b.fail( 'should not change an array length' );
67+
}
68+
}
69+
b.toc();
70+
if ( arr.length !== len ) {
71+
b.fail( 'should not change an array length' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
76+
function callback( value ) {
77+
if ( realf( value ) !== imagf( value ) ) {
78+
throw new Error( 'something went wrong' );
79+
}
80+
}
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( pkg+':forEach:len='+len, f );
106+
}
107+
}
108+
109+
main();

complex64/docs/types/index.d.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable max-lines */
2+
13
/*
24
* @license Apache-2.0
35
*
@@ -105,6 +107,50 @@ type TernaryPredicate<U> = ( this: U, value: Complex64, index: number, arr: Comp
105107
*/
106108
type Predicate<U> = NullaryPredicate<U> | UnaryPredicate<U> | BinaryPredicate<U> | TernaryPredicate<U>;
107109

110+
/**
111+
* Callback invoked for each element in an array.
112+
*
113+
* @returns undefined
114+
*/
115+
type NullaryCallback<U> = ( this: U ) => void;
116+
117+
/**
118+
* Callback invoked for each element in an array.
119+
*
120+
* @param value - current array element
121+
* @returns undefined
122+
*/
123+
type UnaryCallback<U> = ( this: U, value: Complex64 ) => void;
124+
125+
/**
126+
* Callback invoked for each element in an array.
127+
*
128+
* @param value - current array element
129+
* @param index - current array element index
130+
* @returns undefined
131+
*/
132+
type BinaryCallback<U> = ( this: U, value: Complex64, index: number ) => void;
133+
134+
/**
135+
* Callback invoked for each element in an array.
136+
*
137+
* @param value - current array element
138+
* @param index - current array element index
139+
* @param arr - array on which the method was called
140+
* @returns undefined
141+
*/
142+
type TernaryCallback<U> = ( this: U, value: Complex64, index: number, arr: Complex64Array ) => void;
143+
144+
/**
145+
* Callback invoked for each element in an array.
146+
*
147+
* @param value - current array element
148+
* @param index - current array element index
149+
* @param arr - array on which the method was called
150+
* @returns undefined
151+
*/
152+
type Callback<U> = NullaryCallback<U> | UnaryCallback<U> | BinaryCallback<U> | TernaryCallback<U>;
153+
108154
/**
109155
* Class for creating a 64-bit complex number array.
110156
*/
@@ -501,6 +547,33 @@ declare class Complex64Array implements Complex64ArrayInterface {
501547
*/
502548
findLastIndex<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): number;
503549

550+
/**
551+
* Invokes a function once for each array element.
552+
*
553+
* @param fcn - function to invoke
554+
* @param thisArg - execution context
555+
* @returns undefined
556+
*
557+
* @example
558+
* var Complex64 = require( '@stdlib/complex/float32' );
559+
*
560+
* function log( v, i ) {
561+
* console.log( '%s: %s', i, v.toString() );
562+
* }
563+
*
564+
* var arr = new Complex64Array( 3 );
565+
*
566+
* arr.set( [ 1.0, 1.0 ], 0 );
567+
* arr.set( [ 2.0, 2.0 ], 1 );
568+
* arr.set( [ 3.0, 3.0 ], 2 );
569+
*
570+
* arr.forEach( log );
571+
* // => 0: 1 + 1i
572+
* // => 1: 2 + 2i
573+
* // => 2: 3 + 3i
574+
*/
575+
forEach<U = unknown>( fcn: Callback, thisArg?: ThisParameterType<Callback<U>> ): void;
576+
504577
/**
505578
* Returns an array element.
506579
*

complex64/lib/main.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,49 @@ setReadOnly( Complex64Array.prototype, 'findLastIndex', function findLastIndex(
11921192
return -1;
11931193
});
11941194

1195+
/**
1196+
* Invokes a function once for each array element.
1197+
*
1198+
* @name forEach
1199+
* @memberof Complex64Array.prototype
1200+
* @type {Function}
1201+
* @param {Function} fcn - function to invoke
1202+
* @param {*} [thisArg] - function invocation context
1203+
* @throws {TypeError} `this` must be a complex number array
1204+
* @throws {TypeError} first argument must be a function
1205+
*
1206+
* @example
1207+
* var Complex64 = require( '@stdlib/complex/float32' );
1208+
*
1209+
* function log( v, i ) {
1210+
* console.log( '%s: %s', i, v.toString() );
1211+
* }
1212+
*
1213+
* var arr = new Complex64Array( 3 );
1214+
*
1215+
* arr.set( [ 1.0, 1.0 ], 0 );
1216+
* arr.set( [ 2.0, 2.0 ], 1 );
1217+
* arr.set( [ 3.0, 3.0 ], 2 );
1218+
*
1219+
* arr.forEach( log );
1220+
*/
1221+
setReadOnly( Complex64Array.prototype, 'forEach', function forEach( fcn, thisArg ) {
1222+
var buf;
1223+
var i;
1224+
var z;
1225+
if ( !isComplexArray( this ) ) {
1226+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1227+
}
1228+
if ( !isFunction( fcn ) ) {
1229+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
1230+
}
1231+
buf = this._buffer;
1232+
for ( i = 0; i < this._length; i++ ) {
1233+
z = getComplex64( buf, i );
1234+
fcn.call( thisArg, z, i, this );
1235+
}
1236+
});
1237+
11951238
/**
11961239
* Returns an array element.
11971240
*

0 commit comments

Comments
 (0)