Skip to content

Commit 1464d5b

Browse files
Jaysukh-409kgryte
andauthored
feat: add includes method to array/complex64
PR-URL: #1205 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent c896236 commit 1464d5b

File tree

6 files changed

+533
-0
lines changed

6 files changed

+533
-0
lines changed

lib/node_modules/@stdlib/array/complex64/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,34 @@ var z = arr.get( 100 );
11571157
// returns undefined
11581158
```
11591159

1160+
<a name="method-includes"></a>
1161+
1162+
#### Complex64Array.prototype.includes( searchElement\[, fromIndex] )
1163+
1164+
Returns a boolean indicating whether an array includes a provided value.
1165+
1166+
```javascript
1167+
var Complex64 = require( '@stdlib/complex/float32' );
1168+
1169+
var arr = new Complex64Array( 5 );
1170+
1171+
arr.set( [ 1.0, -1.0 ], 0 );
1172+
arr.set( [ 2.0, -2.0 ], 1 );
1173+
arr.set( [ 3.0, -3.0 ], 2 );
1174+
arr.set( [ 4.0, -4.0 ], 3 );
1175+
arr.set( [ 5.0, -5.0 ], 4 );
1176+
1177+
var bool = arr.includes( new Complex64( 3.0, -3.0 ) );
1178+
// returns true
1179+
1180+
bool = arr.includes( new Complex64( 3.0, -3.0 ), 3 );
1181+
// returns false
1182+
1183+
bool = arr.includes( new Complex64( 4.0, -4.0 ), -3 );
1184+
// returns true
1185+
```
1186+
1187+
11601188
<a name="method-indexOf"></a>
11611189

11621190
#### Complex64Array.prototype.indexOf( searchElement\[, fromIndex] )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var Complex64 = require( '@stdlib/complex/float32' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var Complex64Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':includes', function benchmark( b ) {
33+
var bool;
34+
var arr;
35+
var v;
36+
var i;
37+
38+
arr = [];
39+
for ( i = 0; i < 10; i++ ) {
40+
arr.push( new Complex64( i, i ) );
41+
}
42+
arr = new Complex64Array( arr );
43+
44+
v = new Complex64( 10.0, 10.0 );
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
bool = arr.includes( v, 0 );
49+
if ( typeof bool !== 'boolean' ) {
50+
b.fail( 'should return a boolean' );
51+
}
52+
}
53+
b.toc();
54+
if ( !isBoolean( bool ) ) {
55+
b.fail( 'should return a boolean' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var Complex64 = require( '@stdlib/complex/float32' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var Complex64Array = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var arr;
42+
var i;
43+
44+
arr = [];
45+
for ( i = 0; i < len-1; i++ ) {
46+
arr.push( new Complex64( i, -i ) );
47+
}
48+
arr.push( new Complex64( i, i ) );
49+
arr = new Complex64Array( arr );
50+
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var bool;
61+
var v;
62+
var i;
63+
64+
v = new Complex64( len-1, len-1 );
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
bool = arr.includes( v );
69+
if ( typeof bool !== 'boolean' ) {
70+
b.fail( 'should return a boolean' );
71+
}
72+
}
73+
b.toc();
74+
if ( !isBoolean( bool ) ) {
75+
b.fail( 'should return a boolean' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':includes:len='+len, f );
104+
}
105+
}
106+
107+
main();

lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts

+27
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,33 @@ declare class Complex64Array implements Complex64ArrayInterface {
483483
*/
484484
get( i: number ): Complex64 | void;
485485

486+
/**
487+
* Returns a boolean indicating whether an array contains a provided value.
488+
*
489+
* @param searchElement - search element
490+
* @param fromIndex - starting index (inclusive)
491+
* @returns boolean indicating whether an array contains a provided value
492+
*
493+
* @example
494+
* var arr = new Complex64Array( 5 );
495+
*
496+
* arr.set( [ 1.0, -1.0 ], 0 );
497+
* arr.set( [ 2.0, -2.0 ], 1 );
498+
* arr.set( [ 3.0, -3.0 ], 2 );
499+
* arr.set( [ 4.0, -4.0 ], 3 );
500+
* arr.set( [ 5.0, -5.0 ], 4 );
501+
*
502+
* var bool = arr.includes( new Complex64( 3.0, -3.0 ) );
503+
* // returns true
504+
*
505+
* bool = arr.includes( new Complex64( 3.0, -3.0 ), 3 );
506+
* // returns false
507+
*
508+
* bool = arr.includes( new Complex64( 4.0, -4.0 ), -3 );
509+
* // returns true
510+
*/
511+
includes( searchElement: ComplexLike, fromIndex?: number ): boolean;
512+
486513
/**
487514
* Returns the first index at which a given element can be found.
488515
*

lib/node_modules/@stdlib/array/complex64/lib/main.js

+70
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,76 @@ setReadOnly( Complex64Array.prototype, 'get', function get( idx ) {
11811181
return getComplex64( this._buffer, idx );
11821182
});
11831183

1184+
/**
1185+
* Returns a boolean indicating whether an array includes a provided value.
1186+
*
1187+
* @name includes
1188+
* @memberof Complex64Array.prototype
1189+
* @type {Function}
1190+
* @param {Complex64} searchElement - search element
1191+
* @param {integer} [fromIndex=0] - starting index (inclusive)
1192+
* @throws {TypeError} `this` must be a complex number array
1193+
* @throws {TypeError} first argument must be a complex number
1194+
* @throws {TypeError} second argument must be an integer
1195+
* @returns {boolean} boolean indicating whether an array includes a provided value
1196+
*
1197+
* @example
1198+
* var Complex64 = require( '@stdlib/complex/float32' );
1199+
*
1200+
* var arr = new Complex64Array( 5 );
1201+
*
1202+
* arr.set( [ 1.0, -1.0 ], 0 );
1203+
* arr.set( [ 2.0, -2.0 ], 1 );
1204+
* arr.set( [ 3.0, -3.0 ], 2 );
1205+
* arr.set( [ 4.0, -4.0 ], 3 );
1206+
* arr.set( [ 5.0, -5.0 ], 4 );
1207+
*
1208+
* var bool = arr.includes( new Complex64( 3.0, -3.0 ) );
1209+
* // returns true
1210+
*
1211+
* bool = arr.includes( new Complex64( 3.0, -3.0 ), 3 );
1212+
* // returns false
1213+
*
1214+
* bool = arr.includes( new Complex64( 4.0, -4.0 ), -3 );
1215+
* // returns true
1216+
*/
1217+
setReadOnly( Complex64Array.prototype, 'includes', function includes( searchElement, fromIndex ) {
1218+
var buf;
1219+
var idx;
1220+
var re;
1221+
var im;
1222+
var i;
1223+
if ( !isComplexArray( this ) ) {
1224+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1225+
}
1226+
if ( !isComplexLike( searchElement ) ) {
1227+
throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) );
1228+
}
1229+
if ( arguments.length > 1 ) {
1230+
if ( !isInteger( fromIndex ) ) {
1231+
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
1232+
}
1233+
if ( fromIndex < 0 ) {
1234+
fromIndex += this._length;
1235+
if ( fromIndex < 0 ) {
1236+
fromIndex = 0;
1237+
}
1238+
}
1239+
} else {
1240+
fromIndex = 0;
1241+
}
1242+
re = realf( searchElement );
1243+
im = imagf( searchElement );
1244+
buf = this._buffer;
1245+
for ( i = fromIndex; i < this._length; i++ ) {
1246+
idx = 2 * i;
1247+
if ( re === buf[ idx ] && im === buf[ idx+1 ] ) {
1248+
return true;
1249+
}
1250+
}
1251+
return false;
1252+
});
1253+
11841254
/**
11851255
* Returns the first index at which a given element can be found.
11861256
*

0 commit comments

Comments
 (0)