Skip to content

Commit c566265

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

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
@@ -989,6 +989,79 @@ var count = context.count;
989989
// returns 3
990990
```
991991

992+
<a name="method-findLast"></a>
993+
994+
#### Complex64Array.prototype.findLast( predicate\[, thisArg] )
995+
996+
Returns the last element in an array for which a predicate function returns a truthy value.
997+
998+
```javascript
999+
var realf = require( '@stdlib/complex/realf' );
1000+
var imagf = require( '@stdlib/complex/imagf' );
1001+
var Complex64 = require( '@stdlib/complex/float32' );
1002+
1003+
function predicate( v ) {
1004+
return ( realf( v ) === imagf( v ) );
1005+
}
1006+
1007+
var arr = new Complex64Array( 3 );
1008+
1009+
// Set the first three elements:
1010+
arr.set( [ 1.0, 1.0 ], 0 );
1011+
arr.set( [ 2.0, 2.0 ], 1 );
1012+
arr.set( [ 3.0, 3.0 ], 2 );
1013+
1014+
var z = arr.findLast( predicate );
1015+
// returns <Complex64>
1016+
1017+
var re = realf( z );
1018+
// returns 3.0
1019+
1020+
var im = imagf( z );
1021+
// returns 3.0
1022+
```
1023+
1024+
The `predicate` function is provided three arguments:
1025+
1026+
- **value**: current array element.
1027+
- **index**: current array element index.
1028+
- **arr**: the array on which this method was called.
1029+
1030+
To set the function execution context, provide a `thisArg`.
1031+
1032+
```javascript
1033+
var realf = require( '@stdlib/complex/realf' );
1034+
var imagf = require( '@stdlib/complex/imagf' );
1035+
1036+
function predicate( v, i ) {
1037+
this.count += 1;
1038+
return ( i >= 0 && realf( v ) === imagf( v ) );
1039+
}
1040+
1041+
var arr = new Complex64Array( 3 );
1042+
1043+
var context = {
1044+
'count': 0
1045+
};
1046+
1047+
// Set the first three elements:
1048+
arr.set( [ 1.0, -1.0 ], 0 );
1049+
arr.set( [ 2.0, 2.0 ], 1 );
1050+
arr.set( [ 3.0, -3.0 ], 2 );
1051+
1052+
var z = arr.findLast( predicate, context );
1053+
// returns <Complex64>
1054+
1055+
var re = realf( z );
1056+
// returns 2.0
1057+
1058+
var im = imagf( z );
1059+
// returns 2.0
1060+
1061+
var count = context.count;
1062+
// returns 2
1063+
```
1064+
9921065
<a name="method-get"></a>
9931066

9941067
#### Complex64Array.prototype.get( i )
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+':findLast', 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.findLast( 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+
arr.push( new Complex64( 1, -1 ) );
61+
for ( i = 1; i < len; i++ ) {
62+
arr.push( new Complex64( i, i ) );
63+
}
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.findLast( 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+':findLast: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
@@ -403,6 +403,39 @@ declare class Complex64Array implements Complex64ArrayInterface {
403403
*/
404404
findIndex<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): number;
405405

406+
/**
407+
* Returns the last element in an array for which a predicate function returns a truthy value.
408+
*
409+
* @param predicate - test function
410+
* @param thisArg - execution context
411+
* @returns array element or undefined
412+
*
413+
* @example
414+
* var realf = require( '@stdlib/complex/realf' );
415+
* var imagf = require( '@stdlib/complex/imagf' );
416+
* var Complex64 = require( '@stdlib/complex/float32' );
417+
*
418+
* function predicate( v ) {
419+
* return ( realf( v ) === imagf( v ) );
420+
* }
421+
*
422+
* var arr = new Complex64Array( 3 );
423+
*
424+
* arr.set( [ 1.0, 1.0 ], 0 );
425+
* arr.set( [ 2.0, 2.0 ], 1 );
426+
* arr.set( [ 3.0, 3.0 ], 2 );
427+
*
428+
* var z = arr.findLast( predicate );
429+
* // returns <Complex64>
430+
*
431+
* var re = realf( z );
432+
* // returns 3.0
433+
*
434+
* var im = imagf( z );
435+
* // returns 3.0
436+
*/
437+
findLast<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): Complex64 | void;
438+
406439
/**
407440
* Returns an array element.
408441
*

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,61 @@ setReadOnly( Complex64Array.prototype, 'findIndex', function findIndex( predicat
10241024
return -1;
10251025
});
10261026

1027+
/**
1028+
* Returns the last element in an array for which a predicate function returns a truthy value.
1029+
*
1030+
* @name findLast
1031+
* @memberof Complex64Array.prototype
1032+
* @type {Function}
1033+
* @param {Function} predicate - test function
1034+
* @param {*} [thisArg] - predicate function execution context
1035+
* @throws {TypeError} `this` must be a complex number array
1036+
* @throws {TypeError} first argument must be a function
1037+
* @returns {(Complex64|void)} array element or undefined
1038+
*
1039+
* @example
1040+
* var realf = require( '@stdlib/complex/realf' );
1041+
* var imagf = require( '@stdlib/complex/imagf' );
1042+
* var Complex64 = require( '@stdlib/complex/float32' );
1043+
*
1044+
* function predicate( v ) {
1045+
* return ( realf( v ) === imagf( v ) );
1046+
* }
1047+
*
1048+
* var arr = new Complex64Array( 3 );
1049+
*
1050+
* arr.set( [ 1.0, 1.0 ], 0 );
1051+
* arr.set( [ 2.0, 2.0 ], 1 );
1052+
* arr.set( [ 3.0, 3.0 ], 2 );
1053+
*
1054+
* var z = arr.findLast( predicate );
1055+
* // returns <Complex64>
1056+
*
1057+
* var re = realf( z );
1058+
* // returns 3.0
1059+
*
1060+
* var im = imagf( z );
1061+
* // returns 3.0
1062+
*/
1063+
setReadOnly( Complex64Array.prototype, 'findLast', function findLast( predicate, thisArg ) {
1064+
var buf;
1065+
var i;
1066+
var z;
1067+
if ( !isComplexArray( this ) ) {
1068+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1069+
}
1070+
if ( !isFunction( predicate ) ) {
1071+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
1072+
}
1073+
buf = this._buffer;
1074+
for ( i = this._length-1; i >= 0; i-- ) {
1075+
z = getComplex64( buf, i );
1076+
if ( predicate.call( thisArg, z, i, this ) ) {
1077+
return z;
1078+
}
1079+
}
1080+
});
1081+
10271082
/**
10281083
* Returns an array element.
10291084
*

0 commit comments

Comments
 (0)