Skip to content

Commit c2f6104

Browse files
committed
Auto-generated commit
1 parent fbc0e58 commit c2f6104

File tree

8 files changed

+658
-3
lines changed

8 files changed

+658
-3
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,6 +2375,88 @@ im = imagf( z );
23752375
// returns 1.0
23762376
```
23772377

2378+
<a name="method-to-sorted"></a>
2379+
2380+
#### Complex64Array.prototype.toSorted( compareFcn )
2381+
2382+
Returns a new typed array containing the elements in sorted order.
2383+
2384+
```javascript
2385+
var realf = require( '@stdlib/complex-realf' );
2386+
var imagf = require( '@stdlib/complex-imagf' );
2387+
2388+
function compare( a, b ) {
2389+
var re1;
2390+
var re2;
2391+
var im1;
2392+
var im2;
2393+
re1 = realf( a );
2394+
re2 = realf( b );
2395+
if ( re1 < re2 ) {
2396+
return -1;
2397+
}
2398+
if ( re1 > re2 ) {
2399+
return 1;
2400+
}
2401+
im1 = imagf( a );
2402+
im2 = imagf( b );
2403+
if ( im1 < im2 ) {
2404+
return -1;
2405+
}
2406+
if ( im1 > im2 ) {
2407+
return 1;
2408+
}
2409+
return 0;
2410+
}
2411+
2412+
var arr = new Complex64Array( 3 );
2413+
2414+
arr.set( [ 3.0, -3.0 ], 0 );
2415+
arr.set( [ 1.0, -1.0 ], 1 );
2416+
arr.set( [ 2.0, -2.0 ], 2 );
2417+
2418+
var out = arr.toSorted( compare );
2419+
// returns <Complex64Array>
2420+
2421+
var z = out.get( 0 );
2422+
// returns <Complex64>
2423+
2424+
var re = realf( z );
2425+
// returns 1.0
2426+
2427+
var im = imagf( z );
2428+
// returns -1.0
2429+
2430+
z = out.get( 1 );
2431+
// returns <Complex64>
2432+
2433+
re = realf( z );
2434+
// returns 2.0
2435+
2436+
im = imagf( z );
2437+
// returns -2.0
2438+
2439+
z = out.get( 2 );
2440+
// returns <Complex64>
2441+
2442+
re = realf( z );
2443+
// returns 3.0
2444+
2445+
im = imagf( z );
2446+
// returns -3.0
2447+
```
2448+
2449+
The `compareFcn` determines the order of the elements. The function is called with the following arguments:
2450+
2451+
- **a**: the first element for comparison.
2452+
- **b**: the second element for comparison.
2453+
2454+
The function should return a number where:
2455+
2456+
- a negative value indicates that `a` should come before `b`.
2457+
- a positive value indicates that `a` should come after `b`.
2458+
- zero or `NaN` indicates that `a` and `b` are considered equal.
2459+
23782460
<a name="method-to-string"></a>
23792461

23802462
#### Complex64Array.prototype.toString()

benchmark/benchmark.to_sorted.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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-harness' );
24+
var isComplex64Array = require( '@stdlib/assert-is-complex64array' );
25+
var realf = require( '@stdlib/complex-realf' );
26+
var imagf = require( '@stdlib/complex-imagf' );
27+
var pkg = require( './../package.json' ).name;
28+
var Complex64Array = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Comparison function.
35+
*
36+
* @private
37+
* @param {Complex64} a - first value for comparison
38+
* @param {Complex64} b - second value for comparison
39+
* @returns {number} comparison result
40+
*/
41+
function compareFcn( a, b ) {
42+
var re1;
43+
var re2;
44+
var im1;
45+
var im2;
46+
re1 = realf( a );
47+
re2 = realf( b );
48+
if ( re1 < re2 ) {
49+
return -1;
50+
}
51+
if ( re1 > re2 ) {
52+
return 1;
53+
}
54+
im1 = imagf( a );
55+
im2 = imagf( b );
56+
if ( im1 < im2 ) {
57+
return -1;
58+
}
59+
if ( im1 > im2 ) {
60+
return 1;
61+
}
62+
return 0;
63+
}
64+
65+
66+
// MAIN //
67+
68+
bench( pkg+':toSorted', function benchmark( b ) {
69+
var out;
70+
var arr;
71+
var i;
72+
73+
arr = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
out = arr.toSorted( compareFcn );
78+
if ( typeof out !== 'object' ) {
79+
b.fail( 'should return an object' );
80+
}
81+
}
82+
b.toc();
83+
if ( !isComplex64Array( out ) ) {
84+
b.fail( 'should return a Complex64Array' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
});
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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-harness' );
24+
var isComplex64Array = require( '@stdlib/assert-is-complex64array' );
25+
var realf = require( '@stdlib/complex-realf' );
26+
var imagf = require( '@stdlib/complex-imagf' );
27+
var pow = require( '@stdlib/math-base-special-pow' );
28+
var Complex64 = require( '@stdlib/complex-float32' );
29+
var pkg = require( './../package.json' ).name;
30+
var Complex64Array = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Comparison function.
37+
*
38+
* @private
39+
* @param {Complex64} a - first value for comparison
40+
* @param {Complex64} b - second value for comparison
41+
* @returns {number} comparison result
42+
*/
43+
function compareFcn( a, b ) {
44+
var re1;
45+
var re2;
46+
var im1;
47+
var im2;
48+
re1 = realf( a );
49+
re2 = realf( b );
50+
if ( re1 < re2 ) {
51+
return -1;
52+
}
53+
if ( re1 > re2 ) {
54+
return 1;
55+
}
56+
im1 = imagf( a );
57+
im2 = imagf( b );
58+
if ( im1 < im2 ) {
59+
return -1;
60+
}
61+
if ( im1 > im2 ) {
62+
return 1;
63+
}
64+
return 0;
65+
}
66+
67+
/**
68+
* Creates a benchmark function.
69+
*
70+
* @private
71+
* @param {PositiveInteger} len - array length
72+
* @returns {Function} benchmark function
73+
*/
74+
function createBenchmark( len ) {
75+
var arr;
76+
var i;
77+
78+
arr = [];
79+
for ( i = 0; i < len; i++ ) {
80+
arr.push( new Complex64( i, i ) );
81+
}
82+
arr = new Complex64Array( arr );
83+
84+
return benchmark;
85+
86+
/**
87+
* Benchmark function.
88+
*
89+
* @private
90+
* @param {Benchmark} b - benchmark instance
91+
*/
92+
function benchmark( b ) {
93+
var out;
94+
var i;
95+
96+
b.tic();
97+
for ( i = 0; i < b.iterations; i++ ) {
98+
out = arr.toSorted( compareFcn );
99+
if ( typeof out !== 'object' ) {
100+
b.fail( 'should return an object' );
101+
}
102+
}
103+
b.toc();
104+
if ( !isComplex64Array( out ) ) {
105+
b.fail( 'should return a Complex64Array' );
106+
}
107+
b.pass( 'benchmark finished' );
108+
b.end();
109+
}
110+
}
111+
112+
113+
// MAIN //
114+
115+
/**
116+
* Main execution sequence.
117+
*
118+
* @private
119+
*/
120+
function main() {
121+
var len;
122+
var min;
123+
var max;
124+
var f;
125+
var i;
126+
127+
min = 1; // 10^min
128+
max = 6; // 10^max
129+
130+
for ( i = min; i <= max; i++ ) {
131+
len = pow( 10, i );
132+
f = createBenchmark( len );
133+
bench( pkg+':toSorted:len='+len, f );
134+
}
135+
}
136+
137+
main();

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/types/index.d.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,78 @@ declare class Complex64Array implements Complex64ArrayInterface {
13771377
*/
13781378
toReversed(): Complex64Array;
13791379

1380+
/**
1381+
* Returns a new typed array containing the elements in sorted order.
1382+
*
1383+
* @param compareFcn - comparison function
1384+
* @returns sorted array
1385+
*
1386+
* @example
1387+
* var realf = require( '@stdlib/complex-realf' );
1388+
* var imagf = require( '@stdlib/complex-imagf' );
1389+
*
1390+
* function compare( a, b ) {
1391+
* var re1;
1392+
* var re2;
1393+
* var im1;
1394+
* var im2;
1395+
* re1 = realf( a );
1396+
* re2 = realf( b );
1397+
* if ( re1 < re2 ) {
1398+
* return -1;
1399+
* }
1400+
* if ( re1 > re2 ) {
1401+
* return 1;
1402+
* }
1403+
* im1 = imagf( a );
1404+
* im2 = imagf( b );
1405+
* if ( im1 < im2 ) {
1406+
* return -1;
1407+
* }
1408+
* if ( im1 > im2 ) {
1409+
* return 1;
1410+
* }
1411+
* return 0;
1412+
* }
1413+
*
1414+
* var arr = new Complex64Array( 3 );
1415+
*
1416+
* arr.set( [ 3.0, -3.0 ], 0 );
1417+
* arr.set( [ 1.0, -1.0 ], 1 );
1418+
* arr.set( [ 2.0, -2.0 ], 2 );
1419+
*
1420+
* var out = arr.toSorted( compare );
1421+
* // returns <Complex64Array>
1422+
*
1423+
* var z = out.get( 0 );
1424+
* // returns <Complex64>
1425+
*
1426+
* var re = realf( z );
1427+
* // returns 1.0
1428+
*
1429+
* var im = imagf( z );
1430+
* // returns -1.0
1431+
*
1432+
* z = out.get( 1 );
1433+
* // returns <Complex64>
1434+
*
1435+
* re = realf( z );
1436+
* // returns 2.0
1437+
*
1438+
* im = imagf( z );
1439+
* // returns -2.0
1440+
*
1441+
* z = out.get( 2 );
1442+
* // returns <Complex64>
1443+
*
1444+
* re = realf( z );
1445+
* // returns 3.0
1446+
*
1447+
* im = imagf( z );
1448+
* // returns -3.0
1449+
*/
1450+
toSorted( compareFcn: CompareFcn ): Complex64Array;
1451+
13801452
/**
13811453
* Serializes an array as a string.
13821454
*

0 commit comments

Comments
 (0)