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 23, 2023
1 parent 24b2b39 commit 826eeab
Show file tree
Hide file tree
Showing 8 changed files with 536 additions and 3 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,34 @@ var z = arr.get( 100 );
// returns undefined
```

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

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

Returns a boolean indicating whether an array includes a provided value.

```javascript
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( [ 5.0, -5.0 ], 4 );

var bool = arr.includes( new Complex64( 3.0, -3.0 ) );
// returns true

bool = arr.includes( new Complex64( 3.0, -3.0 ), 3 );
// returns false

bool = arr.includes( new Complex64( 4.0, -4.0 ), -3 );
// returns true
```


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

#### Complex64Array.prototype.indexOf( searchElement\[, fromIndex] )
Expand Down
59 changes: 59 additions & 0 deletions benchmark/benchmark.includes.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 isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var Complex64Array = require( './../lib' );


// MAIN //

bench( pkg+':includes', function benchmark( b ) {
var bool;
var arr;
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++ ) {
bool = arr.includes( v, 0 );
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( bool ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});
107 changes: 107 additions & 0 deletions benchmark/benchmark.includes.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-harness' );
var pow = require( '@stdlib/math-base-special-pow' );
var Complex64 = require( '@stdlib/complex-float32' );
var isBoolean = require( '@stdlib/assert-is-boolean' ).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 bool;
var v;
var i;

v = new Complex64( len-1, len-1 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
bool = arr.includes( v );
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( bool ) ) {
b.fail( 'should return a boolean' );
}
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+':includes: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.

27 changes: 27 additions & 0 deletions docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,33 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
get( i: number ): Complex64 | void;

/**
* Returns a boolean indicating whether an array contains a provided value.
*
* @param searchElement - search element
* @param fromIndex - starting index (inclusive)
* @returns boolean indicating whether an array contains a provided value
*
* @example
* 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( [ 5.0, -5.0 ], 4 );
*
* var bool = arr.includes( new Complex64( 3.0, -3.0 ) );
* // returns true
*
* bool = arr.includes( new Complex64( 3.0, -3.0 ), 3 );
* // returns false
*
* bool = arr.includes( new Complex64( 4.0, -4.0 ), -3 );
* // returns true
*/
includes( searchElement: ComplexLike, fromIndex?: number ): boolean;

/**
* Returns the first index at which a given element can be found.
*
Expand Down
70 changes: 70 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,76 @@ setReadOnly( Complex64Array.prototype, 'get', function get( idx ) {
return getComplex64( this._buffer, idx );
});

/**
* Returns a boolean indicating whether an array includes a provided value.
*
* @name includes
* @memberof Complex64Array.prototype
* @type {Function}
* @param {Complex64} searchElement - search element
* @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 {boolean} boolean indicating whether an array includes a provided value
*
* @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( [ 5.0, -5.0 ], 4 );
*
* var bool = arr.includes( new Complex64( 3.0, -3.0 ) );
* // returns true
*
* bool = arr.includes( new Complex64( 3.0, -3.0 ), 3 );
* // returns false
*
* bool = arr.includes( new Complex64( 4.0, -4.0 ), -3 );
* // returns true
*/
setReadOnly( Complex64Array.prototype, 'includes', function includes( 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 = 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 true;
}
}
return false;
});

/**
* Returns the first index at which a given element can be found.
*
Expand Down
Loading

0 comments on commit 826eeab

Please sign in to comment.