diff --git a/lib/node_modules/@stdlib/array/bool/README.md b/lib/node_modules/@stdlib/array/bool/README.md index c6148a1c5491..04573050b865 100644 --- a/lib/node_modules/@stdlib/array/bool/README.md +++ b/lib/node_modules/@stdlib/array/bool/README.md @@ -440,7 +440,7 @@ var count = context.count; -#### 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. @@ -790,6 +790,56 @@ v = out.get( 2 ); // returns true ``` + + +#### 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 + +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. + diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_sorted.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_sorted.js new file mode 100644 index 000000000000..cab83d5f81cf --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_sorted.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_sorted.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_sorted.length.js new file mode 100644 index 000000000000..0e94fef16fae --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.to_sorted.length.js @@ -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(); diff --git a/lib/node_modules/@stdlib/array/bool/docs/repl.txt b/lib/node_modules/@stdlib/array/bool/docs/repl.txt index b80f9d704812..6760579d5cf1 100644 --- a/lib/node_modules/@stdlib/array/bool/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/bool/docs/repl.txt @@ -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 ] ) + + > var out = arr.toSorted( compare ); + > var v = out.get( 0 ) + true + > v = out.get( 1 ) + true + > v = out.get( 2 ) + false + + See Also -------- diff --git a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts index 96a4f91536f2..17dbb5e93fff 100644 --- a/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/bool/docs/types/index.d.ts @@ -548,6 +548,46 @@ declare class BooleanArray implements BooleanArrayInterface { * // 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 + * + * var v = out.get( 0 ); + * // returns true + * + * v = out.get( 1 ); + * // returns true + * + * v = out.get( 2 ); + * // returns false + */ + toSorted( compareFcn: CompareFcn ): BooleanArray; } /** diff --git a/lib/node_modules/@stdlib/array/bool/lib/main.js b/lib/node_modules/@stdlib/array/bool/lib/main.js index a097d9b39752..34a9c19cc28c 100644 --- a/lib/node_modules/@stdlib/array/bool/lib/main.js +++ b/lib/node_modules/@stdlib/array/bool/lib/main.js @@ -940,7 +940,6 @@ setReadOnly( BooleanArray.prototype, 'set', function set( value ) { * * v = arr.get( 2 ); * // returns false -* */ setReadOnly( BooleanArray.prototype, 'sort', function sort( compareFcn ) { var buf; @@ -1020,6 +1019,89 @@ setReadOnly( BooleanArray.prototype, 'toReversed', function toReversed() { return out; }); +/** +* Returns a new typed array containing the elements in sorted order. +* +* @name toSorted +* @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 ); +* +* var out = arr.sort( compare ); +* // returns +* +* var v = out.get( 0 ); +* // returns true +* +* v = out.get( 1 ); +* // returns true +* +* v = out.get( 2 ); +* // returns false +*/ +setReadOnly( BooleanArray.prototype, 'toSorted', function toSorted( compareFcn ) { + var outbuf; + var out; + var len; + var buf; + var i; + + if ( !isBooleanArray( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a boolean array.' ); + } + len = this._length; + out = new this.constructor( len ); + buf = this._buffer; + outbuf = out._buffer; // eslint-disable-line no-underscore-dangle + for ( i = 0; i < len; i++ ) { + outbuf[ i ] = buf[ i ]; + } + if ( arguments.length === 0 ) { + outbuf.sort(); + return out; + } + if ( !isFunction( compareFcn ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFcn ) ); + } + outbuf.sort( compare ); + return out; + + /** + * 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 // diff --git a/lib/node_modules/@stdlib/array/bool/test/test.to_sorted.js b/lib/node_modules/@stdlib/array/bool/test/test.to_sorted.js new file mode 100644 index 000000000000..9cd08032b987 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.to_sorted.js @@ -0,0 +1,167 @@ +/** +* @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 tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +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; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof BooleanArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is a `toSorted` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'toSorted' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.toSorted ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a boolean array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toSorted.call( value, compareFcn ); + }; + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var arr; + var i; + + arr = new BooleanArray( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.toSorted( value ); + }; + } +}); + +tape( 'the method returns an empty array if operating on an empty boolean array', function test( t ) { + var arr; + var out; + + arr = new BooleanArray(); + out = arr.toSorted( compareFcn ); + + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method returns a new typed array containing elements in sorted order', function test( t ) { + var expected; + var arr; + var out; + + arr = new BooleanArray( [ true, false, false, true, true, false ] ); + expected = new Uint8Array( [ 0, 0, 0, 1, 1, 1 ] ); + out = arr.toSorted( compareFcn ); + + t.strictEqual( instanceOf( out, BooleanArray ), true, 'returns expected value' ); + t.notEqual( out, arr, 'returns a new instance' ); + t.strictEqual( out.length, expected.length, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( out, 0 ), expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the method does not change the array length', function test( t ) { + var arr; + var out; + + arr = new BooleanArray( [ true, false, false, true, true, false ] ); + out = arr.toSorted( compareFcn ); + + t.strictEqual( out.length, arr.length, 'returns expected value' ); + t.end(); +});