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 indexOf method to array/complex64 #1191

Merged
merged 14 commits into from
Dec 19, 2023
Merged
41 changes: 41 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,47 @@ var z = arr.get( 100 );
// returns undefined
```

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

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

Returns the first 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.indexOf( new Complex64( 3.0, -3.0 ) );
// returns 2

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

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.indexOf( new Complex64( 3.0, -3.0 ) );
// returns -1

idx = arr.indexOf( new Complex64( 1.0, -1.0 ), 1 );
// returns -1
```

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

#### Complex64Array.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 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+':indexOf', 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.indexOf( v, 0 );
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,107 @@
/**
* @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 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-1; i++ ) {
arr.push( new Complex64( i, -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( len-1, len-1 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = arr.indexOf( 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+':indexOf:len='+len, f );
}
}

main();
23 changes: 23 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,29 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
get( i: number ): Complex64 | void;

/**
* Returns the first index at which a given element can be found.
*
* @param searchElement - element to find
* @param fromIndex - starting index (inclusive)
* @returns index or -1
*
* @example
* 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( [ 2.0, 2.0 ], 3 );
*
* var idx = arr.indexOf( new Complex64( [ 2.0, 2.0 ] ) );
* // returns 1
*
* idx = arr.indexOf( new Complex64( [ 2.0, 2.0 ] ), 2 );
* // returns 3
*/
indexOf( searchElement: ComplexLike, fromIndex?: number ): number;

/**
* Sets an array element.
*
Expand Down
61 changes: 61 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
* @param {*} value - value to test
* @returns {boolean} boolean indicating if a value is a `Complex64Array`
*/
function isComplex64Array( value ) { // TODO: move to array/base/assert/is-complex64-array

Check warning on line 109 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: move to...'
return (
typeof value === 'object' &&
value !== null &&
Expand All @@ -122,7 +122,7 @@
* @param {*} value - value to test
* @returns {boolean} boolean indicating if a value is a `Complex128Array`
*/
function isComplex128Array( value ) { // TODO: move to array/base/assert/is-complex128-array

Check warning on line 125 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: move to...'
return (
typeof value === 'object' &&
value !== null &&
Expand Down Expand Up @@ -281,7 +281,7 @@
}
buf = buf[ ITERATOR_SYMBOL ]();
if ( !isFunction( buf.next ) ) {
throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); // FIXME: `buf` is what is returned from above, NOT the original value

Check warning on line 284 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: `buf` is what is returned from...'
}
buf = fromIterator( buf );
if ( buf instanceof Error ) {
Expand Down Expand Up @@ -746,7 +746,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 749 in lib/node_modules/@stdlib/array/complex64/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 @@ -971,6 +971,67 @@
return getComplex64( this._buffer, idx );
});

/**
* Returns the first index at which a given element can be found.
*
* @name indexOf
* @memberof Complex64Array.prototype
* @type {Function}
* @param {Complex64} searchElement - element to find
* @param {integer} [fromIndex=0] - starting index (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( 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( [ 5.0, -5.0 ], 4 );
*
* var idx = arr.indexOf( new Complex64( 3.0, -3.0 ) );
* // returns 2
*
* idx = arr.indexOf( new Complex64( 3.0, -3.0 ), 3 );
* // returns -1
*/
setReadOnly( Complex64Array.prototype, 'indexOf', function indexOf( 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 ) );
}
} else {
fromIndex = 0;
}
re = realf( searchElement );
im = imagf( searchElement );
buf = this._buffer;
for ( i = fromIndex; i < this._length; i++ ) {
idx = 2 * i;
if ( re === buf[ idx ] && im === buf[ idx+1 ] ) {
return i;
}
}
return -1;
});

/**
* Number of array elements.
*
Expand Down Expand Up @@ -1147,7 +1208,7 @@
// We need to copy source values...
tmp = new Float32Array( N );
for ( i = 0; i < N; i++ ) {
tmp[ i ] = sbuf[ i ]; // TODO: handle accessor arrays

Check warning on line 1211 in lib/node_modules/@stdlib/array/complex64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: handle accessor arrays'
}
sbuf = tmp;
}
Expand Down
Loading
Loading