Skip to content

Commit 2277b01

Browse files
Jaysukh-409kgryte
andauthored
feat: add find method to array/complex64
PR-URL: #1200 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent b597754 commit 2277b01

File tree

6 files changed

+512
-0
lines changed

6 files changed

+512
-0
lines changed

lib/node_modules/@stdlib/array/complex64/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,79 @@ var count = context.count;
856856
// returns 3
857857
```
858858

859+
<a name="method-find"></a>
860+
861+
#### Complex64Array.prototype.find( predicate\[, thisArg] )
862+
863+
Returns the first element in an array for which a predicate function returns a truthy value.
864+
865+
```javascript
866+
var realf = require( '@stdlib/complex/realf' );
867+
var imagf = require( '@stdlib/complex/imagf' );
868+
var Complex64 = require( '@stdlib/complex/float32' );
869+
870+
function predicate( v ) {
871+
return ( realf( v ) === imagf( v ) );
872+
}
873+
874+
var arr = new Complex64Array( 3 );
875+
876+
// Set the first three elements:
877+
arr.set( [ 1.0, 1.0 ], 0 );
878+
arr.set( [ 2.0, 2.0 ], 1 );
879+
arr.set( [ 3.0, 3.0 ], 2 );
880+
881+
var z = arr.find( predicate );
882+
// returns <Complex64>
883+
884+
var re = realf( z );
885+
// returns 1.0
886+
887+
var im = imagf( z );
888+
// returns 1.0
889+
```
890+
891+
The `predicate` function is provided three arguments:
892+
893+
- **value**: current array element.
894+
- **index**: current array element index.
895+
- **arr**: the array on which this method was called.
896+
897+
To set the function execution context, provide a `thisArg`.
898+
899+
```javascript
900+
var realf = require( '@stdlib/complex/realf' );
901+
var imagf = require( '@stdlib/complex/imagf' );
902+
903+
function predicate( v, i ) {
904+
this.count += 1;
905+
return ( i >= 0 && realf( v ) === imagf( v ) );
906+
}
907+
908+
var arr = new Complex64Array( 3 );
909+
910+
var context = {
911+
'count': 0
912+
};
913+
914+
// Set the first three elements:
915+
arr.set( [ 1.0, -1.0 ], 0 );
916+
arr.set( [ 2.0, 2.0 ], 1 );
917+
arr.set( [ 3.0, 3.0 ], 2 );
918+
919+
var z = arr.find( predicate, context );
920+
// returns <Complex64>
921+
922+
var re = realf( z );
923+
// returns 2.0
924+
925+
var im = imagf( z );
926+
// returns 2.0
927+
928+
var count = context.count;
929+
// returns 2
930+
```
931+
859932
<a name="method-findIndex"></a>
860933

861934
#### Complex64Array.prototype.findIndex( predicate\[, thisArg] )
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+':find', function benchmark( b ) {
32+
var arr;
33+
var z;
34+
var i;
35+
36+
arr = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
z = arr.find( predicate );
41+
if ( typeof z !== 'object' ) {
42+
b.fail( 'should return an object' );
43+
}
44+
}
45+
b.toc();
46+
if ( !isComplex64( z ) ) {
47+
b.fail( 'should return a complex number' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
52+
function predicate( v ) {
53+
return isComplex64( v );
54+
}
55+
});
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 isComplex64 = require( '@stdlib/assert/is-complex64' );
27+
var realf = require( '@stdlib/complex/realf' );
28+
var imagf = require( '@stdlib/complex/imagf' );
29+
var pkg = require( './../package.json' ).name;
30+
var Complex64Array = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Predicate function.
37+
*
38+
* @private
39+
* @param {Complex64} value - array element
40+
* @param {NonNegativeInteger} idx - array element index
41+
* @param {Complex64Array} arr - array instance
42+
* @returns {boolean} boolean indicating whether a value passes a test
43+
*/
44+
function predicate( value ) {
45+
return ( realf( value ) === -imagf( value ) );
46+
}
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {PositiveInteger} len - array length
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len ) {
56+
var arr;
57+
var i;
58+
59+
arr = [];
60+
for ( i = 0; i < len-1; i++ ) {
61+
arr.push( new Complex64( i, i ) );
62+
}
63+
arr.push( new Complex64( len-1, -( len-1 ) ) );
64+
arr = new Complex64Array( arr );
65+
66+
return benchmark;
67+
68+
/**
69+
* Benchmark function.
70+
*
71+
* @private
72+
* @param {Benchmark} b - benchmark instance
73+
*/
74+
function benchmark( b ) {
75+
var z;
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
z = arr.find( predicate );
81+
if ( typeof z !== 'object' ) {
82+
b.fail( 'should return an object' );
83+
}
84+
}
85+
b.toc();
86+
if ( !isComplex64( z ) ) {
87+
b.fail( 'should return a complex number' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
}
92+
}
93+
94+
95+
// MAIN //
96+
97+
/**
98+
* Main execution sequence.
99+
*
100+
* @private
101+
*/
102+
function main() {
103+
var len;
104+
var min;
105+
var max;
106+
var f;
107+
var i;
108+
109+
min = 1; // 10^min
110+
max = 6; // 10^max
111+
112+
for ( i = min; i <= max; i++ ) {
113+
len = pow( 10, i );
114+
f = createBenchmark( len );
115+
bench( pkg+':find:len='+len, f );
116+
}
117+
}
118+
119+
main();

lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,39 @@ declare class Complex64Array implements Complex64ArrayInterface {
343343
*/
344344
every<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): boolean;
345345

346+
/**
347+
* Returns the first element in an array for which a predicate function returns a truthy value.
348+
*
349+
* @param predicate - test function
350+
* @param thisArg - execution context
351+
* @returns array element or undefined
352+
*
353+
* @example
354+
* var realf = require( '@stdlib/complex/realf' );
355+
* var imagf = require( '@stdlib/complex/imagf' );
356+
* var Complex64 = require( '@stdlib/complex/float32' );
357+
*
358+
* function predicate( v ) {
359+
* return ( realf( v ) === imagf( v ) );
360+
* }
361+
*
362+
* var arr = new Complex64Array( 3 );
363+
*
364+
* arr.set( [ 1.0, 1.0 ], 0 );
365+
* arr.set( [ 2.0, 2.0 ], 1 );
366+
* arr.set( [ 3.0, 3.0 ], 2 );
367+
*
368+
* var z = arr.find( predicate );
369+
* // returns <Complex64>
370+
*
371+
* var re = realf( z );
372+
* // returns 1.0
373+
*
374+
* var im = imagf( z );
375+
* // returns 1.0
376+
*/
377+
find<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): Complex64 | void;
378+
346379
/**
347380
* Returns the index of the first element in an array for which a predicate function returns a truthy value.
348381
*

lib/node_modules/@stdlib/array/complex64/lib/main.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,61 @@ setReadOnly( Complex64Array.prototype, 'every', function every( predicate, thisA
919919
return true;
920920
});
921921

922+
/**
923+
* Returns the first element in an array for which a predicate function returns a truthy value.
924+
*
925+
* @name find
926+
* @memberof Complex64Array.prototype
927+
* @type {Function}
928+
* @param {Function} predicate - test function
929+
* @param {*} [thisArg] - predicate function execution context
930+
* @throws {TypeError} `this` must be a complex number array
931+
* @throws {TypeError} first argument must be a function
932+
* @returns {(Complex64|void)} array element or undefined
933+
*
934+
* @example
935+
* var realf = require( '@stdlib/complex/realf' );
936+
* var imagf = require( '@stdlib/complex/imagf' );
937+
* var Complex64 = require( '@stdlib/complex/float32' );
938+
*
939+
* function predicate( v ) {
940+
* return ( realf( v ) === imagf( v ) );
941+
* }
942+
*
943+
* var arr = new Complex64Array( 3 );
944+
*
945+
* arr.set( [ 1.0, 1.0 ], 0 );
946+
* arr.set( [ 2.0, 2.0 ], 1 );
947+
* arr.set( [ 3.0, 3.0 ], 2 );
948+
*
949+
* var z = arr.find( predicate );
950+
* // returns <Complex64>
951+
*
952+
* var re = realf( z );
953+
* // returns 1.0
954+
*
955+
* var im = imagf( z );
956+
* // returns 1.0
957+
*/
958+
setReadOnly( Complex64Array.prototype, 'find', function find( predicate, thisArg ) {
959+
var buf;
960+
var i;
961+
var z;
962+
if ( !isComplexArray( this ) ) {
963+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
964+
}
965+
if ( !isFunction( predicate ) ) {
966+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
967+
}
968+
buf = this._buffer;
969+
for ( i = 0; i < this._length; i++ ) {
970+
z = getComplex64( buf, i );
971+
if ( predicate.call( thisArg, z, i, this ) ) {
972+
return z;
973+
}
974+
}
975+
});
976+
922977
/**
923978
* Returns the index of the first element in an array for which a predicate function returns a truthy value.
924979
*

0 commit comments

Comments
 (0)