Skip to content

Commit e244066

Browse files
Jaysukh-409kgryte
andauthored
feat: add findIndex method to array/complex64
PR-URL: #1201 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 7801277 commit e244066

File tree

6 files changed

+487
-0
lines changed

6 files changed

+487
-0
lines changed

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

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

859+
<a name="method-findIndex"></a>
860+
861+
#### Complex64Array.prototype.findIndex( predicate\[, thisArg] )
862+
863+
Returns the index of 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+
869+
function predicate( v ) {
870+
return ( realf( v ) === imagf( v ) );
871+
}
872+
873+
var arr = new Complex64Array( 3 );
874+
875+
// Set the first three elements:
876+
arr.set( [ 1.0, -1.0 ], 0 );
877+
arr.set( [ 2.0, -2.0 ], 1 );
878+
arr.set( [ 3.0, 3.0 ], 2 );
879+
880+
var idx = arr.findIndex( predicate );
881+
// returns 2
882+
```
883+
884+
The `predicate` function is provided three arguments:
885+
886+
- **value**: current array element.
887+
- **index**: current array element index.
888+
- **arr**: the array on which this method was called.
889+
890+
To set the function execution context, provide a `thisArg`.
891+
892+
```javascript
893+
var realf = require( '@stdlib/complex/realf' );
894+
var imagf = require( '@stdlib/complex/imagf' );
895+
896+
function predicate( v, i ) {
897+
this.count += 1;
898+
return ( i >= 0 && realf( v ) === imagf( v ) );
899+
}
900+
901+
var arr = new Complex64Array( 3 );
902+
903+
var context = {
904+
'count': 0
905+
};
906+
907+
// Set the first three elements:
908+
arr.set( [ 1.0, -1.0 ], 0 );
909+
arr.set( [ 2.0, -2.0 ], 1 );
910+
arr.set( [ 3.0, -3.0 ], 2 );
911+
912+
var idx = arr.findIndex( predicate, context );
913+
// returns -1
914+
915+
var count = context.count;
916+
// returns 3
917+
```
918+
859919
<a name="method-get"></a>
860920

861921
#### 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var Complex64Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':findIndex', function benchmark( b ) {
33+
var arr;
34+
var idx;
35+
var i;
36+
37+
arr = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
idx = arr.findIndex( predicate );
42+
if ( typeof idx !== 'number' ) {
43+
b.fail( 'should return an integer' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isInteger( idx ) ) {
48+
b.fail( 'should return an integer' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
53+
function predicate( v ) {
54+
return isComplex64( v );
55+
}
56+
});
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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
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 idx;
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
idx = arr.findIndex( predicate );
81+
if ( typeof idx !== 'number' ) {
82+
b.fail( 'should return an integer' );
83+
}
84+
}
85+
b.toc();
86+
if ( !isInteger( idx ) ) {
87+
b.fail( 'should return an integer' );
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+':findIndex:len='+len, f );
116+
}
117+
}
118+
119+
main();

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

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

346+
/**
347+
* Returns the index of 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 index or -1
352+
*
353+
* @example
354+
* var Complex64 = require( '@stdlib/complex/float32' );
355+
* var realf = require( '@stdlib/complex/realf' );
356+
* var imagf = require( '@stdlib/complex/imagf' );
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 idx = arr.findIndex( predicate );
369+
* // returns 2
370+
*/
371+
findIndex<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): number;
372+
346373
/**
347374
* Returns an array element.
348375
*

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

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

922+
/**
923+
* Returns the index of the first element in an array for which a predicate function returns a truthy value.
924+
*
925+
* @name findIndex
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 {integer} index or -1
933+
*
934+
* @example
935+
* var Complex64 = require( '@stdlib/complex/float32' );
936+
* var realf = require( '@stdlib/complex/realf' );
937+
* var imagf = require( '@stdlib/complex/imagf' );
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 idx = arr.findIndex( predicate );
950+
* // returns 2
951+
*/
952+
setReadOnly( Complex64Array.prototype, 'findIndex', function findIndex( predicate, thisArg ) {
953+
var buf;
954+
var i;
955+
var z;
956+
if ( !isComplexArray( this ) ) {
957+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
958+
}
959+
if ( !isFunction( predicate ) ) {
960+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
961+
}
962+
buf = this._buffer;
963+
for ( i = 0; i < this._length; i++ ) {
964+
z = getComplex64( buf, i );
965+
if ( predicate.call( thisArg, z, i, this ) ) {
966+
return i;
967+
}
968+
}
969+
return -1;
970+
});
971+
922972
/**
923973
* Returns an array element.
924974
*

0 commit comments

Comments
 (0)