Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,39 @@ var v = arr.get( 100 );
// returns undefined
```

<a name="method-sort"></a>

#### TypedArrayFE.prototype.sort( compareFcn )

Sorts an array in-place.

```javascript
function compareFcn( a, b ) {
if ( a > b ) {
return 1;
}
if ( a < b ) {
return -1;
}
return 0;
}
var Float64ArrayFE = fixedEndianFactory( 'float64' );
var arr = new Float64ArrayFE( 'little-endian', [ 3.0, 1.0, 2.0 ] );
// returns <Float64ArrayFE>

var out=arr.sort( compareFcn );
// returns <Float64ArrayFE>

var v=out.get(0);
// returns 1.0

v=out.get(1);
// returns 2.0

v=out.get(2);
// returns 3.0
```

<a name="method-index-of"></a>

#### TypedArrayFE.prototype.indexOf( searchElement\[, fromIndex] )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
var pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// VARIABLES //

var Float64ArrayFE = factory( 'float64' );


// MAIN //

bench( pkg+':sort', function benchmark( b ) {
var out;
var arr;
var i;

arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.sort( compareFcn );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isTypedArray( out ) ) {
b.fail( 'should return a typed array' );
}
b.pass( 'benchmark finished' );
b.end();

function compareFcn( x, y ) {
return x - y;
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var zeroTo = require( '@stdlib/array/zero-to' );
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
var pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// VARIABLES //

var Float64ArrayFE = factory( 'float64' );


// FUNCTIONS //

/**
* Comparison function.
*
* @private
* @param {number} a - first value for comparison
* @param {number} b - second value for comparison
* @returns {number} comparison result
*/
function compareFcn( a, b ) {
if ( a > b ) {
return 1;
}
if ( a < b) {
return -1;
}
return 0;
}

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.sort( compareFcn );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isTypedArray( out ) ) {
b.fail( 'should return a typed array' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':sort:len='+len, f );
}
}

main();
54 changes: 54 additions & 0 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,60 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
}
return true;
});
/**
* Sorts an array in-place.
*
* @private
* @name sort
* @memberof TypedArray.prototype
* @type {Function}
* @param {Function} compareFcn - comparison function
* @throws {TypeError} `this` must be a typed array
* @throws {TypeError} first argument must be a function
* @returns {TypedArray} sorted array
*/
setReadOnly( TypedArray.prototype, 'sort', function sort( compareFcn ) {
var minIdx;
var minVal;
var val;
var buf;
var i;
var j;

if ( !isTypedArray( this ) ) {
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
}
if ( arguments.length === 0 ) {
compareFcn = function defaultCompareFcn( a, b ) {
if ( a > b ) {
return 1;
}
if ( a < b ) {
return -1;
}
return 0;
};
} else if ( !isFunction( compareFcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) );
}
buf = this._buffer;
for ( i = 0; i < this._length-1; i++ ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than a hand-rolled sort implementation, just copy to a temporary buffer (e.g., a plain array), use the built-in sort, and then copy back to the original buffer. That will be 2N + N log N, which is still better than N^2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll update it

minIdx = i;
minVal = buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE );
for ( j = i+1; j < this._length; j++ ) {
val = buf[ GETTER ]( j * BYTES_PER_ELEMENT, this._isLE );
if ( compareFcn( minVal, val ) > 0 ) {
minIdx = j;
minVal = val;
}
}
if ( i !== minIdx ) {
buf[ SETTER ]( minIdx * BYTES_PER_ELEMENT, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), this._isLE );
buf[ SETTER ]( i * BYTES_PER_ELEMENT, minVal, this._isLE );
}
}
return this;
});

/**
* Invokes a function once for each array element.
Expand Down
Loading
Loading