Skip to content

Commit 4b8bdc5

Browse files
committed
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into develop
2 parents dc5bb62 + a170198 commit 4b8bdc5

File tree

6 files changed

+551
-0
lines changed

6 files changed

+551
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,50 @@ var z = arr.get( 100 );
12301230
// returns undefined
12311231
```
12321232

1233+
<a name="method-index-of"></a>
1234+
1235+
#### Complex128Array.prototype.indexOf( searchElement\[, fromIndex] )
1236+
1237+
Returns the first index at which a given element can be found.
1238+
1239+
```javascript
1240+
var Complex128 = require( '@stdlib/complex/float64' );
1241+
1242+
var arr = new Complex128Array( 5 );
1243+
1244+
arr.set( [ 1.0, -1.0 ], 0 );
1245+
arr.set( [ 2.0, -2.0 ], 1 );
1246+
arr.set( [ 3.0, -3.0 ], 2 );
1247+
arr.set( [ 4.0, -4.0 ], 3 );
1248+
arr.set( [ 2.0, -2.0 ], 4 );
1249+
1250+
var idx = arr.indexOf( new Complex128( 3.0, -3.0 ) );
1251+
// returns 2
1252+
1253+
idx = arr.indexOf( new Complex128( 2.0, -2.0 ), 2 );
1254+
// returns 4
1255+
1256+
idx = arr.indexOf( new Complex128( 4.0, -4.0 ), -3 );
1257+
// returns 3
1258+
```
1259+
1260+
If `searchElement` is not present in the array, the method returns `-1`.
1261+
1262+
```javascript
1263+
var Complex128 = require( '@stdlib/complex/float64' );
1264+
1265+
var arr = new Complex128Array( 5 );
1266+
1267+
arr.set( [ 1.0, -1.0 ], 0 );
1268+
arr.set( [ 2.0, -2.0 ], 1 );
1269+
1270+
var idx = arr.indexOf( new Complex128( 3.0, -3.0 ) );
1271+
// returns -1
1272+
1273+
idx = arr.indexOf( new Complex128( 1.0, -1.0 ), 1 );
1274+
// returns -1
1275+
```
1276+
12331277
<a name="method-set"></a>
12341278

12351279
#### Complex128Array.prototype.set( z\[, i] )
Lines changed: 59 additions & 0 deletions
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 Complex128 = require( '@stdlib/complex/float64' );
25+
var isInteger = require('@stdlib/assert/is-integer' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var Complex128Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':indexOf', function benchmark( b ) {
33+
var arr;
34+
var idx;
35+
var v;
36+
var i;
37+
38+
arr = [];
39+
for ( i = 0; i < 10; i++ ) {
40+
arr.push( new Complex128( i, i ) );
41+
}
42+
arr = new Complex128Array( arr );
43+
44+
v = new Complex128( 10.0, 10.0 );
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
idx = arr.indexOf( v, 0 );
49+
if ( typeof idx !== 'number' ) {
50+
b.fail( 'should return an integer' );
51+
}
52+
}
53+
b.toc();
54+
if ( !isInteger( idx ) ) {
55+
b.fail( 'should return an integer' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
Lines changed: 107 additions & 0 deletions
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 Complex128 = require( '@stdlib/complex/float64' );
26+
var isInteger = require('@stdlib/assert/is-integer' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var Complex128Array = 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 Complex128( i, -i ) );
47+
}
48+
arr.push( new Complex128( i, i ) );
49+
arr = new Complex128Array( 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 idx;
61+
var v;
62+
var i;
63+
64+
v = new Complex128( len-1, len-1 );
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
idx = arr.indexOf( v );
69+
if ( typeof idx !== 'number' ) {
70+
b.fail( 'should return an integer' );
71+
}
72+
}
73+
b.toc();
74+
if ( !isInteger( idx ) ) {
75+
b.fail( 'should return an integer' );
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+':indexOf:len='+len, f );
104+
}
105+
}
106+
107+
main();

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,33 @@ declare class Complex128Array implements Complex128ArrayInterface {
517517
*/
518518
get( i: number ): Complex128 | void;
519519

520+
/**
521+
* Returns the first index at which a given element can be found.
522+
*
523+
* @param searchElement - element to find
524+
* @param fromIndex - starting index (inclusive)
525+
* @returns index or -1
526+
*
527+
* @example
528+
* var arr = new Complex128Array( 5 );
529+
*
530+
* arr.set( [ 1.0, 1.0 ], 0 );
531+
* arr.set( [ 2.0, 2.0 ], 1 );
532+
* arr.set( [ 3.0, 3.0 ], 2 );
533+
* arr.set( [ 2.0, 2.0 ], 3 );
534+
* arr.set( [ 5.0, 5.0 ], 4 );
535+
*
536+
* var idx = arr.indexOf( new Complex128( [ 2.0, 2.0 ] ) );
537+
* // returns 1
538+
*
539+
* idx = arr.indexOf( new Complex128( [ 2.0, 2.0 ] ), 2 );
540+
* // returns 3
541+
*
542+
* idx = arr.indexOf( new Complex128( [ 2.0, 2.0 ] ), -3 );
543+
* // returns 3
544+
*/
545+
indexOf( searchElement: ComplexLike, fromIndex?: number ): number;
546+
520547
/**
521548
* Sets an array element.
522549
*

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,76 @@ setReadOnlyAccessor( Complex128Array.prototype, 'length', function get() {
12591259
return this._length;
12601260
});
12611261

1262+
/**
1263+
* Returns the first index at which a given element can be found.
1264+
*
1265+
* @name indexOf
1266+
* @memberof Complex128Array.prototype
1267+
* @type {Function}
1268+
* @param {Complex128} searchElement - element to find
1269+
* @param {integer} [fromIndex=0] - starting index (inclusive)
1270+
* @throws {TypeError} `this` must be a complex number array
1271+
* @throws {TypeError} first argument must be a complex number
1272+
* @throws {TypeError} second argument must be an integer
1273+
* @returns {integer} index or -1
1274+
*
1275+
* @example
1276+
* var Complex128 = require( '@stdlib/complex/float64' );
1277+
*
1278+
* var arr = new Complex128Array( 5 );
1279+
*
1280+
* arr.set( [ 1.0, -1.0 ], 0 );
1281+
* arr.set( [ 2.0, -2.0 ], 1 );
1282+
* arr.set( [ 3.0, -3.0 ], 2 );
1283+
* arr.set( [ 4.0, -4.0 ], 3 );
1284+
* arr.set( [ 5.0, -5.0 ], 4 );
1285+
*
1286+
* var idx = arr.indexOf( new Complex128( 3.0, -3.0 ) );
1287+
* // returns 2
1288+
*
1289+
* idx = arr.indexOf( new Complex128( 3.0, -3.0 ), 3 );
1290+
* // returns -1
1291+
*
1292+
* idx = arr.indexOf( new Complex128( 4.0, -4.0 ), -3 );
1293+
* // returns 3
1294+
*/
1295+
setReadOnly( Complex128Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) {
1296+
var buf;
1297+
var idx;
1298+
var re;
1299+
var im;
1300+
var i;
1301+
if ( !isComplexArray( this ) ) {
1302+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1303+
}
1304+
if ( !isComplexLike( searchElement ) ) {
1305+
throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) );
1306+
}
1307+
if ( arguments.length > 1 ) {
1308+
if ( !isInteger( fromIndex ) ) {
1309+
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
1310+
}
1311+
if ( fromIndex < 0 ) {
1312+
fromIndex += this._length;
1313+
if ( fromIndex < 0 ) {
1314+
fromIndex = 0;
1315+
}
1316+
}
1317+
} else {
1318+
fromIndex = 0;
1319+
}
1320+
re = real( searchElement );
1321+
im = imag( searchElement );
1322+
buf = this._buffer;
1323+
for ( i = fromIndex; i < this._length; i++ ) {
1324+
idx = 2 * i;
1325+
if ( re === buf[ idx ] && im === buf[ idx+1 ] ) {
1326+
return i;
1327+
}
1328+
}
1329+
return -1;
1330+
});
1331+
12621332
/**
12631333
* Sets an array element.
12641334
*

0 commit comments

Comments
 (0)