Skip to content

Commit 26c27af

Browse files
Jaysukh-409kgryte
andauthored
feat: add lastIndexOf method to array/complex128
PR-URL: #1229 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 398a2ac commit 26c27af

File tree

6 files changed

+542
-0
lines changed

6 files changed

+542
-0
lines changed

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

+44
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,50 @@ idx = arr.indexOf( new Complex128( 1.0, -1.0 ), 1 );
12741274
// returns -1
12751275
```
12761276

1277+
<a name="method-last-index-of"></a>
1278+
1279+
#### Complex128Array.prototype.lastIndexOf( searchElement\[, fromIndex] )
1280+
1281+
Returns the last index at which a given element can be found.
1282+
1283+
```javascript
1284+
var Complex128 = require( '@stdlib/complex/float64' );
1285+
1286+
var arr = new Complex128Array( 5 );
1287+
1288+
arr.set( [ 1.0, -1.0 ], 0 );
1289+
arr.set( [ 2.0, -2.0 ], 1 );
1290+
arr.set( [ 3.0, -3.0 ], 2 );
1291+
arr.set( [ 4.0, -4.0 ], 3 );
1292+
arr.set( [ 2.0, -2.0 ], 4 );
1293+
1294+
var idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ) );
1295+
// returns 2
1296+
1297+
idx = arr.lastIndexOf( new Complex128( 2.0, -2.0 ), 2 );
1298+
// returns 1
1299+
1300+
idx = arr.lastIndexOf( new Complex128( 4.0, -4.0 ), -1 );
1301+
// returns 3
1302+
```
1303+
1304+
If `searchElement` is not present in the array, the method returns `-1`.
1305+
1306+
```javascript
1307+
var Complex128 = require( '@stdlib/complex/float64' );
1308+
1309+
var arr = new Complex128Array( 5 );
1310+
1311+
arr.set( [ 1.0, -1.0 ], 0 );
1312+
arr.set( [ 2.0, -2.0 ], 1 );
1313+
1314+
var idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ) );
1315+
// returns -1
1316+
1317+
idx = arr.lastIndexOf( new Complex128( 2.0, -2.0 ), 0 );
1318+
// returns -1
1319+
```
1320+
12771321
<a name="method-set"></a>
12781322

12791323
#### Complex128Array.prototype.set( z\[, i] )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 Complex128 = require( '@stdlib/complex/float64' );
25+
var isInteger = require('@stdlib/assert/is-integer' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var Complex128Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':lastIndexOf', function benchmark( b ) {
33+
var arr;
34+
var idx;
35+
var v;
36+
var i;
37+
38+
arr = [];
39+
for ( i = 0; i < 10; i++ ) {
40+
arr.push( new Complex128( i, i ) );
41+
}
42+
arr = new Complex128Array( arr );
43+
44+
v = new Complex128( 10.0, 10.0 );
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
idx = arr.lastIndexOf( v, 9 );
49+
if ( typeof idx !== 'number' ) {
50+
b.fail( 'should return an integer' );
51+
}
52+
}
53+
b.toc();
54+
if ( !isInteger( idx ) ) {
55+
b.fail( 'should return an integer' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 Complex128 = require( '@stdlib/complex/float64' );
26+
var isInteger = require('@stdlib/assert/is-integer' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var Complex128Array = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var arr;
42+
var i;
43+
44+
arr = [];
45+
for ( i = 0; i < len; i++ ) {
46+
arr.push( new Complex128( i, -i ) );
47+
}
48+
arr = new Complex128Array( arr );
49+
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var idx;
60+
var v;
61+
var i;
62+
63+
v = new Complex128( 0, 0 );
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
idx = arr.lastIndexOf( v );
68+
if ( typeof idx !== 'number' ) {
69+
b.fail( 'should return an integer' );
70+
}
71+
}
72+
b.toc();
73+
if ( !isInteger( idx ) ) {
74+
b.fail( 'should return an integer' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( pkg+':lastIndexOf:len='+len, f );
103+
}
104+
}
105+
106+
main();

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

+29
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,35 @@ declare class Complex128Array implements Complex128ArrayInterface {
544544
*/
545545
indexOf( searchElement: ComplexLike, fromIndex?: number ): number;
546546

547+
/**
548+
* Returns the last index at which a given element can be found.
549+
*
550+
* @param searchElement - element to find
551+
* @param fromIndex - index at which to start searching backward (inclusive)
552+
* @returns index or -1
553+
*
554+
* @example
555+
* var Complex128 = require( '@stdlib/complex/float64' );
556+
*
557+
* var arr = new Complex128Array( 5 );
558+
*
559+
* arr.set( [ 1.0, -1.0 ], 0 );
560+
* arr.set( [ 2.0, -2.0 ], 1 );
561+
* arr.set( [ 3.0, -3.0 ], 2 );
562+
* arr.set( [ 4.0, -4.0 ], 3 );
563+
* arr.set( [ 3.0, -3.0 ], 4 );
564+
*
565+
* var idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ) );
566+
* // returns 4
567+
*
568+
* idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ), 3 );
569+
* // returns 2
570+
*
571+
* idx = arr.lastIndexOf( new Complex128( 2.0, -2.0 ), -3 );
572+
* // returns 1
573+
*/
574+
lastIndexOf( searchElement: ComplexLike, fromIndex?: number ): number;
575+
547576
/**
548577
* Sets an array element.
549578
*

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

+72
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,78 @@ setReadOnly( Complex128Array.prototype, 'indexOf', function indexOf( searchEleme
13291329
return -1;
13301330
});
13311331

1332+
/**
1333+
* Returns the last index at which a given element can be found.
1334+
*
1335+
* @name lastIndexOf
1336+
* @memberof Complex128Array.prototype
1337+
* @type {Function}
1338+
* @param {Complex128} searchElement - element to find
1339+
* @param {integer} [fromIndex] - index at which to start searching backward (inclusive)
1340+
* @throws {TypeError} `this` must be a complex number array
1341+
* @throws {TypeError} first argument must be a complex number
1342+
* @throws {TypeError} second argument must be an integer
1343+
* @returns {integer} index or -1
1344+
*
1345+
* @example
1346+
* var Complex128 = require( '@stdlib/complex/float64' );
1347+
*
1348+
* var arr = new Complex128Array( 5 );
1349+
*
1350+
* arr.set( [ 1.0, -1.0 ], 0 );
1351+
* arr.set( [ 2.0, -2.0 ], 1 );
1352+
* arr.set( [ 3.0, -3.0 ], 2 );
1353+
* arr.set( [ 4.0, -4.0 ], 3 );
1354+
* arr.set( [ 3.0, -3.0 ], 4 );
1355+
*
1356+
* var idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ) );
1357+
* // returns 4
1358+
*
1359+
* idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ), 3 );
1360+
* // returns 2
1361+
*
1362+
* idx = arr.lastIndexOf( new Complex128( 5.0, -5.0 ), 3 );
1363+
* // returns -1
1364+
*
1365+
* idx = arr.lastIndexOf( new Complex128( 2.0, -2.0 ), -3 );
1366+
* // returns 1
1367+
*/
1368+
setReadOnly( Complex128Array.prototype, 'lastIndexOf', function lastIndexOf( searchElement, fromIndex ) {
1369+
var buf;
1370+
var idx;
1371+
var re;
1372+
var im;
1373+
var i;
1374+
if ( !isComplexArray( this ) ) {
1375+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1376+
}
1377+
if ( !isComplexLike( searchElement ) ) {
1378+
throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) );
1379+
}
1380+
if ( arguments.length > 1 ) {
1381+
if ( !isInteger( fromIndex ) ) {
1382+
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
1383+
}
1384+
if ( fromIndex >= this._length ) {
1385+
fromIndex = this._length - 1;
1386+
} else if ( fromIndex < 0 ) {
1387+
fromIndex += this._length;
1388+
}
1389+
} else {
1390+
fromIndex = this._length - 1;
1391+
}
1392+
re = real( searchElement );
1393+
im = imag( searchElement );
1394+
buf = this._buffer;
1395+
for ( i = fromIndex; i >= 0; i-- ) {
1396+
idx = 2 * i;
1397+
if ( re === buf[ idx ] && im === buf[ idx+1 ] ) {
1398+
return i;
1399+
}
1400+
}
1401+
return -1;
1402+
});
1403+
13321404
/**
13331405
* Sets an array element.
13341406
*

0 commit comments

Comments
 (0)