Skip to content

Commit b866471

Browse files
committed
Auto-generated commit
1 parent 56a3ecc commit b866471

File tree

8 files changed

+676
-5
lines changed

8 files changed

+676
-5
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,88 @@ var count = context.count;
21242124
// returns 2
21252125
```
21262126

2127+
<a name="method-sort"></a>
2128+
2129+
#### Complex128Array.prototype.sort( compareFcn )
2130+
2131+
Sorts an array in-place.
2132+
2133+
```javascript
2134+
var real = require( '@stdlib/complex-real' );
2135+
var imag = require( '@stdlib/complex-imag' );
2136+
2137+
function compare( a, b ) {
2138+
var re1;
2139+
var re2;
2140+
var im1;
2141+
var im2;
2142+
re1 = real( a );
2143+
re2 = real( b );
2144+
if ( re1 < re2 ) {
2145+
return -1;
2146+
}
2147+
if ( re1 > re2 ) {
2148+
return 1;
2149+
}
2150+
im1 = imag( a );
2151+
im2 = imag( b );
2152+
if ( im1 < im2 ) {
2153+
return -1;
2154+
}
2155+
if ( im1 > im2 ) {
2156+
return 1;
2157+
}
2158+
return 0;
2159+
}
2160+
2161+
var arr = new Complex128Array( 3 );
2162+
2163+
arr.set( [ 3.0, -3.0 ], 0 );
2164+
arr.set( [ 1.0, -1.0 ], 1 );
2165+
arr.set( [ 2.0, -2.0 ], 2 );
2166+
2167+
var out = arr.sort( compare );
2168+
// returns <Complex128Array>
2169+
2170+
var z = out.get( 0 );
2171+
// returns <Complex128>
2172+
2173+
var re = real( z );
2174+
// returns 1.0
2175+
2176+
var im = imag( z );
2177+
// returns -1.0
2178+
2179+
z = out.get( 1 );
2180+
// returns <Complex128>
2181+
2182+
re = real( z );
2183+
// returns 2.0
2184+
2185+
im = imag( z );
2186+
// returns -2.0
2187+
2188+
z = out.get( 2 );
2189+
// returns <Complex128>
2190+
2191+
re = real( z );
2192+
// returns 3.0
2193+
2194+
im = imag( z );
2195+
// returns -3.0
2196+
```
2197+
2198+
The `compareFcn` determines the order of the elements. The function is called with the following arguments:
2199+
2200+
- **a**: the first element for comparison.
2201+
- **b**: the second element for comparison.
2202+
2203+
The function should return a number where:
2204+
2205+
- a negative value indicates that `a` should come before `b`.
2206+
- a positive value indicates that `a` should come after `b`.
2207+
- zero or `NaN` indicates that `a` and `b` are considered equal.
2208+
21272209
<a name="method-subarray"></a>
21282210

21292211
#### Complex128Array.prototype.subarray( \[begin\[, end]] )

benchmark/benchmark.sort.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 isComplex128Array = require( '@stdlib/assert-is-complex128array' );
25+
var real = require( '@stdlib/complex-real' );
26+
var imag = require( '@stdlib/complex-imag' );
27+
var pkg = require( './../package.json' ).name;
28+
var Complex128Array = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Comparison function.
35+
*
36+
* @private
37+
* @param {Complex128} a - first value for comparison
38+
* @param {Complex128} 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 = real( a );
47+
re2 = real( b );
48+
if ( re1 < re2 ) {
49+
return -1;
50+
}
51+
if ( re1 > re2 ) {
52+
return 1;
53+
}
54+
im1 = imag( a );
55+
im2 = imag( 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+':sort', function benchmark( b ) {
69+
var out;
70+
var arr;
71+
var i;
72+
73+
arr = new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] );
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
out = arr.sort( compareFcn );
78+
if ( typeof out !== 'object' ) {
79+
b.fail( 'should return an object' );
80+
}
81+
}
82+
b.toc();
83+
if ( !isComplex128Array( out ) ) {
84+
b.fail( 'should return a Complex128Array' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
});

benchmark/benchmark.sort.length.js

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 isComplex128Array = require( '@stdlib/assert-is-complex128array' );
25+
var real = require( '@stdlib/complex-real' );
26+
var imag = require( '@stdlib/complex-imag' );
27+
var pow = require( '@stdlib/math-base-special-pow' );
28+
var Complex128 = require( '@stdlib/complex-float64' );
29+
var pkg = require( './../package.json' ).name;
30+
var Complex128Array = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Comparison function.
37+
*
38+
* @private
39+
* @param {Complex128} a - first value for comparison
40+
* @param {Complex128} 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 = real( a );
49+
re2 = real( b );
50+
if ( re1 < re2 ) {
51+
return -1;
52+
}
53+
if ( re1 > re2 ) {
54+
return 1;
55+
}
56+
im1 = imag( a );
57+
im2 = imag( 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 Complex128( i, i ) );
81+
}
82+
arr = new Complex128Array( 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.sort( compareFcn );
99+
if ( typeof out !== 'object' ) {
100+
b.fail( 'should return an object' );
101+
}
102+
}
103+
b.toc();
104+
if ( !isComplex128Array( out ) ) {
105+
b.fail( 'should return a Complex128Array' );
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+':sort:len='+len, f );
134+
}
135+
}
136+
137+
main();

dist/index.js

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

dist/index.js.map

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

0 commit comments

Comments
 (0)