Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lastIndexOf method to array/complex128 #1229

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/node_modules/@stdlib/array/complex128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,50 @@ idx = arr.indexOf( new Complex128( 1.0, -1.0 ), 1 );
// returns -1
```

<a name="method-last-index-of"></a>

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

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

```javascript
var Complex128 = require( '@stdlib/complex/float64' );

var arr = new Complex128Array( 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( [ 2.0, -2.0 ], 4 );

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

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

idx = arr.lastIndexOf( new Complex128( 4.0, -4.0 ), -1 );
// returns 3
```

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

```javascript
var Complex128 = require( '@stdlib/complex/float64' );

var arr = new Complex128Array( 5 );

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

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

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

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

#### Complex128Array.prototype.set( z\[, i] )
Expand Down
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' );
var Complex128 = require( '@stdlib/complex/float64' );
var isInteger = require('@stdlib/assert/is-integer' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var Complex128Array = 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 Complex128( i, i ) );
}
arr = new Complex128Array( arr );

v = new Complex128( 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();
});
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' );
var pow = require( '@stdlib/math/base/special/pow' );
var Complex128 = require( '@stdlib/complex/float64' );
var isInteger = require('@stdlib/assert/is-integer' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var Complex128Array = 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 Complex128( i, -i ) );
}
arr = new Complex128Array( arr );

return benchmark;

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

v = new Complex128( 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();
29 changes: 29 additions & 0 deletions lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,35 @@ declare class Complex128Array implements Complex128ArrayInterface {
*/
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 Complex128 = require( '@stdlib/complex/float64' );
*
* var arr = new Complex128Array( 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 Complex128( 3.0, -3.0 ) );
* // returns 4
*
* idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ), 3 );
* // returns 2
*
* idx = arr.lastIndexOf( new Complex128( 2.0, -2.0 ), -3 );
* // returns 1
*/
lastIndexOf( searchElement: ComplexLike, fromIndex?: number ): number;

/**
* Sets an array element.
*
Expand Down
72 changes: 72 additions & 0 deletions lib/node_modules/@stdlib/array/complex128/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@
if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
// FIXME: prefer a functional `copyWithin` implementation which addresses lack of universal browser support (e.g., IE11 and Safari) or ensure that typed arrays are polyfilled

Check warning on line 750 in lib/node_modules/@stdlib/array/complex128/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: prefer a functional `copyWithin`...'
if ( arguments.length === 2 ) {
this._buffer.copyWithin( target*2, start*2 );
} else {
Expand Down Expand Up @@ -1329,6 +1329,78 @@
return -1;
});

/**
* Returns the last index at which a given element can be found.
*
* @name lastIndexOf
* @memberof Complex128Array.prototype
* @type {Function}
* @param {Complex128} 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 integer
* @returns {integer} index or -1
*
* @example
* var Complex128 = require( '@stdlib/complex/float64' );
*
* var arr = new Complex128Array( 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 Complex128( 3.0, -3.0 ) );
* // returns 4
*
* idx = arr.lastIndexOf( new Complex128( 3.0, -3.0 ), 3 );
* // returns 2
*
* idx = arr.lastIndexOf( new Complex128( 5.0, -5.0 ), 3 );
* // returns -1
*
* idx = arr.lastIndexOf( new Complex128( 2.0, -2.0 ), -3 );
* // returns 1
*/
setReadOnly( Complex128Array.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 ( !isInteger( fromIndex ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
}
if ( fromIndex >= this._length ) {
fromIndex = this._length - 1;
} else if ( fromIndex < 0 ) {
fromIndex += this._length;
}
} else {
fromIndex = this._length - 1;
}
re = real( searchElement );
im = imag( 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;
});

/**
* Sets an array element.
*
Expand Down
Loading
Loading