Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Dec 19, 2023
1 parent e7f296a commit 79d1a5f
Show file tree
Hide file tree
Showing 8 changed files with 500 additions and 3 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,47 @@ idx = arr.indexOf( new Complex64( 1.0, -1.0 ), 1 );
// returns -1
```

<a name="method-lastIndexOf"></a>

#### Complex64Array.prototype.lastIndexOf( searchElement\[, fromIndex] )

Returns the last index at which a given element can be found.

```javascript
var Complex64 = require( '@stdlib/complex-float32' );

var arr = new Complex64Array( 10 );

arr.set( [ 1.0, -1.0 ], 0 );
arr.set( [ 2.0, -2.0 ], 1 );
arr.set( [ 3.0, -3.0 ], 2 );
arr.set( [ 4.0, -4.0 ], 3 );
arr.set( [ 2.0, -2.0 ], 4 );

var idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ) );
// returns 2

idx = arr.lastIndexOf( new Complex64( 2.0, -2.0 ), 2 );
// returns 1
```

If `searchElement` is not present in the array, the method returns `-1`.

```javascript
var Complex64 = require( '@stdlib/complex-float32' );

var arr = new Complex64Array( 10 );

arr.set( [ 1.0, -1.0 ], 0 );
arr.set( [ 2.0, -2.0 ], 1 );

var idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ) );
// returns -1

idx = arr.lastIndexOf( new Complex64( 2.0, -2.0 ), 0 );
// returns -1
```

<a name="method-set"></a>

#### Complex64Array.prototype.set( z\[, i] )
Expand Down
59 changes: 59 additions & 0 deletions benchmark/benchmark.last_index_of.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench-harness' );
var Complex64 = require( '@stdlib/complex-float32' );
var isInteger = require('@stdlib/assert-is-integer' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var Complex64Array = require( './../lib' );


// MAIN //

bench( pkg+':lastIndexOf', function benchmark( b ) {
var arr;
var idx;
var v;
var i;

arr = [];
for ( i = 0; i < 10; i++ ) {
arr.push( new Complex64( i, i ) );
}
arr = new Complex64Array( arr );

v = new Complex64( 10.0, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = arr.lastIndexOf( v, 9 );
if ( typeof idx !== 'number' ) {
b.fail( 'should return an integer' );
}
}
b.toc();
if ( !isInteger( idx ) ) {
b.fail( 'should return an integer' );
}
b.pass( 'benchmark finished' );
b.end();
});
106 changes: 106 additions & 0 deletions benchmark/benchmark.last_index_of.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench-harness' );
var pow = require( '@stdlib/math-base-special-pow' );
var Complex64 = require( '@stdlib/complex-float32' );
var isInteger = require('@stdlib/assert-is-integer' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var Complex64Array = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr;
var i;

arr = [];
for ( i = 0; i < len; i++ ) {
arr.push( new Complex64( i, -i ) );
}
arr = new Complex64Array( arr );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var idx;
var v;
var i;

v = new Complex64( 0, 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = arr.lastIndexOf( v );
if ( typeof idx !== 'number' ) {
b.fail( 'should return an integer' );
}
}
b.toc();
if ( !isInteger( idx ) ) {
b.fail( 'should return an integer' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':lastIndexOf:len='+len, f );
}
}

main();
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index.js.map

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,35 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
indexOf( searchElement: ComplexLike, fromIndex?: number ): number;

/**
* Returns the last index at which a given element can be found.
*
* @param searchElement - element to find
* @param fromIndex - index at which to start searching backward (inclusive)
* @returns index or -1
*
* @example
* var Complex64 = require( '@stdlib/complex-float32' );
*
* var arr = new Complex64Array( 5 );
*
* arr.set( [ 1.0, -1.0 ], 0 );
* arr.set( [ 2.0, -2.0 ], 1 );
* arr.set( [ 3.0, -3.0 ], 2 );
* arr.set( [ 4.0, -4.0 ], 3 );
* arr.set( [ 3.0, -3.0 ], 4 );
*
* var idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ) );
* // returns 4
*
* idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ), 3 );
* // returns 2
*
* idx = arr.lastIndexOf( new Complex64( 5.0, -5.0 ), 3 );
* // returns -1
*/
lastIndexOf( searchElement: ComplexLike, fromIndex?: number ): number;

/**
* Sets an array element.
*
Expand Down
67 changes: 67 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,73 @@ setReadOnly( Complex64Array.prototype, 'indexOf', function indexOf( searchElemen
return -1;
});

/**
* Returns the last index at which a given element can be found.
*
* @name lastIndexOf
* @memberof Complex64Array.prototype
* @type {Function}
* @param {Complex64} searchElement - element to find
* @param {integer} [fromIndex] - index at which to start searching backward (inclusive)
* @throws {TypeError} `this` must be a complex number array
* @throws {TypeError} first argument must be a complex number
* @throws {TypeError} second argument must be an nonnegative integer
* @returns {integer} index or -1
*
* @example
* var Complex64 = require( '@stdlib/complex-float32' );
*
* var arr = new Complex64Array( 5 );
*
* arr.set( [ 1.0, -1.0 ], 0 );
* arr.set( [ 2.0, -2.0 ], 1 );
* arr.set( [ 3.0, -3.0 ], 2 );
* arr.set( [ 4.0, -4.0 ], 3 );
* arr.set( [ 3.0, -3.0 ], 4 );
*
* var idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ) );
* // returns 4
*
* idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ), 3 );
* // returns 2
*
* idx = arr.lastIndexOf( new Complex64( 5.0, -5.0 ), 3 );
* // returns -1
*/
setReadOnly( Complex64Array.prototype, 'lastIndexOf', function lastIndexOf( searchElement, fromIndex ) {
var buf;
var idx;
var re;
var im;
var i;
if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
if ( !isComplexLike( searchElement ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) );
}
if ( arguments.length > 1 ) {
if ( !isNonNegativeInteger( fromIndex ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', fromIndex ) );
}
if ( fromIndex >= this._length ) {
fromIndex = this._length - 1;
}
} else {
fromIndex = this._length - 1;
}
re = realf( searchElement );
im = imagf( searchElement );
buf = this._buffer;
for ( i = fromIndex; i >= 0; i-- ) {
idx = 2 * i;
if ( re === buf[ idx ] && im === buf[ idx+1 ] ) {
return i;
}
}
return -1;
});

/**
* Number of array elements.
*
Expand Down
Loading

0 comments on commit 79d1a5f

Please sign in to comment.