Skip to content

feat: add sort method to array/bool #2363

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 2 commits into from
Jun 12, 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
49 changes: 49 additions & 0 deletions lib/node_modules/@stdlib/array/bool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,55 @@ A few notes:
- If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error.
- If provided a [typed array][@stdlib/array/typed] which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range.

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

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

Sorts an array in-place.

```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 );

arr.sort( compare );

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

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

v = arr.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
75 changes: 75 additions & 0 deletions lib/node_modules/@stdlib/array/bool/benchmark/benchmark.sort.js
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+':sort', 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.sort( 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();
});
124 changes: 124 additions & 0 deletions lib/node_modules/@stdlib/array/bool/benchmark/benchmark.sort.length.js
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 Boolean = require( '@stdlib/boolean/ctor' );
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var pow = require( '@stdlib/math/base/special/pow' );
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.sort( 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+':sort:len='+len, f );
}
}

main();
48 changes: 48 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 @@ -106,6 +106,15 @@ type TernaryMapFcn<U> = ( this: U, value: boolean, index: number, arr: BooleanAr
*/
type MapFcn<U> = NullaryMapFcn<U> | UnaryMapFcn<U> | BinaryMapFcn<U> | TernaryMapFcn<U>;

/**
* Comparator function.
*
* @param a - first boolean value for comparison
* @param b - second boolean value for comparison
* @returns number indicating comparison result
*/
type CompareFcn = ( a: boolean, b: boolean ) => number;

/**
* Class for creating a Boolean array.
*/
Expand Down Expand Up @@ -312,6 +321,45 @@ declare class BooleanArray implements BooleanArrayInterface {
* // returns true
*/
set( value: ArrayLike<any> | any, i?: number ): void;

/**
* Sorts an array in-place.
*
* @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 );
*
* arr.sort( compare );
*
* var v = arr.get( 0 );
* // returns true
*
* v = arr.get( 1 );
* // returns true
*
* v = arr.get( 2 );
* // returns false
*/
sort( compareFcn: CompareFcn ): BooleanArray;
}

/**
Expand Down
73 changes: 73 additions & 0 deletions lib/node_modules/@stdlib/array/bool/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,79 @@ setReadOnly( BooleanArray.prototype, 'set', function set( value ) {
buf[ idx ] = ( value ) ? 1 : 0;
});

/**
* Sorts an array in-place.
*
* @name sort
* @memberof BooleanArray.prototype
* @type {Function}
* @param {Function} [compareFcn] - comparison function
* @throws {TypeError} `this` must be a boolean array
* @throws {TypeError} first argument must be a function
* @returns {BooleanArray} 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 );
*
* arr.sort( compare );
*
* var v = arr.get( 0 );
* // returns true
*
* v = arr.get( 1 );
* // returns true
*
* v = arr.get( 2 );
* // returns false
*
*/
setReadOnly( BooleanArray.prototype, 'sort', function sort( compareFcn ) {
var buf;

if ( !isBooleanArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a boolean array.' );
}
buf = this._buffer;
if ( arguments.length === 0 ) {
buf.sort();
return this;
}
if ( !isFunction( compareFcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) );
}
buf.sort( compare );
return this;

/**
* Comparison function for sorting.
*
* @private
* @param {boolean} a - first boolean value for comparison
* @param {boolean} b - second boolean value for comparison
* @returns {number} comparison result
*/
function compare( a, b ) {
return compareFcn( Boolean( a ), Boolean( b ) );
}
});


// EXPORTS //

Expand Down
Loading