Skip to content

Commit c896236

Browse files
authored
feat: add findLastIndex method to array/complex64
PR-URL: #1204 Reviewed-by: Athan Reines <[email protected]>
1 parent c566265 commit c896236

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
@@ -1062,6 +1062,66 @@ var count = context.count;
10621062
// returns 2
10631063
```
10641064

1065+
<a name="method-findLastIndex"></a>
1066+
1067+
#### Complex64Array.prototype.findLastIndex( predicate\[, thisArg] )
1068+
1069+
Returns the index of the last element in an array for which a predicate function returns a truthy value.
1070+
1071+
```javascript
1072+
var realf = require( '@stdlib/complex/realf' );
1073+
var imagf = require( '@stdlib/complex/imagf' );
1074+
1075+
function predicate( v ) {
1076+
return ( realf( v ) === imagf( v ) );
1077+
}
1078+
1079+
var arr = new Complex64Array( 3 );
1080+
1081+
// Set the first three elements:
1082+
arr.set( [ 1.0, 1.0 ], 0 );
1083+
arr.set( [ 2.0, 2.0 ], 1 );
1084+
arr.set( [ 3.0, -3.0 ], 2 );
1085+
1086+
var idx = arr.findLastIndex( predicate );
1087+
// returns 1
1088+
```
1089+
1090+
The `predicate` function is provided three arguments:
1091+
1092+
- **value**: current array element.
1093+
- **index**: current array element index.
1094+
- **arr**: the array on which this method was called.
1095+
1096+
To set the function execution context, provide a `thisArg`.
1097+
1098+
```javascript
1099+
var realf = require( '@stdlib/complex/realf' );
1100+
var imagf = require( '@stdlib/complex/imagf' );
1101+
1102+
function predicate( v, i ) {
1103+
this.count += 1;
1104+
return ( i >= 0 && realf( v ) === imagf( v ) );
1105+
}
1106+
1107+
var arr = new Complex64Array( 3 );
1108+
1109+
var context = {
1110+
'count': 0
1111+
};
1112+
1113+
// Set the first three elements:
1114+
arr.set( [ 1.0, -1.0 ], 0 );
1115+
arr.set( [ 2.0, -2.0 ], 1 );
1116+
arr.set( [ 3.0, -3.0 ], 2 );
1117+
1118+
var idx = arr.findLastIndex( predicate, context );
1119+
// returns -1
1120+
1121+
var count = context.count;
1122+
// returns 3
1123+
```
1124+
10651125
<a name="method-get"></a>
10661126

10671127
#### 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+':findLastIndex', 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.findLastIndex( 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+
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 idx;
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
idx = arr.findLastIndex( 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+':findLastIndex: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
@@ -436,6 +436,33 @@ declare class Complex64Array implements Complex64ArrayInterface {
436436
*/
437437
findLast<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): Complex64 | void;
438438

439+
/**
440+
* Returns the index of the last element in an array for which a predicate function returns a truthy value.
441+
*
442+
* @param predicate - test function
443+
* @param thisArg - execution context
444+
* @returns index or -1
445+
*
446+
* @example
447+
* var Complex64 = require( '@stdlib/complex/float32' );
448+
* var realf = require( '@stdlib/complex/realf' );
449+
* var imagf = require( '@stdlib/complex/imagf' );
450+
*
451+
* function predicate( v ) {
452+
* return ( realf( v ) === imagf( v ) );
453+
* }
454+
*
455+
* var arr = new Complex64Array( 3 );
456+
*
457+
* arr.set( [ 1.0, 1.0 ], 0 );
458+
* arr.set( [ 2.0, 2.0 ], 1 );
459+
* arr.set( [ 3.0, -3.0 ], 2 );
460+
*
461+
* var idx = arr.findLastIndex( predicate );
462+
* // returns 1
463+
*/
464+
findLastIndex<U = unknown>( predicate: Predicate, thisArg?: ThisParameterType<Predicate<U>> ): number;
465+
439466
/**
440467
* Returns an array element.
441468
*

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,56 @@ setReadOnly( Complex64Array.prototype, 'findLast', function findLast( predicate,
10791079
}
10801080
});
10811081

1082+
/**
1083+
* Returns the index of the last element in an array for which a predicate function returns a truthy value.
1084+
*
1085+
* @name findLastIndex
1086+
* @memberof Complex64Array.prototype
1087+
* @type {Function}
1088+
* @param {Function} predicate - test function
1089+
* @param {*} [thisArg] - predicate function execution context
1090+
* @throws {TypeError} `this` must be a complex number array
1091+
* @throws {TypeError} first argument must be a function
1092+
* @returns {integer} index or -1
1093+
*
1094+
* @example
1095+
* var Complex64 = require( '@stdlib/complex/float32' );
1096+
* var realf = require( '@stdlib/complex/realf' );
1097+
* var imagf = require( '@stdlib/complex/imagf' );
1098+
*
1099+
* function predicate( v ) {
1100+
* return ( realf( v ) === imagf( v ) );
1101+
* }
1102+
*
1103+
* var arr = new Complex64Array( 3 );
1104+
*
1105+
* arr.set( [ 1.0, 1.0 ], 0 );
1106+
* arr.set( [ 2.0, 2.0 ], 1 );
1107+
* arr.set( [ 3.0, -3.0 ], 2 );
1108+
*
1109+
* var idx = arr.findLastIndex( predicate );
1110+
* // returns 1
1111+
*/
1112+
setReadOnly( Complex64Array.prototype, 'findLastIndex', function findLastIndex( predicate, thisArg ) {
1113+
var buf;
1114+
var i;
1115+
var z;
1116+
if ( !isComplexArray( this ) ) {
1117+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1118+
}
1119+
if ( !isFunction( predicate ) ) {
1120+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
1121+
}
1122+
buf = this._buffer;
1123+
for ( i = this._length-1; i >= 0; i-- ) {
1124+
z = getComplex64( buf, i );
1125+
if ( predicate.call( thisArg, z, i, this ) ) {
1126+
return i;
1127+
}
1128+
}
1129+
return -1;
1130+
});
1131+
10821132
/**
10831133
* Returns an array element.
10841134
*

0 commit comments

Comments
 (0)