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 @@ -67,7 +67,7 @@
*
* @returns returned value
*/
type NullaryMapFcn<U> = ( this: U ) => any;

Check warning on line 70 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 @@ -75,7 +75,7 @@
* @param value - current array element
* @returns returned value
*/
type UnaryMapFcn<U> = ( this: U, value: boolean ) => any;

Check warning on line 78 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 @@ -84,7 +84,7 @@
* @param index - current array element index
* @returns returned value
*/
type BinaryMapFcn<U> = ( this: U, value: boolean, index: number ) => any;

Check warning on line 87 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 @@ -94,7 +94,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 97 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 @@ -106,6 +106,15 @@
*/
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 @@ -172,7 +181,7 @@
* var len = arr.length;
* // returns 2
*/
constructor( arg?: number | ArrayLike<any> | ArrayBuffer | Iterable<any>, byteOffset?: number, length?: number );

Check warning on line 184 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 184 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 @@ -311,7 +320,46 @@
* v = arr.get( 0 );
* // returns true
*/
set( value: ArrayLike<any> | any, i?: number ): void;

Check warning on line 323 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 323 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.
*
* @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 Expand Up @@ -380,7 +428,7 @@
* var len = arr.length;
* // returns 2
*/
new( arg?: number | ArrayLike<any> | ArrayBuffer | Iterable<any>, byteOffset?: number, length?: number ): BooleanArray;

Check warning on line 431 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 431 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
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 @@ -554,7 +554,7 @@
out = new this.constructor( this._length );
outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
for ( i = 0; i < this._length; i++ ) {
outbuf[ i ] = Boolean( fcn.call( thisArg, Boolean( buf[ i ] ), i, this ) );

Check warning on line 557 in lib/node_modules/@stdlib/array/bool/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 83. Maximum allowed is 80
}
return out;
});
Expand Down Expand Up @@ -646,7 +646,7 @@
// We need to copy source values...
tmp = new Uint8Array( sbuf.length );
for ( i = 0; i < sbuf.length; i++ ) {
tmp[ i ] = sbuf[ i ]; // TODO: handle accessor arrays

Check warning on line 649 in lib/node_modules/@stdlib/array/bool/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: handle accessor arrays'
}
sbuf = tmp;
}
Expand All @@ -661,6 +661,79 @@
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
Loading