Skip to content

Commit 83eb3e4

Browse files
committed
Auto-generated commit
1 parent 4ab44c4 commit 83eb3e4

File tree

8 files changed

+549
-7
lines changed

8 files changed

+549
-7
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,50 @@ idx = arr.indexOf( new Complex128( 1.0, -1.0 ), 1 );
13051305
// returns -1
13061306
```
13071307

1308+
<a name="method-last-index-of"></a>
1309+
1310+
#### Complex128Array.prototype.lastIndexOf( searchElement\[, fromIndex] )
1311+
1312+
Returns the last index at which a given element can be found.
1313+
1314+
```javascript
1315+
var Complex128 = require( '@stdlib/complex-float64' );
1316+
1317+
var arr = new Complex128Array( 5 );
1318+
1319+
arr.set( [ 1.0, -1.0 ], 0 );
1320+
arr.set( [ 2.0, -2.0 ], 1 );
1321+
arr.set( [ 3.0, -3.0 ], 2 );
1322+
arr.set( [ 4.0, -4.0 ], 3 );
1323+
arr.set( [ 2.0, -2.0 ], 4 );
1324+
1325+
var idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ) );
1326+
// returns 2
1327+
1328+
idx = arr.lastIndexOf( new Complex128( 2.0, -2.0 ), 2 );
1329+
// returns 1
1330+
1331+
idx = arr.lastIndexOf( new Complex128( 4.0, -4.0 ), -1 );
1332+
// returns 3
1333+
```
1334+
1335+
If `searchElement` is not present in the array, the method returns `-1`.
1336+
1337+
```javascript
1338+
var Complex128 = require( '@stdlib/complex-float64' );
1339+
1340+
var arr = new Complex128Array( 5 );
1341+
1342+
arr.set( [ 1.0, -1.0 ], 0 );
1343+
arr.set( [ 2.0, -2.0 ], 1 );
1344+
1345+
var idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ) );
1346+
// returns -1
1347+
1348+
idx = arr.lastIndexOf( new Complex128( 2.0, -2.0 ), 0 );
1349+
// returns -1
1350+
```
1351+
13081352
<a name="method-set"></a>
13091353

13101354
#### Complex128Array.prototype.set( z\[, i] )
Lines changed: 59 additions & 0 deletions
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-harness' );
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+
});
Lines changed: 106 additions & 0 deletions
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-harness' );
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();

dist/index.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/types/index.d.ts

Lines changed: 29 additions & 0 deletions
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/main.js

Lines changed: 72 additions & 0 deletions
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)