-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add toSorted
method to array/fixed-endian-factory
#3302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 3 commits
b79d687
bb097df
872f8b2
6c18ce2
e61542a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -607,6 +607,39 @@ var idx = arr.lastIndexOf( 5.0 ); | |||||
// returns -1 | ||||||
``` | ||||||
|
||||||
<a name="method-toSorted"></a> | ||||||
|
||||||
#### TypedArrayFE.prototype.toSorted( compareFcn ) | ||||||
|
||||||
|
||||||
Returns a new typed array containing the elements in sorted order. | ||||||
|
||||||
```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.toSorted( compareFcn ); | ||||||
|
var out=arr.toSorted( compareFcn ); | |
var out = arr.toSorted( compareFcn ); |
You need spaces around operators here and below.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var v=out.get(0); | |
var v = out.get( 0 ); |
Spaces around arguments here and below.
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+':toSorted', function benchmark( b ) { | ||
var out; | ||
var arr; | ||
var i; | ||
|
||
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 5.0, 3.0, 2.0, 3.0 ] ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = arr.toSorted( 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.toSorted( 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+':toSorted:len='+len, f ); | ||
} | ||
} | ||
|
||
main(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -669,6 +669,70 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli | |
return this._buffer[ GETTER ]( idx*BYTES_PER_ELEMENT, this._isLE ); | ||
}); | ||
|
||
/** | ||
* Returns a new typed array containing the elements in sorted order. | ||
* | ||
* @private | ||
* @name toSorted | ||
* @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, 'toSorted', function sort( compareFcn ) { | ||
|
||
var minIdx; | ||
var minVal; | ||
var obuf; | ||
var buf; | ||
var len; | ||
var out; | ||
var val; | ||
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 ) ); | ||
} | ||
len = this._length; | ||
out = new this.constructor( flag2byteOrder( this._isLE ), len ); | ||
buf = this._buffer; | ||
obuf = out._buffer; // eslint-disable-line no-underscore-dangle | ||
for ( i = 0; i < len; i++ ) { | ||
obuf[ SETTER ]( i * BYTES_PER_ELEMENT, buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), this._isLE ); | ||
} | ||
for ( i = 0; i < len - 1; i++ ) { | ||
minIdx = i; | ||
minVal = obuf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ); | ||
for ( j = i+1; j < len; j++ ) { | ||
val = obuf[ GETTER ]( j * BYTES_PER_ELEMENT, this._isLE ); | ||
if ( compareFcn( minVal, val ) > 0 ) { | ||
|
||
minIdx = j; | ||
minVal = val; | ||
} | ||
} | ||
if ( i !== minIdx ) { | ||
obuf[ SETTER ]( minIdx * BYTES_PER_ELEMENT, obuf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ), this._isLE ); | ||
obuf[ SETTER ]( i * BYTES_PER_ELEMENT, minVal, this._isLE ); | ||
} | ||
} | ||
return out; | ||
}); | ||
|
||
/** | ||
* Returns the index of the first occurrence of a given element. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should be inserted into the docs such that methods are listed in alphabetical order.