Skip to content

feat: add toSorted method to array/bool #2413

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

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 51 additions & 1 deletion lib/node_modules/@stdlib/array/bool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ var count = context.count;

<a name="method-find-last"></a>

#### Complex64Array.prototype.findLast( predicate\[, thisArg] )
#### BooleanArray.prototype.findLast( predicate\[, thisArg] )

Returns the last element in an array for which a predicate function returns a truthy value.

Expand Down Expand Up @@ -790,6 +790,56 @@ v = out.get( 2 );
// returns true
```

<a name="method-to-sorted"></a>

#### BooleanArray.prototype.toSorted( \[compareFcn] )

Returns a new typed array containing the elements in sorted order.

```javascript
function compare( a, b ) {
if ( a === false ) {
if ( b === false ) {
return 0;
}
return 1;
}
if ( b === true ) {
return 0;
}
return -1;
}

var arr = new BooleanArray( 3 );

arr.set( true, 0 );
arr.set( false, 1 );
arr.set( true, 2 );

var out = arr.sort( compare );
// returns <BooleanArray>

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

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

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

The `compareFcn` determines the order of the elements. The function is called with the following arguments:

- **a**: the first boolean value for comparison.
- **b**: the second boolean value for comparison.

The function should return a number where:

- a negative value indicates that `a` should come before `b`.
- a positive value indicates that `a` should come after `b`.
- zero or `NaN` indicates that `a` and `b` are considered equal.

</section>

<!-- /.usage -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// FUNCTIONS //

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


// MAIN //

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

arr = new BooleanArray( [ true, false, false, true, true, false ] );

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 ( !isBooleanArray( out ) ) {
b.fail( 'should return a BooleanArray' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* @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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pow = require( '@stdlib/math/base/special/pow' );
var Boolean = require( '@stdlib/boolean/ctor' );
var pkg = require( './../package.json' ).name;
var BooleanArray = require( './../lib' );


// FUNCTIONS //

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

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr;
var i;

arr = [];
for ( i = 0; i < len; i++ ) {
arr.push( Boolean( i%2 ) );
}
arr = new BooleanArray( arr );

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 ( !isBooleanArray( out ) ) {
b.fail( 'should return a BooleanArray' );
}
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();
37 changes: 37 additions & 0 deletions lib/node_modules/@stdlib/array/bool/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -603,5 +603,42 @@
true


{{alias}}.prototype.toSorted( [compareFunction] )
Returns a new typed array containing the elements in sorted order.

A comparison function determines the order of the array elements. The
function is provided two arguments:

- a: first boolean value for comparison.
- b: second boolean value for comparison.

The function should return a value less than zero if `a` comes before `b`,
a value greater than zero if `a` comes after `b`, and zero if `a` and `b`
are equivalent.

Parameters
----------
compareFunction: Function (optional)
Comparison function.

Returns
-------
out: BooleanArray
New typed array.

Examples
--------
> function compare( a, b ) { return a === true ? -1 : 1; }
> var arr = new {{alias}}( [ true, false, true ] )
<BooleanArray>
> var out = arr.toSorted( compare );
> var v = out.get( 0 )
true
> v = out.get( 1 )
true
> v = out.get( 2 )
false


See Also
--------
40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
*
* @returns returned value
*/
type NullaryMapFcn<U> = ( this: U ) => any;

Check warning on line 114 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Callback invoked for each element in an array.
Expand All @@ -119,7 +119,7 @@
* @param value - current array element
* @returns returned value
*/
type UnaryMapFcn<U> = ( this: U, value: boolean ) => any;

Check warning on line 122 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Callback invoked for each element in an array.
Expand All @@ -128,7 +128,7 @@
* @param index - current array element index
* @returns returned value
*/
type BinaryMapFcn<U> = ( this: U, value: boolean, index: number ) => any;

Check warning on line 131 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Callback invoked for each element in an array.
Expand All @@ -138,7 +138,7 @@
* @param arr - array on which the method was called
* @returns returned value
*/
type TernaryMapFcn<U> = ( this: U, value: boolean, index: number, arr: BooleanArray ) => any;

Check warning on line 141 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Callback invoked for each element in an array.
Expand Down Expand Up @@ -225,7 +225,7 @@
* var len = arr.length;
* // returns 2
*/
constructor( arg?: number | ArrayLike<any> | ArrayBuffer | Iterable<any>, byteOffset?: number, length?: number );

Check warning on line 228 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 228 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Length (in bytes) of the array.
Expand Down Expand Up @@ -482,7 +482,7 @@
* v = arr.get( 0 );
* // returns true
*/
set( value: ArrayLike<any> | any, i?: number ): void;

Check warning on line 485 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 485 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Sorts an array in-place.
Expand Down Expand Up @@ -548,6 +548,46 @@
* // returns true
*/
toReversed(): BooleanArray;

/**
* Returns a new typed array containing the elements in sorted order.
*
* @param compareFcn - comparison function
* @returns sorted array
*
* @example
* function compare( a, b ) {
* if ( a === false ) {
* if ( b === false ) {
* return 0;
* }
* return 1;
* }
* if ( b === true ) {
* return 0;
* }
* return -1;
* }
*
* var arr = new BooleanArray( 3 );
*
* arr.set( true, 0 );
* arr.set( false, 1 );
* arr.set( true, 2 );
*
* var out = arr.sort( compare );
* // returns <BooleanArray>
*
* var v = out.get( 0 );
* // returns true
*
* v = out.get( 1 );
* // returns true
*
* v = out.get( 2 );
* // returns false
*/
toSorted( compareFcn: CompareFcn ): BooleanArray;
}

/**
Expand Down Expand Up @@ -616,7 +656,7 @@
* var len = arr.length;
* // returns 2
*/
new( arg?: number | ArrayLike<any> | ArrayBuffer | Iterable<any>, byteOffset?: number, length?: number ): BooleanArray;

Check warning on line 659 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 659 in lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Boolean array constructor.
Expand Down
Loading
Loading