Skip to content

Commit b79d687

Browse files
feat: add logic, tests, and benchmarks to toSorted method
1 parent 4231541 commit b79d687

File tree

4 files changed

+453
-0
lines changed

4 files changed

+453
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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' );
24+
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
25+
var pkg = require( './../package.json' ).name;
26+
var factory = require( './../lib' );
27+
28+
29+
// VARIABLES //
30+
31+
var Float64ArrayFE = factory( 'float64' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg+':toSorted', function benchmark( b ) {
37+
var out;
38+
var arr;
39+
var i;
40+
41+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 5.0, 3.0, 2.0, 3.0 ] );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = arr.toSorted( compareFcn );
46+
if ( typeof out !== 'object' ) {
47+
b.fail( 'should return an object' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isTypedArray( out ) ) {
52+
b.fail( 'should return a typed array' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
57+
function compareFcn( x, y ) {
58+
return x - y;
59+
}
60+
});
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/zero-to' );
26+
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var factory = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var Float64ArrayFE = factory( 'float64' );
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Comparison function.
40+
*
41+
* @private
42+
* @param {number} a - first value for comparison
43+
* @param {number} b - second value for comparison
44+
* @returns {number} comparison result
45+
*/
46+
function compareFcn( a, b ) {
47+
if ( a > b ) {
48+
return 1;
49+
}
50+
if ( a < b) {
51+
return -1;
52+
}
53+
return 0;
54+
}
55+
56+
/**
57+
* Creates a benchmark function.
58+
*
59+
* @private
60+
* @param {PositiveInteger} len - array length
61+
* @returns {Function} benchmark function
62+
*/
63+
function createBenchmark( len ) {
64+
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
65+
return benchmark;
66+
67+
/**
68+
* Benchmark function.
69+
*
70+
* @private
71+
* @param {Benchmark} b - benchmark instance
72+
*/
73+
function benchmark( b ) {
74+
var out;
75+
var i;
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
out = arr.toSorted( compareFcn );
80+
if ( typeof out !== 'object' ) {
81+
b.fail( 'should return an object' );
82+
}
83+
}
84+
b.toc();
85+
if ( !isTypedArray( out ) ) {
86+
b.fail( 'should return a typed array' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
}
91+
}
92+
93+
94+
// MAIN //
95+
96+
/**
97+
* Main execution sequence.
98+
*
99+
* @private
100+
*/
101+
function main() {
102+
var len;
103+
var min;
104+
var max;
105+
var f;
106+
var i;
107+
108+
min = 1; // 10^min
109+
max = 6; // 10^max
110+
111+
for ( i = min; i <= max; i++ ) {
112+
len = pow( 10, i );
113+
f = createBenchmark( len );
114+
bench( pkg+':toSorted:len='+len, f );
115+
}
116+
}
117+
118+
main();

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,70 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
634634
return this._buffer[ GETTER ]( idx*BYTES_PER_ELEMENT, this._isLE );
635635
});
636636

637+
/**
638+
* Returns a new typed array containing the elements in sorted order.
639+
*
640+
* @private
641+
* @name toSorted
642+
* @memberof TypedArray.prototype
643+
* @type {Function}
644+
* @param {Function} compareFcn - comparison function
645+
* @throws {TypeError} `this` must be a typed array
646+
* @throws {TypeError} first argument must be a function
647+
* @returns {TypedArray} sorted array
648+
*/
649+
setReadOnly( TypedArray.prototype, 'toSorted', function sort( compareFcn ) {
650+
var minIdx;
651+
var minVal;
652+
var obuf;
653+
var buf;
654+
var len;
655+
var out;
656+
var val;
657+
var i;
658+
var j;
659+
660+
if ( !isTypedArray( this ) ) {
661+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
662+
}
663+
if ( arguments.length === 0 ) {
664+
compareFcn = function defaultCompareFcn( a, b ) {
665+
if ( a > b ) {
666+
return 1;
667+
}
668+
if ( a < b ) {
669+
return -1;
670+
}
671+
return 0;
672+
};
673+
} else if ( !isFunction( compareFcn ) ) {
674+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) );
675+
}
676+
len = this._length;
677+
out = new this.constructor( flag2byteOrder( this._isLE ), len );
678+
buf = this._buffer;
679+
obuf = out._buffer; // eslint-disable-line no-underscore-dangle
680+
for ( i = 0; i < len; i++ ) {
681+
obuf[ SETTER ]( i * BYTES_PER_ELEMENT, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), this._isLE );
682+
}
683+
for ( i = 0; i < len - 1; i++ ) {
684+
minIdx = i;
685+
minVal = obuf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE );
686+
for ( j = i+1; j < len; j++ ) {
687+
val = obuf[ GETTER ]( j * BYTES_PER_ELEMENT, this._isLE );
688+
if ( compareFcn( minVal, val ) > 0 ) {
689+
minIdx = j;
690+
minVal = val;
691+
}
692+
}
693+
if ( i !== minIdx ) {
694+
obuf[ SETTER ]( minIdx * BYTES_PER_ELEMENT, obuf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), this._isLE );
695+
obuf[ SETTER ]( i * BYTES_PER_ELEMENT, minVal, this._isLE );
696+
}
697+
}
698+
return out;
699+
});
700+
637701
/**
638702
* Returns the index of the first occurrence of a given element.
639703
*

0 commit comments

Comments
 (0)