diff --git a/lib/node_modules/@stdlib/array/bool/README.md b/lib/node_modules/@stdlib/array/bool/README.md index 584fa70f3ab4..0708a55e5f0e 100644 --- a/lib/node_modules/@stdlib/array/bool/README.md +++ b/lib/node_modules/@stdlib/array/bool/README.md @@ -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. + + +#### 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. + diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.sort.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.sort.js new file mode 100644 index 000000000000..a8e15baa82b2 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.sort.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+':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(); +}); diff --git a/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.sort.length.js b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.sort.length.js new file mode 100644 index 000000000000..2efc4b813f15 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/benchmark/benchmark.sort.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 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(); 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 523d3e1bc287..8219ea4c0fb3 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 @@ -106,6 +106,15 @@ type TernaryMapFcn = ( this: U, value: boolean, index: number, arr: BooleanAr */ type MapFcn = NullaryMapFcn | UnaryMapFcn | BinaryMapFcn | TernaryMapFcn; +/** +* 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. */ @@ -312,6 +321,45 @@ declare class BooleanArray implements BooleanArrayInterface { * // returns true */ set( value: ArrayLike | 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; } /** diff --git a/lib/node_modules/@stdlib/array/bool/lib/main.js b/lib/node_modules/@stdlib/array/bool/lib/main.js index aaa6d05f53ee..98d0b4173984 100644 --- a/lib/node_modules/@stdlib/array/bool/lib/main.js +++ b/lib/node_modules/@stdlib/array/bool/lib/main.js @@ -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 // diff --git a/lib/node_modules/@stdlib/array/bool/test/test.sort.js b/lib/node_modules/@stdlib/array/bool/test/test.sort.js new file mode 100644 index 000000000000..20f068a09348 --- /dev/null +++ b/lib/node_modules/@stdlib/array/bool/test/test.sort.js @@ -0,0 +1,168 @@ +/** +* @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 `sort` method', function test( t ) { + t.strictEqual( hasOwnProp( BooleanArray.prototype, 'sort' ), true, 'has property' ); + t.strictEqual( isFunction( BooleanArray.prototype.sort ), 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.sort.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.sort( 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.sort( compareFcn ); + + t.strictEqual( out.buffer, arr.buffer, 'returns expected value' ); + t.strictEqual( out.length, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the method sorts elements of a boolean array in-place', function test( t ) { + var expected; + var arr; + var out; + + arr = new BooleanArray( [ true, false, true, false, true ] ); + expected = new Uint8Array( [ 0, 0, 1, 1, 1 ] ); + out = arr.sort( compareFcn ); + + t.strictEqual( instanceOf( arr, BooleanArray ), true, 'returns expected value' ); + t.strictEqual( out, arr, 'returns expected value' ); + t.strictEqual( arr.length, expected.length, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( arr, 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, true, false, true ] ); + out = arr.sort( compareFcn ); + + t.strictEqual( out.length, arr.length, 'returns expected value' ); + t.end(); +});