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 27, 2023
1 parent d421bc0 commit 5cfcc4c
Show file tree
Hide file tree
Showing 8 changed files with 555 additions and 4 deletions.
44 changes: 44 additions & 0 deletions complex128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,50 @@ var z = arr.get( 100 );
// returns undefined
```

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

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

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

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

idx = arr.indexOf( new Complex128( 4.0, -4.0 ), -3 );
// 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.indexOf( new Complex128( 3.0, -3.0 ) );
// returns -1

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

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

#### Complex128Array.prototype.set( z\[, i] )
Expand Down
59 changes: 59 additions & 0 deletions complex128/benchmark/benchmark.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' );
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+':indexOf', 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.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();
});
107 changes: 107 additions & 0 deletions complex128/benchmark/benchmark.index_of.length.js
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 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-1; i++ ) {
arr.push( new Complex128( i, -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( 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();
27 changes: 27 additions & 0 deletions complex128/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,33 @@ declare class Complex128Array implements Complex128ArrayInterface {
*/
get( i: number ): Complex128 | 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 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( [ 2.0, 2.0 ], 3 );
* arr.set( [ 5.0, 5.0 ], 4 );
*
* var idx = arr.indexOf( new Complex128( [ 2.0, 2.0 ] ) );
* // returns 1
*
* idx = arr.indexOf( new Complex128( [ 2.0, 2.0 ] ), 2 );
* // returns 3
*
* idx = arr.indexOf( new Complex128( [ 2.0, 2.0 ] ), -3 );
* // returns 3
*/
indexOf( searchElement: ComplexLike, fromIndex?: number ): number;

/**
* Sets an array element.
*
Expand Down
70 changes: 70 additions & 0 deletions complex128/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,76 @@ setReadOnlyAccessor( Complex128Array.prototype, 'length', function get() {
return this._length;
});

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

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

0 comments on commit 5cfcc4c

Please sign in to comment.