Skip to content
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

feat: add toSorted method to array/fixed-endian-factory #3302

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

ahmad-kashkoush
Copy link
Contributor

Resolves #3160

Description

What is the purpose of this pull request?

This pull request:

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

@stdlib-bot
Copy link
Contributor

stdlib-bot commented Nov 30, 2024

Coverage Report

Package Statements Branches Functions Lines
array/fixed-endian-factory 1237 / 1488
+ 83.13 %
170 / 182
+ 93.41 %
25 / 35
+ 71.43 %
1237 / 1488
+ 83.13 %

The above coverage report was generated for the changes in this PR.

@Planeshifter Planeshifter changed the title feat: add toSorted method to array/fixed-endian-factory feat: add toSorted method to array/fixed-endian-factory Dec 4, 2024
@kgryte kgryte added Needs Review A pull request which needs code review. Accepted RFC feature request which has been accepted. Feature Issue or pull request for adding a new feature. and removed Accepted RFC feature request which has been accepted. labels Dec 4, 2024
}
}
b.toc();
if ( !isTypedArray( out ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

This is not a valid check, as out is not an actual typed array instance.

}
}
b.toc();
if ( !isTypedArray( out ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Same comment.

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 ) {
Copy link
Member

Choose a reason for hiding this comment

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

We don't typically use function expressions, only declarations. I suggest refactoring accordingly. It is also not necessary to have this function in a nested scope and can be lifted to the top most scope.

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 ) {
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 rolling your own sort function, copy to a temporary generic array, sort using the built-in #.sort method, and then copy to an output array.

expected = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
out = arr.toSorted( compareFcn );
values = [];
fcn();
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 use fcn to generate a values array, just use array/base/assert/has-same-values and compare to an expected Float64ArrayFE.

expected = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
out = arr.toSorted();
values = [];
fcn();
Copy link
Member

Choose a reason for hiding this comment

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

Same comment.

var arr = new Float64ArrayFE( 'little-endian', [ 3.0, 1.0, 2.0 ] );
// returns <Float64ArrayFE>

var out=arr.toSorted( compareFcn );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var out=arr.toSorted( compareFcn );
var out = arr.toSorted( compareFcn );

You need spaces around operators here and below.

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

var v=out.get(0);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var v=out.get(0);
var v = out.get( 0 );

Spaces around arguments here and below.

@@ -607,6 +607,39 @@ var idx = arr.lastIndexOf( 5.0 );
// returns -1
```

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

#### TypedArrayFE.prototype.toSorted( compareFcn )
Copy link
Member

Choose a reason for hiding this comment

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

The compareFcn is optional.

@@ -607,6 +607,39 @@ var idx = arr.lastIndexOf( 5.0 );
// returns -1
```

<a name="method-toSorted"></a>
Copy link
Member

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.

* @throws {TypeError} first argument must be a function
* @returns {TypedArray} sorted array
*/
setReadOnly( TypedArray.prototype, 'toSorted', function sort( compareFcn ) {
Copy link
Member

Choose a reason for hiding this comment

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

Insert methods in alphabetical order.

@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Dec 4, 2024
- update method logic and insert it into correct place in both main.js and readme
- update type checking in benchmarks
- use hasSameValues in test file
@ahmad-kashkoush
Copy link
Contributor Author

@kgryte @Planeshifter I have applyed all changes you've suggested in this PR and also in #3298

@ahmad-kashkoush
Copy link
Contributor Author

@kgryte may you give it a review?

@Planeshifter Planeshifter added the Potential Duplicate There might be another pull request resolving the same issue. label Mar 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Issue or pull request for adding a new feature. Needs Changes Pull request which needs changes before being merged. Potential Duplicate There might be another pull request resolving the same issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC]: add toSorted method to array/fixed-endian-factory
4 participants